-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestmain.cpp
198 lines (148 loc) · 4.52 KB
/
testmain.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
/*
* Unit-tests for run-time timer mechanisms
*/
// (C)Copyright 2017-2024, RW Penney
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/test/unit_test.hpp>
#include <cmath>
#include "rtimers/core.hpp"
#include "testdefns.hpp"
namespace BoostUT = boost::unit_test;
namespace rtimers {
namespace testing {
struct TestStartStop : BoostUT::test_suite
{
typedef Timer<SerialManager<C89clock, BoundStats>, NullLogger> QuietTimer;
TestStartStop()
: BoostUT::test_suite("timer start/stop counting")
{
add(BOOST_TEST_CASE(plain));
add(BOOST_TEST_CASE(scoped));
}
static void plain() {
QuietTimer tmr("basic");
const unsigned count = 7831;
for (unsigned i=0; i<count; ++i) {
tmr.start();
tmr.stop();
}
BOOST_CHECK_EQUAL(tmr.getStats().count, count);
}
static void scoped() {
QuietTimer tmr("basic");
const unsigned count = 1384;
for (unsigned i=0; i<count; ++i) {
QuietTimer::Scoper sc = tmr.scopedStart();
}
BOOST_CHECK_EQUAL(tmr.getStats().count, count);
}
};
/** Inject sequence of samples with well-known mean and variance */
template <typename STATS>
void pushSineSamples(STATS& stats, unsigned count, double offset, double amp)
{
for (unsigned i=0; i<count; ++i) {
const double x = i / (double)count;
stats.addSample(offset + amp * std::sin(8 * Pi * x));
}
}
struct TestVarianceStats : BoostUT::test_suite
{
TestVarianceStats()
: BoostUT::test_suite("recursive mean/variance computation")
{
add(BOOST_TEST_CASE(simple));
add(BOOST_TEST_CASE(sine));
}
static void simple() {
VarBoundStats stats;
const double eps = 1e-9;
stats.addSample(1);
stats.addSample(3);
stats.addSample(4);
stats.addSample(2);
BOOST_CHECK_EQUAL(stats.count, 4);
BOOST_CHECK_EQUAL(stats.tmin, 1.0);
BOOST_CHECK_EQUAL(stats.tmax, 4.0);
BOOST_CHECK_CLOSE(stats.mean, 2.5, eps);
BOOST_CHECK_CLOSE(stats.nVariance, 2 * (0.25 + 2.25), eps);
BOOST_CHECK_CLOSE(stats.getStddev(), std::sqrt((0.25 + 2.25) / 2), eps);
}
static void sine() {
VarBoundStats stats;
const double mean = 16.5, amp = 2.3, eps = 1e-3;
const unsigned count = 10000;
pushSineSamples(stats, count, mean, amp);
BOOST_CHECK_EQUAL(stats.count, count);
BOOST_CHECK_CLOSE(stats.tmin, mean - amp, eps);
BOOST_CHECK_CLOSE(stats.tmax, mean + amp, eps);
BOOST_CHECK_CLOSE(stats.mean, mean, eps);
BOOST_CHECK_CLOSE(stats.nVariance, count * 0.5 * amp * amp, eps);
BOOST_CHECK_CLOSE(stats.getStddev(), std::sqrt(0.5) * amp, eps);
}
};
struct TestLogVarianceStats : BoostUT::test_suite
{
TestLogVarianceStats()
: BoostUT::test_suite("geometric mean computation")
{
add(BOOST_TEST_CASE(simple));
add(BOOST_TEST_CASE(threshold));
}
static void simple() {
LogBoundStats stats;
const double eps = 1e-9;
stats.addSample(1e-8);
stats.addSample(1e-7);
stats.addSample(1e-6);
stats.addSample(1e-5);
stats.addSample(1e-4);
BOOST_CHECK_EQUAL(stats.count, 5);
BOOST_CHECK_EQUAL(stats.tmin, 1e-8);
BOOST_CHECK_EQUAL(stats.tmax, 1e-4);
BOOST_CHECK_CLOSE(stats.getGeometricMean(), 1e-6, eps);
BOOST_CHECK_CLOSE(stats.getLog10stddev(), std::sqrt(2), eps);
}
static void threshold() {
LogBoundStats stats;
const double eps = 1e-9;
stats.addSample(0);
stats.addSample(1e-9);
stats.addSample(1e-8);
BOOST_CHECK_EQUAL(stats.count, 3);
BOOST_CHECK_EQUAL(stats.tmin, 0.0);
BOOST_CHECK_EQUAL(stats.tmax, 1e-8);
BOOST_CHECK_CLOSE(stats.getGeometricMean(), 1e-9, eps);
BOOST_CHECK_CLOSE(stats.getLog10stddev(), std::sqrt(2.0/3.0), eps);
}
};
struct RTtestSuite : BoostUT::test_suite
{
RTtestSuite()
: BoostUT::test_suite("runtime timer tests")
{
add(new TestStartStop);
add(new TestVarianceStats);
add(new TestLogVarianceStats);
add(new TestBoost);
add(new TestCxx11);
add(new TestPosix);
}
};
//! Unit-test initialization mechanism for boost::unit_test
bool init_unit_test_suite()
{
BoostUT::framework::master_test_suite().add(new RTtestSuite);
return true;
}
} // namespace testing
} // namespace rtimers
int main(int argc, char *argv[])
{
return BoostUT::unit_test_main(
(BoostUT::init_unit_test_func)rtimers::testing::init_unit_test_suite,
argc, argv);
}