A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
35
class
OneIntegerTestCase
:
public
TestCase
36
{
37
public
:
38
OneIntegerTestCase
();
39
virtual
~OneIntegerTestCase
();
40
41
private
:
42
virtual
void
DoRun
(
void
);
43
};
44
45
OneIntegerTestCase::OneIntegerTestCase
()
46
:
TestCase
(
"Basic Statistical Functions using One Integer"
)
47
48
{
49
}
50
51
OneIntegerTestCase::~OneIntegerTestCase
()
52
{
53
}
54
55
void
56
OneIntegerTestCase::DoRun
(
void
)
57
{
58
MinMaxAvgTotalCalculator<int>
calculator;
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
107
class
FiveIntegersTestCase
:
public
TestCase
108
{
109
public
:
110
FiveIntegersTestCase
();
111
virtual
~FiveIntegersTestCase
();
112
113
private
:
114
virtual
void
DoRun
(
void
);
115
};
116
117
FiveIntegersTestCase::FiveIntegersTestCase
()
118
:
TestCase
(
"Basic Statistical Functions using Five Integers"
)
119
120
{
121
}
122
123
FiveIntegersTestCase::~FiveIntegersTestCase
()
124
{
125
}
126
127
void
128
FiveIntegersTestCase::DoRun
(
void
)
129
{
130
MinMaxAvgTotalCalculator<int>
calculator;
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
179
class
FiveDoublesTestCase
:
public
TestCase
180
{
181
public
:
182
FiveDoublesTestCase
();
183
virtual
~FiveDoublesTestCase
();
184
185
private
:
186
virtual
void
DoRun
(
void
);
187
};
188
189
FiveDoublesTestCase::FiveDoublesTestCase
()
190
:
TestCase
(
"Basic Statistical Functions using Five Double Values"
)
191
192
{
193
}
194
195
FiveDoublesTestCase::~FiveDoublesTestCase
()
196
{
197
}
198
199
void
200
FiveDoublesTestCase::DoRun
(
void
)
201
{
202
MinMaxAvgTotalCalculator<double>
calculator;
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
251
class
BasicDataCalculatorsTestSuite
:
public
TestSuite
252
{
253
public
:
254
BasicDataCalculatorsTestSuite
();
255
};
256
257
BasicDataCalculatorsTestSuite::BasicDataCalculatorsTestSuite
()
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
266
static
BasicDataCalculatorsTestSuite
basicDataCalculatorsTestSuite
;
min
#define min(a, b)
Definition:
80211b.c:42
max
#define max(a, b)
Definition:
80211b.c:43
basicDataCalculatorsTestSuite
static BasicDataCalculatorsTestSuite basicDataCalculatorsTestSuite
Static variable for test initialization.
Definition:
basic-data-calculators-test-suite.cc:266
TOLERANCE
const double TOLERANCE
Definition:
basic-data-calculators-test-suite.cc:28
BasicDataCalculatorsTestSuite
MinMaxAvgTotalCalculator class TestSuite.
Definition:
basic-data-calculators-test-suite.cc:252
BasicDataCalculatorsTestSuite::BasicDataCalculatorsTestSuite
BasicDataCalculatorsTestSuite()
Definition:
basic-data-calculators-test-suite.cc:257
FiveDoublesTestCase
MinMaxAvgTotalCalculator class - Test case for five double values.
Definition:
basic-data-calculators-test-suite.cc:180
FiveDoublesTestCase::~FiveDoublesTestCase
virtual ~FiveDoublesTestCase()
Definition:
basic-data-calculators-test-suite.cc:195
FiveDoublesTestCase::FiveDoublesTestCase
FiveDoublesTestCase()
Definition:
basic-data-calculators-test-suite.cc:189
FiveDoublesTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
basic-data-calculators-test-suite.cc:200
FiveIntegersTestCase
MinMaxAvgTotalCalculator class - Test case for five integers.
Definition:
basic-data-calculators-test-suite.cc:108
FiveIntegersTestCase::FiveIntegersTestCase
FiveIntegersTestCase()
Definition:
basic-data-calculators-test-suite.cc:117
FiveIntegersTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
basic-data-calculators-test-suite.cc:128
FiveIntegersTestCase::~FiveIntegersTestCase
virtual ~FiveIntegersTestCase()
Definition:
basic-data-calculators-test-suite.cc:123
OneIntegerTestCase
MinMaxAvgTotalCalculator class - Test case for a single integer.
Definition:
basic-data-calculators-test-suite.cc:36
OneIntegerTestCase::OneIntegerTestCase
OneIntegerTestCase()
Definition:
basic-data-calculators-test-suite.cc:45
OneIntegerTestCase::~OneIntegerTestCase
virtual ~OneIntegerTestCase()
Definition:
basic-data-calculators-test-suite.cc:51
OneIntegerTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
basic-data-calculators-test-suite.cc:56
ns3::MinMaxAvgTotalCalculator
Template class MinMaxAvgTotalCalculator.
Definition:
basic-data-calculators.h:40
ns3::MinMaxAvgTotalCalculator::getSqrSum
double getSqrSum() const
Returns the sum of squares.
Definition:
basic-data-calculators.h:106
ns3::MinMaxAvgTotalCalculator::getCount
long getCount() const
Returns the count.
Definition:
basic-data-calculators.h:71
ns3::MinMaxAvgTotalCalculator::getSum
double getSum() const
Returns the sum.
Definition:
basic-data-calculators.h:76
ns3::MinMaxAvgTotalCalculator::getStddev
double getStddev() const
Returns the standard deviation.
Definition:
basic-data-calculators.h:96
ns3::MinMaxAvgTotalCalculator::getMean
double getMean() const
Returns the mean value.
Definition:
basic-data-calculators.h:91
ns3::MinMaxAvgTotalCalculator::getVariance
double getVariance() const
Returns the current variance.
Definition:
basic-data-calculators.h:101
ns3::MinMaxAvgTotalCalculator::getMax
double getMax() const
Returns the maximum value.
Definition:
basic-data-calculators.h:86
ns3::MinMaxAvgTotalCalculator::getMin
double getMin() const
Returns the minimum value.
Definition:
basic-data-calculators.h:81
ns3::MinMaxAvgTotalCalculator::Update
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
Definition:
basic-data-calculators.h:178
ns3::TestCase
encapsulates test code
Definition:
test.h:994
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1188
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:323
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
stats
test
basic-data-calculators-test-suite.cc
Generated on Sun May 1 2022 12:01:42 for ns-3 by
1.9.3