A Discrete-Event Network Simulator
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 
37 {
38  public:
40  ~OneIntegerAverageTestCase() override;
41 
42  private:
43  void DoRun() override;
44 };
45 
47  : TestCase("Average Object Test using One Integer")
48 
49 {
50 }
51 
53 {
54 }
55 
56 void
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 
128 {
129  public:
131  ~FiveIntegersAverageTestCase() override;
132 
133  private:
134  void DoRun() override;
135 };
136 
138  : TestCase("Average Object Test using Five Integers")
139 
140 {
141 }
142 
144 {
145 }
146 
147 void
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 
221 {
222  public:
224  ~FiveDoublesAverageTestCase() override;
225 
226  private:
227  void DoRun() override;
228 };
229 
231  : TestCase("Average Object Test using Five Double Values")
232 
233 {
234 }
235 
237 {
238 }
239 
240 void
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 
314 {
315  public:
317 };
318 
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 
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
static AverageTestSuite averageTestSuite
Static variable for test initialization.
const double TOLERANCE
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:43
T Min() const
Sample minimum.
Definition: average.h:90
double Var() const
Sample unbiased nbiased estimate of variance.
Definition: average.h:126
T Max() const
Sample maximum.
Definition: average.h:99
void Update(const T &x)
Add new sample.
Definition: average.h:56
uint32_t Count() const
Sample size.
Definition: average.h:81
double Stddev() const
Sample standard deviation.
Definition: average.h:135
double Mean() const
Sample estimate of mean, alias to Avg.
Definition: average.h:117
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
#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
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.
value
Definition: second.py:48