GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/zserio/ZserioTreeCreatorTest.cpp Lines: 363 363 100.0 %
Date: 2023-12-13 14:51:09 Branches: 2001 8208 24.4 %

Line Branch Exec Source
1
#include "gtest/gtest.h"
2
3
#include "zserio/StringConvertUtil.h"
4
#include "zserio/StringView.h"
5
#include "zserio/ZserioTreeCreator.h"
6
#include "zserio/TypeInfo.h"
7
8
#include "test_object/std_allocator/CreatorBitmask.h"
9
#include "test_object/std_allocator/CreatorEnum.h"
10
#include "test_object/std_allocator/CreatorNested.h"
11
#include "test_object/std_allocator/CreatorObject.h"
12
#include "test_object/std_allocator/CreatorUnsignedEnum.h"
13
14
using test_object::std_allocator::CreatorBitmask;
15
using test_object::std_allocator::CreatorEnum;
16
using test_object::std_allocator::CreatorNested;
17
using test_object::std_allocator::CreatorObject;
18
using test_object::std_allocator::CreatorUnsignedEnum;
19
20
namespace zserio
21
{
22
23


802
TEST(ZserioTreeCreator, makeAnyValue)
24
{
25
2
    const std::allocator<uint8_t> allocator;
26
27
    // bool
28

2
    auto any = detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), true, allocator);
29



1
    ASSERT_EQ(true, any.get<bool>());
30










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), 1, allocator), CppRuntimeException);
31











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), 1.0F, allocator), CppRuntimeException);
32










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), "1", allocator), CppRuntimeException);
33










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getBool(), BitBuffer(), allocator),
34
            CppRuntimeException);
35
36
    // uint8
37

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), 8, allocator);
38



1
    ASSERT_EQ(8, any.get<uint8_t>());
39










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), -1, allocator), CppRuntimeException);
40










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), 256, allocator), CppRuntimeException);
41











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), 1.0F, allocator), CppRuntimeException);
42










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), "1", allocator), CppRuntimeException);
43










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), true, allocator), CppRuntimeException);
44










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt8(), BitBuffer(), allocator),
45
            CppRuntimeException);
46
47
    // uint16
48

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), 16, allocator);
49



1
    ASSERT_EQ(16, any.get<uint16_t>());
50










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), -1, allocator), CppRuntimeException);
51










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), 65536, allocator), CppRuntimeException);
52











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), 1.0F, allocator), CppRuntimeException);
53










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), "1", allocator), CppRuntimeException);
54










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), true, allocator), CppRuntimeException);
55










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt16(), BitBuffer(), allocator),
56
            CppRuntimeException);
57
58
    // uint32
59

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), 32, allocator);
60



1
    ASSERT_EQ(32, any.get<uint32_t>());
61










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), -1, allocator), CppRuntimeException);
62










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), 4294967296, allocator),
63
            CppRuntimeException);
64











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), 1.0F, allocator), CppRuntimeException);
65










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), "1", allocator), CppRuntimeException);
66










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), true, allocator), CppRuntimeException);
67










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt32(), BitBuffer(), allocator),
68
            CppRuntimeException);
69
70
    // uint64
71

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), 64, allocator);
72



1
    ASSERT_EQ(64, any.get<uint64_t>());
73










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), -1, allocator), CppRuntimeException);
74











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), 1.0F, allocator), CppRuntimeException);
75










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), "1", allocator), CppRuntimeException);
76










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), true, allocator), CppRuntimeException);
77










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getUInt64(), BitBuffer(), allocator),
78
            CppRuntimeException);
79
80
    // int8
81

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), 8, allocator);
82



1
    ASSERT_EQ(8, any.get<int8_t>());
83










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), -129, allocator), CppRuntimeException);
84










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), 128, allocator), CppRuntimeException);
85











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), 1.0F, allocator), CppRuntimeException);
86










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), "1", allocator), CppRuntimeException);
87










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), true, allocator), CppRuntimeException);
88










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt8(), BitBuffer(), allocator),
89
            CppRuntimeException);
90
91
    // int16
92

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), 16, allocator);
93



1
    ASSERT_EQ(16, any.get<int16_t>());
