A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rtt-test.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  */
17 
18 #include "ns3/test.h"
19 #include "ns3/core-module.h"
20 #include "ns3/internet-module.h"
21 #include "ns3/rtt-estimator.h"
22 #include "ns3/log.h"
23 
24 NS_LOG_COMPONENT_DEFINE ("RttTestSuite");
25 
26 using namespace ns3;
27 
28 class RttTestCase : public TestCase
29 {
30 public:
31  RttTestCase (double mean,
32  double variance,
33  double gain);
34 
35 private:
36  virtual void DoRun (void);
37  virtual void DoTeardown (void);
38 
39  double m_mean;
40  double m_variance;
41  double m_gain;
42 
43 };
44 
45 RttTestCase::RttTestCase (double mean,
46  double variance,
47  double gain)
48  : TestCase ("Rtt Estimate Test"),
49  m_mean (mean),
50  m_variance (variance),
51  m_gain (gain)
52 {
53 }
54 
55 void
57 {
58  Config::SetDefault ("ns3::RttEstimator::InitialEstimation", TimeValue (MilliSeconds (m_mean)));
59  Config::SetDefault ("ns3::RttMeanDeviation::Gain", DoubleValue (m_gain));
60  Config::SetDefault ("ns3::RttEstimator::MinRTO", TimeValue (Seconds (0)));
61 
62  Ptr<RttMeanDeviation> rtt = CreateObject<RttMeanDeviation> ();
63  Ptr<NormalRandomVariable> nv = CreateObject<NormalRandomVariable> ();
64  nv->SetAttribute ("Mean", DoubleValue (m_mean));
65  nv->SetAttribute ("Variance", DoubleValue (m_variance));
66 
67  NS_TEST_EXPECT_MSG_EQ (m_mean, rtt->GetCurrentEstimate ().GetMilliSeconds (), "Initial estimate should match mean");
68 
69  double a, v, g;
70  a = v = m_mean;
71  g = m_gain;
72 
73  for (uint32_t i = 0; i < 10000; ++i)
74  {
75  int measurement = nv->GetInteger ();
76  rtt->Measurement (Time::FromInteger (measurement, Time::MS));
77  double err = (measurement - a);
78  a = a + g * err;
79  v = v + g * (std::abs (err) - v);
80  }
81 
82  //5% tolerance
83  double tolerance = m_mean * .05;
84 
85  NS_TEST_ASSERT_MSG_EQ_TOL (m_mean, rtt->GetCurrentEstimate ().GetMilliSeconds (), tolerance, "Unexpected estimate");
86 
87  int expectedTimeout = (int)a + 4 * (int)v;
88 
89  NS_TEST_EXPECT_MSG_EQ (rtt->RetransmitTimeout ().GetMilliSeconds (), expectedTimeout, "Timeout values do not match");
90 }
91 void
93 {
94 }
95 
96 
97 static class RttTestSuite : public TestSuite
98 {
99 public:
100  RttTestSuite ()
101  : TestSuite ("rtt", UNIT)
102  {
103  AddTestCase (new RttTestCase (150.0, 10.0, .1), TestCase::QUICK);
104  AddTestCase (new RttTestCase (5000.0, 5.0, .5), TestCase::QUICK);
105  AddTestCase (new RttTestCase (200.0, 25.0, .7), TestCase::QUICK);
106  }
107 
108 } g_tcpTestSuite;
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
A suite of tests to run.
Definition: test.h:962
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
encapsulates test code
Definition: test.h:834
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
virtual void DoRun(void)
Implementation to actually run this test case.
Definition: rtt-test.cc:56
hold objects of type ns3::Time
Definition: nstime.h:700
Time GetCurrentEstimate(void) const
gets the current RTT estimate.
Time RetransmitTimeout()
Returns the estimated RTO.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
void Measurement(Time measure)
Add a new measurement to the estimator.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601
Hold an floating point type.
Definition: double.h:41
int64_t GetMilliSeconds(void) const
Definition: nstime.h:271
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
Definition: rtt-test.cc:92