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
26using namespace ns3;
27
28NS_LOG_COMPONENT_DEFINE ("RttEstimatorTestSuite");
29
37{
38public:
40
41private:
42 virtual void DoRun (void);
43 virtual void DoTeardown (void);
44
52 void CheckValues (Ptr<RttEstimator> rtt, Time m, Time e, Time v);
61};
62
64 : TestCase ("Rtt Estimator Test")
65{
66}
67
68void
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
76void
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
85void
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)));
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
170void
172{
173}
174
182{
183public:
185 : TestSuite ("rtt-estimator", UNIT)
186 {
187 AddTestCase (new RttEstimatorTestCase, TestCase::QUICK);
188 }
189
190};
191
193
RTT estimator Test.
Definition: rtt-test.cc:37
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: rtt-test.cc:86
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: rtt-test.cc:171
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
void CheckValues(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values.
Definition: rtt-test.cc:69
RTT estimator TestSuite.
Definition: rtt-test.cc:182
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
double Get(void) const
Definition: double.cc:35
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:423
AttributeValue implementation for Time.
Definition: nstime.h:1308
Time Get(void) const
Definition: time.cc:519
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:240
#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:491
#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:323
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1260
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static RttEstimatorTestSuite g_rttEstimatorTestSuite
Static variable for test initialization.
Definition: rtt-test.cc:192