test/zserio/DataViewTest.cpp
Line | Count | Source |
1 | | #include "gtest/gtest.h" |
2 | | #include "zserio/ArrayView.h" |
3 | | #include "zserio/DataView.h" |
4 | | |
5 | | namespace zserio |
6 | | { |
7 | | |
8 | | struct TestParam |
9 | | { |
10 | | VarSize len; |
11 | | }; |
12 | | |
13 | | template <> |
14 | | class View<TestParam> |
15 | | { |
16 | | public: |
17 | | explicit View(const TestParam& data) : |
18 | 6 | m_data(data) |
19 | 6 | {} |
20 | | |
21 | | VarSize len() const |
22 | 10 | { |
23 | 10 | return m_data.len; |
24 | 10 | } |
25 | | |
26 | | private: |
27 | | const TestParam& m_data; |
28 | | }; |
29 | | |
30 | | struct TestObject |
31 | | { |
32 | | Vector<UInt32> array; |
33 | | }; |
34 | | |
35 | | template <> |
36 | | class View<TestObject> |
37 | | { |
38 | | public: |
39 | | View(const TestObject& data, const View<TestParam>& param) : |
40 | 6 | m_data(data), |
41 | 6 | m_param(param) |
42 | 6 | {} |
43 | | |
44 | | ArrayView<const UInt32> array() const |
45 | 16 | { |
46 | 16 | return ArrayView<const UInt32>(m_data.array); |
47 | 16 | } |
48 | | |
49 | | View<TestParam> param() const |
50 | 12 | { |
51 | 12 | return m_param; |
52 | 12 | } |
53 | | |
54 | | protected: |
55 | | View(const TestObject& data, const View& other) : |
56 | 2 | m_data(data), |
57 | 2 | m_param(other.param()) |
58 | 2 | {} |
59 | | |
60 | | private: |
61 | | const TestObject& m_data; |
62 | | View<TestParam> m_param; |
63 | | }; |
64 | | |
65 | | namespace detail |
66 | | { |
67 | | |
68 | | template <> |
69 | | struct ObjectTraits<TestObject> |
70 | | { |
71 | | static void validate(const zserio::View<TestObject>& view, std::string_view fieldName) |
72 | 6 | { |
73 | 6 | if (view.param().len() != view.array().size()) |
74 | 2 | { |
75 | 2 | throw CppRuntimeException("Validation error (") << fieldName << ")!"; |
76 | 2 | } |
77 | 6 | } |
78 | | |
79 | | static BitSize bitSizeOf(const zserio::View<TestObject>& view, BitSize bitPosition) |
80 | 4 | { |
81 | 4 | BitSize endBitPosition = bitPosition; |
82 | | |
83 | 4 | endBitPosition = detail::bitSizeOf<ArrayType::AUTO>(view.array()); |
84 | | |
85 | 4 | return endBitPosition - bitPosition; |
86 | 4 | } |
87 | | }; |
88 | | |
89 | | } // namespace detail |
90 | | |
91 | | TEST(DataViewTest, dataConstructor) |
92 | 1 | { |
93 | 1 | TestParam param{3}; |
94 | 1 | TestObject data{Vector<UInt32>{{0, 1, 2}}}; |
95 | 1 | DataView dataView(data, View(param)); |
96 | | |
97 | | // TODO[Mi-L@]: How to check that the raw array data doesn't remain on the same address? |
98 | | |
99 | 1 | ASSERT_EQ(3, dataView.param().len()); |
100 | 1 | ASSERT_EQ(3, dataView.array().size()); |
101 | 1 | ASSERT_EQ(0, dataView.array().at(0)); |
102 | | |
103 | | // validation error |
104 | 1 | TestParam wrongParam{4}; |
105 | 1 | ASSERT_THROW(DataView(data, View(wrongParam)), CppRuntimeException); |
106 | 1 | } |
107 | | |
108 | | TEST(DataViewTest, dataMoveConstructor) |
109 | 1 | { |
110 | 1 | TestParam param{3}; |
111 | 1 | TestObject data{Vector<UInt32>{{0, 1, 2}}}; |
112 | 1 | DataView dataView(std::move(data), View(param)); |
113 | | |
114 | | // TODO[Mi-L@]: How to check that the raw array data remain on the same address? |
115 | | |
116 | 1 | ASSERT_EQ(3, dataView.param().len()); |
117 | 1 | ASSERT_EQ(3, dataView.array().size()); |
118 | 1 | ASSERT_EQ(0, dataView.array().at(0)); |
119 | | |
120 | | // validation error |
121 | 1 | TestParam wrongParam{4}; |
122 | 1 | TestObject otherData{Vector<UInt32>{{0, 1, 2}}}; |
123 | 1 | ASSERT_THROW(DataView(std::move(otherData), View(wrongParam)), CppRuntimeException); |
124 | 1 | } |
125 | | |
126 | | TEST(DataViewTest, copyConstructor) |
127 | 1 | { |
128 | 1 | TestParam param{3}; |
129 | 1 | TestObject data{Vector<UInt32>{{0, 1, 2}}}; |
130 | 1 | DataView dataView(data, View(param)); |
131 | | |
132 | 1 | DataView copied(dataView); |
133 | 1 | ASSERT_EQ(3, copied.param().len()); |
134 | 1 | ASSERT_EQ(0, copied.array().at(0)); |
135 | 1 | } |
136 | | |
137 | | TEST(DataViewTest, moveConstructor) |
138 | 1 | { |
139 | 1 | TestParam param{3}; |
140 | 1 | TestObject data{Vector<UInt32>{{0, 1, 2}}}; |
141 | 1 | DataView dataView(data, View(param)); |
142 | | |
143 | 1 | DataView copied(std::move(dataView)); |
144 | 1 | ASSERT_EQ(3, copied.param().len()); |
145 | 1 | ASSERT_EQ(0, copied.array().at(0)); |
146 | 1 | } |
147 | | |
148 | | } // namespace zserio |