aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/converter/ut/test_conversion.cpp
blob: 84027dda75addb1a3980181a8618af1908fe38ad (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
#include "library/cpp/json/converter/converter.h"
#include <library/cpp/json/json_reader.h>
#include <library/cpp/json/json_writer.h>
#include "library/cpp/json/writer/json_value.h"
#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/array_ref.h>
#include <util/generic/deque.h>
#include <util/generic/hash.h>
#include <util/generic/list.h>
#include <util/generic/map.h>
#include <util/generic/maybe.h>


namespace NJson {
    void AssertJsonsEqual(const TJsonValue& actualJson, const TJsonValue& expectedJson) {
        const auto actualString = WriteJson(actualJson, /*formatOutput*/ true, /*sortkeys*/ true, /*validateUtf8*/ true);
        const auto expectedString = WriteJson(expectedJson, /*formatOutput*/ true, /*sortkeys*/ true, /*validateUtf8*/ true);
        UNIT_ASSERT_NO_DIFF(actualString, expectedString);
    }

    void AssertJsonsEqual(const TJsonValue& actualJson, TStringBuf expectedString) {
        const auto expectedJson = ReadJsonFastTree(expectedString);
        AssertJsonsEqual(actualJson, expectedJson);
    }

    template<typename T>
    struct TConverterTester {
        using TValues = THashMap<TStringBuf, T>;

        static void TestEncoding(const TValues& values) {
            for (const auto& [serializedValue, value] : values) {
                const auto encodedValue = TConverter<T>::Encode(value);
                AssertJsonsEqual(encodedValue, serializedValue);
            }
        }

        static void TestDecoding(const TValues& values) {
            for (const auto& [serializedValue, value] : values) {
                const auto decodedValue = TConverter<T>::Decode(ReadJsonFastTree(serializedValue));
                UNIT_ASSERT_EQUAL(decodedValue, value);
            }
        }

        static void TestDecodingException(TStringBuf serializedValue) {
            try {
                TConverter<T>::Decode(ReadJsonFastTree(serializedValue));
                UNIT_ASSERT(false);
            } catch (...) {
            }
        }

        static void Test(const TValues& values) {
            TestEncoding(values);
            TestDecoding(values);

            for (const auto& [serializedValue, value] : values) {
                const auto encodedValue = TConverter<T>::Encode(value);
                const auto decodedValue = TConverter<T>::Decode(encodedValue);
                UNIT_ASSERT_EQUAL(value, decodedValue);
            }
        }
    };

    template<typename T>
    requires std::is_floating_point_v<T>
    struct TConverterTester<T> {
        using TValues = THashMap<TStringBuf, T>;

        static void TestDecoding(const TValues& values) {
            for (const auto& [serializedValue, value] : values) {
                {
                    const auto decodedValue = TConverter<T>::Decode(ReadJsonFastTree(serializedValue));
                    UNIT_ASSERT_DOUBLES_EQUAL(decodedValue, value, 0.000001);
                }
            }
        }

        static void Test(const TValues& values) {
            TestDecoding(values);

            for (const auto& [serializedValue, value] : values) {
                {
                    const auto encodedValue = TConverter<T>::Encode(value);
                    const auto decodedValue = TConverter<T>::Decode(encodedValue);
                    UNIT_ASSERT_DOUBLES_EQUAL(decodedValue, value, 0.000001);
                }
            }
        }
    };

    Y_UNIT_TEST_SUITE(ConversionTests) {
        Y_UNIT_TEST(PrimitivesTest) {
            TConverterTester<bool>::Test({{"true", true}, {"false", false}});

            TConverterTester<ui8>::Test({{"0", 0}, {"255", 255}});
            TConverterTester<i8>::Test({{"-128", -128}, {"127", 127}});
            TConverterTester<ui16>::Test({{"0", 0}, {"65535", 65535}});
            TConverterTester<i16>::Test({{"-32768", -32768}, {"32767", 32767}});
            TConverterTester<ui32>::Test({{"0", 0}, {"4294967295", 4294967295}});
            TConverterTester<i32>::Test({{"-2147483648", -2147483648}, {"2147483647", 2147483647}});
            TConverterTester<ui64>::Test({{"0", 0}, {"18446744073709551615", 18446744073709551615u}});
            TConverterTester<i64>::Test({
                {"-9223372036854775808", -9223372036854775808u},
                {"9223372036854775807", 9223372036854775807},
            });

            TConverterTester<i8>::TestDecodingException("128");
            TConverterTester<i8>::TestDecodingException("-129");
            TConverterTester<ui8>::TestDecodingException("256");
            TConverterTester<i16>::TestDecodingException("32768");
            TConverterTester<i16>::TestDecodingException("-32769");
            TConverterTester<ui16>::TestDecodingException("65536");
            TConverterTester<i32>::TestDecodingException("-2147483649");
            TConverterTester<i32>::TestDecodingException("2147483649");
            TConverterTester<ui32>::TestDecodingException("4294967296");

            TConverterTester<unsigned char>::Test({{"0", 0}, {"255", 255}});
            TConverterTester<signed char>::Test({{"-128", -128}, {"127", 127}});
            TConverterTester<unsigned short int>::Test({{"0", 0}, {"65535", 65535}});
            TConverterTester<signed short int>::Test({{"-32768", -32768}, {"32767", 32767}});
            TConverterTester<unsigned int>::Test({{"0", 0}, {"65535", 65535}});
            TConverterTester<signed int>::Test({{"-32768", -32768}, {"32767", 32767}});
            TConverterTester<unsigned long int>::Test({{"0", 0}, {"4294967295", 4294967295}});
            TConverterTester<signed long int>::Test({{"-2147483648", -2147483648}, {"2147483647", 2147483647}});
            TConverterTester<unsigned long long int>::Test({{"0", 0}, {"18446744073709551615", 18446744073709551615u}});
            TConverterTester<signed long long int>::Test({
                {"-9223372036854775808", -9223372036854775808u},
                {"9223372036854775807", 9223372036854775807},
            });

            TConverterTester<size_t>::Test({{"0", 0}, {"65535", 65535}});

            TConverterTester<float>::Test({{"-1.12312", -1.12312}, {"3434.25674", 3434.25674}});
            TConverterTester<double>::Test({{"-1.12312E+42", -1.12312E+42}, {"3.25E+120", 3.25E+120}});
        }

        Y_UNIT_TEST(StringsTest) {
            TConverterTester<TStringBuf>::TestEncoding({
                {R"("Let's permit using of Rust in Arcadia")", "Let's permit using of Rust in Arcadia"},
            });
            TConverterTester<TString>::Test({
                {
                    R"("Всякое непрерывное отображение замкнутого n-мерного шара в себя обладает неподвижной точкой")",
                    "Всякое непрерывное отображение замкнутого n-мерного шара в себя обладает неподвижной точкой",
                },
            });
        }

        Y_UNIT_TEST(MaybeTest) {
            TConverterTester<TMaybe<bool>>::Test({
                {"true", TMaybe<bool>(true)},
                {"null", Nothing()},
                {"false", TMaybe<bool>(false)},
            });
        }

        Y_UNIT_TEST(ArraysTest) {
            TConverterTester<TVector<bool>>::Test({{"[true, true, false]", {true, true, false}}});
            TConverterTester<TList<TString>>::Test({{R"(["a", "b"])", {"a", "b"}}});
            TConverterTester<TDeque<bool>>::Test({{"[false, true, false]", {false, true, false}}});
        }

        Y_UNIT_TEST(MapsTest) {
            TConverterTester<THashMap<TStringBuf, bool>>::TestEncoding({
                {R"({"a": true, "b": false})", {{"a", true}, {"b", false}}},
            });
            TConverterTester<THashMap<TString, bool>>::Test({
                {R"({"a": true, "b": false})", {{"a", true}, {"b", false}}},
            });
            TConverterTester<TMap<TStringBuf, TStringBuf>>::TestEncoding({
                {R"({"a": "A", "b": "B"})", {{"a", "A"}, {"b", "B"}}},
            });
            TConverterTester<TMap<TString, TString>>::Test({
                {R"({"a": "A", "b": "B"})", {{"a", "A"}, {"b", "B"}}},
            });
        }

        Y_UNIT_TEST(NestedTest) {
            TConverterTester<TVector<THashMap<TString, TVector<ui64>>>>::Test({
                {
                    R"([
                        {
                            "Three": [0, 1, 2],
                            "Five": [0, 1, 2, 3, 4]
                        },
                        {
                            "Four": [0, 1, 2, 3],
                            "Six": [0, 1, 2, 3, 4, 5]
                        },
                    ])",
                    {
                        {
                            {"Three", {0, 1, 2}},
                            {"Five", {0, 1, 2, 3, 4}},
                        },
                        {
                            {"Four", {0, 1, 2, 3}},
                            {"Six", {0, 1, 2, 3, 4, 5}},
                        },
                    },
                },
            });
        }
    }
}