A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
random-variable-test-suite.cc
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 <cmath>
24 #include <vector>
25 
26 #include "ns3/test.h"
27 #include "ns3/assert.h"
28 #include "ns3/integer.h"
29 #include "ns3/random-variable.h"
30 
31 using namespace ns3;
32 
34 {
35 public:
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
52 {
53  const double desiredMean = 1.0;
54  const double desiredStdDev = 1.0;
55 
56  double tmp = std::log (1 + (desiredStdDev / desiredMean) * (desiredStdDev / desiredMean));
57  double sigma = std::sqrt (tmp);
58  double mu = std::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  std::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 (std::vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
87  {
88  double tmp = (*iter - obtainedMean);
89  sum += tmp * tmp;
90  }
91  double obtainedStdDev = std::sqrt (sum / (NSAMPLES - 1));
92  NS_TEST_EXPECT_MSG_EQ_TOL (obtainedStdDev, desiredStdDev, 0.1, "Got unexpected standard deviation from LogNormalVariable");
93 }
94 
96 {
97 public:
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
114 {
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 
133 {
134 public:
136 };
137 
138 BasicRandomNumberTestSuite::BasicRandomNumberTestSuite ()
139  : TestSuite ("basic-random-number", UNIT)
140 {
141  AddTestCase (new BasicRandomNumberTestCase, TestCase::QUICK);
142  AddTestCase (new RandomNumberSerializationTestCase, TestCase::QUICK);
143 }
144 
A suite of tests to run.
Definition: test.h:962
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
encapsulates test code
Definition: test.h:834
hold objects of type ns3::RandomVariable
Log-normal Distributed random varLogNormalVariable defines a random variable with log-normal distribu...
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
The basic RNG for NS-3.Note: The underlying random number generation method used by NS-3 is the RngSt...
double GetValue(void) const
Returns a random double from the underlying distribution.