-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathparser.cxx
More file actions
251 lines (207 loc) · 8.58 KB
/
parser.cxx
File metadata and controls
251 lines (207 loc) · 8.58 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
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// 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.
/// @file parser.cxx
/// @author Matthias Richter
/// @since 2017-09-20
/// @brief Unit test for data parsing methods in Algorithm/Parser.h
#define BOOST_TEST_MODULE Test Algorithm Parser
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <iomanip>
#include <vector>
#include "../include/Algorithm/Parser.h"
#include "StaticSequenceAllocator.h"
// header test class
struct Header {
unsigned identifier = 0xdeadbeef;
size_t payloadSize = 0;
Header(size_t ps) : payloadSize(ps) {}
};
// trailer test class
struct Trailer {
unsigned identifier = 0xaaffee00;
unsigned char flags = 0xaa;
Trailer(unsigned char f) : flags(f) {}
};
// trailer test class including payload size
struct SizedTrailer {
unsigned identifier = 0xaaffee00;
unsigned char flags = 0xaa;
size_t payloadSize = 0;
SizedTrailer(size_t s, unsigned char f) : payloadSize(s), flags(f) {}
};
BOOST_AUTO_TEST_CASE(test_forwardparser_header_and_trailer)
{
using FrameT = o2::algorithm::Composite<Header, Trailer>;
// note: the length of the data is set in the header word
using TestFrame = o2::algorithm::StaticSequenceAllocator;
TestFrame tf(FrameT(16, "lotsofsillydata", 0xaa),
FrameT(5, "test", 0xcc),
FrameT(10, "dummydata", 0x33));
using ParserT = o2::algorithm::ForwardParser<typename FrameT::HeaderType,
typename FrameT::TrailerType>;
auto checkHeader = [](const typename FrameT::HeaderType& header) {
return header.identifier == 0xdeadbeef;
};
auto checkTrailer = [](const typename FrameT::TrailerType& trailer) {
return trailer.identifier == 0xaaffee00;
};
auto getFrameSize = [](const typename ParserT::HeaderType& header) {
// frame size includes total offset from header and trailer
return header.payloadSize + ParserT::totalOffset;
};
std::vector<typename ParserT::FrameInfo> frames;
auto insert = [&frames](typename ParserT::FrameInfo& info) {
frames.emplace_back(info);
return true;
};
ParserT parser;
auto result = parser.parse(tf.buffer.get(), tf.size(),
checkHeader,
checkTrailer,
getFrameSize,
insert);
BOOST_REQUIRE(result == 3);
BOOST_REQUIRE(frames.size() == 3);
BOOST_CHECK(memcmp(frames[0].payload, "lotsofsillydata", frames[0].length) == 0);
BOOST_CHECK(memcmp(frames[1].payload, "test", frames[1].length) == 0);
BOOST_CHECK(memcmp(frames[2].payload, "dummydata", frames[2].length) == 0);
}
BOOST_AUTO_TEST_CASE(test_forwardparser_header_and_void_trailer)
{
using FrameT = o2::algorithm::Composite<Header>;
// note: the length of the data is set in the header word
using TestFrame = o2::algorithm::StaticSequenceAllocator;
TestFrame tf(FrameT(16, "lotsofsillydata"),
FrameT(5, "test"),
FrameT(10, "dummydata"));
using ParserT = o2::algorithm::ForwardParser<typename FrameT::HeaderType,
typename FrameT::TrailerType>;
auto checkHeader = [](const typename FrameT::HeaderType& header) {
return header.identifier == 0xdeadbeef;
};
auto getFrameSize = [](const typename ParserT::HeaderType& header) {
// frame size includes total offset from header and trailer
return header.payloadSize + ParserT::totalOffset;
};
std::vector<typename ParserT::FrameInfo> frames;
auto insert = [&frames](typename ParserT::FrameInfo& info) {
frames.emplace_back(info);
return true;
};
ParserT parser;
auto result = parser.parse(tf.buffer.get(), tf.size(),
checkHeader,
getFrameSize,
insert);
BOOST_REQUIRE(result == 3);
BOOST_REQUIRE(frames.size() == 3);
BOOST_CHECK(memcmp(frames[0].payload, "lotsofsillydata", frames[0].length) == 0);
BOOST_CHECK(memcmp(frames[1].payload, "test", frames[1].length) == 0);
BOOST_CHECK(memcmp(frames[2].payload, "dummydata", frames[2].length) == 0);
}
BOOST_AUTO_TEST_CASE(test_forwardparser_no_frames)
{
using FrameT = o2::algorithm::Composite<Header>;
// note: the length of the data is set in the header word
using TestFrame = o2::algorithm::StaticSequenceAllocator;
TestFrame tf(FrameT(16, "lotsofsillydata"),
FrameT(5, "test"),
FrameT(10, "dummydata"));
using ParserT = o2::algorithm::ForwardParser<typename FrameT::HeaderType,
typename FrameT::TrailerType>;
auto checkHeader = [](const typename FrameT::HeaderType& header) {
// simply indicate invalid header to read no frames
return false;
};
auto getFrameSize = [](const typename ParserT::HeaderType& header) {
// frame size includes total offset from header and trailer
return header.payloadSize + ParserT::totalOffset;
};
std::vector<typename ParserT::FrameInfo> frames;
auto insert = [&frames](typename ParserT::FrameInfo& info) {
frames.emplace_back(info);
return true;
};
ParserT parser;
auto result = parser.parse(tf.buffer.get(), tf.size(),
checkHeader,
getFrameSize,
insert);
// check that there are really no frames found
BOOST_REQUIRE(result == 0);
}
BOOST_AUTO_TEST_CASE(test_forwardparser_format_error)
{
using FrameT = o2::algorithm::Composite<Header>;
// note: the length of the data is set in the header word
using TestFrame = o2::algorithm::StaticSequenceAllocator;
TestFrame tf(FrameT(16, "lotsofsillydata"),
FrameT(4, "test"), // <- note wrong size
FrameT(10, "dummydata"));
using ParserT = o2::algorithm::ForwardParser<typename FrameT::HeaderType,
typename FrameT::TrailerType>;
auto checkHeader = [](const typename FrameT::HeaderType& header) {
return header.identifier == 0xdeadbeef;
};
auto getFrameSize = [](const typename ParserT::HeaderType& header) {
// frame size includes total offset from header and trailer
return header.payloadSize + ParserT::totalOffset;
};
std::vector<typename ParserT::FrameInfo> frames;
auto insert = [&frames](typename ParserT::FrameInfo& info) {
frames.emplace_back(info);
return true;
};
ParserT parser;
auto result = parser.parse(tf.buffer.get(), tf.size(),
checkHeader,
getFrameSize,
insert);
BOOST_REQUIRE(result == -1);
}
BOOST_AUTO_TEST_CASE(test_reverseparser)
{
using FrameT = o2::algorithm::Composite<Header, SizedTrailer>;
// note: the length of the data is set in the trailer word
using TestFrame = o2::algorithm::StaticSequenceAllocator;
TestFrame tf(FrameT(0, "lotsofsillydata", {16, 0xaa}),
FrameT(0, "test", {5, 0xcc}),
FrameT(0, "dummydata", {10, 0x33}));
using ParserT = o2::algorithm::ReverseParser<typename FrameT::HeaderType,
typename FrameT::TrailerType>;
auto checkHeader = [](const typename FrameT::HeaderType& header) {
return header.identifier == 0xdeadbeef;
};
auto checkTrailer = [](const typename FrameT::TrailerType& trailer) {
return trailer.identifier == 0xaaffee00;
};
auto getFrameSize = [](const typename ParserT::TrailerType& trailer) {
return trailer.payloadSize + ParserT::totalOffset;
};
std::vector<typename ParserT::FrameInfo> frames;
auto insert = [&frames](const typename ParserT::FrameInfo& info) {
frames.emplace_back(info);
return true;
};
ParserT parser;
auto result = parser.parse(tf.buffer.get(), tf.size(),
checkHeader,
checkTrailer,
getFrameSize,
insert);
BOOST_REQUIRE(result == 3);
BOOST_REQUIRE(frames.size() == 3);
BOOST_CHECK(memcmp(frames[2].payload, "lotsofsillydata", frames[2].length) == 0);
BOOST_CHECK(memcmp(frames[1].payload, "test", frames[1].length) == 0);
BOOST_CHECK(memcmp(frames[0].payload, "dummydata", frames[0].length) == 0);
}