File: tserialstream.cpp

package info (click to toggle)
wsclean 3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,968 kB
  • sloc: cpp: 85,742; python: 3,526; sh: 245; makefile: 21
file content (294 lines) | stat: -rw-r--r-- 9,520 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <boost/test/unit_test.hpp>

#include <aocommon/io/serialistream.h>
#include <aocommon/io/serialostream.h>

#include <limits>

using aocommon::SerialIStream;
using aocommon::SerialOStream;

BOOST_AUTO_TEST_SUITE(serialization)

BOOST_AUTO_TEST_CASE(one_by_one_input_syntax) {
  SerialOStream ostr;
  ostr.Bool(true)
      .UInt8(80)
      .UInt16(160)
      .UInt32(320)
      .UInt64(640)
      .Float(1.5)
      .Double(3.14)
      .LDouble(2.71)
      .String("hi!");

  SerialIStream istr(std::move(ostr));

  BOOST_CHECK_EQUAL(istr.Bool(), true);
  BOOST_CHECK_EQUAL(istr.UInt8(), 80u);
  BOOST_CHECK_EQUAL(istr.UInt16(), 160u);
  BOOST_CHECK_EQUAL(istr.UInt32(), 320u);
  BOOST_CHECK_EQUAL(istr.UInt64(), 640u);
  BOOST_CHECK_EQUAL(istr.Float(), 1.5);
  BOOST_CHECK_EQUAL(istr.Double(), 3.14);
  BOOST_CHECK_EQUAL(istr.LDouble(), 2.71);
  BOOST_CHECK_EQUAL(istr.String(), "hi!");
}

BOOST_AUTO_TEST_CASE(concatenated_input_syntax) {
  SerialOStream ostr;
  ostr.Bool(true)
      .UInt8(80)
      .UInt16(160)
      .UInt32(320)
      .UInt64(640)
      .Float(1.5)
      .Double(3.14)
      .LDouble(2.71)
      .String("hi!");

  bool b = false;
  uint8_t u8 = 0;
  uint16_t u16 = 0;
  uint32_t u32 = 0;
  uint64_t u64 = 0;
  float f = 0.0f;
  double d = 0.0;
  long double ld = 0.0;
  std::string s;

  SerialIStream istr(std::move(ostr));
  istr.Bool(b)
      .UInt8(u8)
      .UInt16(u16)
      .UInt32(u32)
      .UInt64(u64)
      .Float(f)
      .Double(d)
      .LDouble(ld)
      .String(s);

  BOOST_CHECK_EQUAL(b, true);
  BOOST_CHECK_EQUAL(u8, 80u);
  BOOST_CHECK_EQUAL(u16, 160u);
  BOOST_CHECK_EQUAL(u32, 320u);
  BOOST_CHECK_EQUAL(u64, 640u);
  BOOST_CHECK_EQUAL(f, 1.5);
  BOOST_CHECK_EQUAL(d, 3.14);
  BOOST_CHECK_EQUAL(ld, 2.71);
  BOOST_CHECK_EQUAL(s, "hi!");
}

BOOST_AUTO_TEST_CASE(vector) {
  const std::vector<std::int32_t> int32_a;
  const std::vector<std::int32_t> int32_b{12, -13, 14};
  const std::vector<std::uint64_t> uint64_a;
  const std::vector<std::uint64_t> uint64_b{18, 19, 20};
  const std::vector<std::complex<float>> cf_a;
  const std::vector<std::complex<float>> cf_b{{1, 1}, {-2, -2}, {3, 3}};

  SerialOStream ostr;
  ostr.Vector(int32_a)
      .Vector(int32_b)
      .Vector(uint64_a)
      .Vector(uint64_b)
      .Vector(cf_a)
      .Vector(cf_b);

  SerialIStream istr(std::move(ostr));

  std::vector<std::int32_t> out_int32_a, out_int32_b;
  std::vector<std::uint64_t> out_uint64_a, out_uint64_b;
  std::vector<std::complex<float>> out_cf_a, out_cf_b;
  istr.Vector(out_int32_a)
      .Vector(out_int32_b)
      .Vector(out_uint64_a)
      .Vector(out_uint64_b)
      .Vector(out_cf_a)
      .Vector(out_cf_b);
  BOOST_CHECK(out_int32_a.empty());
  BOOST_CHECK_EQUAL_COLLECTIONS(int32_b.begin(), int32_b.end(),
                                out_int32_b.begin(), out_int32_b.end());
  BOOST_CHECK(out_uint64_a.empty());
  BOOST_CHECK_EQUAL_COLLECTIONS(uint64_b.begin(), uint64_b.end(),
                                out_uint64_b.begin(), out_uint64_b.end());
  BOOST_CHECK(out_cf_a.empty());
  BOOST_CHECK_EQUAL_COLLECTIONS(cf_b.begin(), cf_b.end(), out_cf_b.begin(),
                                out_cf_b.end());
}

