A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
stats
test
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
30
// ===========================================================================
31
// Test case for a single integer.
32
// ===========================================================================
33
34
class
OneIntegerTestCase
:
public
TestCase
35
{
36
public
:
37
OneIntegerTestCase
();
38
virtual
~
OneIntegerTestCase
();
39
40
private
:
41
virtual
void
DoRun (
void
);
42
};
43
44
OneIntegerTestCase::OneIntegerTestCase
()
45
:
TestCase
(
"Basic Statistical Functions using One Integer"
)
46
47
{
48
}
49
50
OneIntegerTestCase::~OneIntegerTestCase
()
51
{
52
}
53
54
void
55
OneIntegerTestCase::DoRun
(
void
)
56
{
57
MinMaxAvgTotalCalculator<int>
calculator;
58
59
long
count = 1;
60
61
double
sum = 0;
62
double
sqrSum = 0;
63
double
min
;
64
double
max
;
65
double
mean;
66
double
stddev;
67
double
variance;
68
69
// Put all of the values into the calculator.
70
int
multiple = 5;
71
int
value;
72
for
(
long
i = 0; i < count; i++)
73
{
74
value = multiple * (i + 1);
75
76
calculator.
Update
(value);
77
78
sum += value;
79
sqrSum += value * value;
80
}
81
82
// Calculate the expected values for the statistical functions.
83
min
= multiple;
84
max
= multiple * count;
85
mean = sum / count;
86
variance = 0;
87
stddev = std::sqrt (variance);
88
89
// Test the calculator.
90
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getCount
(), count,
TOLERANCE
,
"Count value wrong"
);
91
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSum
(), sum,
TOLERANCE
,
"Sum value wrong"
);
92
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMin
(),
min
,
TOLERANCE
,
"Min value wrong"
);
93
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMax
(),
max
,
TOLERANCE
,
"Max value wrong"
);
94
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
95
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getStddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
96
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(), variance,
TOLERANCE
,
"Variance value wrong"
);
97
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSqrSum
(), sqrSum,
TOLERANCE
,
"SqrSum value wrong"
);
98
}
99
100
101
// ===========================================================================
102
// Test case for five integers.
103
// ===========================================================================
104
105
class
FiveIntegersTestCase
:
public
TestCase
106
{
107
public
:
108
FiveIntegersTestCase
();
109
virtual
~FiveIntegersTestCase
();
110
111
private
:
112
virtual
void
DoRun
(
void
);
113
};
114
115
FiveIntegersTestCase::FiveIntegersTestCase
()
116
:
TestCase
(
"Basic Statistical Functions using Five Integers"
)
117
118
{
119
}
120
121
FiveIntegersTestCase::~FiveIntegersTestCase
()
122
{
123
}
124
125
void
126
FiveIntegersTestCase::DoRun
(
void
)
127
{
128
MinMaxAvgTotalCalculator<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.
getCount
(), count,
TOLERANCE
,
"Count value wrong"
);
162
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSum
(), sum,
TOLERANCE
,
"Sum value wrong"
);
163
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMin
(),
min
,
TOLERANCE
,
"Min value wrong"
);
164
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMax
(),
max
,
TOLERANCE
,
"Max value wrong"
);
165
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
166
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getStddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
167
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(), variance,
TOLERANCE
,
"Variance value wrong"
);
168
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSqrSum
(), sqrSum,
TOLERANCE
,
"SqrSum value wrong"
);
169
}
170
171
172
// ===========================================================================
173
// Test case for five double values.
174
// ===========================================================================
175
176
class
FiveDoublesTestCase
:
public
TestCase
177
{
178
public
:
179
FiveDoublesTestCase
();
180
virtual
~FiveDoublesTestCase
();
181
182
private
:
183
virtual
void
DoRun
(
void
);
184
};
185
186
FiveDoublesTestCase::FiveDoublesTestCase
()
187
:
TestCase
(
"Basic Statistical Functions using Five Double Values"
)
188
189
{
190
}
191
192
FiveDoublesTestCase::~FiveDoublesTestCase
()
193
{
194
}
195
196
void
197
FiveDoublesTestCase::DoRun
(
void
)
198
{
199
MinMaxAvgTotalCalculator<double>
calculator;
200
201
long
count = 5;
202
203
double
sum = 0;
204
double
sqrSum = 0;
205
double
min
;
206
double
max
;
207
double
mean;
208
double
stddev;
209
double
variance;
210
211
// Put all of the values into the calculator.
212
double
multiple = 3.14;
213
double
value;
214
for
(
long
i = 0; i < count; i++)
215
{
216
value = multiple * (i + 1);
217
218
calculator.
Update
(value);
219
220
sum += value;
221
sqrSum += value * value;
222
}
223
224
// Calculate the expected values for the statistical functions.
225
min
= multiple;
226
max
= multiple * count;
227
mean = sum / count;
228
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
229
stddev = std::sqrt (variance);
230
231
// Test the calculator.
232
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getCount
(), count,
TOLERANCE
,
"Count value wrong"
);
233
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSum
(), sum,
TOLERANCE
,
"Sum value wrong"
);
234
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMin
(),
min
,
TOLERANCE
,
"Min value wrong"
);
235
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMax
(),
max
,
TOLERANCE
,
"Max value wrong"
);
236
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
237
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getStddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
238
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(), variance,
TOLERANCE
,
"Variance value wrong"
);
239
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSqrSum
(), sqrSum,
TOLERANCE
,
"SqrSum value wrong"
);
240
}
241
242
243
class
BasicDataCalculatorsTestSuite
:
public
TestSuite
244
{
245
public
:
246
BasicDataCalculatorsTestSuite
();
247
};
248
249
BasicDataCalculatorsTestSuite::BasicDataCalculatorsTestSuite
()
250
:
TestSuite
(
"basic-data-calculators"
, UNIT)
251
{
252
AddTestCase
(
new
OneIntegerTestCase
, TestCase::QUICK);
253
AddTestCase
(
new
FiveIntegersTestCase
, TestCase::QUICK);
254
AddTestCase
(
new
FiveDoublesTestCase
, TestCase::QUICK);
255
}
256
257
static
BasicDataCalculatorsTestSuite
basicDataCalculatorsTestSuite
;
ns3::MinMaxAvgTotalCalculator::getSum
double getSum() const
Returns the sum.
Definition:
basic-data-calculators.h:76
min
#define min(a, b)
Definition:
80211b.c:42
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1342
ns3::MinMaxAvgTotalCalculator::getMin
double getMin() const
Returns the minimum value.
Definition:
basic-data-calculators.h:81
TOLERANCE
const double TOLERANCE
Definition:
basic-data-calculators-test-suite.cc:28
ns3::MinMaxAvgTotalCalculator::getVariance
double getVariance() const
Returns the current variance.
Definition:
basic-data-calculators.h:101
FiveIntegersTestCase::~FiveIntegersTestCase
virtual ~FiveIntegersTestCase()
Definition:
basic-data-calculators-test-suite.cc:121
OneIntegerTestCase::~OneIntegerTestCase
virtual ~OneIntegerTestCase()
Definition:
basic-data-calculators-test-suite.cc:50
ns3::TestCase
encapsulates test code
Definition:
test.h:1155
ns3::MinMaxAvgTotalCalculator::getSqrSum
double getSqrSum() const
Returns the sum of squares.
Definition:
basic-data-calculators.h:106
FiveDoublesTestCase::FiveDoublesTestCase
FiveDoublesTestCase()
Definition:
basic-data-calculators-test-suite.cc:186
ns3::MinMaxAvgTotalCalculator::getMax
double getMax() const
Returns the maximum value.
Definition:
basic-data-calculators.h:86
BasicDataCalculatorsTestSuite
Definition:
basic-data-calculators-test-suite.cc:243
max
#define max(a, b)
Definition:
80211b.c:43
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
OneIntegerTestCase::OneIntegerTestCase
OneIntegerTestCase()
Definition:
basic-data-calculators-test-suite.cc:44
basicDataCalculatorsTestSuite
static BasicDataCalculatorsTestSuite basicDataCalculatorsTestSuite
Definition:
basic-data-calculators-test-suite.cc:257
FiveIntegersTestCase::FiveIntegersTestCase
FiveIntegersTestCase()
Definition:
basic-data-calculators-test-suite.cc:115
OneIntegerTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
basic-data-calculators-test-suite.cc:55
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:380
OneIntegerTestCase
Definition:
basic-data-calculators-test-suite.cc:34
FiveIntegersTestCase
Definition:
basic-data-calculators-test-suite.cc:105
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
FiveDoublesTestCase
Definition:
basic-data-calculators-test-suite.cc:176
ns3::MinMaxAvgTotalCalculator
Template class MinMaxAvgTotalCalculator.
Definition:
basic-data-calculators.h:39
ns3::MinMaxAvgTotalCalculator::Update
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
Definition:
basic-data-calculators.h:175
FiveIntegersTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
basic-data-calculators-test-suite.cc:126
FiveDoublesTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
basic-data-calculators-test-suite.cc:197
ns3::MinMaxAvgTotalCalculator::getCount
long getCount() const
Returns the count.
Definition:
basic-data-calculators.h:71
ns3::TOLERANCE
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
Definition:
rtt-estimator.cc:42
ns3::MinMaxAvgTotalCalculator::getMean
double getMean() const
Returns the mean value.
Definition:
basic-data-calculators.h:91
BasicDataCalculatorsTestSuite::BasicDataCalculatorsTestSuite
BasicDataCalculatorsTestSuite()
Definition:
basic-data-calculators-test-suite.cc:249
ns3::MinMaxAvgTotalCalculator::getStddev
double getStddev() const
Returns the standard deviation.
Definition:
basic-data-calculators.h:96
FiveDoublesTestCase::~FiveDoublesTestCase
virtual ~FiveDoublesTestCase()
Definition:
basic-data-calculators-test-suite.cc:192
Generated on Wed Nov 7 2018 10:02:10 for ns-3 by
1.8.14