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
random-variable-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) 2006 Georgia Tech Research Corporation
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: Rajib Bhattacharjea<raj.b@gatech.edu>
19
// Author: Hadi Arbabi<marbabi@cs.odu.edu>
20
//
21
22
#include <iostream>
23
#include <math.h>
24
25
#include "ns3/test.h"
26
#include "ns3/assert.h"
27
#include "ns3/integer.h"
28
#include "ns3/random-variable.h"
29
30
using namespace
std;
31
32
namespace
ns3 {
33
class
BasicRandomNumberTestCase
:
public
TestCase
34
{
35
public
:
36
BasicRandomNumberTestCase
();
37
virtual
~
BasicRandomNumberTestCase
()
38
{
39
}
40
41
private
:
42
virtual
void
DoRun (
void
);
43
};
44
45
BasicRandomNumberTestCase::BasicRandomNumberTestCase ()
46
:
TestCase
(
"Check basic random number operation"
)
47
{
48
}
49
50
void
51
BasicRandomNumberTestCase::DoRun
(
void
)
52
{
53
const
double
desiredMean = 1.0;
54
const
double
desiredStdDev = 1.0;
55
56
double
tmp = log (1 + (desiredStdDev / desiredMean) * (desiredStdDev / desiredMean));
57
double
sigma = sqrt (tmp);
58
double
mu = log (desiredMean) - 0.5 * tmp;
59
60
//
61
// Test a custom lognormal instance to see if its moments have any relation
62
// expected reality.
63
//
64
LogNormalVariable
lognormal (mu, sigma);
65
vector<double> samples;
66
const
int
NSAMPLES = 10000;
67
double
sum = 0;
68
69
//
70
// Get and store a bunch of samples. As we go along sum them and then find
71
// the mean value of the samples.
72
//
73
for
(
int
n = NSAMPLES; n; --n)
74
{
75
double
value = lognormal.
GetValue
();
76
sum += value;
77
samples.push_back (value);
78
}
79
double
obtainedMean = sum / NSAMPLES;
80
NS_TEST_EXPECT_MSG_EQ_TOL
(obtainedMean, desiredMean, 0.1,
"Got unexpected mean value from LogNormalVariable"
);
81
82
//
83
// Wander back through the saved stamples and find their standard deviation
84
//
85
sum = 0;
86
for
(vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
87
{
88
double
tmp = (*iter - obtainedMean);
89
sum += tmp * tmp;
90
}
91
double
obtainedStdDev = sqrt (sum / (NSAMPLES - 1));
92
NS_TEST_EXPECT_MSG_EQ_TOL
(obtainedStdDev, desiredStdDev, 0.1,
"Got unexpected standard deviation from LogNormalVariable"
);
93
}
94
95
class
RandomNumberSerializationTestCase
:
public
TestCase
96
{
97
public
:
98
RandomNumberSerializationTestCase
();
99
virtual
~RandomNumberSerializationTestCase
()
100
{
101
}
102
103
private
:
104
virtual
void
DoRun
(
void
);
105
};
106
107
RandomNumberSerializationTestCase::RandomNumberSerializationTestCase
()
108
:
TestCase
(
"Check basic random number operation"
)
109
{
110
}
111
112
void
113
RandomNumberSerializationTestCase::DoRun
(
void
)
114
{
115
RandomVariableValue
val;
116
val.
DeserializeFromString
(
"Uniform:0.1:0.2"
, MakeRandomVariableChecker ());
117
RandomVariable
rng
= val.
Get
();
118
NS_TEST_ASSERT_MSG_EQ
(val.
SerializeToString
(MakeRandomVariableChecker ()),
"Uniform:0.1:0.2"
,
119
"Deserialize and Serialize \"Uniform:0.1:0.2\" mismatch"
);
120
121
val.
DeserializeFromString
(
"Normal:0.1:0.2"
, MakeRandomVariableChecker ());
122
rng = val.
Get
();
123
NS_TEST_ASSERT_MSG_EQ
(val.
SerializeToString
(MakeRandomVariableChecker ()),
"Normal:0.1:0.2"
,
124
"Deserialize and Serialize \"Normal:0.1:0.2\" mismatch"
);
125
126
val.
DeserializeFromString
(
"Normal:0.1:0.2:0.15"
, MakeRandomVariableChecker ());
127
rng = val.
Get
();
128
NS_TEST_ASSERT_MSG_EQ
(val.
SerializeToString
(MakeRandomVariableChecker ()),
"Normal:0.1:0.2:0.15"
,
129
"Deserialize and Serialize \"Normal:0.1:0.2:0.15\" mismatch"
);
130
}
131
132
class
BasicRandomNumberTestSuite
:
public
TestSuite
133
{
134
public
:
135
BasicRandomNumberTestSuite
();
136
};
137
138
BasicRandomNumberTestSuite::BasicRandomNumberTestSuite
()
139
:
TestSuite
(
"basic-random-number"
, UNIT)
140
{
141
AddTestCase
(
new
BasicRandomNumberTestCase
);
142
AddTestCase
(
new
RandomNumberSerializationTestCase
);
143
}
144
145
static
BasicRandomNumberTestSuite
BasicRandomNumberTestSuite
;
146
147
}
// namespace ns3
src
core
test
random-variable-test-suite.cc
Generated on Tue Oct 9 2012 16:45:36 for ns-3 by
1.8.1.2