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 
36 {
37 public:
39  virtual ~OneIntegerTestCase ();
40 
41 private:
42  virtual void DoRun (void);
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");
97  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getVariance(), variance, TOLERANCE, "Variance value wrong");
98  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
99 }
100 
101 
108 {
109 public:
111  virtual ~FiveIntegersTestCase ();
112 
113 private:
114  virtual void DoRun (void);
115 };
116 
118  : TestCase ("Basic Statistical Functions using Five Integers")
119 
120 {
121 }
122 
124 {
125 }
126 
127 void
129 {
131 
132  long count = 5;
133 
134  double sum = 0;
135  double sqrSum = 0;
136  double min;
137  double max;
138  double mean;
139  double stddev;
140  double variance;
141 
142  // Put all of the values into the calculator.
143  int multiple = 5;
144  int value;
145  for (long i = 0; i < count; i++)
146  {
147  value = multiple * (i + 1);
148 
149  calculator.Update (value);
150 
151  sum += value;
152  sqrSum += value * value;
153  }
154 
155  // Calculate the expected values for the statistical functions.
156  min = multiple;
157  max = multiple * count;
158  mean = sum / count;
159  variance = (count * sqrSum - sum * sum) / (count * (count - 1));
160  stddev = std::sqrt (variance);
161 
162  // Test the calculator.
163  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getCount(), count, TOLERANCE, "Count value wrong");
164  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
165  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMin(), min, TOLERANCE, "Min value wrong");
166  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMax(), max, TOLERANCE, "Max value wrong");
167  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
168  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
169  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getVariance(), variance, TOLERANCE, "Variance value wrong");
170  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
171 }
172 
173 
180 {
181 public:
183  virtual ~FiveDoublesTestCase ();
184 
185 private:
186  virtual void DoRun (void);
187 };
188 
190  : TestCase ("Basic Statistical Functions using Five Double Values")
191 
192 {
193 }
194 
196 {
197 }
198 
199 void
201 {
203 
204  long count = 5;
205 
206  double sum = 0;
207  double sqrSum = 0;
208  double min;
209  double max;
210  double mean;
211  double stddev;
212  double variance;
213 
214  // Put all of the values into the calculator.
215  double multiple = 3.14;
216  double value;
217  for (long i = 0; i < count; i++)
218  {
219  value = multiple * (i + 1);
220 
221  calculator.Update (value);
222 
223  sum += value;
224  sqrSum += value * value;
225  }
226 
227  // Calculate the expected values for the statistical functions.
228  min = multiple;
229  max = multiple * count;
230  mean = sum / count;
231  variance = (count * sqrSum - sum * sum) / (count * (count - 1));
232  stddev = std::sqrt (variance);
233 
234  // Test the calculator.
235  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getCount(), count, TOLERANCE, "Count value wrong");
236  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
237  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMin(), min, TOLERANCE, "Min value wrong");
238  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMax(), max, TOLERANCE, "Max value wrong");
239  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
240  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
241  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getVariance(), variance, TOLERANCE, "Variance value wrong");
242  NS_TEST_ASSERT_MSG_EQ_TOL (calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
243 }
244 
245 
252 {
253 public:
255 };
256 
258  : TestSuite ("basic-data-calculators", UNIT)
259 {
260  AddTestCase (new OneIntegerTestCase, TestCase::QUICK);
261  AddTestCase (new FiveIntegersTestCase, TestCase::QUICK);
262  AddTestCase (new FiveDoublesTestCase, TestCase::QUICK);
263 }
264 
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
static BasicDataCalculatorsTestSuite basicDataCalculatorsTestSuite
Static variable for test initialization.
const double TOLERANCE
MinMaxAvgTotalCalculator class TestSuite.
MinMaxAvgTotalCalculator class - Test case for five double values.
virtual void DoRun(void)
Implementation to actually run this TestCase.
MinMaxAvgTotalCalculator class - Test case for five integers.
virtual void DoRun(void)
Implementation to actually run this TestCase.
MinMaxAvgTotalCalculator class - Test case for a single integer.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Template class MinMaxAvgTotalCalculator.
double getSqrSum() const
Returns the sum of squares.
long getCount() const
Returns the count.
double getSum() const
Returns the sum.
double getStddev() const
Returns the standard deviation.
double getMean() const
Returns the mean value.
double getVariance() const
Returns the current variance.
double getMax() const
Returns the maximum value.
double getMin() const
Returns the minimum value.
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
#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:323
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.