aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/ut/writer_ut.cpp
blob: f191b581fb6c9a7ee82d31ef800d4bcfc9c04e83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <library/cpp/yson_pull/scalar.h> 
#include <library/cpp/yson_pull/detail/writer.h> 

#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/string.h>

#include <climits>
#include <limits>

using namespace std::string_view_literals;

namespace {
    template <typename Writer, typename Function>
    TString with_writer(Function&& function) {
        TString result;
        auto writer = NYsonPull::NDetail::make_writer<Writer>(
            NYsonPull::NOutput::FromString(&result),
            NYsonPull::EStreamType::Node);

        function(writer);

        return result;
    }

    template <typename Writer>
    TString to_yson_string(const NYsonPull::TScalar& value) {
        return with_writer<Writer>([&](NYsonPull::TWriter& writer) {
            writer.BeginStream().Scalar(value).EndStream();
        });
    }

    template <typename T>
    TString to_yson_binary_string(T&& value) {
        return to_yson_string<NYsonPull::NDetail::TBinaryWriterImpl>(std::forward<T>(value));
    }

    template <typename T>
    TString to_yson_text_string(T&& value) {
        return to_yson_string<NYsonPull::NDetail::TTextWriterImpl>(std::forward<T>(value));
    }

} // anonymous namespace

// =================== Text format =====================