94










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), -32769, allocator), CppRuntimeException);
95










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), 32768, allocator), CppRuntimeException);
96











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), 1.0F, allocator), CppRuntimeException);
97










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), "1", allocator), CppRuntimeException);
98










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), true, allocator), CppRuntimeException);
99










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt16(), BitBuffer(), allocator),
100
            CppRuntimeException);
101
102
    // int32
103

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), 32, allocator);
104



1
    ASSERT_EQ(32, any.get<int32_t>());
105










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), -2147483649LL, allocator),
106
            CppRuntimeException);
107










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), 2147483648LL, allocator),
108
            CppRuntimeException);
109











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), 1.0F, allocator), CppRuntimeException);
110










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), "1", allocator), CppRuntimeException);
111










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), true, allocator), CppRuntimeException);
112










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt32(), BitBuffer(), allocator),
113
            CppRuntimeException);
114
115
    // int64
116

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), 64, allocator);
117



1
    ASSERT_EQ(64, any.get<int64_t>());
118










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), 9223372036854775808ULL, allocator),
119
            CppRuntimeException);
120











2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), 1.0F, allocator), CppRuntimeException);
121










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), "1", allocator), CppRuntimeException);
122










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), true, allocator), CppRuntimeException);
123










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getInt64(), BitBuffer(), allocator),
124
            CppRuntimeException);
125
126
    // float
127

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), 3.5F, allocator);
128



1
    ASSERT_EQ(3.5F, any.get<float>());
129

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), 1, allocator);
130



1
    ASSERT_EQ(1.0F, any.get<float>());
131

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), 1.0, allocator);
132




1
    ASSERT_EQ(1.0F, any.get<float>());
133










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), "1", allocator), CppRuntimeException);
134










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), true, allocator), CppRuntimeException);
135










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat32(), BitBuffer(), allocator),
136
            CppRuntimeException);
137
138
    // double
139

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), 3.14, allocator);
140



1
    ASSERT_EQ(3.14, any.get<double>());
141

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), 1, allocator);
142



1
    ASSERT_EQ(1.0, any.get<double>());
143

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), 1.0F, allocator);
144




1
    ASSERT_EQ(1.0, any.get<double>());
145










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), "1", allocator), CppRuntimeException);
146










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), true, allocator), CppRuntimeException);
147










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getFloat64(), BitBuffer(), allocator),
148
            CppRuntimeException);
149
150
    // string
151

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), "text", allocator);
152



1
    ASSERT_EQ("text", any.get<std::string>());
153

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), "text"_sv, allocator);
154



1
    ASSERT_EQ("text", any.get<std::string>());
155


1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), std::string("text"), allocator);
156



1
    ASSERT_EQ("text", any.get<std::string>());
157

2
    const std::string stringString = "text";
158

1
    any = detail::makeAnyValue(BuiltinTypeInfo<>::getString(), stringString, allocator);
159



1
    ASSERT_EQ("text", any.get<std::string>());
160










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getString(), 1, allocator), CppRuntimeException);
161










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getString(), 1.0F, allocator), CppRuntimeException);
162










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getString(), true, allocator), CppRuntimeException);
163










2
    ASSERT_THROW(detail::makeAnyValue(BuiltinTypeInfo<>::getString(), BitBuffer(), allocator),
164
            CppRuntimeException);
165
166
    // enum
167

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), CreatorEnum::ONE, allocator);
168



1
    ASSERT_EQ(CreatorEnum::ONE, any.get<CreatorEnum>());
169

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), 0, allocator);
170



1
    ASSERT_EQ(enumToValue(CreatorEnum::ONE), any.get<int8_t>());
171

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "ONE"_sv, allocator);
172



1
    ASSERT_EQ(enumToValue(CreatorEnum::ONE), any.get<int8_t>());
173

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "MinusOne"_sv, allocator);
174



1
    ASSERT_EQ(enumToValue(CreatorEnum::MinusOne), any.get<int8_t>());
175










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "NONEXISTING"_sv, allocator),
176
            CppRuntimeException);
177










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "{nonexisting}"_sv, allocator),
178
            CppRuntimeException);
179










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "nonexisting"_sv, allocator),
180
            CppRuntimeException);
181










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "_nonexisting"_sv, allocator),
182
            CppRuntimeException);
183










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "***"_sv, allocator),
184
            CppRuntimeException);
