A Discrete-Event Network Simulator
API
basic-data-calculators-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 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/basic-data-calculators.h"
21 #include "ns3/test.h"
22 
23 #include <cmath>
24 
25 using namespace ns3;
26 
27 // See issue #698 for discussion of this tolerance
28 const double TOLERANCE = 1e-13;
29 
36 {
37  public:
39  ~OneIntegerTestCase() override;
40 
41  private:
42  void DoRun() override;
43 };
44 
46  : TestCase("Basic Statistical Functions using One Integer")
47 
48 {
49 }
50 
52 {
53 }
54 
55 void
57 {
59 
60  long count = 1;
61 
62  double sum = 0;
63  double sqrSum = 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  sqrSum += value * value;
81  }
82 
83  // Calculate the expected values for the statistical functions.
84  min = multiple;
85  max = multiple * count;
86  mean = sum / count;
87  variance = 0;
88  stddev = std::sqrt(variance);
89 
90  // Test the calculator.
91  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getCount(), count, TOLERANCE, "Count value wrong");
92  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
93  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMin(), min, TOLERANCE, "Min value wrong");
94  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMax(), max, TOLERANCE, "Max value wrong");
95  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
96  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
98  variance,
99  TOLERANCE,
100  "Variance value wrong");
101  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
102 }
103 
110 {
111  public:
113  ~FiveIntegersTestCase() override;
114 
115  private:
116  void DoRun() override;
117 };
118 
120  : TestCase("Basic Statistical Functions using Five Integers")
121 
122 {
123 }
124 
126 {
127 }
128 
129 void
131 {
133 
134  long count = 5;
135 
136  double sum = 0;
137  double sqrSum = 0;
138  double min;
139  double max;
140  double mean;
141  double stddev;
142  double variance;
143 
144  // Put all of the values into the calculator.
145  int multiple = 5;
146  int value;
147  for (long i = 0; i < count; i++)
148  {
149  value = multiple * (i + 1);
150 
151  calculator.Update(value);
152 
153  sum += value;
154  sqrSum += value * value;
155  }
156 
157  // Calculate the expected values for the statistical functions.
158  min = multiple;
159  max = multiple * count;
160  mean = sum / count;
161  variance = (count * sqrSum - sum * sum) / (count * (count - 1));
162  stddev = std::sqrt(variance);
163 
164  // Test the calculator.
165  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getCount(), count, TOLERANCE, "Count value wrong");
166  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
167  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMin(), min, TOLERANCE, "Min value wrong");
168  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMax(), max, TOLERANCE, "Max value wrong");
169  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
170  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
172  variance,
173  TOLERANCE,
174  "Variance value wrong");
175  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
176 }
177 
184 {
185  public:
187  ~FiveDoublesTestCase() override;
188 
189  private:
190  void DoRun() override;
191 };
192 
194  : TestCase("Basic Statistical Functions using Five Double Values")
195 
196 {
197 }
198 
200 {
201 }
202 
203 void
205 {
207 
208  long count = 5;
209 
210  double sum = 0;
211  double sqrSum = 0;
212  double min;
213  double max;
214  double mean;
215  double stddev;
216  double variance;
217 
218  // Put all of the values into the calculator.
219  double multiple = 3.14;
220  double value;
221  for (long i = 0; i < count; i++)
222  {
223  value = multiple * (i + 1);
224 
225  calculator.Update(value);
226 
227  sum += value;
228  sqrSum += value * value;
229  }
230 
231  // Calculate the expected values for the statistical functions.
232  min = multiple;
233  max = multiple * count;
234  mean = sum / count;
235  variance = (count * sqrSum - sum * sum) / (count * (count - 1));
236  stddev = std::sqrt(variance);
237 
238  // Test the calculator.
239  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getCount(), count, TOLERANCE, "Count value wrong");
240  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
241  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMin(), min, TOLERANCE, "Min value wrong");
242  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMax(), max, TOLERANCE, "Max value wrong");
243  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
244  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
246  variance,
247  TOLERANCE,
248  "Variance value wrong");
249  NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
250 }
251 
258 {
259  public:
261 };
262 
264  : TestSuite("basic-data-calculators", UNIT)
265 {
266  AddTestCase(new OneIntegerTestCase, TestCase::QUICK);
267  AddTestCase(new FiveIntegersTestCase, TestCase::QUICK);
268  AddTestCase(new FiveDoublesTestCase, TestCase::QUICK);
269 }
270 
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
static BasicDataCalculatorsTestSuite basicDataCalculatorsTestSuite
Static variable for test initialization.
const double TOLERANCE
MinMaxAvgTotalCalculator class TestSuite.
MinMaxAvgTotalCalculator class - Test case for five double values.
void DoRun() override
Implementation to actually run this TestCase.
MinMaxAvgTotalCalculator class - Test case for five integers.
void DoRun() override
Implementation to actually run this TestCase.
MinMaxAvgTotalCalculator class - Test case for a single integer.
void DoRun() override
Implementation to actually run this TestCase.
Template class MinMaxAvgTotalCalculator.
long getCount() const override
Returns the count.
double getVariance() const override
Returns the current variance.
double getMax() const override
Returns the maximum value.
double getSqrSum() const override
Returns the sum of squares.
double getSum() const override
Returns the sum.
double getStddev() const override
Returns the standard deviation.
double getMean() const override
Returns the mean value.
double getMin() const override
Returns the minimum value.
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
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