Y_UNIT_TEST_SUITE(Writer) {
    Y_UNIT_TEST(TextEntity) {
        UNIT_ASSERT_VALUES_EQUAL(
            "#",
            to_yson_text_string(NYsonPull::TScalar{}));
    }

    Y_UNIT_TEST(TextBoolean) {
        UNIT_ASSERT_VALUES_EQUAL(
            "%false",
            to_yson_text_string(NYsonPull::TScalar{false}));
        UNIT_ASSERT_VALUES_EQUAL(
            "%true",
            to_yson_text_string(NYsonPull::TScalar{true}));
    }

    Y_UNIT_TEST(TextInt64) {
        UNIT_ASSERT_VALUES_EQUAL(
            "0",
            to_yson_text_string(NYsonPull::TScalar{i64{0}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "200",
            to_yson_text_string(NYsonPull::TScalar{i64{200}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "20000",
            to_yson_text_string(NYsonPull::TScalar{i64{20000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "200000000",
            to_yson_text_string(NYsonPull::TScalar{i64{200000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "20000000000000000",
            to_yson_text_string(NYsonPull::TScalar{i64{20000000000000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "9223372036854775807",
            to_yson_text_string(NYsonPull::TScalar{i64{INT64_MAX}}));

        UNIT_ASSERT_VALUES_EQUAL(
            "-200",
            to_yson_text_string(NYsonPull::TScalar{i64{-200}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "-20000",
            to_yson_text_string(NYsonPull::TScalar{i64{-20000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "-200000000",
            to_yson_text_string(NYsonPull::TScalar{i64{-200000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "-20000000000000000",
            to_yson_text_string(NYsonPull::TScalar{i64{-20000000000000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "-9223372036854775808",
            to_yson_text_string(NYsonPull::TScalar{i64{INT64_MIN}}));
    }

    Y_UNIT_TEST(TextUInt64) {
        UNIT_ASSERT_VALUES_EQUAL(
            "0u",
            to_yson_text_string(NYsonPull::TScalar{ui64{0}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "200u",
            to_yson_text_string(NYsonPull::TScalar{ui64{200}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "20000u",
            to_yson_text_string(NYsonPull::TScalar{ui64{20000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "200000000u",
            to_yson_text_string(NYsonPull::TScalar{ui64{200000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "20000000000000000u",
            to_yson_text_string(NYsonPull::TScalar{ui64{20000000000000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "9223372036854775807u",
            to_yson_text_string(NYsonPull::TScalar{ui64{INT64_MAX}}));
        UNIT_ASSERT_VALUES_EQUAL(
            "18446744073709551615u",
            to_yson_text_string(NYsonPull::TScalar{ui64{UINT64_MAX}}));
    }

    Y_UNIT_TEST(TextFloat64) {
        UNIT_ASSERT_VALUES_EQUAL(
            "%inf",
            to_yson_text_string(NYsonPull::TScalar{std::numeric_limits<double>::infinity()}));
        UNIT_ASSERT_VALUES_EQUAL(
            "%-inf",
            to_yson_text_string(NYsonPull::TScalar{-std::numeric_limits<double>::infinity()}));
        UNIT_ASSERT_VALUES_EQUAL(
            "%nan",
            to_yson_text_string(NYsonPull::TScalar{std::numeric_limits<double>::quiet_NaN()}));
    }

    Y_UNIT_TEST(TextString) {
        UNIT_ASSERT_VALUES_EQUAL(
            R"("")",
            to_yson_text_string(NYsonPull::TScalar{""}));
        UNIT_ASSERT_VALUES_EQUAL(
            R"("hello")",
            to_yson_text_string(NYsonPull::TScalar{"hello"}));
        UNIT_ASSERT_VALUES_EQUAL(
            R"("hello\nworld")",
            to_yson_text_string(NYsonPull::TScalar{"hello\nworld"}));
    }

    // =================== Binary format =====================

    Y_UNIT_TEST(BinaryEntity) {
        UNIT_ASSERT_VALUES_EQUAL(
            "#",
            to_yson_binary_string(NYsonPull::TScalar{}));
    }

    Y_UNIT_TEST(BinaryBoolean) {
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x4"),
            to_yson_binary_string(NYsonPull::TScalar{false}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x5"),
            to_yson_binary_string(NYsonPull::TScalar{true}));
    }

    Y_UNIT_TEST(BinaryInt64) {
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\0"sv),
            to_yson_binary_string(NYsonPull::TScalar{i64{0}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\x90\x3"),
            to_yson_binary_string(NYsonPull::TScalar{i64{200}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\xC0\xB8\x2"),
            to_yson_binary_string(NYsonPull::TScalar{i64{20000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\x80\x88\xDE\xBE\x1"),
            to_yson_binary_string(NYsonPull::TScalar{i64{200000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\x80\x80\x90\xF8\x9B\xF9\x86G"),
            to_yson_binary_string(NYsonPull::TScalar{i64{20000000000000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1"),
            to_yson_binary_string(NYsonPull::TScalar{i64{INT64_MAX}}));

        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\x8F\x3"),
            to_yson_binary_string(NYsonPull::TScalar{i64{-200}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\xBF\xB8\x2"),
            to_yson_binary_string(NYsonPull::TScalar{i64{-20000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\xFF\x87\xDE\xBE\x1"),
            to_yson_binary_string(NYsonPull::TScalar{i64{-200000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\xFF\xFF\x8F\xF8\x9B\xF9\x86G"),
            to_yson_binary_string(NYsonPull::TScalar{i64{-20000000000000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x2\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1"),
            to_yson_binary_string(NYsonPull::TScalar{i64{INT64_MIN}}));
    }

    Y_UNIT_TEST(BinaryUInt64) {
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x6\0"sv),
            to_yson_binary_string(NYsonPull::TScalar{ui64{0}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x6\xC8\x1"),
            to_yson_binary_string(NYsonPull::TScalar{ui64{200}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x6\xA0\x9C\x1"),
            to_yson_binary_string(NYsonPull::TScalar{ui64{20000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x6\x80\x84\xAF_"),
            to_yson_binary_string(NYsonPull::TScalar{ui64{200000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x6\x80\x80\x88\xFC\xCD\xBC\xC3#"),
            to_yson_binary_string(NYsonPull::TScalar{ui64{20000000000000000}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x6\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F"),
            to_yson_binary_string(NYsonPull::TScalar{ui64{INT64_MAX}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x6\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1"),
            to_yson_binary_string(NYsonPull::TScalar{ui64{UINT64_MAX}}));
    }

    Y_UNIT_TEST(BinaryFloat64) {
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x03\x00\x00\x00\x00\x00\x00\xf0\x7f"sv),
            to_yson_binary_string(NYsonPull::TScalar{std::numeric_limits<double>::infinity()}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x03\x00\x00\x00\x00\x00\x00\xf0\xff"sv),
            to_yson_binary_string(NYsonPull::TScalar{-std::numeric_limits<double>::infinity()}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x03\x00\x00\x00\x00\x00\x00\xf8\x7f"sv),
            to_yson_binary_string(NYsonPull::TScalar{std::numeric_limits<double>::quiet_NaN()}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x03\x9a\x99\x99\x99\x99\x99\xf1\x3f"),
            to_yson_binary_string(NYsonPull::TScalar{double{1.1}}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x03\x9a\x99\x99\x99\x99\x99\xf1\xbf"),
            to_yson_binary_string(NYsonPull::TScalar{double{-1.1}}));
    }

    Y_UNIT_TEST(BinaryString) {
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x1\0"sv),
            to_yson_binary_string(NYsonPull::TScalar{""}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x1\nhello"),
            to_yson_binary_string(NYsonPull::TScalar{"hello"}));
        UNIT_ASSERT_VALUES_EQUAL(
            TStringBuf("\x1\x16hello\nworld"),
            to_yson_binary_string(NYsonPull::TScalar{"hello\nworld"}));
    }

} // Y_UNIT_TEST_SUITE(Writer)