A Discrete-Event Network Simulator
API
average-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) 2012 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/average.h"
25
26using namespace ns3;
27
28// Note, the rationale for this particular value of TOLERANCE is not
29// documented. Current value is sufficient for all test platforms.
30const double TOLERANCE = 2e-14;
31
38{
39public:
42
43private:
44 virtual void DoRun (void);
45};
46
48 : TestCase ("Average Object Test using One Integer")
49
50{
51}
52
54{
55}
56
57void
59{
60 Average<int> calculator;
61
62 long count = 1;
63
64 double sum = 0;
65 double min;
66 double max;
67 double mean;
68 double stddev;
69 double variance;
70
71 // Put all of the values into the calculator.
72 int multiple = 5;
73 int value;
74 for (long i = 0; i < count; i++)
75 {
76 value = multiple * (i + 1);
77
78 calculator.Update (value);
79
80 sum += 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.Count (), count, TOLERANCE, "Count value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Count () - count);
92 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Min (), min, TOLERANCE, "Min value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Min () - min);
93 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Max (), max, TOLERANCE, "Max value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Max () - max);
94 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Mean (), mean, TOLERANCE, "Mean value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Mean () - mean);
95 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Stddev (), stddev, TOLERANCE, "Stddev value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Stddev () - stddev);
96 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Var (), variance, TOLERANCE, "Variance value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Var () - variance);
97}
98
99
106{
107public:
110
111private:
112 virtual void DoRun (void);
113};
114
116 : TestCase ("Average Object Test using Five Integers")
117
118{
119}
120
122{
123}
124
125void
127{
128 Average<int> calculator;
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.Count (), count, TOLERANCE, "Count value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Count () - count);
162 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Min (), min, TOLERANCE, "Min value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Min () - min);
163 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Max (), max, TOLERANCE, "Max value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Max () - max);
164 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Mean (), mean, TOLERANCE, "Mean value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Mean () - mean);
165 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Stddev (), stddev, TOLERANCE, "Stddev value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Stddev () - stddev);
166 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Var (), variance, TOLERANCE, "Variance value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Var () - variance);
167}
168
169
176{
177public:
180
181private:
182 virtual void DoRun (void);
183};
184
186 : TestCase ("Average Object Test using Five Double Values")
187
188{
189}
190
192{
193}
194
195void
197{
198 Average<double> calculator;
199
200 long count = 5;
201
202 double sum = 0;
203 double sqrSum = 0;
204 double min;
205 double max;
206 double mean;
207 double stddev;
208 double variance;
209
210 // Put all of the values into the calculator.
211 double multiple = 3.14;
212 double value;
213 for (long i = 0; i < count; i++)
214 {
215 value = multiple * (i + 1);
216
217 calculator.Update (value);
218
219 sum += value;
220 sqrSum += value * value;
221 }
222
223 // Calculate the expected values for the statistical functions.
224 min = multiple;
225 max = multiple * count;
226 mean = sum / count;
227 variance = (count * sqrSum - sum * sum) / (count * (count - 1));
228 stddev = std::sqrt (variance);
229
230 // Test the calculator.
231 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Count (), count, TOLERANCE, "Count value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Count () - count);
232 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Min (), min, TOLERANCE, "Min value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Min () - min);
233 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Max (), max, TOLERANCE, "Max value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Max () - max);
234 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Mean (), mean, TOLERANCE, "Mean value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Mean () - mean);
235 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Stddev (), stddev, TOLERANCE, "Stddev value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Stddev () - stddev);
236 NS_TEST_ASSERT_MSG_EQ_TOL (calculator.Var (), variance, TOLERANCE, "Variance value outside of tolerance " << TOLERANCE << "; difference: " << calculator.Var () - variance);
237}
238
239
246{
247public:
249};
250
252 : TestSuite ("average", UNIT)
253{
254 AddTestCase (new OneIntegerAverageTestCase, TestCase::QUICK);
255 AddTestCase (new FiveIntegersAverageTestCase, TestCase::QUICK);
256 AddTestCase (new FiveDoublesAverageTestCase, TestCase::QUICK);
257}
258
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
static AverageTestSuite averageTestSuite
Static variable for test initialization.
const double TOLERANCE
Average class TestSuite.
Average class - Test case for five double values.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Average class - Test case for five integers.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Average class - Test case for a single integer.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Simple average, min, max and std.
Definition: average.h:41
void Update(T const &x)
Add new sample.
Definition: average.h:52
T Min() const
Sample minimum.
Definition: average.h:81
double Var() const
Sample unbiased nbiased estimate of variance.
Definition: average.h:101
T Max() const
Sample maximum.
Definition: average.h:86
uint32_t Count() const
Sample size.
Definition: average.h:76
double Stddev() const
Sample standard deviation.
Definition: average.h:106
double Mean() const
Sample estimate of mean, alias to Avg.
Definition: average.h:96
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.