A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
14
using namespace
ns3
;
15
16
// See issue #698 for discussion of this tolerance
17
const
double
TOLERANCE
= 1e-13;
18
19
/**
20
* \ingroup stats-tests
21
*
22
* \brief MinMaxAvgTotalCalculator class - Test case for a single integer.
23
*/
24
class
OneIntegerTestCase
:
public
TestCase
25
{
26
public
:
27
OneIntegerTestCase
();
28
~OneIntegerTestCase
()
override
;
29
30
private
:
31
void
DoRun
()
override
;
32
};
33
34
OneIntegerTestCase::OneIntegerTestCase
()
35
:
TestCase
(
"Basic Statistical Functions using One Integer"
)
36
37
{
38
}
39
40
OneIntegerTestCase::~OneIntegerTestCase
()
41
{
42
}
43
44
void
45
OneIntegerTestCase::DoRun
()
46
{
47
MinMaxAvgTotalCalculator<int>
calculator;
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"
);
86
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(),
87
variance,
88
TOLERANCE
,
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
*/
98
class
FiveIntegersTestCase
:
public
TestCase
99
{
100
public
:
101
FiveIntegersTestCase
();
102
~FiveIntegersTestCase
()
override
;
103
104
private
:
105
void
DoRun
()
override
;
106
};
107
108
FiveIntegersTestCase::FiveIntegersTestCase
()
109
:
TestCase
(
"Basic Statistical Functions using Five Integers"
)
110
111
{
112
}
113
114
FiveIntegersTestCase::~FiveIntegersTestCase
()
115
{
116
}
117
118
void
119
FiveIntegersTestCase::DoRun
()
120
{
121
MinMaxAvgTotalCalculator<int>
calculator;
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"
);
160
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(),
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
*/
172
class
FiveDoublesTestCase
:
public
TestCase
173
{
174
public
:
175
FiveDoublesTestCase
();
176
~FiveDoublesTestCase
()
override
;
177
178
private
:
179
void
DoRun
()
override
;
180
};
181
182
FiveDoublesTestCase::FiveDoublesTestCase
()
183
:
TestCase
(
"Basic Statistical Functions using Five Double Values"
)
184
185
{
186
}
187
188
FiveDoublesTestCase::~FiveDoublesTestCase
()
189
{
190
}
191
192
void
193
FiveDoublesTestCase::DoRun
()
194
{
195
MinMaxAvgTotalCalculator<double>
calculator;
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"
);
234
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(),
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
*/
246
class
BasicDataCalculatorsTestSuite
:
public
TestSuite
247
{
248
public
:
249
BasicDataCalculatorsTestSuite
();
250
};
251
252
BasicDataCalculatorsTestSuite::BasicDataCalculatorsTestSuite
()
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
261
static
BasicDataCalculatorsTestSuite
basicDataCalculatorsTestSuite
;
basicDataCalculatorsTestSuite
static BasicDataCalculatorsTestSuite basicDataCalculatorsTestSuite
Static variable for test initialization.
Definition
basic-data-calculators-test-suite.cc:261
BasicDataCalculatorsTestSuite
MinMaxAvgTotalCalculator class TestSuite.
Definition
basic-data-calculators-test-suite.cc:247
BasicDataCalculatorsTestSuite::BasicDataCalculatorsTestSuite
BasicDataCalculatorsTestSuite()
Definition
basic-data-calculators-test-suite.cc:252
FiveDoublesTestCase
MinMaxAvgTotalCalculator class - Test case for five double values.
Definition
basic-data-calculators-test-suite.cc:173
FiveDoublesTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
basic-data-calculators-test-suite.cc:193
FiveDoublesTestCase::FiveDoublesTestCase
FiveDoublesTestCase()
Definition
basic-data-calculators-test-suite.cc:182
FiveDoublesTestCase::~FiveDoublesTestCase
~FiveDoublesTestCase() override
Definition
basic-data-calculators-test-suite.cc:188
FiveIntegersTestCase
MinMaxAvgTotalCalculator class - Test case for five integers.
Definition
basic-data-calculators-test-suite.cc:99
FiveIntegersTestCase::FiveIntegersTestCase
FiveIntegersTestCase()
Definition
basic-data-calculators-test-suite.cc:108
FiveIntegersTestCase::~FiveIntegersTestCase
~FiveIntegersTestCase() override
Definition
basic-data-calculators-test-suite.cc:114
FiveIntegersTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
basic-data-calculators-test-suite.cc:119
OneIntegerTestCase
MinMaxAvgTotalCalculator class - Test case for a single integer.
Definition
basic-data-calculators-test-suite.cc:25
OneIntegerTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
basic-data-calculators-test-suite.cc:45
OneIntegerTestCase::OneIntegerTestCase
OneIntegerTestCase()
Definition
basic-data-calculators-test-suite.cc:34
OneIntegerTestCase::~OneIntegerTestCase
~OneIntegerTestCase() override
Definition
basic-data-calculators-test-suite.cc:40
ns3::MinMaxAvgTotalCalculator
Template class MinMaxAvgTotalCalculator.
Definition
basic-data-calculators.h:30
ns3::MinMaxAvgTotalCalculator::getCount
long getCount() const override
Returns the count.
Definition
basic-data-calculators.h:61
ns3::MinMaxAvgTotalCalculator::getVariance
double getVariance() const override
Returns the current variance.
Definition
basic-data-calculators.h:115
ns3::MinMaxAvgTotalCalculator::getMax
double getMax() const override
Returns the maximum value.
Definition
basic-data-calculators.h:88
ns3::MinMaxAvgTotalCalculator::getSqrSum
double getSqrSum() const override
Returns the sum of squares.
Definition
basic-data-calculators.h:124
ns3::MinMaxAvgTotalCalculator::getSum
double getSum() const override
Returns the sum.
Definition
basic-data-calculators.h:70
ns3::MinMaxAvgTotalCalculator::getStddev
double getStddev() const override
Returns the standard deviation.
Definition
basic-data-calculators.h:106
ns3::MinMaxAvgTotalCalculator::getMean
double getMean() const override
Returns the mean value.
Definition
basic-data-calculators.h:97
ns3::MinMaxAvgTotalCalculator::getMin
double getMin() const override
Returns the minimum value.
Definition
basic-data-calculators.h:79
ns3::MinMaxAvgTotalCalculator::Update
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
Definition
basic-data-calculators.h:196
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
NS_TEST_ASSERT_MSG_EQ_TOL
#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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TOLERANCE
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
Definition
rtt-estimator.cc:32
src
stats
test
basic-data-calculators-test-suite.cc
Generated on Fri Nov 8 2024 13:59:06 for ns-3 by
1.11.0