185
    // check all string overloads!
186











2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "10 /* no match */"_sv, allocator),
187
            CppRuntimeException);
188










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "-10 /* no match */", allocator),
189
            CppRuntimeException);
190











2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(),
191
            string<>("10 /* no match */", allocator), allocator), CppRuntimeException);
192

2
    const string<> enumString("10 /* no match */", allocator);
193










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), enumString, allocator), CppRuntimeException);
194
    // out-of-range int64_t
195










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "99999999999999999999", allocator),
196
            CppRuntimeException);
197










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), "-99999999999999999999"_sv, allocator),
198
            CppRuntimeException);
199










2
    ASSERT_THROW(detail::makeAnyValue(enumTypeInfo<CreatorEnum>(), ""_sv, allocator), CppRuntimeException);
200
201
    // unsigned enum
202

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), CreatorUnsignedEnum::ONE, allocator);
203



1
    ASSERT_EQ(CreatorUnsignedEnum::ONE, any.get<CreatorUnsignedEnum>());
204

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), 0, allocator);
205



1
    ASSERT_EQ(enumToValue(CreatorUnsignedEnum::ONE), any.get<uint8_t>());
206

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), "ONE"_sv, allocator);
207



1
    ASSERT_EQ(enumToValue(CreatorUnsignedEnum::ONE), any.get<uint8_t>());
208

1
    any = detail::makeAnyValue(enumTypeInfo<CreatorUnsignedEnum>(), "TWO"_sv, allocator);
209



1
    ASSERT_EQ(enumToValue(CreatorUnsignedEnum::TWO), any.get<uint8_t>());
210
211
    // bitmask
212

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), CreatorBitmask(CreatorBitmask::Values::READ), allocator);
213



1
    ASSERT_EQ(CreatorBitmask::Values::READ, any.get<CreatorBitmask>());
214

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), 1, allocator);
215



1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), any.get<uint8_t>());
216

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ"_sv, allocator);
217



1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), any.get<uint8_t>());
218

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ | WRITE"_sv, allocator);
219



1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ | CreatorBitmask::Values::WRITE).getValue(),
220
            any.get<uint8_t>());
221

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ|WRITE"_sv, allocator);
222



1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ | CreatorBitmask::Values::WRITE).getValue(),
223
            any.get<uint8_t>());
224










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "NONEXISTING"_sv, allocator),
225
            CppRuntimeException);
226










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ "_sv, allocator),
227
            CppRuntimeException);
228










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ |"_sv, allocator),
229
            CppRuntimeException);
230










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ | NONEXISTING"_sv, allocator),
231
            CppRuntimeException);
232










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "READ * NONEXISTING"_sv, allocator),
233
            CppRuntimeException);
234










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "***"_sv, allocator),
235
            CppRuntimeException);
236

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "7 /* READ | WRITE */"_sv, allocator);
237



1
    ASSERT_EQ(7, any.get<uint8_t>());
238

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "7"_sv, allocator);
239



1
    ASSERT_EQ(7, any.get<uint8_t>());
240
    // check all string overloads!
241

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "4 /* no match */"_sv, allocator);
242



1
    ASSERT_EQ(4, any.get<uint8_t>());
243

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), "4 /* no match */", allocator);
244



1
    ASSERT_EQ(4, any.get<uint8_t>());
245


1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), string<>("4 /* no match */", allocator), allocator);
246



1
    ASSERT_EQ(4, any.get<uint8_t>());
247

2
    const string<> bitmaskString("4 /* no match */", allocator);
248

1
    any = detail::makeAnyValue(CreatorBitmask::typeInfo(), bitmaskString, allocator);
249



1
    ASSERT_EQ(4, any.get<uint8_t>());
250
    // out-of-range uint64_t
251










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "99999999999999999999"_sv, allocator),
252
            CppRuntimeException);
253










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "@WRONG"_sv, allocator),
254
            CppRuntimeException);
255










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "_UNKNOWN"_sv, allocator),
256
            CppRuntimeException);
257










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "unknown"_sv, allocator),
258
            CppRuntimeException);
259










2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), "{unknown}"_sv, allocator),
260
            CppRuntimeException);
261











2
    ASSERT_THROW(detail::makeAnyValue(CreatorBitmask::typeInfo(), ""_sv, allocator), CppRuntimeException);
