test/zserio/ReflectableUtilTest.cpp
Line | Count | Source |
1 | | #include <cmath> |
2 | | |
3 | | #include "gtest/gtest.h" |
4 | | #include "test_object/std_allocator/ReflectableUtilBitmask.h" |
5 | | #include "test_object/std_allocator/ReflectableUtilEnum.h" |
6 | | #include "test_object/std_allocator/ReflectableUtilUnion.h" |
7 | | #include "zserio/Reflectable.h" |
8 | | #include "zserio/ReflectableUtil.h" |
9 | | #include "zserio/TypeInfo.h" |
10 | | |
11 | | using test_object::std_allocator::ReflectableUtilBitmask; |
12 | | using test_object::std_allocator::ReflectableUtilChoice; |
13 | | using test_object::std_allocator::ReflectableUtilEnum; |
14 | | using test_object::std_allocator::ReflectableUtilObject; |
15 | | using test_object::std_allocator::ReflectableUtilUnion; |
16 | | |
17 | | namespace zserio |
18 | | { |
19 | | |
20 | | TEST(ReflectableUtilTest, unequalTypeInfo) |
21 | 1 | { |
22 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(false), ReflectableFactory::getUInt8(13))); |
23 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(0), ReflectableFactory::getInt16(0))); |
24 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(0), ReflectableFactory::getUInt8(0))); |
25 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt64(0), ReflectableFactory::getVarSize(0))); |
26 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(0), ReflectableFactory::getFloat32(0.0))); |
27 | 1 | ASSERT_FALSE( |
28 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat16(0.0F), ReflectableFactory::getFloat32(0.0F))); |
29 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getString(""), ReflectableFactory::getInt8(0))); |
30 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
31 | 1 | ReflectableFactory::getBitBuffer(BitBuffer()), ReflectableFactory::getString(""))); |
32 | 1 | } |
33 | | |
34 | | TEST(ReflectableUtilTest, equalsNullValues) |
35 | 1 | { |
36 | 1 | ASSERT_TRUE(ReflectableUtil::equal(nullptr, nullptr)); |
37 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(false), nullptr)); |
38 | 1 | ASSERT_FALSE(ReflectableUtil::equal(nullptr, ReflectableFactory::getInt8(0))); |
39 | 1 | } |
40 | | |
41 | | TEST(ReflectableUtilTest, equalBools) |
42 | 1 | { |
43 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getBool(true), ReflectableFactory::getBool(true))); |
44 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(false), ReflectableFactory::getBool(true))); |
45 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(true), ReflectableFactory::getBool(false))); |
46 | 1 | } |
47 | | |
48 | | TEST(ReflectableUtilTest, equalSingedIntegrals) |
49 | 1 | { |
50 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
51 | 1 | ReflectableFactory::getInt8(INT8_MIN), ReflectableFactory::getInt8(INT8_MIN))); |
52 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
53 | 1 | ReflectableFactory::getInt8(INT8_MAX), ReflectableFactory::getInt8(INT8_MAX))); |
54 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(-1), ReflectableFactory::getInt8(0))); |
55 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
56 | 1 | ReflectableFactory::getInt8(INT8_MIN), ReflectableFactory::getInt8(INT8_MAX))); |
57 | | |
58 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
59 | 1 | ReflectableFactory::getInt16(INT16_MIN), ReflectableFactory::getInt16(INT16_MIN))); |
60 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
61 | 1 | ReflectableFactory::getInt16(INT16_MAX), ReflectableFactory::getInt16(INT16_MAX))); |
62 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt16(-1), ReflectableFactory::getInt16(0))); |
63 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
64 | 1 | ReflectableFactory::getInt16(INT16_MIN), ReflectableFactory::getInt16(INT16_MAX))); |
65 | | |
66 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
67 | 1 | ReflectableFactory::getInt32(INT32_MIN), ReflectableFactory::getInt32(INT32_MIN))); |
68 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
69 | 1 | ReflectableFactory::getInt32(INT32_MAX), ReflectableFactory::getInt32(INT32_MAX))); |
70 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt32(-1), ReflectableFactory::getInt32(0))); |
71 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
72 | 1 | ReflectableFactory::getInt32(INT32_MIN), ReflectableFactory::getInt32(INT32_MAX))); |
73 | | |
74 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
75 | 1 | ReflectableFactory::getInt64(INT64_MIN), ReflectableFactory::getInt64(INT64_MIN))); |
76 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
77 | 1 | ReflectableFactory::getInt64(INT64_MAX), ReflectableFactory::getInt64(INT64_MAX))); |
78 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt64(-1), ReflectableFactory::getInt64(0))); |
79 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
80 | 1 | ReflectableFactory::getInt64(INT64_MIN), ReflectableFactory::getInt64(INT64_MAX))); |
81 | 1 | } |
82 | | |
83 | | TEST(ReflectableUtilTest, equalUnsignedIntegrals) |
84 | 1 | { |
85 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt8(0), ReflectableFactory::getUInt8(0))); |
86 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
87 | 1 | ReflectableFactory::getUInt8(UINT8_MAX), ReflectableFactory::getUInt8(UINT8_MAX))); |
88 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt8(0), ReflectableFactory::getUInt8(1))); |
89 | 1 | ASSERT_FALSE( |
90 | 1 | ReflectableUtil::equal(ReflectableFactory::getUInt8(0), ReflectableFactory::getUInt8(UINT8_MAX))); |
91 | | |
92 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt16(0), ReflectableFactory::getUInt16(0))); |
93 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
94 | 1 | ReflectableFactory::getUInt16(UINT16_MAX), ReflectableFactory::getUInt16(UINT16_MAX))); |
95 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt16(0), ReflectableFactory::getUInt16(1))); |
96 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
97 | 1 | ReflectableFactory::getUInt16(0), ReflectableFactory::getUInt16(UINT16_MAX))); |
98 | | |
99 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt32(0), ReflectableFactory::getUInt32(0))); |
100 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
101 | 1 | ReflectableFactory::getUInt32(UINT32_MAX), ReflectableFactory::getUInt32(UINT32_MAX))); |
102 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt32(0), ReflectableFactory::getUInt32(1))); |
103 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
104 | 1 | ReflectableFactory::getUInt32(0), ReflectableFactory::getUInt32(UINT32_MAX))); |
105 | | |
106 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt64(0), ReflectableFactory::getUInt64(0))); |
107 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
108 | 1 | ReflectableFactory::getUInt64(UINT64_MAX), ReflectableFactory::getUInt64(UINT64_MAX))); |
109 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt64(0), ReflectableFactory::getUInt64(1))); |
110 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
111 | 1 | ReflectableFactory::getUInt64(0), ReflectableFactory::getUInt64(UINT64_MAX))); |
112 | 1 | } |
113 | | |
114 | | TEST(ReflectableUtilTest, equalFloatingPoints) |
115 | 1 | { |
116 | 1 | ASSERT_TRUE( |
117 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat16(0.0F), ReflectableFactory::getFloat16(0.0F))); |
118 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
119 | 1 | ReflectableFactory::getFloat16(-1.0F), ReflectableFactory::getFloat16(1.0F))); |
120 | | |
121 | 1 | ASSERT_TRUE( |
122 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat32(0.0F), ReflectableFactory::getFloat32(0.0F))); |
123 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
124 | 1 | ReflectableFactory::getFloat32(-1.0F), ReflectableFactory::getFloat32(1.0F))); |
125 | | |
126 | 1 | ASSERT_TRUE( |
127 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat64(0.0), ReflectableFactory::getFloat64(0.0))); |
128 | 1 | ASSERT_FALSE( |
129 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat64(-1.0), ReflectableFactory::getFloat64(1.0))); |
130 | | |
131 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getFloat64(std::numeric_limits<double>::epsilon()), |
132 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::epsilon()))); |
133 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getFloat64(std::numeric_limits<double>::min()), |
134 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::min()))); |
135 | | |
136 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
137 | 1 | ReflectableFactory::getFloat64(1.0 + std::numeric_limits<double>::epsilon()), |
138 | 1 | ReflectableFactory::getFloat64(1.0))); |
139 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
140 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::denorm_min() * 2), |
141 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::denorm_min()))); |
142 | | |
143 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(NAN)), |
144 | 1 | ReflectableFactory::getFloat64(static_cast<double>(NAN)))); |
145 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(INFINITY)), |
146 | 1 | ReflectableFactory::getFloat64(static_cast<double>(INFINITY)))); |
147 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)), |
148 | 1 | ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)))); |
149 | | |
150 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(0.0), |
151 | 1 | ReflectableFactory::getFloat64(static_cast<double>(INFINITY)))); |
152 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(INFINITY)), |
153 | 1 | ReflectableFactory::getFloat64(0.0))); |
154 | | |
155 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
156 | 1 | ReflectableFactory::getFloat64(0.0), ReflectableFactory::getFloat64(static_cast<double>(NAN)))); |
157 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
158 | 1 | ReflectableFactory::getFloat64(static_cast<double>(NAN)), ReflectableFactory::getFloat64(0.0))); |
159 | | |
160 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(NAN)), |
161 | 1 | ReflectableFactory::getFloat64(static_cast<double>(INFINITY)))); |
162 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(INFINITY)), |
163 | 1 | ReflectableFactory::getFloat64(static_cast<double>(NAN)))); |
164 | | |
165 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(INFINITY)), |
166 | 1 | ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)))); |
167 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)), |
168 | 1 | ReflectableFactory::getFloat64(static_cast<double>(INFINITY)))); |
169 | 1 | } |
170 | | |
171 | | TEST(ReflectableUtilTest, equalStrings) |
172 | 1 | { |
173 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getString(""), ReflectableFactory::getString(""))); |
174 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
175 | 1 | ReflectableFactory::getString("test"), ReflectableFactory::getString("test"))); |
176 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getString(""), ReflectableFactory::getString("a"))); |
177 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getString("1"), ReflectableFactory::getString(""))); |
178 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
179 | 1 | ReflectableFactory::getString("test"), ReflectableFactory::getString("test2"))); |
180 | 1 | } |
181 | | |
182 | | TEST(ReflectableUtilTest, equalBitBuffers) |
183 | 1 | { |
184 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
185 | 1 | ReflectableFactory::getBitBuffer(BitBuffer()), ReflectableFactory::getBitBuffer(BitBuffer()))); |
186 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getBitBuffer(BitBuffer({0xAB}, 8)), |
187 | 1 | ReflectableFactory::getBitBuffer(BitBuffer({0xAB}, 8)))); |
188 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBitBuffer(BitBuffer({0xAB}, 8)), |
189 | 1 | ReflectableFactory::getBitBuffer(BitBuffer()))); |
190 | 1 | } |
191 | | |
192 | | TEST(ReflectableUtilTest, equalBytes) |
193 | 1 | { |
194 | 1 | vector<uint8_t> bytesData1{{0xCA, 0xFE}}; |
195 | 1 | vector<uint8_t> bytesData2{{0xCA, 0xDE}}; |
196 | 1 | vector<uint8_t> bytesEmpty; |
197 | | |
198 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
199 | 1 | ReflectableFactory::getBytes(bytesEmpty), ReflectableFactory::getBytes(bytesEmpty))); |
200 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
201 | 1 | ReflectableFactory::getBytes(bytesData1), ReflectableFactory::getBytes(bytesData1))); |
202 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
203 | 1 | ReflectableFactory::getBytes(bytesData1), ReflectableFactory::getBytes(bytesEmpty))); |
204 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
205 | 1 | ReflectableFactory::getBytes(bytesData1), ReflectableFactory::getBytes(bytesData2))); |
206 | 1 | } |
207 | | |
208 | | TEST(ReflectableUtilTest, equalEnums) |
209 | 1 | { |
210 | 1 | const ReflectableUtilEnum oneEnum = ReflectableUtilEnum::ONE; |
211 | 1 | const ReflectableUtilEnum twoEnum = ReflectableUtilEnum::TWO; |
212 | | |
213 | 1 | ASSERT_TRUE(ReflectableUtil::equal(enumReflectable(oneEnum), enumReflectable(oneEnum))); |
214 | 1 | ASSERT_TRUE(ReflectableUtil::equal(enumReflectable(twoEnum), enumReflectable(twoEnum))); |
215 | 1 | ASSERT_FALSE(ReflectableUtil::equal(enumReflectable(oneEnum), enumReflectable(twoEnum))); |
216 | 1 | } |
217 | | |
218 | | TEST(ReflectableUtilTest, equalBitmasks) |
219 | 1 | { |
220 | 1 | const ReflectableUtilBitmask readBitmask = ReflectableUtilBitmask::Values::READ; |
221 | 1 | const ReflectableUtilBitmask writeBitmask = ReflectableUtilBitmask::Values::WRITE; |
222 | | |
223 | 1 | ASSERT_TRUE(ReflectableUtil::equal(readBitmask.reflectable(), readBitmask.reflectable())); |
224 | 1 | ASSERT_TRUE(ReflectableUtil::equal(writeBitmask.reflectable(), writeBitmask.reflectable())); |
225 | 1 | ASSERT_FALSE(ReflectableUtil::equal(writeBitmask.reflectable(), readBitmask.reflectable())); |
226 | 1 | } |
227 | | |
228 | | TEST(ReflectableUtilTest, equalCompounds) |
229 | 1 | { |
230 | 1 | ReflectableUtilUnion compound1 = ReflectableUtilUnion(); |
231 | 1 | compound1.setReflectableUtilEnum(ReflectableUtilEnum::ONE); |
232 | 1 | compound1.initializeChildren(); |
233 | | |
234 | 1 | ReflectableUtilUnion compound2 = ReflectableUtilUnion(); |
235 | 1 | compound2.setReflectableUtilEnum(ReflectableUtilEnum::TWO); // different enum value |
236 | 1 | compound2.initializeChildren(); |
237 | | |
238 | 1 | ReflectableUtilUnion compound3 = ReflectableUtilUnion(); |
239 | 1 | compound3.setReflectableUtilBitmask(ReflectableUtilBitmask::Values::READ); |
240 | 1 | compound3.initializeChildren(); |
241 | | |
242 | 1 | ReflectableUtilUnion compound4 = ReflectableUtilUnion(); |
243 | 1 | compound4.setReflectableUtilObject(ReflectableUtilObject(0, ReflectableUtilChoice())); |
244 | 1 | compound4.initializeChildren(); |
245 | | |
246 | 1 | ReflectableUtilUnion compound5 = ReflectableUtilUnion(); |
247 | 1 | compound5.setReflectableUtilObject(ReflectableUtilObject(1, ReflectableUtilChoice())); |
248 | 1 | compound5.getReflectableUtilObject().getReflectableUtilChoice().setArray(std::vector<uint32_t>()); |
249 | 1 | compound5.initializeChildren(); |
250 | | |
251 | 1 | ReflectableUtilUnion compound6 = ReflectableUtilUnion(); |
252 | 1 | compound6.setReflectableUtilObject(ReflectableUtilObject(1, ReflectableUtilChoice())); |
253 | 1 | compound6.getReflectableUtilObject().getReflectableUtilChoice().setArray(std::vector<uint32_t>{{1, 2, 3}}); |
254 | 1 | compound6.initializeChildren(); |
255 | | |
256 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound1.reflectable(), compound1.reflectable())); |
257 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound2.reflectable(), compound2.reflectable())); |
258 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound3.reflectable(), compound3.reflectable())); |
259 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound4.reflectable(), compound4.reflectable())); |
260 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound5.reflectable(), compound5.reflectable())); |
261 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound6.reflectable(), compound6.reflectable())); |
262 | | |
263 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound2.reflectable())); |
264 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound3.reflectable())); |
265 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound4.reflectable())); |
266 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound5.reflectable())); |
267 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound6.reflectable())); |
268 | | |
269 | | // unequal fields in choice |
270 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound5.reflectable(), compound6.reflectable())); |
271 | | |
272 | | // unequal parameters in choice |
273 | 1 | ReflectableUtilChoice choice1; |
274 | 1 | choice1.initialize(0); |
275 | 1 | ReflectableUtilChoice choice2; |
276 | 1 | choice2.initialize(3); |
277 | 1 | ASSERT_FALSE(ReflectableUtil::equal(choice1.reflectable(), choice2.reflectable())); |
278 | | |
279 | | // array and not array |
280 | 1 | std::vector<ReflectableUtilUnion> compoundArray(1); |
281 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
282 | 1 | compound1.reflectable(), ReflectableFactory::getCompoundArray(compoundArray))); |
283 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
284 | 1 | ReflectableFactory::getCompoundArray(compoundArray), compound1.reflectable())); |
285 | 1 | } |
286 | | |
287 | | TEST(ReflectableUtilTest, equalArrays) |
288 | 1 | { |
289 | 1 | std::vector<uint32_t> array1 = {1, 2, 3}; |
290 | 1 | std::vector<uint32_t> array2 = {1, 2, 4}; |
291 | 1 | std::vector<uint32_t> array3 = {1, 2}; |
292 | | |
293 | 1 | auto array1Reflectable = ReflectableFactory::getUInt32Array(array1); |
294 | 1 | auto array2Reflectable = ReflectableFactory::getUInt32Array(array2); |
295 | 1 | auto array3Reflectable = ReflectableFactory::getUInt32Array(array3); |
296 | | |
297 | 1 | ASSERT_TRUE(ReflectableUtil::equal(array1Reflectable, array1Reflectable)); |
298 | 1 | ASSERT_TRUE(ReflectableUtil::equal(array2Reflectable, array2Reflectable)); |
299 | 1 | ASSERT_TRUE(ReflectableUtil::equal(array3Reflectable, array3Reflectable)); |
300 | | |
301 | 1 | ASSERT_FALSE(ReflectableUtil::equal(array1Reflectable, array2Reflectable)); |
302 | 1 | ASSERT_FALSE(ReflectableUtil::equal(array1Reflectable, array3Reflectable)); |
303 | 1 | } |
304 | | |
305 | | TEST(ReflectableUtilTest, equalWrong) |
306 | 1 | { |
307 | 1 | using allocator_type = ::std::allocator<uint8_t>; |
308 | 1 | class WrongTypeInfo : public TypeInfoBase<allocator_type> |
309 | 1 | { |
310 | 1 | public: |
311 | 1 | WrongTypeInfo(StringView schemaName, SchemaType schemaType, CppType cppType) : |
312 | 1 | TypeInfoBase(schemaName, schemaType, cppType) |
313 | 2 | {} |
314 | 1 | }; |
315 | | |
316 | 1 | const WrongTypeInfo wrongTypeInfo = {"int<>"_sv, SchemaType::DYNAMIC_SIGNED_BITFIELD, CppType::PUBSUB}; |
317 | 1 | const WrongTypeInfo wrongTypeInfoDiffName = { |
318 | 1 | "diff"_sv, SchemaType::DYNAMIC_SIGNED_BITFIELD, CppType::PUBSUB}; |
319 | | |
320 | 1 | class Reflectable : public ::zserio::ReflectableAllocatorHolderBase<allocator_type> |
321 | 1 | { |
322 | 1 | public: |
323 | 1 | explicit Reflectable(const TypeInfoBase<allocator_type>& typeInfo, const allocator_type& allocator) : |
324 | 1 | ::zserio::ReflectableAllocatorHolderBase<allocator_type>(typeInfo, allocator) |
325 | 2 | {} |
326 | 1 | }; |
327 | | |
328 | 1 | auto wrongReflectable = |
329 | 1 | std::allocate_shared<Reflectable>(allocator_type(), wrongTypeInfo, allocator_type()); |
330 | 1 | auto wrongReflectableDiffName = |
331 | 1 | std::allocate_shared<Reflectable>(allocator_type(), wrongTypeInfoDiffName, allocator_type()); |
332 | | |
333 | 1 | ASSERT_THROW(ReflectableUtil::equal(wrongReflectable, wrongReflectable), CppRuntimeException); |
334 | 1 | ASSERT_FALSE(ReflectableUtil::equal(wrongReflectable, wrongReflectableDiffName)); |
335 | 1 | } |
336 | | |
337 | | TEST(ReflectableUtilTest, getValueArithmeticType) |
338 | 1 | { |
339 | 1 | ASSERT_FALSE(ReflectableUtil::getValue<bool>(ReflectableFactory::getBool(false))); |
340 | | |
341 | 1 | ASSERT_EQ(1, ReflectableUtil::getValue<uint8_t>(ReflectableFactory::getUInt8(1))); |
342 | 1 | ASSERT_EQ(12, ReflectableUtil::getValue<uint16_t>(ReflectableFactory::getUInt16(12))); |
343 | 1 | ASSERT_EQ(123, ReflectableUtil::getValue<uint32_t>(ReflectableFactory::getUInt32(123))); |
344 | 1 | ASSERT_EQ(1234, ReflectableUtil::getValue<uint64_t>(ReflectableFactory::getUInt64(1234))); |
345 | | |
346 | 1 | ASSERT_EQ(-1, ReflectableUtil::getValue<int8_t>(ReflectableFactory::getInt8(-1))); |
347 | 1 | ASSERT_EQ(-12, ReflectableUtil::getValue<int16_t>(ReflectableFactory::getInt16(-12))); |
348 | 1 | ASSERT_EQ(-123, ReflectableUtil::getValue<int32_t>(ReflectableFactory::getInt32(-123))); |
349 | 1 | ASSERT_EQ(-1234, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getInt64(-1234))); |
350 | | |
351 | 1 | ASSERT_EQ(12, ReflectableUtil::getValue<uint16_t>(ReflectableFactory::getVarUInt16(12))); |
352 | 1 | ASSERT_EQ(123, ReflectableUtil::getValue<uint32_t>(ReflectableFactory::getVarUInt32(123))); |
353 | 1 | ASSERT_EQ(1234, ReflectableUtil::getValue<uint64_t>(ReflectableFactory::getVarUInt64(1234))); |
354 | 1 | ASSERT_EQ(UINT64_MAX, ReflectableUtil::getValue<uint64_t>(ReflectableFactory::getVarUInt(UINT64_MAX))); |
355 | | |
356 | 1 | ASSERT_EQ(-12, ReflectableUtil::getValue<int16_t>(ReflectableFactory::getVarInt16(-12))); |
357 | 1 | ASSERT_EQ(-123, ReflectableUtil::getValue<int32_t>(ReflectableFactory::getVarInt32(-123))); |
358 | 1 | ASSERT_EQ(-1234, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getVarInt64(-1234))); |
359 | 1 | ASSERT_EQ(INT64_MIN, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getVarInt(INT64_MIN))); |
360 | 1 | ASSERT_EQ(INT64_MAX, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getVarInt(INT64_MAX))); |
361 | | |
362 | 1 | ASSERT_EQ(0, ReflectableUtil::getValue<uint32_t>(ReflectableFactory::getVarSize(0))); |
363 | | |
364 | 1 | ASSERT_EQ(1.0F, ReflectableUtil::getValue<float>(ReflectableFactory::getFloat16(1.0F))); |
365 | 1 | ASSERT_EQ(3.5F, ReflectableUtil::getValue<float>(ReflectableFactory::getFloat32(3.5F))); |
366 | 1 | ASSERT_EQ(9.875, ReflectableUtil::getValue<double>(ReflectableFactory::getFloat64(9.875))); |
367 | 1 | } |
368 | | |
369 | | TEST(ReflectableUtilTest, getValueString) |
370 | 1 | { |
371 | 1 | ASSERT_EQ("test"_sv, ReflectableUtil::getValue<StringView>(ReflectableFactory::getString("test"))); |
372 | 1 | } |
373 | | |
374 | | TEST(ReflectableUtilTest, getValueBitBuffer) |
375 | 1 | { |
376 | 1 | const BitBuffer bitBuffer; |
377 | 1 | auto reflectable = ReflectableFactory::getBitBuffer(bitBuffer); |
378 | 1 | const BitBuffer& bitBufferRef = ReflectableUtil::getValue<BitBuffer>(reflectable); |
379 | 1 | ASSERT_EQ(bitBuffer, bitBufferRef); |
380 | 1 | } |
381 | | |
382 | | TEST(ReflectableUtilTest, getValueEnum) |
383 | 1 | { |
384 | 1 | ReflectableUtilEnum reflectableUtilEnum = ReflectableUtilEnum::ONE; |
385 | 1 | auto reflectable = enumReflectable(reflectableUtilEnum); |
386 | 1 | ASSERT_EQ(reflectableUtilEnum, ReflectableUtil::getValue<ReflectableUtilEnum>(reflectable)); |
387 | 1 | } |
388 | | |
389 | | TEST(ReflectableUtilTest, getValueBitmask) |
390 | 1 | { |
391 | 1 | ReflectableUtilBitmask reflectableUtilBitmask = ReflectableUtilBitmask::Values::READ; |
392 | 1 | auto reflectable = reflectableUtilBitmask.reflectable(); |
393 | 1 | ASSERT_EQ(reflectableUtilBitmask, ReflectableUtil::getValue<ReflectableUtilBitmask>(reflectable)); |
394 | 1 | } |
395 | | |
396 | | TEST(ReflectableUtilTest, getValueCompound) |
397 | 1 | { |
398 | 1 | ReflectableUtilUnion reflectableUtilUnion; |
399 | 1 | auto reflectable = reflectableUtilUnion.reflectable(); |
400 | 1 | ASSERT_EQ(&reflectableUtilUnion, &ReflectableUtil::getValue<ReflectableUtilUnion>(reflectable)); |
401 | | |
402 | 1 | ReflectableUtilUnion& reflectableUtilUnionRef = |
403 | 1 | ReflectableUtil::getValue<ReflectableUtilUnion>(reflectable); |
404 | 1 | reflectableUtilUnionRef.setReflectableUtilBitmask(ReflectableUtilBitmask::Values::WRITE); |
405 | 1 | ASSERT_EQ(ReflectableUtilBitmask::Values::WRITE, reflectableUtilUnion.getReflectableUtilBitmask()); |
406 | | |
407 | 1 | auto constReflectable = static_cast<const ReflectableUtilUnion&>(reflectableUtilUnion).reflectable(); |
408 | 1 | const ReflectableUtilUnion& reflectableUtilUnionConstRef = |
409 | 1 | ReflectableUtil::getValue<ReflectableUtilUnion>(constReflectable); |
410 | 1 | ASSERT_EQ(ReflectableUtilBitmask::Values::WRITE, reflectableUtilUnionConstRef.getReflectableUtilBitmask()); |
411 | 1 | ASSERT_EQ(&reflectableUtilUnion, &reflectableUtilUnionConstRef); |
412 | 1 | } |
413 | | |
414 | | TEST(ReflectableUtilTest, getValueBuiltinArray) |
415 | 1 | { |
416 | 1 | std::vector<uint8_t> uint8Array{{1, 2, 3}}; |
417 | 1 | auto reflectable = ReflectableFactory::getUInt8Array(uint8Array); |
418 | 1 | std::vector<uint8_t>& uint8ArrayRef = ReflectableUtil::getValue<std::vector<uint8_t>>(reflectable); |
419 | 1 | ASSERT_EQ(&uint8Array, &uint8ArrayRef); |
420 | | |
421 | 1 | auto constReflectable = |
422 | 1 | ReflectableFactory::getUInt8Array(static_cast<const std::vector<uint8_t>&>(uint8Array)); |
423 | 1 | const std::vector<uint8_t>& uint8ArrayConstRef = |
424 | 1 | ReflectableUtil::getValue<std::vector<uint8_t>>(constReflectable); |
425 | 1 | ASSERT_EQ(&uint8Array, &uint8ArrayConstRef); |
426 | 1 | } |
427 | | |
428 | | TEST(ReflectableUtilTest, getValueCompoundArray) |
429 | 1 | { |
430 | 1 | std::vector<ReflectableUtilUnion> reflectableUtilUnionArray; |
431 | 1 | auto reflectable = ReflectableFactory::getCompoundArray(reflectableUtilUnionArray); |
432 | 1 | std::vector<ReflectableUtilUnion>& reflectableUtilUnionArrayRef = |
433 | 1 | ReflectableUtil::getValue<std::vector<ReflectableUtilUnion>>(reflectable); |
434 | 1 | ASSERT_EQ(&reflectableUtilUnionArray, &reflectableUtilUnionArrayRef); |
435 | 1 | } |
436 | | |
437 | | } // namespace zserio |