merge ns-3.0.2
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sun, 20 May 2007 14:38:53 +0200
changeset 6677ac5a4b0969b
parent 666 f29615cccd05
parent 665 6eb8f44439b0
child 668 2df1d1f5778f
child 673 f19556233407
merge
     1.1 --- a/src/core/ptr.h	Sun May 20 14:34:46 2007 +0200
     1.2 +++ b/src/core/ptr.h	Sun May 20 14:38:53 2007 +0200
     1.3 @@ -73,11 +73,12 @@
     1.4    /**
     1.5     * \param ptr raw pointer to manage
     1.6     *
     1.7 -   * Create a smart pointer which points to the
     1.8 -   * input raw pointer. This method takes ownershipt
     1.9 -   * of the input raw pointer. That is, the smart pointer
    1.10 -   * becomes responsible for calling delete on the
    1.11 -   * raw pointer when needed.
    1.12 +   * Create a smart pointer which points to the object pointed to by
    1.13 +   * the input raw pointer ptr. This method creates its own reference
    1.14 +   * to the pointed object. The caller is responsible for Unref()'ing
    1.15 +   * its own reference, and the smart pointer will eventually do the
    1.16 +   * same, so that object is deleted if no more references to it
    1.17 +   * remain.
    1.18     */
    1.19    Ptr (T *ptr);
    1.20    Ptr (Ptr const&o);
     2.1 --- a/src/core/random-variable.cc	Sun May 20 14:34:46 2007 +0200
     2.2 +++ b/src/core/random-variable.cc	Sun May 20 14:38:53 2007 +0200
     2.3 @@ -663,7 +663,7 @@
     2.4    return z;
     2.5  }
     2.6  
     2.7 -double LogNormalVariable::GetSingleValue(double sigma,double mu)
     2.8 +double LogNormalVariable::GetSingleValue (double mu, double sigma)
     2.9  {
    2.10    double u, v, r2, normal, z;
    2.11    do
    2.12 @@ -686,3 +686,104 @@
    2.13  
    2.14  }//namespace ns3
    2.15  
    2.16 +
    2.17 +#ifdef RUN_SELF_TESTS
    2.18 +#include "test.h"
    2.19 +#include <vector>
    2.20 +
    2.21 +namespace ns3 {
    2.22 +
    2.23 +
    2.24 +class RandomVariableTest : public Test
    2.25 +{
    2.26 +public:
    2.27 +  RandomVariableTest () : Test ("RandomVariable") {}
    2.28 +  virtual bool RunTests (void)
    2.29 +  {
    2.30 +    bool ok = true;
    2.31 +    const double desired_mean = 1.0;
    2.32 +    const double desired_stddev = 1.0;
    2.33 +    double tmp = log (1 + (desired_stddev/desired_mean)*(desired_stddev/desired_mean));
    2.34 +    double sigma = sqrt (tmp);
    2.35 +    double mu = log (desired_mean) - 0.5*tmp;
    2.36 +
    2.37 +    // Test a custom lognormal instance
    2.38 +    {
    2.39 +      LogNormalVariable lognormal (mu, sigma);
    2.40 +      vector<double> samples;
    2.41 +      const int NSAMPLES = 10000;
    2.42 +      double sum = 0;
    2.43 +      for (int n = NSAMPLES; n; --n)
    2.44 +        {
    2.45 +          double value = lognormal.GetValue ();
    2.46 +          sum += value;
    2.47 +          samples.push_back (value);
    2.48 +        }
    2.49 +      double obtained_mean = sum / NSAMPLES;
    2.50 +      sum = 0;
    2.51 +      for (vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
    2.52 +        {
    2.53 +          double tmp = (*iter - obtained_mean);
    2.54 +          sum += tmp*tmp;
    2.55 +        }
    2.56 +      double obtained_stddev = sqrt (sum / (NSAMPLES - 1));
    2.57 +
    2.58 +      if (not (obtained_mean/desired_mean > 0.90 and obtained_mean/desired_mean < 1.10))
    2.59 +        {
    2.60 +          ok = false;
    2.61 +          Failure () << "Obtained lognormal mean value " << obtained_mean << ", expected " << desired_mean << std::endl;
    2.62 +        }
    2.63 +
    2.64 +      if (not (obtained_stddev/desired_stddev > 0.90 and obtained_stddev/desired_stddev < 1.10))
    2.65 +        {
    2.66 +          ok = false;
    2.67 +          Failure () << "Obtained lognormal stddev value " << obtained_stddev <<
    2.68 +            ", expected " << desired_stddev << std::endl;
    2.69 +        }
    2.70 +    }
    2.71 +
    2.72 +    // Test GetSingleValue
    2.73 +    {
    2.74 +      vector<double> samples;
    2.75 +      const int NSAMPLES = 10000;
    2.76 +      double sum = 0;
    2.77 +      for (int n = NSAMPLES; n; --n)
    2.78 +        {
    2.79 +          double value = LogNormalVariable::GetSingleValue (mu, sigma);
    2.80 +          sum += value;
    2.81 +          samples.push_back (value);
    2.82 +        }
    2.83 +      double obtained_mean = sum / NSAMPLES;
    2.84 +      sum = 0;
    2.85 +      for (vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
    2.86 +        {
    2.87 +          double tmp = (*iter - obtained_mean);
    2.88 +          sum += tmp*tmp;
    2.89 +        }
    2.90 +      double obtained_stddev = sqrt (sum / (NSAMPLES - 1));
    2.91 +
    2.92 +      if (not (obtained_mean/desired_mean > 0.90 and obtained_mean/desired_mean < 1.10))
    2.93 +        {
    2.94 +          ok = false;
    2.95 +          Failure () << "Obtained LogNormalVariable::GetSingleValue mean value " << obtained_mean
    2.96 +                     << ", expected " << desired_mean << std::endl;
    2.97 +        }
    2.98 +
    2.99 +      if (not (obtained_stddev/desired_stddev > 0.90 and obtained_stddev/desired_stddev < 1.10))
   2.100 +        {
   2.101 +          ok = false;
   2.102 +          Failure () << "Obtained LogNormalVariable::GetSingleValue stddev value " << obtained_stddev <<
   2.103 +            ", expected " << desired_stddev << std::endl;
   2.104 +        }
   2.105 +    }
   2.106 +
   2.107 +    return ok;
   2.108 +  }
   2.109 +};
   2.110 +
   2.111 +
   2.112 +static RandomVariableTest g_random_variable_tests;
   2.113 +
   2.114 +}//namespace ns3
   2.115 +
   2.116 +#endif /* RUN_SELF_TESTS */