A Discrete-Event Network Simulator
API
rtt-test.cc
Go to the documentation of this file.
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/rtt-estimator.h"
20 #include "ns3/attribute.h"
21 #include "ns3/nstime.h"
22 #include "ns3/config.h"
23 #include "ns3/log.h"
24 #include "ns3/double.h"
25 
26 using namespace ns3;
27 
28 NS_LOG_COMPONENT_DEFINE ("RttEstimatorTestSuite");
29 
31 {
32 public:
34 
35 private:
36  virtual void DoRun (void);
37  virtual void DoTeardown (void);
38 
39  void CheckValues (Ptr<RttEstimator> rtt, Time m, Time e, Time v);
40  void CheckValuesWithTolerance (Ptr<RttEstimator> rtt, Time m, Time e, Time v);
41 };
42 
44  : TestCase ("Rtt Estimator Test")
45 {
46 }
47 
48 void
50 {
51  rtt->Measurement (m);
52  NS_TEST_EXPECT_MSG_EQ (rtt->GetEstimate (), e, "Estimate not correct");
53  NS_TEST_EXPECT_MSG_EQ (rtt->GetVariation (), v, "Estimate not correct");
54 }
55 
56 void
58 {
59  rtt->Measurement (m);
60  NS_TEST_EXPECT_MSG_EQ_TOL (rtt->GetEstimate (), e, Time (NanoSeconds (1)), "Estimate not correct");
61  NS_TEST_EXPECT_MSG_EQ_TOL (rtt->GetVariation (), v, Time (NanoSeconds (1)), "Estimate not correct");
62 }
63 
64 
65 void
67 {
68  // Set to a non-default value
69  Config::SetDefault ("ns3::RttEstimator::InitialEstimation", TimeValue (MilliSeconds (500)));
70  Config::SetDefault ("ns3::RttMeanDeviation::Alpha", DoubleValue (0.5));
71  Config::SetDefault ("ns3::RttMeanDeviation::Beta", DoubleValue (0.6));
72 
73  Ptr<RttMeanDeviation> rtt = CreateObject<RttMeanDeviation> ();
74 
75  bool ok;
76  TimeValue timeval;
77  DoubleValue doubleval;
78  ok = rtt->GetAttributeFailSafe ("InitialEstimation", timeval);
79  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be gettable");
80  NS_TEST_EXPECT_MSG_EQ (timeval.Get (), MilliSeconds (500), "Initial estimate should match");
81  ok = rtt->GetAttributeFailSafe ("Alpha", doubleval);
82  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be gettable");
83  NS_TEST_ASSERT_MSG_EQ_TOL (doubleval.Get (), 0.5, 0.001, "Alpha not set");
84  ok = rtt->GetAttributeFailSafe ("Beta", doubleval);
85  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be gettable");
86  NS_TEST_ASSERT_MSG_EQ_TOL (doubleval.Get (), 0.6, 0.001, "Beta not set");
87 
88  // Reset to default values
89  ok = rtt->SetAttributeFailSafe ("InitialEstimation", TimeValue (Seconds (1)));
90  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
91  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (0.125));
92  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
93  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (0.25));
94  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
95  rtt->Reset ();
96 
97  Time t (Seconds (1));
98  Time t2 (MilliSeconds (125));
99  NS_TEST_EXPECT_MSG_EQ (t2, Time::From (t.GetInteger () >> 3), "X");
100  NS_TEST_EXPECT_MSG_EQ (rtt->GetEstimate (), Time (Seconds (1)), "Incorrect initial estimate");
101  NS_TEST_EXPECT_MSG_EQ (rtt->GetVariation (), Time (Seconds (0)), "Incorrect initial variance");
102  NS_TEST_EXPECT_MSG_EQ (rtt->GetNSamples (), 0, "Incorrect initial estimate");
103 
104  // CheckValues (rtt, measurement, new estimate, new variance);
105  // Initial value: SRTT <- measurement; RTTVAR <- measurement/2
106  CheckValues (rtt, Time (Seconds (1)), Time (Seconds (1)), Time (MilliSeconds (500)));
107  // Subsequent values: according to RFC 6298
108  CheckValues (rtt, Time (MilliSeconds (1200)), Time (MilliSeconds (1025)), Time (MilliSeconds (425)));
109  Ptr<RttEstimator> copy = rtt->Copy ();
110  CheckValues (rtt, Time (MilliSeconds (900)), Time (MicroSeconds (1009375)), Time (MilliSeconds (350)));
111 
112  // Check behavior of copy; should have inherited state
113  CheckValues (copy, Time (MilliSeconds (900)), Time (MicroSeconds (1009375)), Time (MilliSeconds (350)));
114 
115  // Floating point arithmetic due to alpha and beta settings
116  rtt->Reset ();
117  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (0.1));
118  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
119  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (0.1));
120  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
121  CheckValuesWithTolerance (rtt, Time (Seconds (1.2)), Time (Seconds (1.2)), Time (Seconds (0.6)));
122  CheckValuesWithTolerance (rtt, Time (MilliSeconds (950)), Time (MilliSeconds (1175)), Time (MilliSeconds (565)));
123  CheckValuesWithTolerance (rtt, Time (MilliSeconds (1400)), Time (MicroSeconds (1197500)), Time (MilliSeconds (531)));
124 
125  // Check boundary values; 0 will not update, 1 will use most recent value
126  rtt->Reset ();
127  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (0));
128  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
129  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (0));
130  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
131  CheckValues (rtt, Time (Seconds (1)), Time (Seconds (1)), Time (MilliSeconds (500)));
132  CheckValues (rtt, Time (Seconds (2)), Time (Seconds (1)), Time (MilliSeconds (500)));
133  CheckValues (rtt, Time (Seconds (3)), Time (Seconds (1)), Time (MilliSeconds (500)));
134  rtt->Reset ();
135  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (1));
136  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
137  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (1));
138  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
139  CheckValues (rtt, Time (Seconds (1)), Time (Seconds (1)), Time (MilliSeconds (500)));
140  CheckValues (rtt, Time (Seconds (2.5)), Time (Seconds (2.5)), Time (Seconds (1.5)));
141  CheckValues (rtt, Time (Seconds (7)), Time (Seconds (7)), Time (Seconds (4.5)));
142 
143  // recheck initial values
144  rtt->Reset ();
145  NS_TEST_EXPECT_MSG_EQ (rtt->GetEstimate (), Time (Seconds (1)), "Incorrect initial estimate");
146  NS_TEST_EXPECT_MSG_EQ (rtt->GetVariation (), Time (Seconds (0)), "Incorrect initial variation");
147  NS_TEST_EXPECT_MSG_EQ (rtt->GetNSamples (), 0, "Incorrect initial estimate");
148 }
149 
150 void
152 {
153 }
154 
156 {
157 public:
159  : TestSuite ("rtt-estimator", UNIT)
160  {
161  AddTestCase (new RttEstimatorTestCase, TestCase::QUICK);
162  }
163 
164 };
165 
167 
168 
Time Get(void) const
Definition: time.cc:443
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
uint32_t GetNSamples(void) const
gets the number of samples used in the estimates
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
A suite of tests to run.
Definition: test.h:1333
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:278
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Set a single attribute without raising errors.
Definition: object-base.cc:211
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:719
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
static RttEstimatorTestSuite g_rttEstimatorTestSuite
Definition: rtt-test.cc:166
bool GetAttributeFailSafe(std::string name, AttributeValue &value) const
Get the value of an attribute without raising erros.
Definition: object-base.cc:264
void CheckValues(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Definition: rtt-test.cc:49
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:297
AttributeValue implementation for Time.
Definition: nstime.h:957
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:919
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: rtt-test.cc:151
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:373
double Get(void) const
Definition: double.cc:35
#define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if ...
Definition: test.h:558
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Measurement(Time t)=0
Add a new measurement to the estimator.
Time GetVariation(void) const
Note that this is not a formal statistical variance; it has the the same units as the estimate...
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:385
void Reset()
Resets the estimator.
Ptr< RttEstimator > Copy() const
Copy object (including current internal state)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void CheckValuesWithTolerance(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Definition: rtt-test.cc:57
Time GetEstimate(void) const
gets the RTT estimate.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:911
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: rtt-test.cc:66
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41