-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestEventBuffers.cpp
179 lines (134 loc) · 4.25 KB
/
TestEventBuffers.cpp
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
//
// Licensed to Green Energy Corp (www.greenenergycorp.com) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. Green Enery Corp licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
#include <boost/test/unit_test.hpp>
#include <APLTestTools/TestHelpers.h>
#include <DNP3/EventBuffers.h>
#include <DNP3/EventTypes.h>
#include <DNP3/VtoData.h>
#include <iostream>
#include <limits>
using namespace std;
using namespace apl;
using namespace apl::dnp;
BOOST_AUTO_TEST_SUITE(SingleEventBufferSuite)
BOOST_AUTO_TEST_CASE(SingleIndexSorting)
{
SingleEventBuffer<AnalogEvent> b(3);
b.Update(Analog(0), PC_CLASS_1, 0);
b.Update(Analog(0), PC_CLASS_1, 1);
BOOST_REQUIRE_EQUAL(b.Size(), 2);
Analog a(0); a.SetTime(TimeStamp_t(2)); //newer timestamp
b.Update(a, PC_CLASS_1, 0); //repeat index 0
BOOST_REQUIRE_EQUAL(b.Size(), 2);
b.Select(PC_CLASS_1);
BOOST_REQUIRE_EQUAL(b.Begin()->mValue.GetTime(), TimeStamp_t(2)); //prove the newest value was kept
}
BOOST_AUTO_TEST_SUITE_END()
// index is irrelevant in these tests, only insertion order matters
BOOST_AUTO_TEST_SUITE(InsertionOrderedEventBufferSuite)
typedef EventInfo<int> intevt;
BOOST_AUTO_TEST_CASE(InsertionOrderSorting)
{
const size_t NUM = 3;
InsertionOrderedEventBuffer<intevt> b(NUM);
int vals[NUM] = {2, 1, 0};
for(size_t i = 0; i < NUM; ++i) b.Update(vals[i], PC_CLASS_1, 0);
{
//now verify the order
b.Select(PC_CLASS_1);
EvtItr<intevt>::Type itr = b.Begin();
for(size_t i = 0; i < b.NumSelected(); ++i) {
BOOST_REQUIRE_EQUAL(itr->mValue, vals[i]);
++itr;
}
}
b.Deselect();
{
//now verify the order
b.Select(PC_CLASS_1);
EvtItr<intevt>::Type itr = b.Begin();
for(size_t i = 0; i < b.NumSelected(); ++i) {
BOOST_REQUIRE_EQUAL(itr->mValue, vals[i]);
++itr;
}
}
}
BOOST_AUTO_TEST_CASE(ResetEventsProperlyOnFailure)
{
const boost::uint8_t NUM = 100;
const boost::uint8_t dataSize = 255;
const boost::uint8_t numEvents = NUM;
InsertionOrderedEventBuffer<VtoEvent> b(NUM);
VtoData info;
size_t numResults;
VtoDataEventIter itr;
for (boost::uint8_t i = 0; i < numEvents; i++) {
boost::uint8_t trash[dataSize];
boost::uint8_t j;
for (j = 0; j < dataSize; ++j)
trash[j] = i;
info.Copy(trash, dataSize);
b.Update(info, PC_CLASS_1, dataSize);
}
numResults = b.Size();
BOOST_REQUIRE_EQUAL(numResults, numEvents);
numResults = b.Select(PC_CLASS_1);
BOOST_REQUIRE_EQUAL(numResults, numEvents);
itr = b.Begin();
for (size_t i = 0; i < b.NumSelected(); ++i) {
const boost::uint8_t* value = itr->mValue.mpData;
for (size_t j = 0; j < dataSize; ++j)
BOOST_REQUIRE_EQUAL(value[j], i);
++itr;
}
b.Deselect();
numResults = b.Select(PC_CLASS_1);
BOOST_REQUIRE_EQUAL(numResults, numEvents);
itr = b.Begin();
for (size_t i = 0; i < b.NumSelected(); ++i) {
const boost::uint8_t* value = itr->mValue.mpData;
for (size_t j = 0; j < dataSize; ++j)
BOOST_REQUIRE_EQUAL(value[j], i);
++itr;
}
b.Deselect();
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(TimeOrderedEventBufferSuite)
BOOST_AUTO_TEST_CASE(TimeBasedSorting)
{
const size_t NUM = 3;
TimeOrderedEventBuffer<BinaryEvent> b(NUM);
TimeStamp_t times[NUM] = {TimeStamp_t(3), TimeStamp_t(2), TimeStamp_t(1)};
Binary v; // false, restart, t = 0
for(size_t i = 0; i < NUM; ++i) {
v.SetTime(times[i]);
b.Update(v, PC_CLASS_1, 0);
}
BOOST_REQUIRE_EQUAL(b.Size(), NUM);
//now verify the order
b.Select(PC_CLASS_1);
EvtItr<BinaryEvent>::Type itr = b.Begin();
for(size_t i = 0; i < b.NumSelected(); ++i) {
BOOST_REQUIRE_EQUAL(itr->mValue.GetTime(), times[NUM - i - 1]); //reverse order of array above
++itr;
}
}
BOOST_AUTO_TEST_SUITE_END()
/* vim: set ts=4 sw=4: */