--- a/src/core/random-variable.cc Wed Feb 25 13:10:22 2009 +0100
+++ b/src/core/random-variable.cc Wed Feb 25 13:40:06 2009 +0100
@@ -1337,3 +1337,78 @@
}//namespace ns3
+
+
+#ifdef RUN_SELF_TESTS
+#include "test.h"
+#include <vector>
+
+namespace ns3 {
+
+
+class RandomVariableTest : public Test
+{
+public:
+ RandomVariableTest () : Test ("RandomVariable") {}
+ virtual bool RunTests (void)
+ {
+ bool result = true;
+ const double desired_mean = 1.0;
+ const double desired_stddev = 1.0;
+ double tmp = log (1 + (desired_stddev/desired_mean)*(desired_stddev/desired_mean));
+ double sigma = sqrt (tmp);
+ double mu = log (desired_mean) - 0.5*tmp;
+
+ // Test a custom lognormal instance
+ {
+ LogNormalVariable lognormal (mu, sigma);
+ vector<double> samples;
+ const int NSAMPLES = 10000;
+ double sum = 0;
+ for (int n = NSAMPLES; n; --n)
+ {
+ double value = lognormal.GetValue ();
+ sum += value;
+ samples.push_back (value);
+ }
+ double obtained_mean = sum / NSAMPLES;
+ sum = 0;
+ for (vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
+ {
+ double tmp = (*iter - obtained_mean);
+ sum += tmp*tmp;
+ }
+ double obtained_stddev = sqrt (sum / (NSAMPLES - 1));
+
+ if (not (obtained_mean/desired_mean > 0.90 and obtained_mean/desired_mean < 1.10))
+ {
+ result = false;
+ Failure () << "Obtained lognormal mean value " << obtained_mean << ", expected " << desired_mean << std::endl;
+ }
+
+ if (not (obtained_stddev/desired_stddev > 0.90 and obtained_stddev/desired_stddev < 1.10))
+ {
+ result = false;
+ Failure () << "Obtained lognormal stddev value " << obtained_stddev <<
+ ", expected " << desired_stddev << std::endl;
+ }
+ }
+
+ // Test attribute serialization
+ {
+ RandomVariableValue val;
+ val.DeserializeFromString ("Uniform:0.1:0.2", MakeRandomVariableChecker ());
+ RandomVariable rng = val.Get ();
+ NS_TEST_ASSERT_EQUAL (val.SerializeToString (MakeRandomVariableChecker ()), "Uniform:0.1:0.2");
+ }
+
+ return result;
+ }
+};
+
+
+static RandomVariableTest g_random_variable_tests;
+
+}//namespace ns3
+
+#endif /* RUN_SELF_TESTS */