A Discrete-Event Network Simulator
API
basic-data-calculators-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) 2011 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/basic-data-calculators.h"
25 
26 using namespace ns3;
27 
28 const double TOLERANCE = 1e-14;
29 
30 // ===========================================================================
31 // Test case for a single integer.
32 // ===========================================================================
33 
35 {
36 public:
38  virtual ~OneIntegerTestCase ();
39 
40 private:
41  virtual void DoRun (void);
42 };
43 
45  : TestCase ("Basic Statistical Functions using One Integer")
46 
47 {
48 }
49 
51 {
52 }
53 
54 void
56 {
58 
59  long count = 1;
60 
61  double sum = 0;
62  double sqrSum = 0;
63  double min;
64  double max;
65  double mean;
66  double stddev;
67  double variance;
68 
69  // Put all of the values into the calculator.
70  int multiple = 5;
71  int value;
72  for (long i = 0; i < count; i++)
73  {
74  value = multiple * (i + 1);
75 
76  calculator.Update (value);
77 
78  sum += value;
79  sqrSum += value * 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.getCount(), count, TOLERANCE, "Count value wrong");
91  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
92  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMin(), min, TOLERANCE, "Min value wrong");
93  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMax(), max, TOLERANCE, "Max value wrong");
94  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
95  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
96  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getVariance(), variance, TOLERANCE, "Variance value wrong");
97  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
98 }
99 
100 
101 // ===========================================================================
102 // Test case for five integers.
103 // ===========================================================================
104 
106 {
107 public:
109  virtual ~FiveIntegersTestCase ();
110 
111 private:
112  virtual void DoRun (void);
113 };
114 
116  : TestCase ("Basic Statistical Functions using Five Integers")
117 
118 {
119 }
120 
122 {
123 }
124 
125 void
127 {
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.getCount(), count, TOLERANCE, "Count value wrong");
162  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
163  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMin(), min, TOLERANCE, "Min value wrong");
164  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMax(), max, TOLERANCE, "Max value wrong");
165  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
166  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
167  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getVariance(), variance, TOLERANCE, "Variance value wrong");
168  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
169 }
170 
171 
172 // ===========================================================================
173 // Test case for five double values.
174 // ===========================================================================
175 
177 {
178 public:
180  virtual ~FiveDoublesTestCase ();
181 
182 private:
183  virtual void DoRun (void);
184 };
185 
187  : TestCase ("Basic Statistical Functions using Five Double Values")
188 
189 {
190 }
191 
193 {
194 }
195 
196 void
198 {
200 
201  long count = 5;
202 
203  double sum = 0;
204  double sqrSum = 0;
205  double min;
206  double max;
207  double mean;
208  double stddev;
209  double variance;
210 
211  // Put all of the values into the calculator.
212  double multiple = 3.14;
213  double value;
214  for (long i = 0; i < count; i++)
215  {
216  value = multiple * (i + 1);
217 
218  calculator.Update (value);
219 
220  sum += value;
221  sqrSum += value * value;
222  }
223 
224  // Calculate the expected values for the statistical functions.
225  min = multiple;
226  max = multiple * count;
227  mean = sum / count;
228  variance = (count * sqrSum - sum * sum) / (count * (count - 1));
229  stddev = std::sqrt (variance);
230 
231  // Test the calculator.
232  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getCount(), count, TOLERANCE, "Count value wrong");
233  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
234  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMin(), min, TOLERANCE, "Min value wrong");
235  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMax(), max, TOLERANCE, "Max value wrong");
236  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
237  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
238  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getVariance(), variance, TOLERANCE, "Variance value wrong");
239  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
240 }
241 
242 
244 {
245 public:
247 };
248 
250  : TestSuite ("basic-data-calculators", UNIT)
251 {
252  AddTestCase (new OneIntegerTestCase, TestCase::QUICK);
253  AddTestCase (new FiveIntegersTestCase, TestCase::QUICK);
254  AddTestCase (new FiveDoublesTestCase, TestCase::QUICK);
255 }
256 
double getSum() const
Returns the sum.
#define min(a, b)
Definition: 80211b.c:42
A suite of tests to run.
Definition: test.h:1342
double getMin() const
Returns the minimum value.
const double TOLERANCE
double getVariance() const
Returns the current variance.
encapsulates test code
Definition: test.h:1155
double getSqrSum() const
Returns the sum of squares.
double getMax() const
Returns the maximum value.
#define max(a, b)
Definition: 80211b.c:43
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
static BasicDataCalculatorsTestSuite basicDataCalculatorsTestSuite
virtual void DoRun(void)
Implementation to actually run this TestCase.
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Template class MinMaxAvgTotalCalculator.
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
long getCount() const
Returns the count.
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
double getMean() const
Returns the mean value.
double getStddev() const
Returns the standard deviation.