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
average-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) 2012 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 <math.h>
22
23
#include "ns3/test.h"
24
#include "ns3/average.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
OneIntegerAverageTestCase
:
public
TestCase
35
{
36
public
:
37
OneIntegerAverageTestCase
();
38
virtual
~
OneIntegerAverageTestCase
();
39
40
private
:
41
virtual
void
DoRun (
void
);
42
};
43
44
OneIntegerAverageTestCase::OneIntegerAverageTestCase
()
45
:
TestCase
(
"Average Object Test using One Integer"
)
46
47
{
48
}
49
50
OneIntegerAverageTestCase::~OneIntegerAverageTestCase
()
51
{
52
}
53
54
void
55
OneIntegerAverageTestCase::DoRun
(
void
)
56
{
57
Average<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 = sqrt (variance);
95
96
// Test the calculator.
97
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value wrong"
);
98
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(), min,
TOLERANCE
,
"Min value wrong"
);
99
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(), max,
TOLERANCE
,
"Max value wrong"
);
100
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
101
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
102
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value wrong"
);
103
}
104
105
106
// ===========================================================================
107
// Test case for five integers.
108
// ===========================================================================
109
110
class
FiveIntegersAverageTestCase
:
public
TestCase
111
{
112
public
:
113
FiveIntegersAverageTestCase
();
114
virtual
~FiveIntegersAverageTestCase
();
115
116
private
:
117
virtual
void
DoRun
(
void
);
118
};
119
120
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
()
121
:
TestCase
(
"Average Object Test using Five Integers"
)
122
123
{
124
}
125
126
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
()
127
{
128
}
129
130
void
131
FiveIntegersAverageTestCase::DoRun
(
void
)
132
{
133
Average<int>
calculator;
134
135
long
count = 5;
136
137
double
sum = 0;
138
double
sqrSum = 0;
139
double
min;
140
double
max;
141
double
mean;
142
double
stddev;
143
double
variance;
144
145
// Put all of the values into the calculator.
146
int
multiple = 5;
147
int
value;
148
for
(
long
i = 0; i < count; i++)
149
{
150
value = multiple * (i + 1);
151
152
calculator.
Update
(value);
153
154
sum += value;
155
sqrSum += value * value;
156
}
157
158
// Calculate the expected values for the statistical functions.
159
min = multiple;
160
max = multiple * count;
161
mean = sum / count;
162
if
(count == 1)
163
{
164
variance = 0;
165
}
166
else
167
{
168
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
169
}
170
stddev = sqrt (variance);
171
172
// Test the calculator.
173
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value wrong"
);
174
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(), min,
TOLERANCE
,
"Min value wrong"
);
175
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(), max,
TOLERANCE
,
"Max value wrong"
);
176
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
177
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
178
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value wrong"
);
179
}
180
181
182
// ===========================================================================
183
// Test case for five double values.
184
// ===========================================================================
185
186
class
FiveDoublesAverageTestCase
:
public
TestCase
187
{
188
public
:
189
FiveDoublesAverageTestCase
();
190
virtual
~FiveDoublesAverageTestCase
();
191
192
private
:
193
virtual
void
DoRun
(
void
);
194
};
195
196
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
()
197
:
TestCase
(
"Average Object Test using Five Double Values"
)
198
199
{
200
}
201
202
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
()
203
{
204
}
205
206
void
207
FiveDoublesAverageTestCase::DoRun
(
void
)
208
{
209
Average<double>
calculator;
210
211
long
count = 5;
212
213
double
sum = 0;
214
double
sqrSum = 0;
215
double
min;
216
double
max;
217
double
mean;
218
double
stddev;
219
double
variance;
220
221
// Put all of the values into the calculator.
222
double
multiple = 3.14;
223
double
value;
224
for
(
long
i = 0; i < count; i++)
225
{
226
value = multiple * (i + 1);
227
228
calculator.
Update
(value);
229
230
sum += value;
231
sqrSum += value * value;
232
}
233
234
// Calculate the expected values for the statistical functions.
235
min = multiple;
236
max = multiple * count;
237
mean = sum / count;
238
if
(count == 1)
239
{
240
variance = 0;
241
}
242
else
243
{
244
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
245
}
246
stddev = sqrt (variance);
247
248
// Test the calculator.
249
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value wrong"
);
250
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(), min,
TOLERANCE
,
"Min value wrong"
);
251
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(), max,
TOLERANCE
,
"Max value wrong"
);
252
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
253
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
254
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value wrong"
);
255
}
256
257
258
class
AverageTestSuite
:
public
TestSuite
259
{
260
public
:
261
AverageTestSuite
();
262
};
263
264
AverageTestSuite::AverageTestSuite
()
265
:
TestSuite
(
"average"
, UNIT)
266
{
267
AddTestCase
(
new
OneIntegerAverageTestCase
);
268
AddTestCase
(
new
FiveIntegersAverageTestCase
);
269
AddTestCase
(
new
FiveDoublesAverageTestCase
);
270
}
271
272
static
AverageTestSuite
averageTestSuite
;
src
tools
test
average-test-suite.cc
Generated on Tue Oct 9 2012 16:45:46 for ns-3 by
1.8.1.2