262
}
263
264


802
TEST(ZserioTreeCreatorTest, parseBitmaskStringValue)
265
{
266
    static const ::std::array<ItemInfo, 3> values = {
267
        ItemInfo{ makeStringView("READ"), static_cast<uint64_t>(1), false, false },
268
        ItemInfo{ makeStringView("READ_EXT"), static_cast<uint64_t>(2), false, false },
269
        ItemInfo{ makeStringView("READ_EXT_EXT"), static_cast<uint64_t>(4), false, false }
270

1
    };
271
272
    static const BitmaskTypeInfo<std::allocator<uint8_t>> typeInfo = {
273
1
        makeStringView("Bitmask"), BuiltinTypeInfo<>::getUInt8(), {}, values
274


2
    };
275
276
2
    const std::allocator<uint8_t> allocator;
277




1
    ASSERT_EQ(1, detail::parseBitmaskStringValue("READ", typeInfo, allocator).get<uint8_t>());
278




1
    ASSERT_EQ(2, detail::parseBitmaskStringValue("READ_EXT", typeInfo, allocator).get<uint8_t>());
279




1
    ASSERT_EQ(3, detail::parseBitmaskStringValue("READ_EXT | READ", typeInfo, allocator).get<uint8_t>());
280




1
    ASSERT_EQ(6, detail::parseBitmaskStringValue("READ_EXT_EXT | READ_EXT",
281
            typeInfo, allocator).get<uint8_t>());
282




1
    ASSERT_EQ(4, detail::parseBitmaskStringValue("READ_EXT_EXT | READ_EXT_EXT",
283
                typeInfo, allocator).get<uint8_t>());
284




1
    ASSERT_EQ(7, detail::parseBitmaskStringValue("READ | READ_EXT | READ_EXT_EXT",
285
                typeInfo, allocator).get<uint8_t>());
286




1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ|", typeInfo, allocator).hasValue());
287




1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ | ", typeInfo, allocator).hasValue());
288




1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ|", typeInfo, allocator).hasValue());
289




1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ_EXT | ", typeInfo, allocator).hasValue());
290




1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ_EXTABC", typeInfo, allocator).hasValue());
291
}
292
293


802
TEST(ZserioTreeCreatorTest, createObject)
294
{
295

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
296
1
    creator.beginRoot();
297

2
    IReflectablePtr reflectable = creator.endRoot();
298



1
    ASSERT_TRUE(reflectable);
299




1
    ASSERT_EQ(CppType::STRUCT, reflectable->getTypeInfo().getCppType());
300
}
301
302


802
TEST(ZserioTreeCreatorTest, createObjectSetFields)
303
{
304

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
305
1
    creator.beginRoot();
306

1
    creator.setValue("value", 13);
307

1
    creator.setValue("text", "test");
308

2
    IReflectablePtr reflectable = creator.endRoot();
309



1
    ASSERT_TRUE(reflectable);
310
311




1
    ASSERT_EQ(13, reflectable->getField("value")->getUInt32());
312




1
    ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView());
313
}
314
315


802
TEST(ZserioTreeCreatorTest, createObjectResetFields)
316
{
317

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
318
1
    creator.beginRoot();
319

1
    creator.setValue("value", 13);
320

1
    creator.setValue("text", nullptr);
321

1
    creator.setValue("text", "test");
322

2
    IReflectablePtr reflectable = creator.endRoot();
323



1
    ASSERT_TRUE(reflectable);
324
325




1
    ASSERT_EQ(13, reflectable->getField("value")->getUInt32());
326




1
    ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView());
327
}
328
329


