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 <cmath>
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
variance = 0;
87
stddev = std::sqrt (variance);
88
89
// Test the calculator.
90
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value wrong"
);
91
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(), min,
TOLERANCE
,
"Min value wrong"
);
92
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(), max,
TOLERANCE
,
"Max value wrong"
);
93
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
94
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
95
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value wrong"
);
96
}
97
98
99
// ===========================================================================
100
// Test case for five integers.
101
// ===========================================================================
102
103
class
FiveIntegersAverageTestCase
:
public
TestCase
104
{
105
public
:
106
FiveIntegersAverageTestCase
();
107
virtual
~FiveIntegersAverageTestCase
();
108
109
private
:
110
virtual
void
DoRun
(
void
);
111
};
112
113
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
()
114
:
TestCase
(
"Average Object Test using Five Integers"
)
115
116
{
117
}
118
119
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
()
120
{
121
}
122
123
void
124
FiveIntegersAverageTestCase::DoRun
(
void
)
125
{
126
Average<int>
calculator;
127
128
long
count = 5;
129
130
double
sum = 0;
131
double
sqrSum = 0;
132
double
min;
133
double
max;
134
double
mean;
135
double
stddev;
136
double
variance;
137
138
// Put all of the values into the calculator.
139
int
multiple = 5;
140
int
value;
141
for
(
long
i = 0; i < count; i++)
142
{
143
value = multiple * (i + 1);
144
145
calculator.
Update
(value);
146
147
sum += value;
148
sqrSum += value * value;
149
}
150
151
// Calculate the expected values for the statistical functions.
152
min = multiple;
153
max = multiple * count;
154
mean = sum / count;
155
variance = (count * sqrSum - sum * sum) / (count * (count - 1));
156
stddev = std::sqrt (variance);
157
158
// Test the calculator.
159
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Count
(), count,
TOLERANCE
,
"Count value wrong"
);
160
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(), min,
TOLERANCE
,
"Min value wrong"
);
161
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(), max,
TOLERANCE
,
"Max value wrong"
);
162
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
163
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
164
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value wrong"
);
165
}
166
167
168
// ===========================================================================
169
// Test case for five double values.
170
// ===========================================================================
171
172
class
FiveDoublesAverageTestCase
:
public
TestCase
173
{
174
public
:
175
FiveDoublesAverageTestCase
();
176
virtual
~FiveDoublesAverageTestCase
();
177
178
private
:
179
virtual
void
DoRun
(
void
);
180
};
181
182
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
()
183
:
TestCase
(
"Average Object Test using Five Double Values"
)
184
185
{
186
}
187
188
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
()
189
{
190
}
191
192
void
193
FiveDoublesAverageTestCase::DoRun
(
void
)
194
{
195
Average<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.
Count
(), count,
TOLERANCE
,
"Count value wrong"
);
229
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Min
(), min,
TOLERANCE
,
"Min value wrong"
);
230
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Max
(), max,
TOLERANCE
,
"Max value wrong"
);
231
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Mean
(), mean,
TOLERANCE
,
"Mean value wrong"
);
232
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Stddev
(), stddev,
TOLERANCE
,
"Stddev value wrong"
);
233
NS_TEST_ASSERT_MSG_EQ_TOL
(calculator.
Var
(), variance,
TOLERANCE
,
"Variance value wrong"
);
234
}
235
236
237
class
AverageTestSuite
:
public
TestSuite
238
{
239
public
:
240
AverageTestSuite
();
241
};
242
243
AverageTestSuite::AverageTestSuite
()
244
:
TestSuite
(
"average"
, UNIT)
245
{
246
AddTestCase
(
new
OneIntegerAverageTestCase
, TestCase::QUICK);
247
AddTestCase
(
new
FiveIntegersAverageTestCase
, TestCase::QUICK);
248
AddTestCase
(
new
FiveDoublesAverageTestCase
, TestCase::QUICK);
249
}
250
251
static
AverageTestSuite
averageTestSuite
;
ns3::Average::Max
T Max() const
Maximum.
Definition:
average.h:75
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:326
ns3::Average::Stddev
double Stddev() const
Standard deviation.
Definition:
average.h:83
ns3::Average::Mean
double Mean() const
Estimate of mean, alias to Avg.
Definition:
average.h:79
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1025
FiveIntegersAverageTestCase::~FiveIntegersAverageTestCase
virtual ~FiveIntegersAverageTestCase()
Definition:
average-test-suite.cc:119
FiveIntegersAverageTestCase
Definition:
average-test-suite.cc:103
AverageTestSuite
Definition:
average-test-suite.cc:237
ns3::TestCase
encapsulates test code
Definition:
test.h:849
FiveIntegersAverageTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:124
averageTestSuite
static AverageTestSuite averageTestSuite
Definition:
average-test-suite.cc:251
OneIntegerAverageTestCase
Definition:
average-test-suite.cc:34
FiveDoublesAverageTestCase::FiveDoublesAverageTestCase
FiveDoublesAverageTestCase()
Definition:
average-test-suite.cc:182
OneIntegerAverageTestCase::OneIntegerAverageTestCase
OneIntegerAverageTestCase()
Definition:
average-test-suite.cc:44
ns3::Average::Min
T Min() const
Minimum.
Definition:
average.h:73
OneIntegerAverageTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:55
TOLERANCE
const double TOLERANCE
Definition:
average-test-suite.cc:28
AverageTestSuite::AverageTestSuite
AverageTestSuite()
Definition:
average-test-suite.cc:243
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition:
test.cc:173
ns3::Average
Simple average, min, max and std.
Definition:
average.h:40
FiveDoublesAverageTestCase::~FiveDoublesAverageTestCase
virtual ~FiveDoublesAverageTestCase()
Definition:
average-test-suite.cc:188
OneIntegerAverageTestCase::~OneIntegerAverageTestCase
virtual ~OneIntegerAverageTestCase()
Definition:
average-test-suite.cc:50
FiveDoublesAverageTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
average-test-suite.cc:193
FiveIntegersAverageTestCase::FiveIntegersAverageTestCase
FiveIntegersAverageTestCase()
Definition:
average-test-suite.cc:113
FiveDoublesAverageTestCase
Definition:
average-test-suite.cc:172
ns3::Average::Count
uint32_t Count() const
Sample size.
Definition:
average.h:71
ns3::Average::Var
double Var() const
Unbiased estimate of variance.
Definition:
average.h:81
ns3::Average::Update
void Update(T const &x)
Add new sample.
Definition:
average.h:49
src
stats
test
average-test-suite.cc
Generated on Sat Apr 19 2014 14:07:09 for ns-3 by
1.8.6