BOOST_AUTO_TEST_CASE(vector64) {
  const std::vector<std::int8_t> int8_a;
  const std::vector<std::int8_t> int8_b{
      0, std::numeric_limits<std::int8_t>::min(),
      std::numeric_limits<std::int8_t>::max()};
  const std::vector<std::int64_t> int64_a;
  const std::vector<std::int64_t> int64_b{
      0, std::numeric_limits<std::int64_t>::min(),
      std::numeric_limits<std::int64_t>::max()};

  SerialOStream ostr;
  ostr.VectorUInt64(int8_a)
      .VectorUInt64(int8_b)
      .VectorUInt64(int64_a)
      .VectorUInt64(int64_b);

  SerialIStream istr(std::move(ostr));

  std::vector<std::int8_t> out_int8_a, out_int8_b;
  std::vector<std::int64_t> out_int64_a, out_int64_b;
  istr.VectorUInt64(out_int8_a)
      .VectorUInt64(out_int8_b)
      .VectorUInt64(out_int64_b)
      .VectorUInt64(out_int64_b);

  BOOST_CHECK(out_int8_a.empty());
  BOOST_CHECK_EQUAL_COLLECTIONS(int8_b.begin(), int8_b.end(),
                                out_int8_b.begin(), out_int8_b.end());
  BOOST_CHECK(out_int64_a.empty());
  BOOST_CHECK_EQUAL_COLLECTIONS(int64_b.begin(), int64_b.end(),
                                out_int64_b.begin(), out_int64_b.end());
}

struct Serializable {
  // By updating the stream pointer members, the test can check that these
  // functions were called with the proper argument.
  void Serialize(SerialOStream& stream) { ostream = &stream; }
  void Unserialize(SerialIStream& stream) { istream = &stream; }

  SerialIStream* istream = nullptr;
  SerialOStream* ostream = nullptr;
};

BOOST_AUTO_TEST_CASE(pointer) {
  std::shared_ptr<Serializable> shared_empty;
  std::shared_ptr<Serializable> shared(new Serializable());
  std::unique_ptr<Serializable> unique_empty;
  std::unique_ptr<Serializable> unique(new Serializable());

  SerialOStream ostr;
  ostr.Ptr(shared_empty).Ptr(shared).Ptr(unique_empty).Ptr(unique);
  BOOST_CHECK_EQUAL(shared->ostream, &ostr);
  BOOST_CHECK_EQUAL(unique->ostream, &ostr);
  BOOST_CHECK_EQUAL(shared->istream, nullptr);
  BOOST_CHECK_EQUAL(unique->istream, nullptr);

  SerialIStream istr(std::move(ostr));

  std::shared_ptr<Serializable> out_shared_empty;
  std::shared_ptr<Serializable> out_shared;
  std::unique_ptr<Serializable> out_unique_empty;
  std::unique_ptr<Serializable> out_unique;
  istr.Ptr(out_shared_empty)
      .Ptr(out_shared)
      .Ptr(out_unique_empty)
      .Ptr(out_unique);
  BOOST_CHECK(!out_shared_empty);
  BOOST_CHECK(!out_unique_empty);
  BOOST_REQUIRE(out_shared);
  BOOST_REQUIRE(out_unique);
  BOOST_CHECK_EQUAL(out_shared->ostream, nullptr);
  BOOST_CHECK_EQUAL(out_unique->ostream, nullptr);
  BOOST_CHECK_EQUAL(out_shared->istream, &istr);
  BOOST_CHECK_EQUAL(out_unique->istream, &istr);
}

struct UnserializeViaConstructor {
  // In contrast to the Serializable struct, this struct supports unserializing
  // via its constructor instead of via an Unserialize function.
  // Similarly to the Serializable struct, it stores received arguments.
  UnserializeViaConstructor() = default;
  explicit UnserializeViaConstructor(SerialIStream& stream)
      : istream(&stream) {}
  void Serialize(SerialOStream& stream) const { ostream = &stream; }

