A Discrete-Event Network Simulator
API
rtt-test.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 */
16
17#include "ns3/attribute.h"
18#include "ns3/config.h"
19#include "ns3/double.h"
20#include "ns3/log.h"
21#include "ns3/nstime.h"
22#include "ns3/rtt-estimator.h"
23#include "ns3/test.h"
24
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE("RttEstimatorTestSuite");
28
36{
37 public:
39
40 private:
41 void DoRun() override;
42 void DoTeardown() override;
43
51 void CheckValues(Ptr<RttEstimator> rtt, Time m, Time e, Time v);
60};
61
63 : TestCase("Rtt Estimator Test")
64{
65}
66
67void
69{
70 rtt->Measurement(m);
71 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), e, "Estimate not correct");
72 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), v, "Estimate not correct");
73}
74
75void
77{
78 rtt->Measurement(m);
79 NS_TEST_EXPECT_MSG_EQ_TOL(rtt->GetEstimate(), e, Time(NanoSeconds(1)), "Estimate not correct");
80 NS_TEST_EXPECT_MSG_EQ_TOL(rtt->GetVariation(), v, Time(NanoSeconds(1)), "Estimate not correct");
81}
82
83void
85{
86 // Set to a non-default value
87 Config::SetDefault("ns3::RttEstimator::InitialEstimation", TimeValue(MilliSeconds(500)));
88 Config::SetDefault("ns3::RttMeanDeviation::Alpha", DoubleValue(0.5));
89 Config::SetDefault("ns3::RttMeanDeviation::Beta", DoubleValue(0.6));
90
91 Ptr<RttMeanDeviation> rtt = CreateObject<RttMeanDeviation>();
92
93 bool ok;
94 TimeValue timeval;
95 DoubleValue doubleval;
96 ok = rtt->GetAttributeFailSafe("InitialEstimation", timeval);
97 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
98 NS_TEST_EXPECT_MSG_EQ(timeval.Get(), MilliSeconds(500), "Initial estimate should match");
99 ok = rtt->GetAttributeFailSafe("Alpha", doubleval);
100 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
101 NS_TEST_ASSERT_MSG_EQ_TOL(doubleval.Get(), 0.5, 0.001, "Alpha not set");
102 ok = rtt->GetAttributeFailSafe("Beta", doubleval);
103 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
104 NS_TEST_ASSERT_MSG_EQ_TOL(doubleval.Get(), 0.6, 0.001, "Beta not set");
105
106 // Reset to default values
107 ok = rtt->SetAttributeFailSafe("InitialEstimation", TimeValue(Seconds(1)));
108 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
109 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0.125));
110 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
111 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0.25));
112 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
113 rtt->Reset();
114
115 Time t(Seconds(1));
116 Time t2(MilliSeconds(125));
117 NS_TEST_EXPECT_MSG_EQ(t2, Time::From(t.GetInteger() >> 3), "X");
118 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), Time(Seconds(1)), "Incorrect initial estimate");
119 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), Time(Seconds(0)), "Incorrect initial variance");
120 NS_TEST_EXPECT_MSG_EQ(rtt->GetNSamples(), 0, "Incorrect initial estimate");
121
122 // CheckValues (rtt, measurement, new estimate, new variance);
123 // Initial value: SRTT <- measurement; RTTVAR <- measurement/2
124 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
125 // Subsequent values: according to RFC 6298
126 CheckValues(rtt, Time(MilliSeconds(1200)), Time(MilliSeconds(1025)), Time(MilliSeconds(425)));
127 Ptr<RttEstimator> copy = rtt->Copy();
128 CheckValues(rtt, Time(MilliSeconds(900)), Time(MicroSeconds(1009375)), Time(MilliSeconds(350)));
129
130 // Check behavior of copy; should have inherited state
131 CheckValues(copy,
132 Time(MilliSeconds(900)),
133 Time(MicroSeconds(1009375)),
134 Time(MilliSeconds(350)));
135
136 // Floating point arithmetic due to alpha and beta settings
137 rtt->Reset();
138 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0.1));
139 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
140 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0.1));
141 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
142 CheckValuesWithTolerance(rtt, Time(Seconds(1.2)), Time(Seconds(1.2)), Time(Seconds(0.6)));
144 Time(MilliSeconds(950)),
145 Time(MilliSeconds(1175)),
146 Time(MilliSeconds(565)));
148 Time(MilliSeconds(1400)),
149 Time(MicroSeconds(1197500)),
150 Time(MilliSeconds(531)));
151
152 // Check boundary values; 0 will not update, 1 will use most recent value
153 rtt->Reset();
154 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0));
155 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
156 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0));
157 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
158 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
159 CheckValues(rtt, Time(Seconds(2)), Time(Seconds(1)), Time(MilliSeconds(500)));
160 CheckValues(rtt, Time(Seconds(3)), Time(Seconds(1)), Time(MilliSeconds(500)));
161 rtt->Reset();
162 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(1));
163 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
164 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(1));
165 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
166 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
167 CheckValues(rtt, Time(Seconds(2.5)), Time(Seconds(2.5)), Time(Seconds(1.5)));
168 CheckValues(rtt, Time(Seconds(7)), Time(Seconds(7)), Time(Seconds(4.5)));
169
170 // recheck initial values
171 rtt->Reset();
172 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), Time(Seconds(1)), "Incorrect initial estimate");
173 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), Time(Seconds(0)), "Incorrect initial variation");
174 NS_TEST_EXPECT_MSG_EQ(rtt->GetNSamples(), 0, "Incorrect initial estimate");
175}
176
177void
179{
180}
181
189{
190 public:
192 : TestSuite("rtt-estimator", UNIT)
193 {
194 AddTestCase(new RttEstimatorTestCase, TestCase::QUICK);
195 }
196};
197
RTT estimator Test.
Definition: rtt-test.cc:36
void DoRun() override
Implementation to actually run this TestCase.
Definition: rtt-test.cc:84
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
Definition: rtt-test.cc:178
void CheckValuesWithTolerance(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values with a 1 nanosecond of tolerance.
Definition: rtt-test.cc:76
void CheckValues(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values.
Definition: rtt-test.cc:68
RTT estimator TestSuite.
Definition: rtt-test.cc:189
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
double Get() const
Definition: double.cc:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:454
AttributeValue implementation for Time.
Definition: nstime.h:1425
Time Get() const
Definition: time.cc:532
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:251
#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:510
#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:337
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1374
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:850
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:198