A Discrete-Event Network Simulator
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
14using 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.
18const double TOLERANCE = 2e-14;
19
20/**
21 * \ingroup stats-tests
22 *
23 * \brief Average class - Test case for a single integer.
24 */
26{
27 public:
30
31 private:
32 void DoRun() override;
33};
34
36 : TestCase("Average Object Test using One Integer")
37
38{
39}
40
44
45void
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.
80 count,
82 "Count value outside of tolerance "
83 << TOLERANCE << "; difference: " << calculator.Count() - count);
84 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.Min(),
85 min,
87 "Min value outside of tolerance "
88 << TOLERANCE << "; difference: " << calculator.Min() - min);
89 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.Max(),
90 max,
92 "Max value outside of tolerance "
93 << TOLERANCE << "; difference: " << calculator.Max() - max);
95 mean,
97 "Mean value outside of tolerance "
98 << TOLERANCE << "; difference: " << calculator.Mean() - mean);
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 */
117{
118 public:
121
122 private:
123 void DoRun() override;
124};
125
127 : TestCase("Average Object Test using Five Integers")
128
129{
130}
131
135
136void
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);
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 */
210{
211 public:
214
215 private:
216 void DoRun() override;
217};
218
220 : TestCase("Average Object Test using Five Double Values")
221
222{
223}
224
228
229void
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);
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 */
303{
304 public:
306};
307
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
static AverageTestSuite averageTestSuite
Static variable for test initialization.
Average class TestSuite.
Average class - Test case for five double values.
void DoRun() override
Implementation to actually run this TestCase.
Average class - Test case for five integers.
void DoRun() override
Implementation to actually run this TestCase.
Average class - Test case for a single integer.
void DoRun() override
Implementation to actually run this TestCase.
Simple average, min, max and std.
Definition average.h:32
T Min() const
Sample minimum.
Definition average.h:79
double Var() const
Sample unbiased nbiased estimate of variance.
Definition average.h:115
T Max() const
Sample maximum.
Definition average.h:88
void Update(const T &x)
Add new sample.
Definition average.h:45
uint32_t Count() const
Sample size.
Definition average.h:70
double Stddev() const
Sample standard deviation.
Definition average.h:124
double Mean() const
Sample estimate of mean, alias to Avg.
Definition average.h:106
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.