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
257
258
259
260
261
262
263
264
265
266
267
268
|
#include "testing/testing.hpp"
#include "coding/serdes_json.hpp"
#include "coding/writer.hpp"
#include "base/string_utils.hpp"
#include "base/visitor.hpp"
#include <array>
#include <chrono>
#include <deque>
#include <limits>
#include <map>
#include <memory>
#include <unordered_set>
#include <vector>
namespace serdes_json_test
{
using namespace std;
namespace
{
template <typename Ptr>
bool SamePtrValue(Ptr const & lhs, Ptr const & rhs)
{
return (!lhs && !rhs) || (lhs && rhs && *lhs == *rhs);
}
template <typename T>
bool TestSerDes(T const & value)
{
string jsonStr;
{
using Sink = MemWriter<string>;
Sink sink(jsonStr);
coding::SerializerJson<Sink> ser(sink);
ser(value);
}
T deserializedValue;
try
{
coding::DeserializerJson des(jsonStr);
des(deserializedValue);
}
catch (coding::JsonException const & exception)
{
LOG(LWARNING, ("Exception while parsing json string, reason:", exception.what(), "json:", jsonStr));
return false;
}
return deserializedValue == value;
}
enum class TestEnum
{
Value0 = 0,
Value1,
Value2,
Count
};
struct ValueTypes
{
DECLARE_VISITOR(visitor(m_boolValue, "boolValue"), visitor(m_uint8Value, "uint8Value"),
visitor(m_uint32Value, "uint32Value"), visitor(m_uint64Value, "uint64Value"),
visitor(m_int8Value, "int8Value"), visitor(m_int32Value, "int32Value"),
visitor(m_int64Value, "int64Value"), visitor(m_doubleValue, "doubleValue"),
visitor(m_stringValue, "stringValue"), visitor(m_enumValue, "enumValue"),
visitor(m_timePointValue, "timePointValue"))
ValueTypes() = default;
ValueTypes(uint32_t testCounter)
: m_boolValue(static_cast<bool>(testCounter % 2))
, m_uint8Value(numeric_limits<uint8_t>::max() - static_cast<uint8_t>(testCounter))
, m_uint32Value(numeric_limits<uint32_t>::max() - testCounter)
, m_uint64Value(numeric_limits<uint64_t>::max() - testCounter)
, m_int8Value(numeric_limits<int8_t>::min() + static_cast<int8_t>(testCounter))
, m_int32Value(numeric_limits<int32_t>::min() + static_cast<int32_t>(testCounter))
, m_int64Value(numeric_limits<int64_t>::min() + static_cast<int64_t>(testCounter))
, m_doubleValue(numeric_limits<double>::max() - testCounter)
, m_stringValue(strings::to_string(testCounter))
, m_enumValue(static_cast<TestEnum>(testCounter % static_cast<uint32_t>(TestEnum::Count)))
, m_timePointValue(chrono::system_clock::now())
{}
bool operator==(ValueTypes const & rhs) const
{
return m_boolValue == rhs.m_boolValue && m_uint8Value == rhs.m_uint8Value && m_uint32Value == rhs.m_uint32Value &&
m_uint64Value == rhs.m_uint64Value && m_int8Value == rhs.m_int8Value && m_int32Value == rhs.m_int32Value &&
m_int64Value == rhs.m_int64Value && m_doubleValue == rhs.m_doubleValue &&
m_stringValue == rhs.m_stringValue && m_enumValue == rhs.m_enumValue &&
m_timePointValue == rhs.m_timePointValue;
}
bool m_boolValue;
uint8_t m_uint8Value;
uint32_t m_uint32Value;
uint64_t m_uint64Value;
int8_t m_int8Value;
int32_t m_int32Value;
int64_t m_int64Value;
double m_doubleValue;
string m_stringValue;
TestEnum m_enumValue;
chrono::system_clock::time_point m_timePointValue;
};
struct ObjectTypes
{
DECLARE_VISITOR(visitor(m_pointValue, "pointValue"), visitor(m_latLonValue, "latLonValue"),
visitor(m_pairValue, "pairValue"))
ObjectTypes() = default;
ObjectTypes(uint32_t testCounter)
: m_pointValue(testCounter, testCounter)
, m_latLonValue(testCounter, testCounter)
, m_pairValue(testCounter, strings::to_string(testCounter))
{}
bool operator==(ObjectTypes const & rhs) const
{
return m_pointValue == rhs.m_pointValue && m_latLonValue == rhs.m_latLonValue && m_pairValue == rhs.m_pairValue;
}
m2::PointD m_pointValue;
ms::LatLon m_latLonValue;
pair<uint32_t, string> m_pairValue;
};
struct PointerTypes
{
DECLARE_VISITOR(visitor(m_uniquePtrValue, "uniquePtrValue"), visitor(m_sharedPtrValue, "sharedPtrValue"))
PointerTypes() = default;
PointerTypes(uint32_t testCounter)
{
m_uniquePtrValue = make_unique<ValueTypes>(testCounter);
m_sharedPtrValue = make_shared<ValueTypes>(testCounter);
}
bool operator==(PointerTypes const & rhs) const
{
return SamePtrValue(m_uniquePtrValue, rhs.m_uniquePtrValue) && SamePtrValue(m_sharedPtrValue, rhs.m_sharedPtrValue);
}
unique_ptr<ValueTypes> m_uniquePtrValue;
shared_ptr<ValueTypes> m_sharedPtrValue;
};
struct ArrayTypes
{
DECLARE_VISITOR(visitor(m_arrayValue, "arrayValue"), visitor(m_dequeValue, "dequeValue"),
visitor(m_vectorValue, "vectorValue"), visitor(m_mapValue, "mapValue"),
visitor(m_unorderedSetValue, "unorderedSetValue"))
ArrayTypes() = default;
ArrayTypes(uint32_t testCounter)
: m_arrayValue({{testCounter, testCounter + 1, testCounter + 2}})
, m_dequeValue({testCounter + 2, testCounter + 1, testCounter})
, m_vectorValue({testCounter, testCounter + 2, testCounter + 1})
, m_mapValue({{testCounter, testCounter}, {testCounter + 1, testCounter + 1}})
, m_unorderedSetValue({testCounter + 2, testCounter, testCounter + 1})
{}
bool operator==(ArrayTypes const & rhs) const
{
return m_arrayValue == rhs.m_arrayValue && m_dequeValue == rhs.m_dequeValue && m_vectorValue == rhs.m_vectorValue &&
m_mapValue == rhs.m_mapValue && m_unorderedSetValue == rhs.m_unorderedSetValue;
}
array<uint32_t, 3> m_arrayValue;
deque<uint32_t> m_dequeValue;
vector<uint32_t> m_vectorValue;
map<uint32_t, uint32_t> m_mapValue;
unordered_set<uint32_t> m_unorderedSetValue;
};
} // namespace
UNIT_TEST(SerdesJsonTest)
{
{
ValueTypes valueTypes(0);
TEST(TestSerDes(valueTypes), ());
ObjectTypes objectTypes(0);
TEST(TestSerDes(objectTypes), ());
PointerTypes pointersTypes(0);
TEST(TestSerDes(pointersTypes), ());
ArrayTypes arrayTypes(0);
TEST(TestSerDes(arrayTypes), ());
}
{
pair<string, m2::PointD> testValue = {"test", m2::PointD(1.0, 2.0)};
TEST(TestSerDes(testValue), ());
}
{
pair<m2::PointD, m2::PointD> testValue = {m2::PointD(1.0, 2.0), m2::PointD(2.0, 3.0)};
TEST(TestSerDes(testValue), ());
}
{
pair<string, pair<string, string>> testValue = {"test", {"test1", "test2"}};
TEST(TestSerDes(testValue), ());
}
{
pair<string, ValueTypes> testValue = {"test", ValueTypes(0)};
TEST(TestSerDes(testValue), ());
}
{
array<ObjectTypes, 2> testValue = {{ObjectTypes(0), ObjectTypes(1)}};
TEST(TestSerDes(testValue), ());
}
{
struct Hasher
{
size_t operator()(pair<string, string> const & item) const { return m_hasher(item.first + item.second); }
hash<string> m_hasher;
};
unordered_set<pair<string, string>, Hasher> testValue = {{"ab", "ab"}, {"ef", "ef"}, {"cd", "cd"}};
TEST(TestSerDes(testValue), ());
}
{
vector<vector<uint32_t>> testValue;
for (uint32_t i = 0; i < 5; ++i)
testValue.push_back({i, i, i});
TEST(TestSerDes(testValue), ());
}
{
vector<ValueTypes> valuesVector;
for (uint32_t i = 0; i < 5; ++i)
valuesVector.push_back(ValueTypes(i));
TEST(TestSerDes(valuesVector), ());
}
{
map<uint32_t, ValueTypes> valuesMap;
for (uint32_t i = 0; i < 5; ++i)
valuesMap.insert(make_pair(i, ValueTypes(i)));
TEST(TestSerDes(valuesMap), ());
}
{
vector<ObjectTypes> objectsVector;
for (uint32_t i = 0; i < 5; ++i)
objectsVector.push_back(ObjectTypes(i));
TEST(TestSerDes(objectsVector), ());
}
{
map<uint32_t, ObjectTypes> objectsMap;
for (uint32_t i = 0; i < 5; ++i)
objectsMap.insert(make_pair(i, ObjectTypes(i)));
TEST(TestSerDes(objectsMap), ());
}
}
} // namespace serdes_json_test
|