forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestDataHeader.cxx
More file actions
373 lines (322 loc) · 14.1 KB
/
testDataHeader.cxx
File metadata and controls
373 lines (322 loc) · 14.1 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#define BOOST_TEST_MODULE Test Headers DataHeaderTest
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <iomanip>
#include "Headers/DataHeader.h"
#include "Headers/DataHeaderHelpers.h"
#include "Headers/NameHeader.h"
#include "Headers/Stack.h"
#include <chrono>
using system_clock = std::chrono::system_clock;
using TimeScale = std::chrono::nanoseconds;
namespace o2
{
namespace header
{
namespace test
{
struct MetaHeader : public BaseHeader {
// Required to do the lookup
static const o2::header::HeaderType sHeaderType;
static const uint32_t sVersion = 1;
MetaHeader(uint32_t v)
: BaseHeader(sizeof(MetaHeader), sHeaderType, o2::header::gSerializationMethodNone, sVersion), secret(v)
{
}
uint64_t secret;
};
constexpr o2::header::HeaderType MetaHeader::sHeaderType = "MetaHead";
} // namespace test
} // namespace header
} // namespace o2
namespace o2
{
namespace header
{
BOOST_AUTO_TEST_CASE(Descriptor_test)
{
// test for the templated Descriptor struct
constexpr int descriptorSize = 8;
using TestDescriptorT = Descriptor<descriptorSize>;
BOOST_CHECK(TestDescriptorT::size == descriptorSize);
BOOST_CHECK(TestDescriptorT::bitcount == descriptorSize * 8);
BOOST_CHECK(sizeof(TestDescriptorT::ItgType) * TestDescriptorT::arraySize == descriptorSize);
static_assert(TestDescriptorT::size == sizeof(TestDescriptorT),
"Descriptor must have size of the underlying data member");
// the Descriptor allows to define an integral value from
// a char sequence so that the in-memory representation shows
// a readable pattern
constexpr char readable[] = "DESCRPTR";
// the inverse sequence as integer value: R T P R C S E D
constexpr TestDescriptorT::ItgType itgvalue = 0x5254505243534544;
TestDescriptorT testDescriptor(readable);
TestDescriptorT anotherDescriptor("ANOTHER");
BOOST_CHECK(TestDescriptorT::size == sizeof(testDescriptor));
// copy constructor and comparison operator
BOOST_CHECK(testDescriptor == TestDescriptorT(testDescriptor));
// comparison operator
BOOST_CHECK(testDescriptor != anotherDescriptor);
// type cast operator
BOOST_CHECK(itgvalue == testDescriptor);
// assignment operator
anotherDescriptor = testDescriptor;
BOOST_CHECK(testDescriptor == anotherDescriptor);
anotherDescriptor = TestDescriptorT("SOMEMORE");
BOOST_CHECK(itgvalue != anotherDescriptor);
// assignment using implicit conversion with constructor
anotherDescriptor = itgvalue;
BOOST_CHECK(testDescriptor == anotherDescriptor);
// check with runtime string
std::string runtimeString = "RUNTIMES";
TestDescriptorT runtimeDescriptor;
runtimeDescriptor.runtimeInit(runtimeString.c_str());
BOOST_CHECK(runtimeDescriptor == TestDescriptorT("RUNTIMES"));
BOOST_CHECK(testDescriptor.as<std::string>().length() == 8);
}
BOOST_AUTO_TEST_CASE(DataDescription_test)
{
char test[16] = "ITSRAW*********";
test[6] = 0;
DataDescription desc("ITSRAW");
BOOST_CHECK(strcmp(desc.str, "ITSRAW") == 0);
// checking the corresponding integer value
// the upper part must be 0 since the string has only up tp 8 chars
// lower part corresponds to reverse ITSRAW
// W A R S T I
uint64_t itgDesc = 0x0000574152535449;
BOOST_CHECK(desc.itg[0] == itgDesc);
BOOST_CHECK(desc.itg[1] == 0);
BOOST_CHECK(desc == DataDescription("ITSRAW"));
DataDescription desc2(test);
BOOST_CHECK(strcmp(desc2.str, "ITSRAW") == 0);
// the upper part must be 0 since the string has only up tp 8 chars
BOOST_CHECK(desc2.itg[1] == 0);
BOOST_CHECK(desc2 == DataDescription("ITSRAW"));
std::string runtimeString = "DATA_DESCRIPTION";
DataDescription runtimeDesc;
runtimeDesc.runtimeInit(runtimeString.c_str());
BOOST_CHECK(runtimeDesc == DataDescription("DATA_DESCRIPTION"));
BOOST_CHECK(desc.as<std::string>().length() == 6);
BOOST_CHECK(runtimeDesc.as<std::string>().length() == 16);
BOOST_CHECK(DataDescription("INVALIDDATA").as<std::string>().length() == 11);
BOOST_CHECK(DataDescription("A") < DataDescription("B"));
BOOST_CHECK(DataDescription("AA") < DataDescription("AB"));
BOOST_CHECK(DataDescription("AAA") < DataDescription("AAB"));
BOOST_CHECK(DataDescription("AAA") < DataDescription("ABA"));
}
BOOST_AUTO_TEST_CASE(DataOrigin_test)
{
// test for the templated Descriptor struct
constexpr int descriptorSize = 4;
using TestDescriptorT = Descriptor<descriptorSize>;
BOOST_CHECK(TestDescriptorT::size == descriptorSize);
BOOST_CHECK(TestDescriptorT::bitcount == descriptorSize * 8);
BOOST_CHECK(sizeof(TestDescriptorT::ItgType) * TestDescriptorT::arraySize == descriptorSize);
BOOST_CHECK(TestDescriptorT::size == sizeof(DataOrigin));
// we want to explicitely have the size of DataOrigin to be 4
static_assert(sizeof(DataOrigin) == 4,
"DataOrigin struct must be of size 4");
// Check that ordering works.
BOOST_CHECK(DataOrigin("A") < DataOrigin("B"));
BOOST_CHECK(DataOrigin("AA") < DataOrigin("AB"));
BOOST_CHECK(DataOrigin("AAA") < DataOrigin("AAB"));
BOOST_CHECK(DataOrigin("AAA") < DataOrigin("ABA"));
std::vector<DataOrigin> v1 = {DataOrigin("B"), DataOrigin("C"), DataOrigin("A")};
std::sort(v1.begin(), v1.end());
BOOST_CHECK_EQUAL(v1[0], DataOrigin("A"));
BOOST_CHECK_EQUAL(v1[1], DataOrigin("B"));
BOOST_CHECK_EQUAL(v1[2], DataOrigin("C"));
std::vector<DataOrigin> v2 = {DataOrigin("A"), DataOrigin("B")};
std::sort(v2.begin(), v2.end());
BOOST_CHECK_EQUAL(v2[0], DataOrigin("A"));
BOOST_CHECK_EQUAL(v2[1], DataOrigin("B"));
using CustomHeader = std::tuple<DataOrigin, DataDescription>;
std::vector<CustomHeader> v3{CustomHeader{"TST", "B"}, CustomHeader{"TST", "A"}};
std::sort(v3.begin(), v3.end());
auto h0 = CustomHeader{"TST", "A"};
auto h1 = CustomHeader{"TST", "B"};
BOOST_CHECK(v3[0] == h0);
BOOST_CHECK(v3[1] == h1);
using CustomHeader2 = std::tuple<DataOrigin, DataDescription, int>;
std::vector<CustomHeader2> v4{CustomHeader2{"TST", "A", 1}, CustomHeader2{"TST", "A", 0}};
std::sort(v4.begin(), v4.end());
auto hh0 = CustomHeader2{"TST", "A", 0};
auto hh1 = CustomHeader2{"TST", "A", 1};
BOOST_CHECK(v4[0] == hh0);
BOOST_CHECK(v4[1] == hh1);
struct CustomHeader3 {
DataOrigin origin;
DataDescription desc;
uint64_t subSpec;
int isOut;
};
std::vector<CustomHeader3> v5{CustomHeader3{"TST", "A", 0, 1}, CustomHeader3{"TST", "A", 0, 0}};
std::sort(v5.begin(), v5.end(), [](CustomHeader3 const& lhs, CustomHeader3 const& rhs) {
return std::tie(lhs.origin, lhs.desc, rhs.subSpec, lhs.isOut) < std::tie(rhs.origin, rhs.desc, rhs.subSpec, rhs.isOut);
});
BOOST_CHECK(v5[0].isOut == 0);
BOOST_CHECK(v5[1].isOut == 1);
}
BOOST_AUTO_TEST_CASE(BaseHeader_test)
{
static_assert(sizeof(HeaderType) == 8,
"HeaderType struct must be of size 8");
static_assert(sizeof(SerializationMethod) == 8,
"SerializationMethod struct must be of size 8");
static_assert(sizeof(BaseHeader) == 32,
"BaseHeader struct must be of size 32");
}
BOOST_AUTO_TEST_CASE(DataHeader_test)
{
DataHeader dh;
bool verbose = false;
if (verbose) {
std::cout << "size of BaseHeader: " << sizeof(BaseHeader) << std::endl;
std::cout << "size of DataHeader: " << sizeof(DataHeader) << std::endl;
std::cout << "dataDescription: "
<< "size " << std::setw(2) << sizeof(dh.dataDescription) << " at " << (char*)(&dh.dataDescription) - (char*)(&dh) << std::endl;
std::cout << "dataOrigin: "
<< "size " << std::setw(2) << sizeof(dh.dataOrigin) << " at " << (char*)(&dh.dataOrigin) - (char*)(&dh) << std::endl;
std::cout << "splitPayloadParts: "
<< "size " << std::setw(2) << sizeof(dh.splitPayloadParts) << " at "
<< (char*)(&dh.splitPayloadParts) - (char*)(&dh) << std::endl;
std::cout << "payloadSerializationMethod: "
<< "size " << std::setw(2) << sizeof(dh.payloadSerializationMethod) << " at " << (char*)(&dh.payloadSerializationMethod) - (char*)(&dh) << std::endl;
std::cout << "subSpecification: "
<< "size " << std::setw(2) << sizeof(dh.subSpecification) << " at " << (char*)(&dh.subSpecification) - (char*)(&dh) << std::endl;
std::cout << "splitPayloadIndex: "
<< "size " << std::setw(2) << sizeof(dh.splitPayloadIndex) << " at "
<< (char*)(&dh.splitPayloadIndex) - (char*)(&dh) << std::endl;
std::cout << "payloadSize "
<< "size " << std::setw(2) << sizeof(dh.payloadSize) << " at " << (char*)(&dh.payloadSize) - (char*)(&dh) << std::endl;
}
// DataHeader must have size 96
static_assert(sizeof(DataHeader) == 96,
"DataHeader struct must be of size 96");
DataHeader dh2;
BOOST_CHECK(dh == dh2);
DataHeader dh3{gDataDescriptionInvalid, gDataOriginInvalid, DataHeader::SubSpecificationType{0}, 0};
BOOST_CHECK(dh == dh3);
DataHeader dh4{gDataDescriptionAny, gDataOriginAny, DataHeader::SubSpecificationType{1}, 1};
BOOST_CHECK(!(dh4 == dh));
dh4 = dh;
BOOST_CHECK(dh4 == dh);
DataHeader dh5{gDataDescriptionAny, gDataOriginAny, DataHeader::SubSpecificationType{1}, 1};
BOOST_REQUIRE_EQUAL(fmt::format("{}", gDataOriginAny), "***");
BOOST_REQUIRE_EQUAL(fmt::format("{}", gDataDescriptionAny), "***************");
BOOST_REQUIRE_EQUAL(fmt::format("{}", DataHeader::SubSpecificationType{1}), "1");
}
BOOST_AUTO_TEST_CASE(headerStack_test)
{
DataHeader dh1{gDataDescriptionInvalid, gDataOriginInvalid, DataHeader::SubSpecificationType{0}, 0};
Stack s1{DataHeader{gDataDescriptionInvalid, gDataOriginInvalid, DataHeader::SubSpecificationType{0}, 0},
NameHeader<9>{"somename"}};
const DataHeader* h1 = get<DataHeader*>(s1.data());
BOOST_CHECK(h1 != nullptr);
BOOST_CHECK(*h1 == dh1);
BOOST_CHECK(h1->flagsNextHeader == 1);
const NameHeader<0>* h2 = get<NameHeader<0>*>(s1.data());
BOOST_CHECK(h2 != nullptr);
BOOST_CHECK(0 == std::strcmp(h2->getName(), "somename"));
BOOST_CHECK(h2->description == NameHeader<0>::sHeaderType);
BOOST_CHECK(h2->serialization == gSerializationMethodNone);
BOOST_CHECK(h2->size() == sizeof(NameHeader<9>));
BOOST_CHECK(h2->getNameLength() == 9);
// create new stack from stack and additional header
auto meta = test::MetaHeader{42};
Stack s2{s1, meta};
BOOST_CHECK(s2.size() == s1.size() + sizeof(decltype(meta)));
// check dynamic construction - where we don't have the type information and need to
// work with BaseHeader pointers
const test::MetaHeader thead{2};
o2::header::BaseHeader const* bname = reinterpret_cast<BaseHeader const*>(&thead);
Stack ds2(s1, *bname);
BOOST_CHECK(ds2.size() == s1.size() + sizeof(thead));
BOOST_CHECK(std::memcmp(get<test::MetaHeader*>(ds2.data()), &thead, sizeof(thead)) == 0);
auto* h3 = get<test::MetaHeader*>(s1.data());
BOOST_CHECK(h3 == nullptr);
h3 = get<test::MetaHeader*>(s2.data());
BOOST_REQUIRE(h3 != nullptr);
BOOST_CHECK(h3->flagsNextHeader == false);
h1 = get<DataHeader*>(s2.data());
BOOST_REQUIRE(h1 != nullptr);
BOOST_CHECK(h1->flagsNextHeader == true);
// create stack from header and empty stack
Stack s3{meta, Stack{}};
BOOST_CHECK(s3.size() == sizeof(meta));
h3 = get<test::MetaHeader*>(s3.data());
BOOST_REQUIRE(h3 != nullptr);
// no next header to be flagged as the stack was empty
BOOST_CHECK(h3->flagsNextHeader == false);
// create new stack from stack, empty stack, and header
Stack s4{s1, Stack{}, meta};
BOOST_CHECK(s4.size() == s1.size() + sizeof(meta));
// check if we can find the header even though there was an empty stack in the middle
h3 = get<test::MetaHeader*>(s4.data());
BOOST_REQUIRE(h3 != nullptr);
BOOST_CHECK(h3->secret == 42);
// test constructing from a buffer and an additional header
Stack s5(std::pmr::new_delete_resource(), s1.data(), Stack{}, meta);
BOOST_CHECK(s5.size() == s1.size() + sizeof(meta));
// check if we can find the header even though there was an empty stack in the middle
h3 = get<test::MetaHeader*>(s5.data());
BOOST_REQUIRE(h3 != nullptr);
BOOST_CHECK(h3->secret == 42);
auto* h4 = Stack::lastHeader(s5.data());
auto* h5 = Stack::firstHeader(s5.data());
auto* h6 = get<DataHeader*>(s5.data());
BOOST_REQUIRE(h5 == h6);
BOOST_REQUIRE(h5 != nullptr);
BOOST_CHECK(h4 == h3);
// let's assume we have some stack that is missing the required DataHeader at the beginning:
Stack s6{std::pmr::new_delete_resource(), DataHeader{}, s1.data()};
BOOST_CHECK(s6.size() == sizeof(DataHeader) + s1.size());
}
BOOST_AUTO_TEST_CASE(Descriptor_benchmark)
{
using TestDescriptor = Descriptor<8>;
TestDescriptor a("TESTDESC");
TestDescriptor b(a);
auto refTime = system_clock::now();
const int nrolls = 1000000;
for (auto count = 0; count < nrolls; ++count) {
if (a == b) {
++a.itg[0];
++b.itg[0];
}
}
auto duration = std::chrono::duration_cast<TimeScale>(std::chrono::system_clock::now() - refTime);
std::cout << nrolls << " operation(s): " << duration.count() << " ns" << std::endl;
// there is not really a check at the moment
}
BOOST_AUTO_TEST_CASE(Descriptor_formatting)
{
using TestDescriptor = Descriptor<8>;
TestDescriptor a("TESTDESC");
TestDescriptor b(a);
auto refTime = system_clock::now();
const int nrolls = 1000000;
for (auto count = 0; count < nrolls; ++count) {
if (a == b) {
++a.itg[0];
++b.itg[0];
}
}
auto duration = std::chrono::duration_cast<TimeScale>(std::chrono::system_clock::now() - refTime);
std::cout << nrolls << " operation(s): " << duration.count() << " ns" << std::endl;
// there is not really a check at the moment
}
} // namespace header
} // namespace o2