A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
average-test-suite.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2012 University of Washington
3
*
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation;
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*
17
* Author: Mitch Watrous (watrous@u.washington.edu)
18
*/
19
20
#include "ns3/average.h"
21
#include "ns3/test.h"
22
23
#include <cmath>
24
25
using namespace
ns3
;
26
27
// Note, the rationale for this particular value of TOLERANCE is not
28
// documented. Current value is sufficient for all test platforms.
29
const
double
TOLERANCE
= 2e-14;
30
36
class
OneIntegerAverageTestCase
:
public
TestCase
37
{
38
public
:
39
OneIntegerAverageTestCase
();
40
~
OneIntegerAverageTestCase
()
override
;
41
42
private
:
43
void
DoRun()
override
;
44
};
45
46
OneIntegerAverageTestCase::OneIntegerAverageTestCase
()
47
:
TestCase
(
"Average Object Test using One Integer"
)
48
49
{
50
}
51
52
OneIntegerAverageTestCase::~OneIntegerAverageTestCase
()
53
{
54
}
55
56
void
57
OneIntegerAverageTestCase::DoRun
()
58
{
59
Average<int>
calculator;
60
61
long
count = 1;
62
63
double
sum = 0;
64
double
min
;
65
double
max
;
66
double
mean;
67
double
stddev;
68
double
variance;
69
70
// Put all of the values into the calculator.
71
int
multiple = 5;
72
int
value
;
73
for
(
long
i = 0; i < count; i++)
74
{
75
value
= multiple * (i + 1);
76
77
calculator.
Update
(
value
);
78
79
sum +=
value
;
80
}
81
82
// Calculate the expected values for the statistical functions.
83
min
= multiple;
84
max
= multiple * count;
85
mean = sum / count;
86
variance = 0;
87
stddev = std::sqrt(variance);
88
89
// Test the calculator.
90
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(),
91
count,
92
TOLERANCE
,
93
"Count value outside of tolerance "
94
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
95
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
96
min
,
97
TOLERANCE
,
98
"Min value outside of tolerance "
99
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() -
min
);
100
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
101
max
,
102
TOLERANCE
,
103
"Max value outside of tolerance "
104
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() -
max
);
105
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(),
106
mean,
107
TOLERANCE
,
108
"Mean value outside of tolerance "
109
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
110
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(),
111
stddev,
112
TOLERANCE
,
113
"Stddev value outside of tolerance "
114
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
115
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(),
116
variance,
117
TOLERANCE
,
118
"Variance value outside of tolerance "
119
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
120
}
121
127
class
FiveIntegersAverageTestCase
:
public
TestCase
128
{
129
public
:
130
FiveIntegersAverageTestCase
();
131
~FiveIntegersAverageTestCase
()
override
;
132
133
private
:
134
void
DoRun
()
override
;
135
};
136
137
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
()
138
:
TestCase
(
"Average Object Test using Five Integers"
)
139
140
{
141
}
142
143
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
()
144
{
145
}
146
147
void
148
FiveIntegersAverageTestCase::DoRun
()
149
{
150
Average<int>
calculator;
151
152
long
count = 5;
153
154
double
sum = 0;
155
double
sqrSum = 0;
156
double
min
;
157
double
max
;
158
double
mean;
159
double
stddev;
160
double
variance;
161
162
// Put all of the values into the calculator.
163
int
multiple = 5;
164
int
value
;
165
for
(
long
i = 0; i < count; i++)
166
{
167
value
= multiple * (i + 1);
168
169
calculator.
Update
(
value
);
170
171
sum +=
value
;
172
sqrSum +=
value
*
value
;
173
}
174
175
// Calculate the expected values for the statistical functions.
176
min
= multiple;
177
max
= multiple * count;
178
mean = sum / count;
179
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
180
stddev = std::sqrt(variance);
181
182
// Test the calculator.
183
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(),
184
count,
185
TOLERANCE
,
186
"Count value outside of tolerance "
187
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
188
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
189
min
,
190
TOLERANCE
,
191
"Min value outside of tolerance "
192
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() -
min
);
193
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
194
max
,
195
TOLERANCE
,
196
"Max value outside of tolerance "
197
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() -
max
);
198
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(),
199
mean,
200
TOLERANCE
,
201
"Mean value outside of tolerance "
202
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
203
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(),
204
stddev,
205
TOLERANCE
,
206
"Stddev value outside of tolerance "
207
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
208
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(),
209
variance,
210
TOLERANCE
,
211
"Variance value outside of tolerance "
212
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
213
}
214
220
class
FiveDoublesAverageTestCase
:
public
TestCase
221
{
222
public
:
223
FiveDoublesAverageTestCase
();
224
~FiveDoublesAverageTestCase
()
override
;
225
226
private
:
227
void
DoRun
()
override
;
228
};
229
230
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
()
231
:
TestCase
(
"Average Object Test using Five Double Values"
)
232
233
{
234
}
235
236
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
()
237
{
238
}
239
240
void
241
FiveDoublesAverageTestCase::DoRun
()
242
{
243
Average<double>
calculator;
244
245
long
count = 5;
246
247
double
sum = 0;
248
double
sqrSum = 0;
249
double
min
;
250
double
max
;
251
double
mean;
252
double
stddev;
253
double
variance;
254
255
// Put all of the values into the calculator.
256
double
multiple = 3.14;
257
double
value
;
258
for
(
long
i = 0; i < count; i++)
259
{
260
value
= multiple * (i + 1);
261
262
calculator.
Update
(
value
);
263
264
sum +=
value
;
265
sqrSum +=
value
*
value
;
266
}
267
268
// Calculate the expected values for the statistical functions.
269
min
= multiple;
270
max
= multiple * count;
271
mean = sum / count;
272
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
273
stddev = std::sqrt(variance);
274
275
// Test the calculator.
276
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(),
277
count,
278
TOLERANCE
,
279
"Count value outside of tolerance "
280
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
281
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
282
min
,
283
TOLERANCE
,
284
"Min value outside of tolerance "
285
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() -
min
);
286
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
287
max
,
288
TOLERANCE
,
289
"Max value outside of tolerance "
290
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() -
max
);
291
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(),
292
mean,
293
TOLERANCE
,
294
"Mean value outside of tolerance "
295
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
296
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(),
297
stddev,
298
TOLERANCE
,
299
"Stddev value outside of tolerance "
300
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
301
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(),
302
variance,
303
TOLERANCE
,
304
"Variance value outside of tolerance "
305
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
306
}
307
313
class
AverageTestSuite
:
public
TestSuite
314
{
315
public
:
316
AverageTestSuite
();
317
};
318
319
AverageTestSuite::AverageTestSuite
()
320
:
TestSuite
(
"average"
, UNIT)
321
{
322
AddTestCase
(
new
OneIntegerAverageTestCase
, TestCase::QUICK);
323
AddTestCase
(
new
FiveIntegersAverageTestCase
, TestCase::QUICK);
324
AddTestCase
(
new
FiveDoublesAverageTestCase
, TestCase::QUICK);
325
}
326
328
static
AverageTestSuite
averageTestSuite
;
min
#define min(a, b)
Definition:
80211b.c:41
max
#define max(a, b)
Definition:
80211b.c:42
averageTestSuite
static AverageTestSuite averageTestSuite
Static variable for test initialization.
Definition:
average-test-suite.cc:328
TOLERANCE
const double TOLERANCE
Definition:
average-test-suite.cc:29
AverageTestSuite
Average class TestSuite.
Definition:
average-test-suite.cc:314
AverageTestSuite::AverageTestSuite
AverageTestSuite()
Definition:
average-test-suite.cc:319
FiveDoublesAverageTestCase
Average class - Test case for five double values.
Definition:
average-test-suite.cc:221
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
~FiveDoublesAverageTestCase() override
Definition:
average-test-suite.cc:236
FiveDoublesAverageTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:241
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
FiveDoublesAverageTestCase()
Definition:
average-test-suite.cc:230
FiveIntegersAverageTestCase
Average class - Test case for five integers.
Definition:
average-test-suite.cc:128
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
~FiveIntegersAverageTestCase() override
Definition:
average-test-suite.cc:143
FiveIntegersAverageTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:148
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
FiveIntegersAverageTestCase()
Definition:
average-test-suite.cc:137
OneIntegerAverageTestCase
Average class - Test case for a single integer.
Definition:
average-test-suite.cc:37
OneIntegerAverageTestCase::~OneIntegerAverageTestCase
~OneIntegerAverageTestCase() override
Definition:
average-test-suite.cc:52
OneIntegerAverageTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:57
OneIntegerAverageTestCase::OneIntegerAverageTestCase
OneIntegerAverageTestCase()
Definition:
average-test-suite.cc:46
ns3::Average
Simple average, min, max and std.
Definition:
average.h:43
ns3::Average::Min
T Min() const
Sample minimum.
Definition:
average.h:90
ns3::Average::Var
double Var() const
Sample unbiased nbiased estimate of variance.
Definition:
average.h:126
ns3::Average::Max
T Max() const
Sample maximum.
Definition:
average.h:99
ns3::Average::Update
void Update(const T &x)
Add new sample.
Definition:
average.h:56
ns3::Average::Count
uint32_t Count() const
Sample size.
Definition:
average.h:81
ns3::Average::Stddev
double Stddev() const
Sample standard deviation.
Definition:
average.h:135
ns3::Average::Mean
double Mean() const
Sample estimate of mean, alias to Avg.
Definition:
average.h:117
ns3::TestCase
encapsulates test code
Definition:
test.h:1060
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:301
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1256
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:337
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:43
second.value
value
Definition:
second.py:48
src
stats
test
average-test-suite.cc
Generated on Sun Mar 3 2024 17:11:08 for ns-3 by
1.9.1