A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
rtt-test.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6#include "ns3/attribute.h"
7#include "ns3/config.h"
8#include "ns3/double.h"
9#include "ns3/log.h"
10#include "ns3/nstime.h"
11#include "ns3/rtt-estimator.h"
12#include "ns3/test.h"
13
14using namespace ns3;
15
16NS_LOG_COMPONENT_DEFINE("RttEstimatorTestSuite");
17
18/**
19 * \ingroup internet-test
20 *
21 * \brief RTT estimator Test
22 */
24{
25 public:
27
28 private:
29 void DoRun() override;
30 void DoTeardown() override;
31
32 /**
33 * \brief Check RTT values.
34 * \param rtt The RTT estimator.
35 * \param m The measurement.
36 * \param e The expected value.
37 * \param v The expected variance.
38 */
39 void CheckValues(Ptr<RttEstimator> rtt, Time m, Time e, Time v);
40 /**
41 * \brief Check RTT values with a 1 nanosecond of tolerance.
42 * \param rtt The RTT estimator.
43 * \param m The measurement.
44 * \param e The expected value.
45 * \param v The expected variance.
46 */
48};
49
51 : TestCase("Rtt Estimator Test")
52{
53}
54
55void
57{
58 rtt->Measurement(m);
59 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), e, "Estimate not correct");
60 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), v, "Estimate not correct");
61}
62
63void
65{
66 rtt->Measurement(m);
67 NS_TEST_EXPECT_MSG_EQ_TOL(rtt->GetEstimate(), e, Time(NanoSeconds(1)), "Estimate not correct");
68 NS_TEST_EXPECT_MSG_EQ_TOL(rtt->GetVariation(), v, Time(NanoSeconds(1)), "Estimate not correct");
69}
70
71void
73{
74 // Set to a non-default value
75 Config::SetDefault("ns3::RttEstimator::InitialEstimation", TimeValue(MilliSeconds(500)));
76 Config::SetDefault("ns3::RttMeanDeviation::Alpha", DoubleValue(0.5));
77 Config::SetDefault("ns3::RttMeanDeviation::Beta", DoubleValue(0.6));
78
80
81 bool ok;
82 TimeValue timeval;
83 DoubleValue doubleval;
84 ok = rtt->GetAttributeFailSafe("InitialEstimation", timeval);
85 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
86 NS_TEST_EXPECT_MSG_EQ(timeval.Get(), MilliSeconds(500), "Initial estimate should match");
87 ok = rtt->GetAttributeFailSafe("Alpha", doubleval);
88 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
89 NS_TEST_ASSERT_MSG_EQ_TOL(doubleval.Get(), 0.5, 0.001, "Alpha not set");
90 ok = rtt->GetAttributeFailSafe("Beta", doubleval);
91 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be gettable");
92 NS_TEST_ASSERT_MSG_EQ_TOL(doubleval.Get(), 0.6, 0.001, "Beta not set");
93
94 // Reset to default values
95 ok = rtt->SetAttributeFailSafe("InitialEstimation", TimeValue(Seconds(1)));
96 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
97 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0.125));
98 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
99 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0.25));
100 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
101 rtt->Reset();
102
103 Time t(Seconds(1));
104 Time t2(MilliSeconds(125));
105 NS_TEST_EXPECT_MSG_EQ(t2, Time::From(t.GetInteger() >> 3), "X");
106 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), Time(Seconds(1)), "Incorrect initial estimate");
107 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), Time(Seconds(0)), "Incorrect initial variance");
108 NS_TEST_EXPECT_MSG_EQ(rtt->GetNSamples(), 0, "Incorrect initial estimate");
109
110 // CheckValues (rtt, measurement, new estimate, new variance);
111 // Initial value: SRTT <- measurement; RTTVAR <- measurement/2
112 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
113 // Subsequent values: according to RFC 6298
114 CheckValues(rtt, Time(MilliSeconds(1200)), Time(MilliSeconds(1025)), Time(MilliSeconds(425)));
115 Ptr<RttEstimator> copy = rtt->Copy();
116 CheckValues(rtt, Time(MilliSeconds(900)), Time(MicroSeconds(1009375)), Time(MilliSeconds(350)));
117
118 // Check behavior of copy; should have inherited state
119 CheckValues(copy,
120 Time(MilliSeconds(900)),
121 Time(MicroSeconds(1009375)),
122 Time(MilliSeconds(350)));
123
124 // Floating point arithmetic due to alpha and beta settings
125 rtt->Reset();
126 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0.1));
127 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
128 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0.1));
129 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
130 CheckValuesWithTolerance(rtt, Time(Seconds(1.2)), Time(Seconds(1.2)), Time(Seconds(0.6)));
132 Time(MilliSeconds(950)),
133 Time(MilliSeconds(1175)),
134 Time(MilliSeconds(565)));
136 Time(MilliSeconds(1400)),
137 Time(MicroSeconds(1197500)),
138 Time(MilliSeconds(531)));
139
140 // Check boundary values; 0 will not update, 1 will use most recent value
141 rtt->Reset();
142 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0));
143 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
144 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0));
145 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
146 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
147 CheckValues(rtt, Time(Seconds(2)), Time(Seconds(1)), Time(MilliSeconds(500)));
148 CheckValues(rtt, Time(Seconds(3)), Time(Seconds(1)), Time(MilliSeconds(500)));
149 rtt->Reset();
150 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(1));
151 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
152 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(1));
153 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
154 CheckValues(rtt, Time(Seconds(1)), Time(Seconds(1)), Time(MilliSeconds(500)));
155 CheckValues(rtt, Time(Seconds(2.5)), Time(Seconds(2.5)), Time(Seconds(1.5)));
156 CheckValues(rtt, Time(Seconds(7)), Time(Seconds(7)), Time(Seconds(4.5)));
157
158 // recheck initial values
159 rtt->Reset();
160 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), Time(Seconds(1)), "Incorrect initial estimate");
161 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), Time(Seconds(0)), "Incorrect initial variation");
162 NS_TEST_EXPECT_MSG_EQ(rtt->GetNSamples(), 0, "Incorrect initial estimate");
163}
164
165void
169
170/**
171 * \ingroup internet-test
172 *
173 * \brief RTT estimator TestSuite
174 */
176{
177 public:
179 : TestSuite("rtt-estimator", Type::UNIT)
180 {
181 AddTestCase(new RttEstimatorTestCase, TestCase::Duration::QUICK);
182 }
183};
184
185static RttEstimatorTestSuite g_rttEstimatorTestSuite; //!< Static variable for test initialization
RTT estimator Test.
Definition rtt-test.cc:24
void DoRun() override
Implementation to actually run this TestCase.
Definition rtt-test.cc:72
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
Definition rtt-test.cc:166
void CheckValuesWithTolerance(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values with a 1 nanosecond of tolerance.
Definition rtt-test.cc:64
void CheckValues(Ptr< RttEstimator > rtt, Time m, Time e, Time v)
Check RTT values.
Definition rtt-test.cc:56
RTT estimator TestSuite.
Definition rtt-test.cc:176
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
double Get() const
Definition double.cc:26
Smart pointer class similar to boost::intrusive_ptr.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
static Time From(const int64x64_t &value)
Create a Time in the current unit.
Definition nstime.h:470
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:444
Time Get() const
Definition time.cc:519
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:241
#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:500
#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:327
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1332
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:185