802
TEST(ZserioTreeCreatorTest, createObjectFull)
330
{
331

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
332
1
    creator.beginRoot();
333

1
    creator.setValue("value", 13);
334

1
    creator.setValue("text", string<>("test"));
335

1
    creator.beginCompound("nested");
336

1
    creator.setValue("value", 10);
337

1
    creator.setValue("text", "nested"_sv);
338


1
    creator.setValue("externData", BitBuffer({0x3C}, 6));
339

1
    creator.setValue("bytesData", vector<uint8_t>({0xFF}));
340

1
    creator.setValue("creatorEnum", CreatorEnum::ONE);
341

1
    creator.setValue("creatorBitmask", CreatorBitmask(CreatorBitmask::Values::WRITE));
342
1
    creator.endCompound();
343

1
    creator.beginArray("nestedArray");
344
1
    creator.beginCompoundElement();
345

1
    creator.setValue("value", 5);
346

2
    const std::string nestedArrayText = "nestedArray";
347

1
    creator.setValue("text", nestedArrayText);
348

1
    creator.setValue("creatorEnum", "MinusOne");
349

1
    creator.setValue("creatorBitmask", CreatorBitmask(CreatorBitmask::Values::READ));
350
1
    creator.endCompoundElement();
351
1
    creator.endArray();
352

1
    creator.beginArray("textArray");
353
1
    creator.addValueElement("this");
354
1
    creator.addValueElement("is");
355
1
    creator.addValueElement("text"_sv);
356
1
    creator.addValueElement("array");
357
1
    creator.endArray();
358

1
    creator.beginArray("externArray");
359

1
    creator.addValueElement(BitBuffer({0x0F}, 4));
360
1
    creator.endArray();
361

1
    creator.beginArray("bytesArray");
362

1
    creator.addValueElement(vector<uint8_t>({0xCA, 0xFE}));
363
1
    creator.endArray();
364

1
    creator.setValue("optionalBool", false);
365

1
    creator.beginCompound("optionalNested");
366

1
    creator.setValue("text", "optionalNested");
367
1
    creator.endCompound();
368

2
    IReflectablePtr reflectable = creator.endRoot();
369



1
    ASSERT_TRUE(reflectable);
370
371
1
    reflectable->initializeChildren();
372
373




1
    ASSERT_EQ(13, reflectable->getField("value")->getUInt32());
374




1
    ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView());
375




1
    ASSERT_EQ(13, reflectable->find("nested.param")->getUInt32());
376




1
    ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32());
377




1
    ASSERT_EQ("nested"_sv, reflectable->find("nested.text")->getStringView());
378




1
    ASSERT_EQ(0x3C, reflectable->find("nested.externData")->getBitBuffer().getData()[0]);
379




1
    ASSERT_EQ(6, reflectable->find("nested.externData")->getBitBuffer().getBitSize());
380




1
    ASSERT_EQ(1, reflectable->find("nested.bytesData")->getBytes().size());
381




1
    ASSERT_EQ(0xFF, reflectable->find("nested.bytesData")->getBytes()[0]);
382




1
    ASSERT_EQ(enumToValue(CreatorEnum::ONE), reflectable->find("nested.creatorEnum")->getInt8());
383




1
    ASSERT_EQ(CreatorBitmask::Values::WRITE,
384
            CreatorBitmask(reflectable->find("nested.creatorBitmask")->getUInt8()));
385




1
    ASSERT_EQ(1, reflectable->getField("nestedArray")->size());
386





1
    ASSERT_EQ(5, reflectable->getField("nestedArray")->at(0)->getField("value")->getUInt32());
387





1
    ASSERT_EQ("nestedArray"_sv, reflectable->getField("nestedArray")->at(0)->getField("text")->getStringView());
388





1
    ASSERT_EQ(enumToValue(CreatorEnum::MinusOne),
389
            reflectable->getField("nestedArray")->at(0)->getField("creatorEnum")->getInt8());
390





1
    ASSERT_EQ(CreatorBitmask::Values::READ,
391
            CreatorBitmask(reflectable->getField("nestedArray")->at(0)->getField("creatorBitmask")->getUInt8()));
392




1
    ASSERT_EQ(4, reflectable->getField("textArray")->size());
393




1
    ASSERT_EQ("this"_sv, reflectable->getField("textArray")->at(0)->getStringView());
394




1
    ASSERT_EQ("is"_sv, reflectable->getField("textArray")->at(1)->getStringView());
395




1
    ASSERT_EQ("text"_sv, reflectable->getField("textArray")->at(2)->getStringView());
396




1
    ASSERT_EQ("array"_sv, reflectable->getField("textArray")->at(3)->getStringView());
397




1
    ASSERT_EQ(1, reflectable->getField("externArray")->size());
398





1
    ASSERT_EQ(0x0f, reflectable->getField("externArray")->at(0)->getBitBuffer().getData()[0]);
