A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
basic-data-calculators-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mitch Watrous (watrous@u.washington.edu)
7 */
8
9#include "ns3/basic-data-calculators.h"
10#include "ns3/test.h"
11
12#include <cmath>
13
14using namespace ns3;
15
16// See issue #698 for discussion of this tolerance
17const double TOLERANCE = 1e-13;
18
19/**
20 * \ingroup stats-tests
21 *
22 * \brief MinMaxAvgTotalCalculator class - Test case for a single integer.
23 */
25{
26 public:
28 ~OneIntegerTestCase() override;
29
30 private:
31 void DoRun() override;
32};
33
35 : TestCase("Basic Statistical Functions using One Integer")
36
37{
38}
39
43
44void
46{
48
49 long count = 1;
50
51 double sum = 0;
52 double sqrSum = 0;
53 double min;
54 double max;
55 double mean;
56 double stddev;
57 double variance;
58
59 // Put all of the values into the calculator.
60 int multiple = 5;
61 int value;
62 for (long i = 0; i < count; i++)
63 {
64 value = multiple * (i + 1);
65
66 calculator.Update(value);
67
68 sum += value;
69 sqrSum += value * value;
70 }
71
72 // Calculate the expected values for the statistical functions.
73 min = multiple;
74 max = multiple * count;
75 mean = sum / count;
76 variance = 0;
77 stddev = std::sqrt(variance);
78
79 // Test the calculator.
80 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getCount(), count, TOLERANCE, "Count value wrong");
81 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
82 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMin(), min, TOLERANCE, "Min value wrong");
83 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMax(), max, TOLERANCE, "Max value wrong");
84 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
85 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
87 variance,
89 "Variance value wrong");
90 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
91}
92
93/**
94 * \ingroup stats-tests
95 *
96 * \brief MinMaxAvgTotalCalculator class - Test case for five integers.
97 */
99{
100 public:
102 ~FiveIntegersTestCase() override;
103
104 private:
105 void DoRun() override;
106};
107
109 : TestCase("Basic Statistical Functions using Five Integers")
110
111{
112}
113
117
118void
120{
122
123 long count = 5;
124
125 double sum = 0;
126 double sqrSum = 0;
127 double min;
128 double max;
129 double mean;
130 double stddev;
131 double variance;
132
133 // Put all of the values into the calculator.
134 int multiple = 5;
135 int value;
136 for (long i = 0; i < count; i++)
137 {
138 value = multiple * (i + 1);
139
140 calculator.Update(value);
141
142 sum += value;
143 sqrSum += value * value;
144 }
145
146 // Calculate the expected values for the statistical functions.
147 min = multiple;
148 max = multiple * count;
149 mean = sum / count;
150 variance = (count * sqrSum - sum * sum) / (count * (count - 1));
151 stddev = std::sqrt(variance);
152
153 // Test the calculator.
154 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getCount(), count, TOLERANCE, "Count value wrong");
155 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
156 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMin(), min, TOLERANCE, "Min value wrong");
157 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMax(), max, TOLERANCE, "Max value wrong");
158 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
159 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
161 variance,
162 TOLERANCE,
163 "Variance value wrong");
164 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
165}
166
167/**
168 * \ingroup stats-tests
169 *
170 * \brief MinMaxAvgTotalCalculator class - Test case for five double values.
171 */
173{
174 public:
176 ~FiveDoublesTestCase() override;
177
178 private:
179 void DoRun() override;
180};
181
183 : TestCase("Basic Statistical Functions using Five Double Values")
184
185{
186}
187
191
192void
194{
196
197 long count = 5;
198
199 double sum = 0;
200 double sqrSum = 0;
201 double min;
202 double max;
203 double mean;
204 double stddev;
205 double variance;
206
207 // Put all of the values into the calculator.
208 double multiple = 3.14;
209 double value;
210 for (long i = 0; i < count; i++)
211 {
212 value = multiple * (i + 1);
213
214 calculator.Update(value);
215
216 sum += value;
217 sqrSum += value * value;
218 }
219
220 // Calculate the expected values for the statistical functions.
221 min = multiple;
222 max = multiple * count;
223 mean = sum / count;
224 variance = (count * sqrSum - sum * sum) / (count * (count - 1));
225 stddev = std::sqrt(variance);
226
227 // Test the calculator.
228 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getCount(), count, TOLERANCE, "Count value wrong");
229 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSum(), sum, TOLERANCE, "Sum value wrong");
230 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMin(), min, TOLERANCE, "Min value wrong");
231 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMax(), max, TOLERANCE, "Max value wrong");
232 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getMean(), mean, TOLERANCE, "Mean value wrong");
233 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getStddev(), stddev, TOLERANCE, "Stddev value wrong");
235 variance,
236 TOLERANCE,
237 "Variance value wrong");
238 NS_TEST_ASSERT_MSG_EQ_TOL(calculator.getSqrSum(), sqrSum, TOLERANCE, "SqrSum value wrong");
239}
240
241/**
242 * \ingroup stats-tests
243 *
244 * \brief MinMaxAvgTotalCalculator class TestSuite
245 */
247{
248 public:
250};
251
253 : TestSuite("basic-data-calculators", Type::UNIT)
254{
255 AddTestCase(new OneIntegerTestCase, TestCase::Duration::QUICK);
256 AddTestCase(new FiveIntegersTestCase, TestCase::Duration::QUICK);
257 AddTestCase(new FiveDoublesTestCase, TestCase::Duration::QUICK);
258}
259
260/// Static variable for test initialization
static BasicDataCalculatorsTestSuite basicDataCalculatorsTestSuite
Static variable for test initialization.
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:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
#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:327
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.