test/zserio/ZserioTreeCreatorTest.cpp
Line | Count | Source |
1 | | #include "gtest/gtest.h" |
2 | | #include "test_object/std_allocator/CreatorBitmask.h" |
3 | | #include "test_object/std_allocator/CreatorEnum.h" |
4 | | #include "test_object/std_allocator/CreatorNested.h" |
5 | | #include "test_object/std_allocator/CreatorObject.h" |
6 | | #include "test_object/std_allocator/CreatorUnsignedEnum.h" |
7 | | #include "zserio/StringConvertUtil.h" |
8 | | #include "zserio/StringView.h" |
9 | | #include "zserio/TypeInfo.h" |
10 | | #include "zserio/ZserioTreeCreator.h" |
11 | | |
12 | | using test_object::std_allocator::CreatorBitmask; |
13 | | using test_object::std_allocator::CreatorEnum; |
14 | | using test_object::std_allocator::CreatorNested; |
15 | | using test_object::std_allocator::CreatorObject; |
16 | | using test_object::std_allocator::CreatorUnsignedEnum; |
17 | | |
18 | | namespace zserio |
19 | | { |
20 | | |
21 | | TEST(ZserioTreeCreator, makeAnyValue) |
22 | 1 | { |
23 | 1 | const std::allocator<uint8_t> allocator; |
24 | | |
25 | | // bool |
26 | 1 | auto any = detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), true, allocator); |
27 | 1 | ASSERT_EQ(true, any.get<bool>()); |
28 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), 1, allocator), CppRuntimeException); |
29 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), 1.0F, allocator), CppRuntimeException); |
30 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), "1", allocator), CppRuntimeException); |
31 | 1 | ASSERT_THROW( |
32 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), BitBuffer(), allocator), CppRuntimeException); |
33 | | |
34 | | // uint8 |
35 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), 8, allocator); |
36 | 1 | ASSERT_EQ(8, any.get<uint8_t>()); |
37 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), -1, allocator), CppRuntimeException); |
38 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), 256, allocator), CppRuntimeException); |
39 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), 1.0F, allocator), CppRuntimeException); |
40 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), "1", allocator), CppRuntimeException); |
41 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), true, allocator), CppRuntimeException); |
42 | 1 | ASSERT_THROW( |
43 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), BitBuffer(), allocator), CppRuntimeException); |
44 | | |
45 | | // uint16 |
46 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), 16, allocator); |
47 | 1 | ASSERT_EQ(16, any.get<uint16_t>()); |
48 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), -1, allocator), CppRuntimeException); |
49 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), 65536, allocator), CppRuntimeException); |
50 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), 1.0F, allocator), CppRuntimeException); |
51 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), "1", allocator), CppRuntimeException); |
52 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), true, allocator), CppRuntimeException); |
53 | 1 | ASSERT_THROW( |
54 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), BitBuffer(), allocator), CppRuntimeException); |
55 | | |
56 | | // uint32 |
57 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), 32, allocator); |
58 | 1 | ASSERT_EQ(32, any.get<uint32_t>()); |
59 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), -1, allocator), CppRuntimeException); |
60 | 1 | ASSERT_THROW( |
61 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), 4294967296, allocator), CppRuntimeException); |
62 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), 1.0F, allocator), CppRuntimeException); |
63 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), "1", allocator), CppRuntimeException); |
64 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), true, allocator), CppRuntimeException); |
65 | 1 | ASSERT_THROW( |
66 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), BitBuffer(), allocator), CppRuntimeException); |
67 | | |
68 | | // uint64 |
69 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), 64, allocator); |
70 | 1 | ASSERT_EQ(64, any.get<uint64_t>()); |
71 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), -1, allocator), CppRuntimeException); |
72 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), 1.0F, allocator), CppRuntimeException); |
73 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), "1", allocator), CppRuntimeException); |
74 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), true, allocator), CppRuntimeException); |
75 | 1 | ASSERT_THROW( |
76 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), BitBuffer(), allocator), CppRuntimeException); |
77 | | |
78 | | // int8 |
79 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), 8, allocator); |
80 | 1 | ASSERT_EQ(8, any.get<int8_t>()); |
81 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), -129, allocator), CppRuntimeException); |
82 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), 128, allocator), CppRuntimeException); |
83 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), 1.0F, allocator), CppRuntimeException); |
84 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), "1", allocator), CppRuntimeException); |
85 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), true, allocator), CppRuntimeException); |
86 | 1 | ASSERT_THROW( |
87 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), BitBuffer(), allocator), CppRuntimeException); |
88 | | |
89 | | // int16 |
90 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), 16, allocator); |
91 | 1 | ASSERT_EQ(16, any.get<int16_t>()); |
92 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), -32769, allocator), CppRuntimeException); |
93 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), 32768, allocator), CppRuntimeException); |
94 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), 1.0F, allocator), CppRuntimeException); |
95 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), "1", allocator), CppRuntimeException); |
96 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), true, allocator), CppRuntimeException); |
97 | 1 | ASSERT_THROW( |
98 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), BitBuffer(), allocator), CppRuntimeException); |
99 | | |
100 | | // int32 |
101 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), 32, allocator); |
102 | 1 | ASSERT_EQ(32, any.get<int32_t>()); |
103 | 1 | ASSERT_THROW( |
104 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), -2147483649LL, allocator), CppRuntimeException); |
105 | 1 | ASSERT_THROW( |
106 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), 2147483648LL, allocator), CppRuntimeException); |
107 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), 1.0F, allocator), CppRuntimeException); |
108 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), "1", allocator), CppRuntimeException); |
109 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), true, allocator), CppRuntimeException); |
110 | 1 | ASSERT_THROW( |
111 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), BitBuffer(), allocator), CppRuntimeException); |
112 | | |
113 | | // int64 |
114 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), 64, allocator); |
115 | 1 | ASSERT_EQ(64, any.get<int64_t>()); |
116 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), 9223372036854775808ULL, allocator), |
117 | 1 | CppRuntimeException); |
118 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), 1.0F, allocator), CppRuntimeException); |
119 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), "1", allocator), CppRuntimeException); |
120 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), true, allocator), CppRuntimeException); |
121 | 1 | ASSERT_THROW( |
122 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), BitBuffer(), allocator), CppRuntimeException); |
123 | | |
124 | | // float |
125 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), 3.5F, allocator); |
126 | 1 | ASSERT_EQ(3.5F, any.get<float>()); |
127 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), 1, allocator); |
128 | 1 | ASSERT_EQ(1.0F, any.get<float>()); |
129 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), 1.0, allocator); |
130 | 1 | ASSERT_EQ(1.0F, any.get<float>()); |
131 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), "1", allocator), CppRuntimeException); |
132 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), true, allocator), CppRuntimeException); |
133 | 1 | ASSERT_THROW( |
134 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), BitBuffer(), allocator), CppRuntimeException); |
135 | | |
136 | | // double |
137 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), 3.14, allocator); |
138 | 1 | ASSERT_EQ(3.14, any.get<double>()); |
139 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), 1, allocator); |
140 | 1 | ASSERT_EQ(1.0, any.get<double>()); |
141 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), 1.0F, allocator); |
142 | 1 | ASSERT_EQ(1.0, any.get<double>()); |
143 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), "1", allocator), CppRuntimeException); |
144 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), true, allocator), CppRuntimeException); |
145 | 1 | ASSERT_THROW( |
146 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), BitBuffer(), allocator), CppRuntimeException); |
147 | | |
148 | | // string |
149 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), "text", allocator); |
150 | 1 | ASSERT_EQ("text", any.get<std::string>()); |
151 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), "text"_sv, allocator); |
152 | 1 | ASSERT_EQ("text", any.get<std::string>()); |
153 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), std::string("text"), allocator); |
154 | 1 | ASSERT_EQ("text", any.get<std::string>()); |
155 | 1 | const std::string stringString = "text"; |
156 | 1 | any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), stringString, allocator); |
157 | 1 | ASSERT_EQ("text", any.get<std::string>()); |
158 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getString(), 1, allocator), CppRuntimeException); |
159 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getString(), 1.0F, allocator), CppRuntimeException); |
160 | 1 | ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getString(), true, allocator), CppRuntimeException); |
161 | 1 | ASSERT_THROW( |
162 | 1 | detail::makeAnyValue(BuiltinTypeInfo<>::getString(), BitBuffer(), allocator), CppRuntimeException); |
163 | | |
164 | | // enum |
165 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), CreatorEnum::ONE, allocator); |
166 | 1 | ASSERT_EQ(CreatorEnum::ONE, any.get<CreatorEnum>()); |
167 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), 0, allocator); |
168 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::ONE), any.get<int8_t>()); |
169 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "ONE"_sv, allocator); |
170 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::ONE), any.get<int8_t>()); |
171 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "MinusOne"_sv, allocator); |
172 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::MinusOne), any.get<int8_t>()); |
173 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "NONEXISTING"_sv, allocator), |
174 | 1 | CppRuntimeException); |
175 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "{nonexisting}"_sv, allocator), |
176 | 1 | CppRuntimeException); |
177 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "nonexisting"_sv, allocator), |
178 | 1 | CppRuntimeException); |
179 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "_nonexisting"_sv, allocator), |
180 | 1 | CppRuntimeException); |
181 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "***"_sv, allocator), CppRuntimeException); |
182 | | // check all string overloads! |
183 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "10 /* no match */"_sv, allocator), |
184 | 1 | CppRuntimeException); |
185 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "-10 /* no match */", allocator), |
186 | 1 | CppRuntimeException); |
187 | 1 | ASSERT_THROW(detail::makeAnyValue( |
188 | 1 | enumTypeInfo<CreatorEnum>(), string<>("10 /* no match */", allocator), allocator), |
189 | 1 | CppRuntimeException); |
190 | 1 | const string<> enumString("10 /* no match */", allocator); |
191 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), enumString, allocator), CppRuntimeException); |
192 | | // out-of-range int64_t |
193 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "99999999999999999999", allocator), |
194 | 1 | CppRuntimeException); |
195 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "-99999999999999999999"_sv, allocator), |
196 | 1 | CppRuntimeException); |
197 | 1 | ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), ""_sv, allocator), CppRuntimeException); |
198 | | |
199 | | // unsigned enum |
200 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), CreatorUnsignedEnum::ONE, allocator); |
201 | 1 | ASSERT_EQ(CreatorUnsignedEnum::ONE, any.get<CreatorUnsignedEnum>()); |
202 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), 0, allocator); |
203 | 1 | ASSERT_EQ(enumToValue(CreatorUnsignedEnum::ONE), any.get<uint8_t>()); |
204 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), "ONE"_sv, allocator); |
205 | 1 | ASSERT_EQ(enumToValue(CreatorUnsignedEnum::ONE), any.get<uint8_t>()); |
206 | 1 | any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), "TWO"_sv, allocator); |
207 | 1 | ASSERT_EQ(enumToValue(CreatorUnsignedEnum::TWO), any.get<uint8_t>()); |
208 | | |
209 | | // bitmask |
210 | 1 | any = detail::makeAnyValue( |
211 | 1 | CreatorBitmask::typeInfo(), CreatorBitmask(CreatorBitmask::Values::READ), allocator); |
212 | 1 | ASSERT_EQ(CreatorBitmask::Values::READ, any.get<CreatorBitmask>()); |
213 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), 1, allocator); |
214 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), any.get<uint8_t>()); |
215 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ"_sv, allocator); |
216 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), any.get<uint8_t>()); |
217 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ | WRITE"_sv, allocator); |
218 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ | CreatorBitmask::Values::WRITE).getValue(), |
219 | 1 | any.get<uint8_t>()); |
220 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ|WRITE"_sv, allocator); |
221 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ | CreatorBitmask::Values::WRITE).getValue(), |
222 | 1 | any.get<uint8_t>()); |
223 | 1 | ASSERT_THROW( |
224 | 1 | detail::makeAnyValue(CreatorBitmask::typeInfo(), "NONEXISTING"_sv, allocator), CppRuntimeException); |
225 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ "_sv, allocator), CppRuntimeException); |
226 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ |"_sv, allocator), CppRuntimeException); |
227 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ | NONEXISTING"_sv, allocator), |
228 | 1 | CppRuntimeException); |
229 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ * NONEXISTING"_sv, allocator), |
230 | 1 | CppRuntimeException); |
231 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "***"_sv, allocator), CppRuntimeException); |
232 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "7 /* READ | WRITE */"_sv, allocator); |
233 | 1 | ASSERT_EQ(7, any.get<uint8_t>()); |
234 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "7"_sv, allocator); |
235 | 1 | ASSERT_EQ(7, any.get<uint8_t>()); |
236 | | // check all string overloads! |
237 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "4 /* no match */"_sv, allocator); |
238 | 1 | ASSERT_EQ(4, any.get<uint8_t>()); |
239 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "4 /* no match */", allocator); |
240 | 1 | ASSERT_EQ(4, any.get<uint8_t>()); |
241 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), string<>("4 /* no match */", allocator), allocator); |
242 | 1 | ASSERT_EQ(4, any.get<uint8_t>()); |
243 | 1 | const string<> bitmaskString("4 /* no match */", allocator); |
244 | 1 | any = detail::makeAnyValue(CreatorBitmask::typeInfo(), bitmaskString, allocator); |
245 | 1 | ASSERT_EQ(4, any.get<uint8_t>()); |
246 | | // out-of-range uint64_t |
247 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "99999999999999999999"_sv, allocator), |
248 | 1 | CppRuntimeException); |
249 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "@WRONG"_sv, allocator), CppRuntimeException); |
250 | 1 | ASSERT_THROW( |
251 | 1 | detail::makeAnyValue(CreatorBitmask::typeInfo(), "_UNKNOWN"_sv, allocator), CppRuntimeException); |
252 | 1 | ASSERT_THROW( |
253 | 1 | detail::makeAnyValue(CreatorBitmask::typeInfo(), "unknown"_sv, allocator), CppRuntimeException); |
254 | 1 | ASSERT_THROW( |
255 | 1 | detail::makeAnyValue(CreatorBitmask::typeInfo(), "{unknown}"_sv, allocator), CppRuntimeException); |
256 | 1 | ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), ""_sv, allocator), CppRuntimeException); |
257 | 1 | } |
258 | | |
259 | | TEST(ZserioTreeCreatorTest, parseBitmaskStringValue) |
260 | 1 | { |
261 | 1 | static const ::std::array<ItemInfo, 3> values = { |
262 | 1 | ItemInfo{makeStringView("READ"), static_cast<uint64_t>(1), false, false}, |
263 | 1 | ItemInfo{makeStringView("READ_EXT"), static_cast<uint64_t>(2), false, false}, |
264 | 1 | ItemInfo{makeStringView("READ_EXT_EXT"), static_cast<uint64_t>(4), false, false}}; |
265 | | |
266 | 1 | static const BitmaskTypeInfo<std::allocator<uint8_t>> typeInfo = { |
267 | 1 | makeStringView("Bitmask"), BuiltinTypeInfo<>::getUInt8(), {}, values}; |
268 | | |
269 | 1 | const std::allocator<uint8_t> allocator; |
270 | 1 | ASSERT_EQ(1, detail::parseBitmaskStringValue("READ", typeInfo, allocator).get<uint8_t>()); |
271 | 1 | ASSERT_EQ(2, detail::parseBitmaskStringValue("READ_EXT", typeInfo, allocator).get<uint8_t>()); |
272 | 1 | ASSERT_EQ(3, detail::parseBitmaskStringValue("READ_EXT | READ", typeInfo, allocator).get<uint8_t>()); |
273 | 1 | ASSERT_EQ( |
274 | 1 | 6, detail::parseBitmaskStringValue("READ_EXT_EXT | READ_EXT", typeInfo, allocator).get<uint8_t>()); |
275 | 1 | ASSERT_EQ(4, |
276 | 1 | detail::parseBitmaskStringValue("READ_EXT_EXT | READ_EXT_EXT", typeInfo, allocator).get<uint8_t>()); |
277 | 1 | ASSERT_EQ(7, |
278 | 1 | detail::parseBitmaskStringValue("READ | READ_EXT | READ_EXT_EXT", typeInfo, allocator) |
279 | 1 | .get<uint8_t>()); |
280 | 1 | ASSERT_FALSE(detail::parseBitmaskStringValue("READ|", typeInfo, allocator).hasValue()); |
281 | 1 | ASSERT_FALSE(detail::parseBitmaskStringValue("READ | ", typeInfo, allocator).hasValue()); |
282 | 1 | ASSERT_FALSE(detail::parseBitmaskStringValue("READ|", typeInfo, allocator).hasValue()); |
283 | 1 | ASSERT_FALSE(detail::parseBitmaskStringValue("READ_EXT | ", typeInfo, allocator).hasValue()); |
284 | 1 | ASSERT_FALSE(detail::parseBitmaskStringValue("READ_EXTABC", typeInfo, allocator).hasValue()); |
285 | 1 | } |
286 | | |
287 | | TEST(ZserioTreeCreatorTest, createObject) |
288 | 1 | { |
289 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
290 | 1 | creator.beginRoot(); |
291 | 1 | IReflectablePtr reflectable = creator.endRoot(); |
292 | 1 | ASSERT_TRUE(reflectable); |
293 | 1 | ASSERT_EQ(CppType::STRUCT, reflectable->getTypeInfo().getCppType()); |
294 | 1 | } |
295 | | |
296 | | TEST(ZserioTreeCreatorTest, createObjectSetFields) |
297 | 1 | { |
298 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
299 | 1 | creator.beginRoot(); |
300 | 1 | creator.setValue("value", 13); |
301 | 1 | creator.setValue("text", "test"); |
302 | 1 | IReflectablePtr reflectable = creator.endRoot(); |
303 | 1 | ASSERT_TRUE(reflectable); |
304 | | |
305 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
306 | 1 | ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView()); |
307 | 1 | } |
308 | | |
309 | | TEST(ZserioTreeCreatorTest, createObjectResetFields) |
310 | 1 | { |
311 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
312 | 1 | creator.beginRoot(); |
313 | 1 | creator.setValue("value", 13); |
314 | 1 | creator.setValue("text", nullptr); |
315 | 1 | creator.setValue("text", "test"); |
316 | 1 | IReflectablePtr reflectable = creator.endRoot(); |
317 | 1 | ASSERT_TRUE(reflectable); |
318 | | |
319 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
320 | 1 | ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView()); |
321 | 1 | } |
322 | | |
323 | | TEST(ZserioTreeCreatorTest, createObjectFull) |
324 | 1 | { |
325 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
326 | 1 | creator.beginRoot(); |
327 | 1 | creator.setValue("value", 13); |
328 | 1 | creator.setValue("text", string<>("test")); |
329 | 1 | creator.beginCompound("nested"); |
330 | 1 | creator.setValue("value", 10); |
331 | 1 | creator.setValue("text", "nested"_sv); |
332 | 1 | creator.setValue("externData", BitBuffer({0x3C}, 6)); |
333 | 1 | creator.setValue("bytesData", vector<uint8_t>({0xFF})); |
334 | 1 | creator.setValue("creatorEnum", CreatorEnum::ONE); |
335 | 1 | creator.setValue("creatorBitmask", CreatorBitmask(CreatorBitmask::Values::WRITE)); |
336 | 1 | creator.endCompound(); |
337 | 1 | creator.beginArray("nestedArray"); |
338 | 1 | creator.beginCompoundElement(); |
339 | 1 | creator.setValue("value", 5); |
340 | 1 | const std::string nestedArrayText = "nestedArray"; |
341 | 1 | creator.setValue("text", nestedArrayText); |
342 | 1 | creator.setValue("creatorEnum", "MinusOne"); |
343 | 1 | creator.setValue("creatorBitmask", CreatorBitmask(CreatorBitmask::Values::READ)); |
344 | 1 | creator.endCompoundElement(); |
345 | 1 | creator.endArray(); |
346 | 1 | creator.beginArray("textArray"); |
347 | 1 | creator.addValueElement("this"); |
348 | 1 | creator.addValueElement("is"); |
349 | 1 | creator.addValueElement("text"_sv); |
350 | 1 | creator.addValueElement("array"); |
351 | 1 | creator.endArray(); |
352 | 1 | creator.beginArray("externArray"); |
353 | 1 | creator.addValueElement(BitBuffer({0x0F}, 4)); |
354 | 1 | creator.endArray(); |
355 | 1 | creator.beginArray("bytesArray"); |
356 | 1 | creator.addValueElement(vector<uint8_t>({0xCA, 0xFE})); |
357 | 1 | creator.endArray(); |
358 | 1 | creator.setValue("optionalBool", false); |
359 | 1 | creator.beginCompound("optionalNested"); |
360 | 1 | creator.setValue("text", "optionalNested"); |
361 | 1 | creator.endCompound(); |
362 | 1 | IReflectablePtr reflectable = creator.endRoot(); |
363 | 1 | ASSERT_TRUE(reflectable); |
364 | | |
365 | 1 | reflectable->initializeChildren(); |
366 | | |
367 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
368 | 1 | ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView()); |
369 | 1 | ASSERT_EQ(13, reflectable->find("nested.param")->getUInt32()); |
370 | 1 | ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32()); |
371 | 1 | ASSERT_EQ("nested"_sv, reflectable->find("nested.text")->getStringView()); |
372 | 1 | ASSERT_EQ(0x3C, reflectable->find("nested.externData")->getBitBuffer().getData()[0]); |
373 | 1 | ASSERT_EQ(6, reflectable->find("nested.externData")->getBitBuffer().getBitSize()); |
374 | 1 | ASSERT_EQ(1, reflectable->find("nested.bytesData")->getBytes().size()); |
375 | 1 | ASSERT_EQ(0xFF, reflectable->find("nested.bytesData")->getBytes()[0]); |
376 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::ONE), reflectable->find("nested.creatorEnum")->getInt8()); |
377 | 1 | ASSERT_EQ(CreatorBitmask::Values::WRITE, |
378 | 1 | CreatorBitmask(reflectable->find("nested.creatorBitmask")->getUInt8())); |
379 | 1 | ASSERT_EQ(1, reflectable->getField("nestedArray")->size()); |
380 | 1 | ASSERT_EQ(5, reflectable->getField("nestedArray")->at(0)->getField("value")->getUInt32()); |
381 | 1 | ASSERT_EQ("nestedArray"_sv, reflectable->getField("nestedArray")->at(0)->getField("text")->getStringView()); |
382 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::MinusOne), |
383 | 1 | reflectable->getField("nestedArray")->at(0)->getField("creatorEnum")->getInt8()); |
384 | 1 | ASSERT_EQ(CreatorBitmask::Values::READ, |
385 | 1 | CreatorBitmask( |
386 | 1 | reflectable->getField("nestedArray")->at(0)->getField("creatorBitmask")->getUInt8())); |
387 | 1 | ASSERT_EQ(4, reflectable->getField("textArray")->size()); |
388 | 1 | ASSERT_EQ("this"_sv, reflectable->getField("textArray")->at(0)->getStringView()); |
389 | 1 | ASSERT_EQ("is"_sv, reflectable->getField("textArray")->at(1)->getStringView()); |
390 | 1 | ASSERT_EQ("text"_sv, reflectable->getField("textArray")->at(2)->getStringView()); |
391 | 1 | ASSERT_EQ("array"_sv, reflectable->getField("textArray")->at(3)->getStringView()); |
392 | 1 | ASSERT_EQ(1, reflectable->getField("externArray")->size()); |
393 | 1 | ASSERT_EQ(0x0f, reflectable->getField("externArray")->at(0)->getBitBuffer().getData()[0]); |
394 | 1 | ASSERT_EQ(4, reflectable->getField("externArray")->at(0)->getBitBuffer().getBitSize()); |
395 | 1 | ASSERT_EQ(1, reflectable->getField("bytesArray")->size()); |
396 | 1 | ASSERT_EQ(2, reflectable->getField("bytesArray")->at(0)->getBytes().size()); |
397 | 1 | ASSERT_EQ(0xCA, reflectable->getField("bytesArray")->at(0)->getBytes()[0]); |
398 | 1 | ASSERT_EQ(0xFE, reflectable->getField("bytesArray")->at(0)->getBytes()[1]); |
399 | 1 | ASSERT_EQ(false, reflectable->getField("optionalBool")->getBool()); |
400 | 1 | ASSERT_TRUE(reflectable->getField("optionalNested")); |
401 | 1 | ASSERT_EQ("optionalNested"_sv, reflectable->find("optionalNested.text")->getStringView()); |
402 | 1 | } |
403 | | |
404 | | TEST(ZserioTreeCreator, exceptionsBeforeRoot) |
405 | 1 | { |
406 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
407 | | |
408 | 1 | ASSERT_THROW(creator.endRoot(), CppRuntimeException); |
409 | 1 | ASSERT_THROW(creator.beginArray("nestedArray"), CppRuntimeException); |
410 | 1 | ASSERT_THROW(creator.endArray(), CppRuntimeException); |
411 | 1 | ASSERT_THROW(creator.getFieldType("nested"), CppRuntimeException); |
412 | 1 | ASSERT_THROW(creator.beginCompound("nested"), CppRuntimeException); |
413 | 1 | ASSERT_THROW(creator.endCompound(), CppRuntimeException); |
414 | 1 | ASSERT_THROW(creator.setValue("value", 13), CppRuntimeException); |
415 | 1 | ASSERT_THROW(creator.setValue("nonexistent", nullptr), CppRuntimeException); |
416 | 1 | ASSERT_THROW(creator.getElementType(), CppRuntimeException); |
417 | 1 | ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException); |
418 | 1 | ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException); |
419 | 1 | ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); |
420 | 1 | } |
421 | | |
422 | | TEST(ZserioTreeCreator, exceptionsInRoot) |
423 | 1 | { |
424 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
425 | 1 | creator.beginRoot(); |
426 | | |
427 | 1 | ASSERT_THROW(creator.beginRoot(), CppRuntimeException); |
428 | 1 | ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException); |
429 | 1 | ASSERT_THROW(creator.beginArray("nested"), CppRuntimeException); // not an array |
430 | 1 | ASSERT_THROW(creator.endArray(), CppRuntimeException); |
431 | 1 | ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException); |
432 | 1 | ASSERT_THROW(creator.beginCompound("nestedArray"), CppRuntimeException); // is array |
433 | 1 | ASSERT_THROW(creator.endCompound(), CppRuntimeException); |
434 | 1 | ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException); |
435 | 1 | ASSERT_THROW(creator.setValue("nestedArray", 13), CppRuntimeException); // is array |
436 | 1 | ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException); |
437 | 1 | ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException); |
438 | 1 | ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); |
439 | 1 | } |
440 | | |
441 | | TEST(ZserioTreeCreator, exceptionsInCompound) |
442 | 1 | { |
443 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
444 | 1 | creator.beginRoot(); |
445 | 1 | creator.beginCompound("nested"); |
446 | | |
447 | 1 | ASSERT_THROW(creator.beginRoot(), CppRuntimeException); |
448 | 1 | ASSERT_THROW(creator.endRoot(), CppRuntimeException); |
449 | 1 | ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException); |
450 | 1 | ASSERT_THROW(creator.beginArray("value"), CppRuntimeException); // not an array |
451 | 1 | ASSERT_THROW(creator.endArray(), CppRuntimeException); |
452 | 1 | ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException); |
453 | 1 | ASSERT_THROW(creator.beginCompound("text"), CppRuntimeException); // not a compound |
454 | 1 | ASSERT_THROW(creator.setValue("nonexistent", "test"), CppRuntimeException); |
455 | 1 | ASSERT_THROW(creator.setValue("value", "test"), CppRuntimeException); // wrong type |
456 | 1 | ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException); |
457 | 1 | ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException); |
458 | 1 | ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); |
459 | 1 | } |
460 | | |
461 | | TEST(ZserioTreeCreator, exceptionsInCompoundArray) |
462 | 1 | { |
463 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
464 | 1 | creator.beginRoot(); |
465 | 1 | creator.beginArray("nestedArray"); |
466 | | |
467 | 1 | ASSERT_THROW(creator.beginRoot(), CppRuntimeException); |
468 | 1 | ASSERT_THROW(creator.endRoot(), CppRuntimeException); |
469 | 1 | ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException); |
470 | 1 | ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException); |
471 | 1 | ASSERT_THROW(creator.endCompound(), CppRuntimeException); |
472 | 1 | ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException); |
473 | 1 | ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException); |
474 | 1 | ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); |
475 | 1 | } |
476 | | |
477 | | TEST(ZserioTreeCreator, exceptionsInSimpleArray) |
478 | 1 | { |
479 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
480 | 1 | creator.beginRoot(); |
481 | 1 | creator.beginArray("textArray"); |
482 | | |
483 | 1 | ASSERT_THROW(creator.beginRoot(), CppRuntimeException); |
484 | 1 | ASSERT_THROW(creator.endRoot(), CppRuntimeException); |
485 | 1 | ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException); |
486 | 1 | ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException); |
487 | 1 | ASSERT_THROW(creator.endCompound(), CppRuntimeException); |
488 | 1 | ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException); |
489 | 1 | ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException); // not a compound array |
490 | 1 | ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException); |
491 | 1 | ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); // wrong type |
492 | 1 | } |
493 | | |
494 | | TEST(ZserioTreeCreator, exceptionsInCompoundElement) |
495 | 1 | { |
496 | 1 | ZserioTreeCreator creator(CreatorObject::typeInfo()); |
497 | 1 | creator.beginRoot(); |
498 | 1 | creator.beginArray("nestedArray"); |
499 | 1 | creator.beginCompoundElement(); |
500 | | |
501 | 1 | ASSERT_THROW(creator.beginRoot(), CppRuntimeException); |
502 | 1 | ASSERT_THROW(creator.endRoot(), CppRuntimeException); |
503 | 1 | ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException); |
504 | 1 | ASSERT_THROW(creator.endArray(), CppRuntimeException); |
505 | 1 | ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException); |
506 | 1 | ASSERT_THROW(creator.endCompound(), CppRuntimeException); |
507 | 1 | ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException); |
508 | 1 | ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException); |
509 | 1 | ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); |
510 | 1 | } |
511 | | |
512 | | } // namespace zserio |