A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
if
(count == 1)
87
{
88
variance = 0;
89
}
90
else
91
{
92
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
93
}
94
stddev = std::sqrt (variance);
95
96
// Test the calculator.
97
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getCount
(), count,
TOLERANCE
,
"Count value wrong"
);
98
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSum
(), sum,
TOLERANCE
,
"Sum value wrong"
);
99
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMin
(), min,
TOLERANCE
,
"Min value wrong"
);
100
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMax
(), max,
TOLERANCE
,
"Max value wrong"
);
101
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
102
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getStddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
103
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(), variance,
TOLERANCE
,
"Variance value wrong"
);
104
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSqrSum
(), sqrSum,
TOLERANCE
,
"SqrSum value wrong"
);
105
}
106
107
108
// ===========================================================================
109
// Test case for five integers.
110
// ===========================================================================
111
112
class
FiveIntegersTestCase
:
public
TestCase
113
{
114
public
:
115
FiveIntegersTestCase
();
116
virtual
~FiveIntegersTestCase
();
117
118
private
:
119
virtual
void
DoRun
(
void
);
120
};
121
122
FiveIntegersTestCase::FiveIntegersTestCase
()
123
:
TestCase
(
"Basic Statistical Functions using Five Integers"
)
124
125
{
126
}
127
128
FiveIntegersTestCase::~FiveIntegersTestCase
()
129
{
130
}
131
132
void
133
FiveIntegersTestCase::DoRun
(
void
)
134
{
135
MinMaxAvgTotalCalculator<int>
calculator;
136
137
long
count = 5;
138
139
double
sum = 0;
140
double
sqrSum = 0;
141
double
min;
142
double
max;
143
double
mean;
144
double
stddev;
145
double
variance;
146
147
// Put all of the values into the calculator.
148
int
multiple = 5;
149
int
value;
150
for
(
long
i = 0; i < count; i++)
151
{
152
value = multiple * (i + 1);
153
154
calculator.
Update
(value);
155
156
sum += value;
157
sqrSum += value * value;
158
}
159
160
// Calculate the expected values for the statistical functions.
161
min = multiple;
162
max = multiple * count;
163
mean = sum / count;
164
if
(count == 1)
165
{
166
variance = 0;
167
}
168
else
169
{
170
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
171
}
172
stddev = std::sqrt (variance);
173
174
// Test the calculator.
175
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getCount
(), count,
TOLERANCE
,
"Count value wrong"
);
176
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSum
(), sum,
TOLERANCE
,
"Sum value wrong"
);
177
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMin
(), min,
TOLERANCE
,
"Min value wrong"
);
178
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMax
(), max,
TOLERANCE
,
"Max value wrong"
);
179
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
180
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getStddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
181
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(), variance,
TOLERANCE
,
"Variance value wrong"
);
182
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSqrSum
(), sqrSum,
TOLERANCE
,
"SqrSum value wrong"
);
183
}
184
185
186
// ===========================================================================
187
// Test case for five double values.
188
// ===========================================================================
189
190
class
FiveDoublesTestCase
:
public
TestCase
191
{
192
public
:
193
FiveDoublesTestCase
();
194
virtual
~FiveDoublesTestCase
();
195
196
private
:
197
virtual
void
DoRun
(
void
);
198
};
199
200
FiveDoublesTestCase::FiveDoublesTestCase
()
201
:
TestCase
(
"Basic Statistical Functions using Five Double Values"
)
202
203
{
204
}
205
206
FiveDoublesTestCase::~FiveDoublesTestCase
()
207
{
208
}
209
210
void
211
FiveDoublesTestCase::DoRun
(
void
)
212
{
213
MinMaxAvgTotalCalculator<double>
calculator;
214
215
long
count = 5;
216
217
double
sum = 0;
218
double
sqrSum = 0;
219
double
min;
220
double
max;
221
double
mean;
222
double
stddev;
223
double
variance;
224
225
// Put all of the values into the calculator.
226
double
multiple = 3.14;
227
double
value;
228
for
(
long
i = 0; i < count; i++)
229
{
230
value = multiple * (i + 1);
231
232
calculator.
Update
(value);
233
234
sum += value;
235
sqrSum += value * value;
236
}
237
238
// Calculate the expected values for the statistical functions.
239
min = multiple;
240
max = multiple * count;
241
mean = sum / count;
242
if
(count == 1)
243
{
244
variance = 0;
245
}
246
else
247
{
248
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
249
}
250
stddev = std::sqrt (variance);
251
252
// Test the calculator.
253
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getCount
(), count,
TOLERANCE
,
"Count value wrong"
);
254
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSum
(), sum,
TOLERANCE
,
"Sum value wrong"
);
255
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMin
(), min,
TOLERANCE
,
"Min value wrong"
);
256
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMax
(), max,
TOLERANCE
,
"Max value wrong"
);
257
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getMean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
258
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getStddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
259
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getVariance
(), variance,
TOLERANCE
,
"Variance value wrong"
);
260
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
getSqrSum
(), sqrSum,
TOLERANCE
,
"SqrSum value wrong"
);
261
}
262
263
264
class
BasicDataCalculatorsTestSuite
:
public
TestSuite
265
{
266
public
:
267
BasicDataCalculatorsTestSuite
();
268
};
269
270
BasicDataCalculatorsTestSuite::BasicDataCalculatorsTestSuite
()
271
:
TestSuite
(
"basic-data-calculators"
, UNIT)
272
{
273
AddTestCase
(
new
OneIntegerTestCase
, TestCase::QUICK);
274
AddTestCase
(
new
FiveIntegersTestCase
, TestCase::QUICK);
275
AddTestCase
(
new
FiveDoublesTestCase
, TestCase::QUICK);
276
}
277
278
static
BasicDataCalculatorsTestSuite
basicDataCalculatorsTestSuite
;
src
stats
test
basic-data-calculators-test-suite.cc
Generated on Tue May 14 2013 11:08:33 for ns-3 by
1.8.1.2