A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
35{
36 public:
38
39 private:
40 void DoRun() override;
41 void DoTeardown() override;
42
50 void CheckValues(Ptr<RttEstimator> rtt, Time m, Time e, Time v);
59};
60
62 : TestCase("Rtt Estimator Test")
63{
64}
65
66void
68{
69 rtt->Measurement(m);
70 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), e, "Estimate not correct");
71 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), v, "Estimate not correct");
72}
73
74void
76{
77 rtt->Measurement(m);
78 NS_TEST_EXPECT_MSG_EQ_TOL(rtt->GetEstimate(), e, Time(NanoSeconds(1)), "Estimate not correct");
79 NS_TEST_EXPECT_MSG_EQ_TOL(rtt->GetVariation(), v, Time(NanoSeconds(1)), "Estimate not correct");
80}
81
82void
84{
85 // Set to a non-default value
86 Config::SetDefault("ns3::RttEstimator::InitialEstimation", TimeValue(MilliSeconds(500)));
87 Config::SetDefault("ns3::RttMeanDeviation::Alpha", DoubleValue(0.5));
88 Config::SetDefault("ns3::RttMeanDeviation::Beta", DoubleValue(0.6));
89
90 Ptr<RttMeanDeviation> rtt = CreateObject<RttMeanDeviation>();
91
92 bool ok;
93 TimeValue timeval;
94 DoubleValue doubleval;
95 ok = rtt->GetAttributeFailSafe("InitialEstimation", timeval);
96 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
97 NS_TEST_EXPECT_MSG_EQ(timeval.Get(), MilliSeconds(500), "Initial estimate should match");
98 ok = rtt->GetAttributeFailSafe("Alpha", doubleval);
99 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
100 NS_TEST_ASSERT_MSG_EQ_TOL(doubleval.Get(), 0.5, 0.001, "Alpha not set");
101 ok = rtt->GetAttributeFailSafe("Beta", doubleval);
102 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
103 NS_TEST_ASSERT_MSG_EQ_TOL(doubleval.Get(), 0.6, 0.001, "Beta not set");
104
105 // Reset to default values
106 ok = rtt->SetAttributeFailSafe("InitialEstimation", TimeValue(Seconds(1)));
107 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
108 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0.125));
109 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
110 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0.25));
111 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
112 rtt->Reset();
113
114 Time t(Seconds(1));
115 Time t2(MilliSeconds(125));
116 NS_TEST_EXPECT_MSG_EQ(t2, Time::From(t.GetInteger() >> 3), "X");
117 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), Time(Seconds(1)), "Incorrect initial estimate");
118 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), Time(Seconds(0)), "Incorrect initial variance");
119 NS_TEST_EXPECT_MSG_EQ(rtt->GetNSamples(), 0, "Incorrect initial estimate");
120
121 // CheckValues (rtt, measurement, new estimate, new variance);
122 // Initial value: SRTT <- measurement; RTTVAR <- measurement/2
123 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
124 // Subsequent values: according to RFC 6298
125 CheckValues(rtt, Time(MilliSeconds(1200)), Time(MilliSeconds(1025)), Time(MilliSeconds(425)));
126 Ptr<RttEstimator> copy = rtt->Copy();
127 CheckValues(rtt, Time(MilliSeconds(900)), Time(MicroSeconds(1009375)), Time(MilliSeconds(350)));
128
129 // Check behavior of copy; should have inherited state
130 CheckValues(copy,
131 Time(MilliSeconds(900)),
132 Time(MicroSeconds(1009375)),
133 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 Time(MilliSeconds(950)),
144 Time(MilliSeconds(1175)),
145 Time(MilliSeconds(565)));
147 Time(MilliSeconds(1400)),
148 Time(MicroSeconds(1197500)),
149 Time(MilliSeconds(531)));
150
151 // Check boundary values; 0 will not update, 1 will use most recent value
152 rtt->Reset();
153 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0));
154 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
155 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0));
156 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
157 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
158 CheckValues(rtt, Time(Seconds(2)), Time(Seconds(1)), Time(MilliSeconds(500)));
159 CheckValues(rtt, Time(Seconds(3)), Time(Seconds(1)), Time(MilliSeconds(500)));
160 rtt->Reset();
161 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(1));
162 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
163 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(1));
164 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
165 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
166 CheckValues(rtt, Time(Seconds(2.5)), Time(Seconds(2.5)), Time(Seconds(1.5)));
167 CheckValues(rtt, Time(Seconds(7)), Time(Seconds(7)), Time(Seconds(4.5)));
168
169 // recheck initial values
170 rtt->Reset();
171 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), Time(Seconds(1)), "Incorrect initial estimate");
172 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), Time(Seconds(0)), "Incorrect initial variation");
173 NS_TEST_EXPECT_MSG_EQ(rtt->GetNSamples(), 0, "Incorrect initial estimate");
174}
175
176void
178{
179}
180
187{
188 public:
190 : TestSuite("rtt-estimator", Type::UNIT)
191 {
192 AddTestCase(new RttEstimatorTestCase, TestCase::Duration::QUICK);
193 }
194};
195
RTT estimator Test.
Definition: rtt-test.cc:35
void DoRun() override
Implementation to actually run this TestCase.
Definition: rtt-test.cc:83
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
Definition: rtt-test.cc:177
void CheckValuesWithTolerance(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values with a 1 nanosecond of tolerance.
Definition: rtt-test.cc:75
void CheckValues(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values.
Definition: rtt-test.cc:67
RTT estimator TestSuite.
Definition: rtt-test.cc:187
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:77
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
static Time From(const int64x64_t &value)
Create a Time in the current unit.
Definition: nstime.h:481
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:455
AttributeValue implementation for Time.
Definition: nstime.h:1413
Time Get() const
Definition: time.cc:530
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
#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:252
#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:511
#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:338
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
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:196