A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
stats
test
average-test-suite.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2012 University of Washington
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mitch Watrous (watrous@u.washington.edu)
19
*/
20
21
#include <cmath>
22
23
#include "ns3/test.h"
24
#include "ns3/average.h"
25
26
using namespace
ns3
;
27
28
// Note, the rationale for this particular value of TOLERANCE is not
29
// documented. Current value is sufficient for all test platforms.
30
const
double
TOLERANCE
= 2e-14;
31
32
// ===========================================================================
33
// Test case for a single integer.
34
// ===========================================================================
35
36
class
OneIntegerAverageTestCase
:
public
TestCase
37
{
38
public
:
39
OneIntegerAverageTestCase
();
40
virtual
~
OneIntegerAverageTestCase
();
41
42
private
:
43
virtual
void
DoRun (
void
);
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
(
void
)
58
{
59
Average<int>
calculator;
60
61
long
count = 1;
62
63
double
sum = 0;
64
double
sqrSum = 0;
65
double
min
;
66
double
max
;
67
double
mean;
68
double
stddev;
69
double
variance;
70
71
// Put all of the values into the calculator.
72
int
multiple = 5;
73
int
value;
74
for
(
long
i = 0; i < count; i++)
75
{
76
value = multiple * (i + 1);
77
78
calculator.
Update
(value);
79
80
sum += value;
81
sqrSum += value * value;
82
}
83
84
// Calculate the expected values for the statistical functions.
85
min
= multiple;
86
max
= multiple * count;
87
mean = sum / count;
88
variance = 0;
89
stddev = std::sqrt (variance);
90
91
// Test the calculator.
92
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
93
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
min
,
TOLERANCE
,
"Min value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() -
min
);
94
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
max
,
TOLERANCE
,
"Max value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() -
max
);
95
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
96
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
97
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
98
}
99
100
101
// ===========================================================================
102
// Test case for five integers.
103
// ===========================================================================
104
105
class
FiveIntegersAverageTestCase
:
public
TestCase
106
{
107
public
:
108
FiveIntegersAverageTestCase
();
109
virtual
~FiveIntegersAverageTestCase
();
110
111
private
:
112
virtual
void
DoRun
(
void
);
113
};
114
115
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
()
116
:
TestCase
(
"Average Object Test using Five Integers"
)
117
118
{
119
}
120
121
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
()
122
{
123
}
124
125
void
126
FiveIntegersAverageTestCase::DoRun
(
void
)
127
{
128
Average<int>
calculator;
129
130
long
count = 5;
131
132
double
sum = 0;
133
double
sqrSum = 0;
134
double
min
;
135
double
max
;
136
double
mean;
137
double
stddev;
138
double
variance;
139
140
// Put all of the values into the calculator.
141
int
multiple = 5;
142
int
value;
143
for
(
long
i = 0; i < count; i++)
144
{
145
value = multiple * (i + 1);
146
147
calculator.
Update
(value);
148
149
sum += value;
150
sqrSum += value * value;
151
}
152
153
// Calculate the expected values for the statistical functions.
154
min
= multiple;
155
max
= multiple * count;
156
mean = sum / count;
157
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
158
stddev = std::sqrt (variance);
159
160
// Test the calculator.
161
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
162
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
min
,
TOLERANCE
,
"Min value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() -
min
);
163
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
max
,
TOLERANCE
,
"Max value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() -
max
);
164
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
165
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
166
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
167
}
168
169
170
// ===========================================================================
171
// Test case for five double values.
172
// ===========================================================================
173
174
class
FiveDoublesAverageTestCase
:
public
TestCase
175
{
176
public
:
177
FiveDoublesAverageTestCase
();
178
virtual
~FiveDoublesAverageTestCase
();
179
180
private
:
181
virtual
void
DoRun
(
void
);
182
};
183
184
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
()
185
:
TestCase
(
"Average Object Test using Five Double Values"
)
186
187
{
188
}
189
190
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
()
191
{
192
}
193
194
void
195
FiveDoublesAverageTestCase::DoRun
(
void
)
196
{
197
Average<double>
calculator;
198
199
long
count = 5;
200
201
double
sum = 0;
202
double
sqrSum = 0;
203
double
min
;
204
double
max
;
205
double
mean;
206
double
stddev;
207
double
variance;
208
209
// Put all of the values into the calculator.
210
double
multiple = 3.14;
211
double
value;
212
for
(
long
i = 0; i < count; i++)
213
{
214
value = multiple * (i + 1);
215
216
calculator.
Update
(value);
217
218
sum += value;
219
sqrSum += value * value;
220
}
221
222
// Calculate the expected values for the statistical functions.
223
min
= multiple;
224
max
= multiple * count;
225
mean = sum / count;
226
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
227
stddev = std::sqrt (variance);
228
229
// Test the calculator.
230
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Count
() - count);
231
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(),
min
,
TOLERANCE
,
"Min value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Min
() -
min
);
232
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(),
max
,
TOLERANCE
,
"Max value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Max
() -
max
);
233
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Mean
() - mean);
234
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Stddev
() - stddev);
235
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value outside of tolerance "
<<
TOLERANCE
<<
"; difference: "
<< calculator.
Var
() - variance);
236
}
237
238
239
class
AverageTestSuite
:
public
TestSuite
240
{
241
public
:
242
AverageTestSuite
();
243
};
244
245
AverageTestSuite::AverageTestSuite
()
246
:
TestSuite
(
"average"
, UNIT)
247
{
248
AddTestCase
(
new
OneIntegerAverageTestCase
, TestCase::QUICK);
249
AddTestCase
(
new
FiveIntegersAverageTestCase
, TestCase::QUICK);
250
AddTestCase
(
new
FiveDoublesAverageTestCase
, TestCase::QUICK);
251
}
252
253
static
AverageTestSuite
averageTestSuite
;
min
#define min(a, b)
Definition:
80211b.c:42
ns3::Average::Max
T Max() const
Maximum.
Definition:
average.h:74
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1342
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
virtual ~FiveIntegersAverageTestCase()
Definition:
average-test-suite.cc:121
FiveIntegersAverageTestCase
Definition:
average-test-suite.cc:105
AverageTestSuite
Definition:
average-test-suite.cc:239
ns3::TestCase
encapsulates test code
Definition:
test.h:1155
FiveIntegersAverageTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:126
averageTestSuite
static AverageTestSuite averageTestSuite
Definition:
average-test-suite.cc:253
ns3::Average::Stddev
double Stddev() const
Standard deviation.
Definition:
average.h:82
ns3::Average::Var
double Var() const
Unbiased estimate of variance.
Definition:
average.h:80
OneIntegerAverageTestCase
Definition:
average-test-suite.cc:36
max
#define max(a, b)
Definition:
80211b.c:43
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
FiveDoublesAverageTestCase()
Definition:
average-test-suite.cc:184
OneIntegerAverageTestCase::OneIntegerAverageTestCase
OneIntegerAverageTestCase()
Definition:
average-test-suite.cc:46
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:380
OneIntegerAverageTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:57
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TOLERANCE
const double TOLERANCE
Definition:
average-test-suite.cc:30
AverageTestSuite::AverageTestSuite
AverageTestSuite()
Definition:
average-test-suite.cc:245
ns3::Average
Simple average, min, max and std.
Definition:
average.h:40
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
virtual ~FiveDoublesAverageTestCase()
Definition:
average-test-suite.cc:190
ns3::Average::Min
T Min() const
Minimum.
Definition:
average.h:72
OneIntegerAverageTestCase::~OneIntegerAverageTestCase
virtual ~OneIntegerAverageTestCase()
Definition:
average-test-suite.cc:52
FiveDoublesAverageTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:195
ns3::TOLERANCE
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
Definition:
rtt-estimator.cc:42
ns3::Average::Mean
double Mean() const
Estimate of mean, alias to Avg.
Definition:
average.h:78
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
FiveIntegersAverageTestCase()
Definition:
average-test-suite.cc:115
FiveDoublesAverageTestCase
Definition:
average-test-suite.cc:174
ns3::Average::Count
uint32_t Count() const
Sample size.
Definition:
average.h:70
ns3::Average::Update
void Update(T const &x)
Add new sample.
Definition:
average.h:49
Generated on Wed Nov 7 2018 10:02:10 for ns-3 by
1.8.14