-
Notifications
You must be signed in to change notification settings - Fork 10
/
analysisrunner-tests.cpp
403 lines (325 loc) · 16.2 KB
/
analysisrunner-tests.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright 2021 Herald project contributors
// SPDX-License-Identifier: Apache-2.0
//
#include "catch.hpp"
#include "herald/herald.h"
#include "test-templates.h"
#include <utility>
#include <iostream>
using namespace herald::analysis::sampling;
using namespace herald::datatype;
struct DummyDistanceDelegate /* : herald::analysis::AnalysisDelegate */ {
using value_type = Distance;
DummyDistanceDelegate() : lastSampledID(0), distances() {};
DummyDistanceDelegate(const DummyDistanceDelegate&) = delete; // copy ctor deleted
DummyDistanceDelegate(DummyDistanceDelegate&& other) noexcept : lastSampledID(other.lastSampledID), distances(std::move(other.distances)) {} // move ctor
~DummyDistanceDelegate() {};
DummyDistanceDelegate& operator=(DummyDistanceDelegate&& other) noexcept {
lastSampledID = other.lastSampledID;
std::swap(distances,other.distances);
return *this;
}
// specific override of template
void newSample(SampledID sampled, Sample<Distance> sample) {
lastSampledID = sampled;
distances.push(sample);
}
void reset() {
distances.clear();
lastSampledID = 0;
}
// Test only methods
SampledID lastSampled() {
return lastSampledID;
}
const SampleList<Sample<Distance>,25>& samples() {
return distances;
}
private:
SampledID lastSampledID;
SampleList<Sample<Distance>,25> distances;
};
struct DummyBrightnessDelegate {
using value_type = RunningMean<Luminosity>;
DummyBrightnessDelegate() : lastSampledID(0), brightness() {};
DummyBrightnessDelegate(const DummyBrightnessDelegate&) = delete; // copy ctor deleted
DummyBrightnessDelegate(DummyBrightnessDelegate&& other) noexcept : lastSampledID(other.lastSampledID), brightness(std::move(other.brightness)) {} // move ctor
~DummyBrightnessDelegate() {};
DummyBrightnessDelegate& operator=(DummyBrightnessDelegate&& other) noexcept {
lastSampledID = other.lastSampledID;
std::swap(brightness,other.brightness);
return *this;
}
// specific override of template
void newSample(SampledID sampled, Sample<RunningMean<Luminosity>> sample) {
lastSampledID = sampled;
brightness.push(sample);
}
void reset() {
brightness.clear();
lastSampledID = 0;
}
// Test only methods
SampledID lastSampled() {
return lastSampledID;
}
const SampleList<Sample<RunningMean<Luminosity>>,25>& samples() {
return brightness;
}
private:
SampledID lastSampledID;
SampleList<Sample<RunningMean<Luminosity>>,25> brightness;
};
TEST_CASE("variantset-basic", "[variantset][basic]") {
SECTION("variantset-basic") {
herald::analysis::VariantSet<int,double> vs;
REQUIRE(vs.size() == 2);
int intValue = vs.get<int>();
REQUIRE(intValue == 0);
double dblValue = vs.get<double>();
REQUIRE(dblValue == 0.0);
}
}
TEST_CASE("variantset-lists", "[variantset][lists]") {
SECTION("variantset-lists") {
herald::analysis::VariantSet<SampleList<Sample<int>,15>,SampleList<Sample<double>,15>> vs;
REQUIRE(vs.size() == 2);
SampleList<Sample<int>,15>& intValue = vs.get<SampleList<Sample<int>,15>>();
REQUIRE(intValue.size() == 0);
SampleList<Sample<double>,15>& dblValue = vs.get<SampleList<Sample<double>,15>>();
REQUIRE(dblValue.size() == 0);
intValue.push(0,12);
dblValue.push(10,14.3);
dblValue.push(20,15.3);
SampleList<Sample<int>,15>& intValue2 = vs.get<SampleList<Sample<int>,15>>();
REQUIRE(intValue2.size() == 1);
SampleList<Sample<double>,15>& dblValue2 = vs.get<SampleList<Sample<double>,15>>();
REQUIRE(dblValue2.size() == 2);
}
}
TEST_CASE("variantset-listmanager", "[variantset][listmanager]") {
SECTION("variantset-listmanager") {
using herald::analysis::ListManager;
herald::analysis::VariantSet<ListManager<int,15>,ListManager<double,15>> vs;
REQUIRE(vs.size() == 2);
ListManager<int,15>& intValue = vs.template get<ListManager<int,15>>();
REQUIRE(intValue.size() == 0);
ListManager<double,15>& dblValue = vs.template get<ListManager<double,15>>();
REQUIRE(dblValue.size() == 0);
SampleList<Sample<int>,15>& intList = intValue.list(1234); // list for entity being sampled with SampledId=1234
REQUIRE(intList.size() == 0);
SampleList<Sample<double>,15>& dblList = dblValue.list(5678); // list for entity being sampled with SampledId=1234
REQUIRE(dblList.size() == 0);
intList.push(Sample(0,12));
dblList.push(Sample(10,14.3));
dblList.push(Sample(20,15.3));
ListManager<int,15>& intValue2 = vs.template get<ListManager<int,15>>();
REQUIRE(intValue2.size() == 1);
ListManager<double,15>& dblValue2 = vs.template get<ListManager<double,15>>();
REQUIRE(dblValue2.size() == 1);
SampleList<Sample<int>,15>& intList2 = intValue2.list(1234); // list for entity being sampled with SampledId=1234
REQUIRE(intList2.size() == 1);
SampleList<Sample<double>,15>& dblList2 = dblValue2.list(5678); // list for entity being sampled with SampledId=1234
REQUIRE(dblList2.size() == 2);
}
}
/// Null test case with zero data, no failures, correct summary output
TEST_CASE("analysisrunner-nodata", "[analysisrunner][nodata]") {
SECTION("analysisrunner-nodata") {
SampleList<Sample<RSSI>,25> srcData;
DummySampleSource src(1234,std::move(srcData));
herald::analysis::algorithms::distance::FowlerBasicAnalyser distanceAnalyser(30, -50, -24);
DummyDistanceDelegate myDelegate;
herald::analysis::AnalysisDelegateManager adm(std::move(myDelegate)); // NOTE: myDelegate MOVED FROM and no longer accessible
herald::analysis::AnalysisProviderManager apm(std::move(distanceAnalyser)); // NOTE: distanceAnalyser MOVED FROM and no longer accessible
herald::analysis::AnalysisRunner<
herald::analysis::AnalysisDelegateManager<DummyDistanceDelegate>,
herald::analysis::AnalysisProviderManager<herald::analysis::algorithms::distance::FowlerBasicAnalyser>,
RSSI,Distance
> runner(adm, apm); // just for Sample<RSSI> types, and their produced output (Sample<Distance>)
src.run(140,runner);
REQUIRE(src.getLastRunAdded() == 0); // No data, no run
auto& delegateRef = adm.get<DummyDistanceDelegate>();
REQUIRE(delegateRef.lastSampled() == 0); // not ran, so 0
auto& samples = delegateRef.samples();
REQUIRE(samples.size() == 0); // 0 as there is no source data
}
}
/// Single data item use case with 1 data item, no failures, correct summary output
TEST_CASE("analysisrunner-singledataitem", "[analysisrunner][singledataitem]") {
SECTION("analysisrunner-singledataitem") {
SampleList<Sample<RSSI>,25> srcData;
srcData.push(50,-55);
DummySampleSource src(1234,std::move(srcData));
herald::analysis::algorithms::distance::FowlerBasicAnalyser distanceAnalyser(30, -50, -24);
DummyDistanceDelegate myDelegate;
herald::analysis::AnalysisDelegateManager adm(std::move(myDelegate)); // NOTE: myDelegate MOVED FROM and no longer accessible
herald::analysis::AnalysisProviderManager apm(std::move(distanceAnalyser)); // NOTE: distanceAnalyser MOVED FROM and no longer accessible
herald::analysis::AnalysisRunner<
herald::analysis::AnalysisDelegateManager<DummyDistanceDelegate>,
herald::analysis::AnalysisProviderManager<herald::analysis::algorithms::distance::FowlerBasicAnalyser>,
RSSI,Distance
> runner(adm, apm); // just for Sample<RSSI> types, and their produced output (Sample<Distance>)
src.run(140,runner);
REQUIRE(src.getLastRunAdded() == 1); // Single data item
auto& delegateRef = adm.get<DummyDistanceDelegate>();
REQUIRE(delegateRef.lastSampled() == 1234); // ran once, past 50, for SampleID=1234
auto& samples = delegateRef.samples();
REQUIRE(samples.size() == 1); // 1 as single data item
}
}
/// Single data item use case with 1 data item, no failures, correct summary output
TEST_CASE("analysisrunner-singledataitem-twoanalyses", "[analysisrunner][singledataitem][twoanalyses][multivariate]") {
SECTION("analysisrunner-singledataitem-twoanalyses") {
SampleList<Sample<RSSI>,25> srcData;
srcData.push(50,-55);
DummySampleSource src(1234,std::move(srcData));
SampleList<Sample<Luminosity>,15> srcLightData;
srcLightData.push(40,12);
srcLightData.push(50,12);
srcLightData.push(60,12);
DummySampleSource srcLight(5678,std::move(srcLightData));
herald::analysis::algorithms::distance::FowlerBasicAnalyser distanceAnalyser(30, -50, -24);
herald::analysis::algorithms::RunningMeanAnalyser<herald::datatype::Luminosity,2> meanLight{60};
DummyDistanceDelegate myDelegate;
DummyBrightnessDelegate myBrightnessDelegate;
herald::analysis::AnalysisDelegateManager adm(std::move(myDelegate),std::move(myBrightnessDelegate)); // NOTE: myDelegate MOVED FROM and no longer accessible
herald::analysis::AnalysisProviderManager apm(std::move(distanceAnalyser), std::move(meanLight)); // NOTE: distanceAnalyser MOVED FROM and no longer accessible
herald::analysis::AnalysisRunner<
herald::analysis::AnalysisDelegateManager<
DummyDistanceDelegate,
DummyBrightnessDelegate
>,
herald::analysis::AnalysisProviderManager<
herald::analysis::algorithms::distance::FowlerBasicAnalyser,
herald::analysis::algorithms::RunningMeanAnalyser<herald::datatype::Luminosity,2>
>,
RSSI,Distance,Luminosity,RunningMean<Luminosity>
> runner(adm, apm); // just for Sample<RSSI> types, and their produced output (Sample<Distance>)
src.run(140,runner);
srcLight.run(160,runner);
REQUIRE(src.getLastRunAdded() == 1); // Single data item
REQUIRE(srcLight.getLastRunAdded() == 3); // Three data items
auto& delegateRef = adm.get<DummyDistanceDelegate>();
REQUIRE(delegateRef.lastSampled() == 1234); // ran once, past 50, for SampleID=1234
auto& samples = delegateRef.samples();
REQUIRE(samples.size() == 1); // 1 as single data item (for THIS delegate)
auto& delegateBRef = adm.get<DummyBrightnessDelegate>();
REQUIRE(delegateBRef.lastSampled() == 5678); // ran once, past 50, for SampleID=1234
auto& samplesB = delegateBRef.samples();
REQUIRE(samplesB.size() == 2); // 2 as we now correctly separate by time interval (0-60, 60-120)
}
}
/// [Who] As a DCT app developer
/// [What] I want to link my live application data to an analysis runner easily
/// [Value] So I don't have to write plumbing code for Herald itself
///
/// [Who] As a DCT app developer
/// [What] I want to periodically run analysis aggregates automatically
/// [Value] So I don't miss any information, and have accurate, regular, samples
TEST_CASE("analysisrunner-basic", "[analysisrunner][basic]") {
SECTION("analysisrunner-basic") {
SampleList<Sample<RSSI>,25> srcData;
srcData.push(10,-55);
srcData.push(20,-55);
srcData.push(30,-55);
srcData.push(40,-55);
srcData.push(50,-55);
srcData.push(60,-55);
srcData.push(70,-55);
srcData.push(80,-55);
srcData.push(90,-55);
srcData.push(100,-55);
DummySampleSource src(1234,std::move(srcData));
herald::analysis::algorithms::distance::FowlerBasicAnalyser distanceAnalyser(30, -50, -24);
DummyDistanceDelegate myDelegate;
herald::analysis::AnalysisDelegateManager adm(std::move(myDelegate)); // NOTE: myDelegate MOVED FROM and no longer accessible
herald::analysis::AnalysisProviderManager apm(std::move(distanceAnalyser)); // NOTE: distanceAnalyser MOVED FROM and no longer accessible
herald::analysis::AnalysisRunner<
herald::analysis::AnalysisDelegateManager<DummyDistanceDelegate>,
herald::analysis::AnalysisProviderManager<herald::analysis::algorithms::distance::FowlerBasicAnalyser>,
RSSI,Distance
> runner(adm, apm); // just for Sample<RSSI> types, and their produced output (Sample<Distance>)
// run at different times and ensure that it only actually runs three times (sample size == 3)
src.run(20,runner);
REQUIRE(src.getLastRunAdded() == 2); // 10, 20 - THIS IS ZERO BUT MUST BE 2!!!
src.run(40,runner); // Runs here, because we have data for 10,20,>>30<<,40 <- next run time based on this 'latest' data time
REQUIRE(src.getLastRunAdded() == 2); // 30, 40
src.run(60,runner);
REQUIRE(src.getLastRunAdded() == 2); // 50, 60
src.run(80,runner); // Runs here because we have extra data for 50,60,>>70<<,80 <- next run time based on this 'latest' data time
REQUIRE(src.getLastRunAdded() == 2); // 70, 80
src.run(95,runner);
REQUIRE(src.getLastRunAdded() == 1); // 90
auto& delegateRef = adm.get<DummyDistanceDelegate>();
REQUIRE(delegateRef.lastSampled() == 1234);
auto& samples = delegateRef.samples();
REQUIRE(samples.size() == 2); // didn't reach 4x30 seconds, so no tenth sample, and didn't run at 60 because previous run was at time 40
REQUIRE(samples[0].taken.secondsSinceUnixEpoch() == 40);
REQUIRE(samples[0].value != 0.0);
REQUIRE(samples[1].taken.secondsSinceUnixEpoch() == 80);
REQUIRE(samples[1].value != 0.0);
// Let's see the total memory in use...
std::cout << "AnalysisRunner::RAM = " << sizeof(runner) << std::endl;
}
}
/// [Who] As a DCT app developer
/// [What] I want to link my live application data to an analysis runner easily
/// [Value] So I don't have to write plumbing code for Herald itself
///
/// [Who] As a DCT app developer
/// [What] I want to periodically run analysis aggregates automatically
/// [Value] So I don't miss any information, and have accurate, regular, samples
TEST_CASE("analysisrunner-nonewdata", "[analysisrunner][nonewdata]") {
SECTION("analysisrunner-nonewdata") {
SampleList<Sample<RSSI>,25> srcData;
srcData.push(10,-55);
srcData.push(20,-55);
srcData.push(30,-55);
srcData.push(40,-55);
srcData.push(50,-55);
srcData.push(60,-55);
srcData.push(70,-55);
srcData.push(80,-55);
srcData.push(90,-55);
srcData.push(100,-55);
DummySampleSource src(1234,std::move(srcData));
herald::analysis::algorithms::distance::FowlerBasicAnalyser distanceAnalyser(30, -50, -24);
DummyDistanceDelegate myDelegate;
herald::analysis::AnalysisDelegateManager adm(std::move(myDelegate)); // NOTE: myDelegate MOVED FROM and no longer accessible
herald::analysis::AnalysisProviderManager apm(std::move(distanceAnalyser)); // NOTE: distanceAnalyser MOVED FROM and no longer accessible
herald::analysis::AnalysisRunner<
herald::analysis::AnalysisDelegateManager<DummyDistanceDelegate>,
herald::analysis::AnalysisProviderManager<herald::analysis::algorithms::distance::FowlerBasicAnalyser>,
RSSI,Distance
> runner(adm, apm); // just for Sample<RSSI> types, and their produced output (Sample<Distance>)
// run at different times and ensure that it only actually runs three times (sample size == 3)
src.run(20,runner);
REQUIRE(src.getLastRunAdded() == 2); // 10, 20
src.run(40,runner); // Runs here, because we have data for 10,20,>>30<<,40 <- next run time based on this 'latest' data time
REQUIRE(src.getLastRunAdded() == 2); // 30, 40
src.run(60,runner);
REQUIRE(src.getLastRunAdded() == 2); // 50, 60
src.run(80,runner); // Runs here because we have extra data for 50,60,>>70<<,80 <- next run time based on this 'latest' data time
REQUIRE(src.getLastRunAdded() == 2); // 70, 80
src.run(95,runner);
REQUIRE(src.getLastRunAdded() == 1); // 90
// Now ensure that running runner past end of data does not cause result to change
src.run(115,runner); // Run here because we have past 80 + 30
REQUIRE(src.getLastRunAdded() == 1); // 100
src.run(150,runner); // Should not run here as there's no new data (even though we're at > 115 + 30)
REQUIRE(src.getLastRunAdded() == 0); // No new data
auto& delegateRef = adm.get<DummyDistanceDelegate>();
REQUIRE(delegateRef.lastSampled() == 1234);
auto& samples = delegateRef.samples();
REQUIRE(samples.size() == 3); // 150 should not result in a run after 145 (min time delay)
REQUIRE(samples[0].taken.secondsSinceUnixEpoch() == 40);
REQUIRE(samples[0].value != 0.0);
REQUIRE(samples[1].taken.secondsSinceUnixEpoch() == 80);
REQUIRE(samples[1].value != 0.0);
REQUIRE(samples[2].taken.secondsSinceUnixEpoch() == 100); // Last data was at 100, not 115, so it takes that time
REQUIRE(samples[2].value != 0.0);
// REQUIRE(samples[3].taken.secondsSinceUnixEpoch() == 150);
// REQUIRE(samples[3].value != 0.0);
}
}