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 
37 {
38 public:
40 
41 private:
42  virtual void DoRun (void);
43  virtual void DoTeardown (void);
44 
52  void CheckValues (Ptr<RttEstimator> rtt, Time m, Time e, Time v);
60  void CheckValuesWithTolerance (Ptr<RttEstimator> rtt, Time m, Time e, Time v);
61 };
62 
64  : TestCase ("Rtt Estimator Test")
65 {
66 }
67 
68 void
70 {
71  rtt->Measurement (m);
72  NS_TEST_EXPECT_MSG_EQ (rtt->GetEstimate (), e, "Estimate not correct");
73  NS_TEST_EXPECT_MSG_EQ (rtt->GetVariation (), v, "Estimate not correct");
74 }
75 
76 void
78 {
79  rtt->Measurement (m);
80  NS_TEST_EXPECT_MSG_EQ_TOL (rtt->GetEstimate (), e, Time (NanoSeconds (1)), "Estimate not correct");
81  NS_TEST_EXPECT_MSG_EQ_TOL (rtt->GetVariation (), v, Time (NanoSeconds (1)), "Estimate not correct");
82 }
83 
84 
85 void
87 {
88  // Set to a non-default value
89  Config::SetDefault ("ns3::RttEstimator::InitialEstimation", TimeValue (MilliSeconds (500)));
90  Config::SetDefault ("ns3::RttMeanDeviation::Alpha", DoubleValue (0.5));
91  Config::SetDefault ("ns3::RttMeanDeviation::Beta", DoubleValue (0.6));
92 
93  Ptr<RttMeanDeviation> rtt = CreateObject<RttMeanDeviation> ();
94 
95  bool ok;
96  TimeValue timeval;
97  DoubleValue doubleval;
98  ok = rtt->GetAttributeFailSafe ("InitialEstimation", timeval);
99  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be gettable");
100  NS_TEST_EXPECT_MSG_EQ (timeval.Get (), MilliSeconds (500), "Initial estimate should match");
101  ok = rtt->GetAttributeFailSafe ("Alpha", doubleval);
102  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be gettable");
103  NS_TEST_ASSERT_MSG_EQ_TOL (doubleval.Get (), 0.5, 0.001, "Alpha not set");
104  ok = rtt->GetAttributeFailSafe ("Beta", doubleval);
105  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be gettable");
106  NS_TEST_ASSERT_MSG_EQ_TOL (doubleval.Get (), 0.6, 0.001, "Beta not set");
107 
108  // Reset to default values
109  ok = rtt->SetAttributeFailSafe ("InitialEstimation", TimeValue (Seconds (1)));
110  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
111  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (0.125));
112  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
113  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (0.25));
114  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
115  rtt->Reset ();
116 
117  Time t (Seconds (1));
118  Time t2 (MilliSeconds (125));
119  NS_TEST_EXPECT_MSG_EQ (t2, Time::From (t.GetInteger () >> 3), "X");
120  NS_TEST_EXPECT_MSG_EQ (rtt->GetEstimate (), Time (Seconds (1)), "Incorrect initial estimate");
121  NS_TEST_EXPECT_MSG_EQ (rtt->GetVariation (), Time (Seconds (0)), "Incorrect initial variance");
122  NS_TEST_EXPECT_MSG_EQ (rtt->GetNSamples (), 0, "Incorrect initial estimate");
123 
124  // CheckValues (rtt, measurement, new estimate, new variance);
125  // Initial value: SRTT <- measurement; RTTVAR <- measurement/2
126  CheckValues (rtt, Time (Seconds (1)), Time (Seconds (1)), Time (MilliSeconds (500)));
127  // Subsequent values: according to RFC 6298
128  CheckValues (rtt, Time (MilliSeconds (1200)), Time (MilliSeconds (1025)), Time (MilliSeconds (425)));
129  Ptr<RttEstimator> copy = rtt->Copy ();
130  CheckValues (rtt, Time (MilliSeconds (900)), Time (MicroSeconds (1009375)), Time (MilliSeconds (350)));
131 
132  // Check behavior of copy; should have inherited state
133  CheckValues (copy, Time (MilliSeconds (900)), Time (MicroSeconds (1009375)), Time (MilliSeconds (350)));
134 
135  // Floating point arithmetic due to alpha and beta settings
136  rtt->Reset ();
137  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (0.1));
138  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
139  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (0.1));
140  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
141  CheckValuesWithTolerance (rtt, Time (Seconds (1.2)), Time (Seconds (1.2)), Time (Seconds (0.6)));
142  CheckValuesWithTolerance (rtt, Time (MilliSeconds (950)), Time (MilliSeconds (1175)), Time (MilliSeconds (565)));
143  CheckValuesWithTolerance (rtt, Time (MilliSeconds (1400)), Time (MicroSeconds (1197500)), Time (MilliSeconds (531)));
144 
145  // Check boundary values; 0 will not update, 1 will use most recent value
146  rtt->Reset ();
147  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (0));
148  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
149  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (0));
150  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
151  CheckValues (rtt, Time (Seconds (1)), Time (Seconds (1)), Time (MilliSeconds (500)));
152  CheckValues (rtt, Time (Seconds (2)), Time (Seconds (1)), Time (MilliSeconds (500)));
153  CheckValues (rtt, Time (Seconds (3)), Time (Seconds (1)), Time (MilliSeconds (500)));
154  rtt->Reset ();
155  ok = rtt->SetAttributeFailSafe ("Alpha", DoubleValue (1));
156  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
157  ok = rtt->SetAttributeFailSafe ("Beta", DoubleValue (1));
158  NS_TEST_EXPECT_MSG_EQ (ok, true, "Attribute should be settable");
159  CheckValues (rtt, Time (Seconds (1)), Time (Seconds (1)), Time (MilliSeconds (500)));
160  CheckValues (rtt, Time (Seconds (2.5)), Time (Seconds (2.5)), Time (Seconds (1.5)));
161  CheckValues (rtt, Time (Seconds (7)), Time (Seconds (7)), Time (Seconds (4.5)));
162 
163  // recheck initial values
164  rtt->Reset ();
165  NS_TEST_EXPECT_MSG_EQ (rtt->GetEstimate (), Time (Seconds (1)), "Incorrect initial estimate");
166  NS_TEST_EXPECT_MSG_EQ (rtt->GetVariation (), Time (Seconds (0)), "Incorrect initial variation");
167  NS_TEST_EXPECT_MSG_EQ (rtt->GetNSamples (), 0, "Incorrect initial estimate");
168 }
169 
170 void
172 {
173 }
174 
182 {
183 public:
185  : TestSuite ("rtt-estimator", UNIT)
186  {
187  AddTestCase (new RttEstimatorTestCase, TestCase::QUICK);
188  }
189 
190 };
191 
193 
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:399
A suite of tests to run.
Definition: test.h:1342
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
Time GetEstimate(void) const
gets the RTT estimate.
#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:285
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Set a single attribute without raising errors.
Definition: object-base.cc:205
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:740
encapsulates test code
Definition: test.h:1155
bool GetAttributeFailSafe(std::string name, AttributeValue &value) const
Get the value of an attribute without raising erros.
Definition: object-base.cc:258
static RttEstimatorTestSuite g_rttEstimatorTestSuite
Static variable for test initialization.
Definition: rtt-test.cc:192
void CheckValues(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values.
Definition: rtt-test.cc:69
AttributeValue implementation for Time.
Definition: nstime.h:1076
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1038
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: rtt-test.cc:171
#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:380
#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:565
double Get(void) const
Definition: double.cc:35
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.
uint32_t GetNSamples(void) const
gets the number of samples used in the estimates
Time GetVariation(void) const
Note that this is not a formal statistical variance; it has the the same units as the estimate...
void Reset()
Resets the estimator.
RTT estimator Test.
Definition: rtt-test.cc:36
RTT estimator TestSuite.
Definition: rtt-test.cc:181
Time Get(void) const
Definition: time.cc:443
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
Ptr< RttEstimator > Copy() const
Copy object (including current internal state)
void CheckValuesWithTolerance(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values with a 1 nanosecond of tolerance.
Definition: rtt-test.cc:77
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1030
This test suite implements a Unit Test.
Definition: test.h:1352
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: rtt-test.cc:86
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41