399




1
    ASSERT_EQ(4, reflectable->getField("externArray")->at(0)->getBitBuffer().getBitSize());
400




1
    ASSERT_EQ(1, reflectable->getField("bytesArray")->size());
401




1
    ASSERT_EQ(2, reflectable->getField("bytesArray")->at(0)->getBytes().size());
402




1
    ASSERT_EQ(0xCA, reflectable->getField("bytesArray")->at(0)->getBytes()[0]);
403




1
    ASSERT_EQ(0xFE, reflectable->getField("bytesArray")->at(0)->getBytes()[1]);
404




1
    ASSERT_EQ(false, reflectable->getField("optionalBool")->getBool());
405




1
    ASSERT_TRUE(reflectable->getField("optionalNested"));
406




1
    ASSERT_EQ("optionalNested"_sv, reflectable->find("optionalNested.text")->getStringView());
407
}
408
409


802
TEST(ZserioTreeCreator, exceptionsBeforeRoot)
410
{
411

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
412
413









2
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
414











2
    ASSERT_THROW(creator.beginArray("nestedArray"), CppRuntimeException);
415









2
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
416










2
    ASSERT_THROW(creator.getFieldType("nested"), CppRuntimeException);
417











2
    ASSERT_THROW(creator.beginCompound("nested"), CppRuntimeException);
418









2
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
419










2
    ASSERT_THROW(creator.setValue("value", 13), CppRuntimeException);
420











2
    ASSERT_THROW(creator.setValue("nonexistent", nullptr), CppRuntimeException);
421










2
    ASSERT_THROW(creator.getElementType(), CppRuntimeException);
422










2
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
423









2
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
424










2
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
425
}
426
427


802
TEST(ZserioTreeCreator, exceptionsInRoot)
428
{
429

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
430
1
    creator.beginRoot();
431
432









2
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
433










2
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
434











2
    ASSERT_THROW(creator.beginArray("nested"), CppRuntimeException); // not an array
435









2
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
436










2
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
437











2
    ASSERT_THROW(creator.beginCompound("nestedArray"), CppRuntimeException); // is array
438









2
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
439










2
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
440











2
    ASSERT_THROW(creator.setValue("nestedArray", 13), CppRuntimeException); // is array
441










2
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
442









2
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
443










2
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
444
}
445
446


802
TEST(ZserioTreeCreator, exceptionsInCompound)
447
{
448

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
449
1
    creator.beginRoot();
450

1
    creator.beginCompound("nested");
451
452










2
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
453









2
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
454










2
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
455











2
    ASSERT_THROW(creator.beginArray("value"), CppRuntimeException); // not an array
456









2
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
457










2
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
458










2
    ASSERT_THROW(creator.beginCompound("text"), CppRuntimeException); // not a compound
459










2
    ASSERT_THROW(creator.setValue("nonexistent", "test"), CppRuntimeException);
460











2
    ASSERT_THROW(creator.setValue("value", "test"), CppRuntimeException); // wrong type
461










2
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
462









2
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
463










2
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
464
}
465
466


802
TEST(ZserioTreeCreator, exceptionsInCompoundArray)
467
{
468

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
469
1
    creator.beginRoot();
470

1
    creator.beginArray("nestedArray");
471
472










2
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
473









2
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
474










2
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
475











2
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
476









2
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
477











2
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
478









2
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
479










2
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
480
}
481
482


802
TEST(ZserioTreeCreator, exceptionsInSimpleArray)
483
{
484

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
485
1
    creator.beginRoot();
486

1
    creator.beginArray("textArray");
487
488










2
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
489









2
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
490










2
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
491











2
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
492









2
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
493











2
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
494










2
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException); // not a compound array
495









2
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
496










2
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); // wrong type
497
}
498
499


802
TEST(ZserioTreeCreator, exceptionsInCompoundElement)
500
{
501

2
    ZserioTreeCreator creator(CreatorObject::typeInfo());
502
1
    creator.beginRoot();
503

1
    creator.beginArray("nestedArray");
504
1
    creator.beginCompoundElement();
505
506










2
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
507









2
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
508











2
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
509









2
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
510











2
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
511









2
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
512











2
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
513









2
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
514










2
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
515
}
516
517

2394
} // namespace zserio