ByteVector_test.cpp
Go to the documentation of this file.
1 #include "ByteVector.hpp"
2 #include <limits>
3 #include "gtest/gtest.h"
4 
5 namespace msp {
6 
7 // The fixture for testing class Foo.
8 template <typename T> class ByteVectorBasicTest : public ::testing::Test {};
9 typedef ::testing::Types<bool, uint8_t, uint16_t, uint32_t, int8_t, int16_t,
10  int32_t, float, double>
13 
14 template <typename T> class ByteVectorScaledTest : public ::testing::Test {};
15 typedef ::testing::Types<uint8_t, uint16_t, uint32_t, int8_t, int16_t, int32_t>
18 
19 // Tests that the Foo::Bar() method does Abc.
20 TEST(ByteVectorBasicTest, Initialzation) {
21  ByteVector b;
22  EXPECT_EQ(std::size_t(0), b.size());
23  EXPECT_EQ(std::size_t(0), b.unpacking_offset());
24  EXPECT_EQ(std::size_t(0), b.unpacking_remaining());
25 }
26 
28  ByteVector b;
29  TypeParam ref = 0;
30  EXPECT_TRUE(b.pack(ref));
31  EXPECT_EQ(sizeof(TypeParam), b.size());
32  EXPECT_TRUE(b.unpack(ref));
33  EXPECT_EQ(TypeParam(0), ref);
34 }
35 
37  ByteVector b;
38  TypeParam ref = 1;
39  EXPECT_TRUE(b.pack(ref));
40  EXPECT_EQ(sizeof(TypeParam), b.size());
41  EXPECT_TRUE(b.unpack(ref));
42  EXPECT_EQ(TypeParam(1), ref);
43 }
44 
46  ByteVector b;
47  TypeParam ref = std::numeric_limits<TypeParam>::max();
48  EXPECT_TRUE(b.pack(ref));
49  EXPECT_EQ(sizeof(TypeParam), b.size());
50  EXPECT_TRUE(b.unpack(ref));
51  EXPECT_EQ(std::numeric_limits<TypeParam>::max(), ref);
52 }
53 
55  ByteVector b;
56  TypeParam ref = std::numeric_limits<TypeParam>::min();
57  EXPECT_TRUE(b.pack(ref));
58  EXPECT_EQ(sizeof(TypeParam), b.size());
59  EXPECT_TRUE(b.unpack(ref));
60  EXPECT_EQ(std::numeric_limits<TypeParam>::min(), ref);
61 }
62 
64  ByteVector b;
66  TypeParam ref = 0;
67  v = ref;
68  EXPECT_TRUE(b.pack(v));
69  EXPECT_EQ(sizeof(TypeParam), b.size());
70  EXPECT_TRUE(b.unpack(v));
71  EXPECT_EQ(ref, v());
72 }
73 
75  ByteVector b;
77  TypeParam ref = 1;
78  v = ref;
79  EXPECT_TRUE(b.pack(v));
80  EXPECT_EQ(sizeof(TypeParam), b.size());
81  EXPECT_TRUE(b.unpack(v));
82  EXPECT_EQ(ref, v());
83 }
84 
86  ByteVector b;
88  TypeParam ref = std::numeric_limits<TypeParam>::max();
89  v = ref;
90  EXPECT_TRUE(b.pack(v));
91  EXPECT_EQ(sizeof(TypeParam), b.size());
92  EXPECT_TRUE(b.unpack(v));
93  EXPECT_EQ(ref, v());
94 }
95 
97  ByteVector b;
99  TypeParam ref = std::numeric_limits<TypeParam>::min();
100  v = ref;
101  EXPECT_TRUE(b.pack(v));
102  EXPECT_EQ(sizeof(TypeParam), b.size());
103  EXPECT_TRUE(b.unpack(v));
104  EXPECT_EQ(ref, v());
105 }
106 
108  ByteVector b;
109  TypeParam ref = 0;
110  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
111  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
112  for(int i = 0; i < 10; ++i) {
113  EXPECT_TRUE(b.unpack(ref));
114  EXPECT_EQ(TypeParam(0), ref);
115  }
116 }
117 
119  ByteVector b;
120  TypeParam ref = 1;
121  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
122  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
123  for(int i = 0; i < 10; ++i) {
124  EXPECT_TRUE(b.unpack(ref));
125  EXPECT_EQ(TypeParam(1), ref);
126  }
127 }
128 
130  ByteVector b;
131  TypeParam ref = std::numeric_limits<TypeParam>::max();
132  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
133  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
134  for(int i = 0; i < 10; ++i) {
135  EXPECT_TRUE(b.unpack(ref));
136  EXPECT_EQ(std::numeric_limits<TypeParam>::max(), ref);
137  }
138 }
139 
141  ByteVector b;
142  TypeParam ref = std::numeric_limits<TypeParam>::min();
143  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(ref));
144  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
145  for(int i = 0; i < 10; ++i) {
146  EXPECT_TRUE(b.unpack(ref));
147  EXPECT_EQ(std::numeric_limits<TypeParam>::min(), ref);
148  }
149 }
150 
152  ByteVector b;
154  TypeParam ref = 0;
155  v = ref;
156  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
157  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
158  for(int i = 0; i < 10; ++i) {
159  EXPECT_TRUE(b.unpack(v));
160  EXPECT_EQ(ref, v());
161  }
162 }
163 
165  ByteVector b;
167  TypeParam ref = 1;
168  v = ref;
169  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
170  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
171  for(int i = 0; i < 10; ++i) {
172  EXPECT_TRUE(b.unpack(v));
173  EXPECT_EQ(ref, v());
174  }
175 }
176 
178  ByteVector b;
180  TypeParam ref = std::numeric_limits<TypeParam>::max();
181  v = ref;
182  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
183  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
184  for(int i = 0; i < 10; ++i) {
185  EXPECT_TRUE(b.unpack(v));
186  EXPECT_EQ(ref, v());
187  }
188 }
189 
191  ByteVector b;
193  TypeParam ref = std::numeric_limits<TypeParam>::min();
194  v = ref;
195  for(int i = 0; i < 10; ++i) EXPECT_TRUE(b.pack(v));
196  EXPECT_EQ(10 * sizeof(TypeParam), b.size());
197  for(int i = 0; i < 10; ++i) {
198  EXPECT_TRUE(b.unpack(v));
199  EXPECT_EQ(ref, v());
200  }
201 }
202 
203 TYPED_TEST(ByteVectorScaledTest, Pack1DoubleZero) {
204  ByteVector b;
205  double ref = 0.0;
206  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
207  EXPECT_EQ(sizeof(TypeParam), b.size());
208  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
209  EXPECT_DOUBLE_EQ(0, ref);
210 }
211 
213  ByteVector b;
214  double ref = 10.0;
215  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
216  EXPECT_EQ(sizeof(TypeParam), b.size());
217  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
218  EXPECT_DOUBLE_EQ(10.f, ref);
219 }
220 
221 TYPED_TEST(ByteVectorScaledTest, Pack1DoubleSaturationMax) {
222  ByteVector b;
223  double ref = (double)std::numeric_limits<TypeParam>::max() + 1.0;
224  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
225  EXPECT_EQ(sizeof(TypeParam), b.size());
226  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
227  EXPECT_DOUBLE_EQ(std::numeric_limits<TypeParam>::max(), ref);
228 }
229 
230 TYPED_TEST(ByteVectorScaledTest, Pack1DoubleSaturationMin) {
231  ByteVector b;
232  double ref = (double)std::numeric_limits<TypeParam>::min() - 1.0;
233  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
234  EXPECT_EQ(sizeof(TypeParam), b.size());
235  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
236  EXPECT_DOUBLE_EQ(std::numeric_limits<TypeParam>::min(), ref);
237 }
238 
240  ByteVector b;
241  float ref = 0.0;
242  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
243  EXPECT_EQ(sizeof(TypeParam), b.size());
244  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
245  EXPECT_FLOAT_EQ(0.f, ref);
246 }
247 
249  ByteVector b;
250  float ref = 10.0;
251  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
252  EXPECT_EQ(sizeof(TypeParam), b.size());
253  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
254  EXPECT_FLOAT_EQ(10.f, ref);
255 }
256 
257 TYPED_TEST(ByteVectorScaledTest, Pack1FloatSaturationMax) {
258  ByteVector b;
259  float ref = (float)std::numeric_limits<TypeParam>::max() + 1.0;
260  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
261  EXPECT_EQ(sizeof(TypeParam), b.size());
262  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
263  EXPECT_FLOAT_EQ(std::numeric_limits<TypeParam>::max(), ref);
264 }
265 
266 TYPED_TEST(ByteVectorScaledTest, Pack1FloatSaturationMin) {
267  ByteVector b;
268  float ref = (float)std::numeric_limits<TypeParam>::min() - 1.0;
269  EXPECT_TRUE(b.pack<TypeParam>(ref, 1));
270  EXPECT_EQ(sizeof(TypeParam), b.size());
271  EXPECT_TRUE(b.unpack<TypeParam>(ref, 1));
272  EXPECT_FLOAT_EQ(std::numeric_limits<TypeParam>::min(), ref);
273 }
274 
275 } // namespace msp
276 
277 int main(int argc, char **argv) {
278  ::testing::InitGoogleTest(&argc, argv);
279  return RUN_ALL_TESTS();
280 }
::testing::Types< uint8_t, uint16_t, uint32_t, int8_t, int16_t, int32_t > scaleOutputTypes
bool unpack(T &val) const
Extracts little endian integers from the ByteVector. Consumes a number of bytes matching sizeof(T)...
Definition: ByteVector.hpp:224
TYPED_TEST_CASE(ByteVectorBasicTest, basicTypes)
::testing::Types< bool, uint8_t, uint16_t, uint32_t, int8_t, int16_t, int32_t, float, double > basicTypes
TEST(ByteVectorBasicTest, Initialzation)
bool pack(const T &val)
Packs integer types into the ByteVector. Ensures little endian packing.
Definition: ByteVector.hpp:51
TYPED_TEST(ByteVectorBasicTest, Pack1zero)
std::size_t unpacking_remaining() const
Returns the number of bytes still avialable for unpacking.
Definition: ByteVector.hpp:448
std::size_t unpacking_offset() const
Gives the number of bytes which have already been consumed by unpack operations.
Definition: ByteVector.hpp:412
int main(int argc, char **argv)


msp
Author(s): Christian Rauch
autogenerated on Tue Oct 6 2020 03:38:57