src/core/test/random-variable-test-suite.cc
changeset 9063 32755d0516f4
parent 7008 82c66d5c01f7
child 9079 1e5921e6507d
equal deleted inserted replaced
9062:d14e2430213d 9063:32755d0516f4
    18 // Author: Rajib Bhattacharjea<raj.b@gatech.edu>
    18 // Author: Rajib Bhattacharjea<raj.b@gatech.edu>
    19 // Author: Hadi Arbabi<marbabi@cs.odu.edu>
    19 // Author: Hadi Arbabi<marbabi@cs.odu.edu>
    20 //
    20 //
    21 
    21 
    22 #include <iostream>
    22 #include <iostream>
    23 #include <math.h>
    23 #include <cmath>
       
    24 #include <vector>
    24 
    25 
    25 #include "ns3/test.h"
    26 #include "ns3/test.h"
    26 #include "ns3/assert.h"
    27 #include "ns3/assert.h"
    27 #include "ns3/integer.h"
    28 #include "ns3/integer.h"
    28 #include "ns3/random-variable.h"
    29 #include "ns3/random-variable.h"
    29 
       
    30 using namespace std;
       
    31 
    30 
    32 namespace ns3 {
    31 namespace ns3 {
    33 class BasicRandomNumberTestCase : public TestCase
    32 class BasicRandomNumberTestCase : public TestCase
    34 {
    33 {
    35 public:
    34 public:
    51 BasicRandomNumberTestCase::DoRun (void)
    50 BasicRandomNumberTestCase::DoRun (void)
    52 {
    51 {
    53   const double desiredMean = 1.0;
    52   const double desiredMean = 1.0;
    54   const double desiredStdDev = 1.0;
    53   const double desiredStdDev = 1.0;
    55 
    54 
    56   double tmp = log (1 + (desiredStdDev / desiredMean) * (desiredStdDev / desiredMean));
    55   double tmp = std::log (1 + (desiredStdDev / desiredMean) * (desiredStdDev / desiredMean));
    57   double sigma = sqrt (tmp);
    56   double sigma = std::sqrt (tmp);
    58   double mu = log (desiredMean) - 0.5 * tmp;
    57   double mu = std::log (desiredMean) - 0.5 * tmp;
    59 
    58 
    60   //
    59   //
    61   // Test a custom lognormal instance to see if its moments have any relation
    60   // Test a custom lognormal instance to see if its moments have any relation
    62   // expected reality.
    61   // expected reality.
    63   //
    62   //
    64   LogNormalVariable lognormal (mu, sigma);
    63   LogNormalVariable lognormal (mu, sigma);
    65   vector<double> samples;
    64   std::vector<double> samples;
    66   const int NSAMPLES = 10000;
    65   const int NSAMPLES = 10000;
    67   double sum = 0;
    66   double sum = 0;
    68 
    67 
    69   //
    68   //
    70   // Get and store a bunch of samples.  As we go along sum them and then find
    69   // Get and store a bunch of samples.  As we go along sum them and then find
    81 
    80 
    82   //
    81   //
    83   // Wander back through the saved stamples and find their standard deviation
    82   // Wander back through the saved stamples and find their standard deviation
    84   //
    83   //
    85   sum = 0;
    84   sum = 0;
    86   for (vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
    85   for (std::vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
    87     {
    86     {
    88       double tmp = (*iter - obtainedMean);
    87       double tmp = (*iter - obtainedMean);
    89       sum += tmp * tmp;
    88       sum += tmp * tmp;
    90     }
    89     }
    91   double obtainedStdDev = sqrt (sum / (NSAMPLES - 1));
    90   double obtainedStdDev = std::sqrt (sum / (NSAMPLES - 1));
    92   NS_TEST_EXPECT_MSG_EQ_TOL (obtainedStdDev, desiredStdDev, 0.1, "Got unexpected standard deviation from LogNormalVariable");
    91   NS_TEST_EXPECT_MSG_EQ_TOL (obtainedStdDev, desiredStdDev, 0.1, "Got unexpected standard deviation from LogNormalVariable");
    93 }
    92 }
    94 
    93 
    95 class RandomNumberSerializationTestCase : public TestCase
    94 class RandomNumberSerializationTestCase : public TestCase
    96 {
    95 {