  SerialIStream* istream = nullptr;
  mutable SerialOStream* ostream = nullptr;  // mutable is for testing purposes.
};

BOOST_AUTO_TEST_CASE(pointer_via_constructor) {
  auto in = std::make_shared<UnserializeViaConstructor>();
  std::unique_ptr<UnserializeViaConstructor> in_empty;
  SerialOStream ostr;
  BOOST_CHECK(&ostr.Ptr(in).Ptr(in_empty) == &ostr);
  BOOST_CHECK_EQUAL(in->ostream, &ostr);
  BOOST_CHECK_EQUAL(in->istream, nullptr);

  SerialIStream istr(std::move(ostr));
  std::shared_ptr<UnserializeViaConstructor> out;
  std::unique_ptr<UnserializeViaConstructor> out_empty;
  BOOST_CHECK(&istr.Ptr(out).Ptr(out_empty) == &istr);
  BOOST_REQUIRE(out);
  BOOST_CHECK_EQUAL(out->ostream, nullptr);
  BOOST_CHECK_EQUAL(out->istream, &istr);
  BOOST_CHECK(!out_empty);
}

struct TestObject {
  uint64_t a, b;
  void Serialize(SerialOStream& stream) const { stream.UInt64(a).UInt64(b); }
  void Unserialize(SerialIStream& stream) { stream.UInt64(a).UInt64(b); }
  // The Boost checks require operator== and operator!= to be implemented.
  bool operator==(const TestObject& rhs) const {
    return a == rhs.a && b == rhs.b;
  }
  bool operator!=(const TestObject& rhs) const { return !operator==(rhs); }
};
void operator<<(std::ostream& str, const TestObject& obj) {
  str << "TestObject{" << obj.a << ", " << obj.b << '}';
}

BOOST_AUTO_TEST_CASE(object) {
  const TestObject in{42, 4242};
  SerialOStream ostr;
  BOOST_CHECK_EQUAL(&ostr.Object(in), &ostr);

  SerialIStream istr(std::move(ostr));
  TestObject out;
  BOOST_CHECK_EQUAL(&istr.Object(out), &istr);
  BOOST_CHECK_EQUAL(out, in);
}

BOOST_AUTO_TEST_CASE(object_vector) {
  const std::vector<TestObject> empty;
  const std::vector<TestObject> filled{{13, 37}, {0, 42}};
  std::vector<UnserializeViaConstructor> via_constructor(4);

  SerialOStream ostr;
  ostr.ObjectVector(empty)
      .ObjectVector(filled)
      .ObjectVector(empty)
      .ObjectVector(filled)
      .ObjectVector(via_constructor);

  SerialIStream istr(std::move(ostr));

  std::vector<TestObject> out_empty1;
  std::vector<TestObject> out_empty2;
  std::vector<TestObject> out_filled1;
  std::vector<TestObject> out_filled2;
  std::vector<UnserializeViaConstructor> out_via_constructor;
  istr.ObjectVector(out_empty1)
      .ObjectVector(out_filled1)
      .ObjectVector(out_empty2)
      .ObjectVector(out_filled2)
      .ObjectVector(out_via_constructor);

  BOOST_CHECK(out_empty1.empty());
  BOOST_CHECK_EQUAL_COLLECTIONS(filled.begin(), filled.end(),
                                out_filled1.begin(), out_filled1.end());
  BOOST_CHECK(out_empty2.empty());
  BOOST_CHECK_EQUAL_COLLECTIONS(filled.begin(), filled.end(),
                                out_filled2.begin(), out_filled2.end());
  BOOST_CHECK_EQUAL(out_via_constructor.size(), via_constructor.size());
  for (const UnserializeViaConstructor& element : via_constructor) {
    BOOST_CHECK_EQUAL(element.istream, nullptr);
    BOOST_CHECK_EQUAL(element.ostream, &ostr);
  }
  for (const UnserializeViaConstructor& element : out_via_constructor) {
    BOOST_CHECK_EQUAL(element.istream, &istr);
    BOOST_CHECK_EQUAL(element.ostream, nullptr);
  }
}

BOOST_AUTO_TEST_SUITE_END()