A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
average-test-suite.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2012 University of Washington
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Mitch Watrous (watrous@u.washington.edu)
7
*/
8
9
#include "ns3/average.h"
10
#include "ns3/test.h"
11
12
#include <cmath>
13
14
using namespace
ns3
;
15
16
// Note, the rationale for this particular value of TOLERANCE is not
17
// documented. Current value is sufficient for all test platforms.
18
const
double
TOLERANCE
= 2e-14;
19
20
/**
21
* @ingroup stats-tests
22
*
23
* @brief Average class - Test case for a single integer.
24
*/
25
class
OneIntegerAverageTestCase
:
public
TestCase
26
{
27
public
:
28
OneIntegerAverageTestCase
();
29
~OneIntegerAverageTestCase
()
override
;
30
31
private
:
32
void
DoRun
()
override
;
33
};
34
35
OneIntegerAverageTestCase::OneIntegerAverageTestCase
()
36
:
TestCase
(
"Average Object Test using One Integer"
)
37
38
{
39
}
40
41
OneIntegerAverageTestCase::~OneIntegerAverageTestCase
()
42
{
43
}
44
45
void
46
OneIntegerAverageTestCase::DoRun
()
47
{
48
Average<int>
calculator;
49
50
long
count = 1;
51
52
double
sum = 0;
53
double
min;
54
double
max;
55
double
mean;
56
double
stddev;
57
double
variance;
58
59
// Put all of the values into the calculator.
60
int
multiple = 5;
61
int
value;
62
for
(
long
i = 0; i < count; i++)
63
{
64
value = multiple * (i + 1);
65
66
calculator.
Update
(value);
67
68
sum += value;
69
}
70
71
// Calculate the expected values for the statistical functions.
72
min = multiple;
73
max = multiple * count;
74
mean = sum / count;
75
variance = 0;
76
stddev = std::sqrt(variance);
77
78
// Test the calculator.
79
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(),
80
count,
81
TOLERANCE
,
82
"Count value outside of tolerance "
83
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
84
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
85
min,
86
TOLERANCE
,
87
"Min value outside of tolerance "
88
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() - min);
89
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
90
max,
91
TOLERANCE
,
92
"Max value outside of tolerance "
93
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() - max);
94
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(),
95
mean,
96
TOLERANCE
,
97
"Mean value outside of tolerance "
98
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
99
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(),
100
stddev,
101
TOLERANCE
,
102
"Stddev value outside of tolerance "
103
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
104
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(),
105
variance,
106
TOLERANCE
,
107
"Variance value outside of tolerance "
108
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
109
}
110
111
/**
112
* @ingroup stats-tests
113
*
114
* @brief Average class - Test case for five integers.
115
*/
116
class
FiveIntegersAverageTestCase
:
public
TestCase
117
{
118
public
:
119
FiveIntegersAverageTestCase
();
120
~FiveIntegersAverageTestCase
()
override
;
121
122
private
:
123
void
DoRun
()
override
;
124
};
125
126
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
()
127
:
TestCase
(
"Average Object Test using Five Integers"
)
128
129
{
130
}
131
132
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
()
133
{
134
}
135
136
void
137
FiveIntegersAverageTestCase::DoRun
()
138
{
139
Average<int>
calculator;
140
141
long
count = 5;
142
143
double
sum = 0;
144
double
sqrSum = 0;
145
double
min;
146
double
max;
147
double
mean;
148
double
stddev;
149
double
variance;
150
151
// Put all of the values into the calculator.
152
int
multiple = 5;
153
int
value;
154
for
(
long
i = 0; i < count; i++)
155
{
156
value = multiple * (i + 1);
157
158
calculator.
Update
(value);
159
160
sum += value;
161
sqrSum += value * value;
162
}
163
164
// Calculate the expected values for the statistical functions.
165
min = multiple;
166
max = multiple * count;
167
mean = sum / count;
168
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
169
stddev = std::sqrt(variance);
170
171
// Test the calculator.
172
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(),
173
count,
174
TOLERANCE
,
175
"Count value outside of tolerance "
176
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
177
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
178
min,
179
TOLERANCE
,
180
"Min value outside of tolerance "
181
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() - min);
182
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
183
max,
184
TOLERANCE
,
185
"Max value outside of tolerance "
186
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() - max);
187
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(),
188
mean,
189
TOLERANCE
,
190
"Mean value outside of tolerance "
191
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
192
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(),
193
stddev,
194
TOLERANCE
,
195
"Stddev value outside of tolerance "
196
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
197
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(),
198
variance,
199
TOLERANCE
,
200
"Variance value outside of tolerance "
201
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
202
}
203
204
/**
205
* @ingroup stats-tests
206
*
207
* @brief Average class - Test case for five double values.
208
*/
209
class
FiveDoublesAverageTestCase
:
public
TestCase
210
{
211
public
:
212
FiveDoublesAverageTestCase
();
213
~FiveDoublesAverageTestCase
()
override
;
214
215
private
:
216
void
DoRun
()
override
;
217
};
218
219
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
()
220
:
TestCase
(
"Average Object Test using Five Double Values"
)
221
222
{
223
}
224
225
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
()
226
{
227
}
228
229
void
230
FiveDoublesAverageTestCase::DoRun
()
231
{
232
Average<double>
calculator;
233
234
long
count = 5;
235
236
double
sum = 0;
237
double
sqrSum = 0;
238
double
min;
239
double
max;
240
double
mean;
241
double
stddev;
242
double
variance;
243
244
// Put all of the values into the calculator.
245
double
multiple = 3.14;
246
double
value;
247
for
(
long
i = 0; i < count; i++)
248
{
249
value = multiple * (i + 1);
250
251
calculator.
Update
(value);
252
253
sum += value;
254
sqrSum += value * value;
255
}
256
257
// Calculate the expected values for the statistical functions.
258
min = multiple;
259
max = multiple * count;
260
mean = sum / count;
261
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
262
stddev = std::sqrt(variance);
263
264
// Test the calculator.
265
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(),
266
count,
267
TOLERANCE
,
268
"Count value outside of tolerance "
269
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
270
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
271
min,
272
TOLERANCE
,
273
"Min value outside of tolerance "
274
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() - min);
275
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
276
max,
277
TOLERANCE
,
278
"Max value outside of tolerance "
279
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() - max);
280
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(),
281
mean,
282
TOLERANCE
,
283
"Mean value outside of tolerance "
284
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
285
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(),
286
stddev,
287
TOLERANCE
,
288
"Stddev value outside of tolerance "
289
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
290
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(),
291
variance,
292
TOLERANCE
,
293
"Variance value outside of tolerance "
294
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
295
}
296
297
/**
298
* @ingroup stats-tests
299
*
300
* @brief Average class TestSuite
301
*/
302
class
AverageTestSuite
:
public
TestSuite
303
{
304
public
:
305
AverageTestSuite
();
306
};
307
308
AverageTestSuite::AverageTestSuite
()
309
:
TestSuite
(
"average"
,
Type
::UNIT)
310
{
311
AddTestCase
(
new
OneIntegerAverageTestCase
, TestCase::Duration::QUICK);
312
AddTestCase
(
new
FiveIntegersAverageTestCase
, TestCase::Duration::QUICK);
313
AddTestCase
(
new
FiveDoublesAverageTestCase
, TestCase::Duration::QUICK);
314
}
315
316
/// Static variable for test initialization
317
static
AverageTestSuite
averageTestSuite
;
averageTestSuite
static AverageTestSuite averageTestSuite
Static variable for test initialization.
Definition
average-test-suite.cc:317
AverageTestSuite
Average class TestSuite.
Definition
average-test-suite.cc:303
AverageTestSuite::AverageTestSuite
AverageTestSuite()
Definition
average-test-suite.cc:308
FiveDoublesAverageTestCase
Average class - Test case for five double values.
Definition
average-test-suite.cc:210
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
~FiveDoublesAverageTestCase() override
Definition
average-test-suite.cc:225
FiveDoublesAverageTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
average-test-suite.cc:230
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
FiveDoublesAverageTestCase()
Definition
average-test-suite.cc:219
FiveIntegersAverageTestCase
Average class - Test case for five integers.
Definition
average-test-suite.cc:117
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
~FiveIntegersAverageTestCase() override
Definition
average-test-suite.cc:132
FiveIntegersAverageTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
average-test-suite.cc:137
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
FiveIntegersAverageTestCase()
Definition
average-test-suite.cc:126
OneIntegerAverageTestCase
Average class - Test case for a single integer.
Definition
average-test-suite.cc:26
OneIntegerAverageTestCase::~OneIntegerAverageTestCase
~OneIntegerAverageTestCase() override
Definition
average-test-suite.cc:41
OneIntegerAverageTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
average-test-suite.cc:46
OneIntegerAverageTestCase::OneIntegerAverageTestCase
OneIntegerAverageTestCase()
Definition
average-test-suite.cc:35
ns3::Average
Simple average, min, max and std.
Definition
average.h:32
ns3::Average::Min
T Min() const
Sample minimum.
Definition
average.h:79
ns3::Average::Var
double Var() const
Sample unbiased nbiased estimate of variance.
Definition
average.h:115
ns3::Average::Max
T Max() const
Sample maximum.
Definition
average.h:88
ns3::Average::Update
void Update(const T &x)
Add new sample.
Definition
average.h:45
ns3::Average::Count
uint32_t Count() const
Sample size.
Definition
average.h:70
ns3::Average::Stddev
double Stddev() const
Sample standard deviation.
Definition
average.h:124
ns3::Average::Mean
double Mean() const
Sample estimate of mean, alias to Avg.
Definition
average.h:106
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
NS_TEST_ASSERT_MSG_EQ_TOL
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition
test.h:327
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TOLERANCE
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
Definition
rtt-estimator.cc:32
src
stats
test
average-test-suite.cc
Generated on Tue Dec 10 2024 18:20:48 for ns-3 by
1.11.0