forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cxx
More file actions
141 lines (118 loc) · 4.8 KB
/
parser.cxx
File metadata and controls
141 lines (118 loc) · 4.8 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
// 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) {
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);
}
// TODO: make a test of a composite with header and payload
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);
}