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, NanoSeconds(1), "Estimate not correct");
68 NS_TEST_EXPECT_MSG_EQ_TOL(rtt->GetVariation(), v, 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(), Seconds(1), "Incorrect initial estimate");
107 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), 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, Seconds(1), Seconds(1), MilliSeconds(500));
113 // Subsequent values: according to RFC 6298
114 CheckValues(rtt, MilliSeconds(1200), MilliSeconds(1025), MilliSeconds(425));
115 Ptr<RttEstimator> copy = rtt->Copy();
116 CheckValues(rtt, MilliSeconds(900), MicroSeconds(1009375), MilliSeconds(350));
117
118 // Check behavior of copy; should have inherited state
119 CheckValues(copy, MilliSeconds(900), MicroSeconds(1009375), MilliSeconds(350));
120
121 // Floating point arithmetic due to alpha and beta settings
122 rtt->Reset();
123 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0.1));
124 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
125 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0.1));
126 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
127 CheckValuesWithTolerance(rtt, Seconds(1.2), Seconds(1.2), Seconds(0.6));
130
131 // Check boundary values; 0 will not update, 1 will use most recent value
132 rtt->Reset();
133 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(0));
134 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
135 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(0));
136 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
137 CheckValues(rtt, Seconds(1), Seconds(1), MilliSeconds(500));
138 CheckValues(rtt, Seconds(2), Seconds(1), MilliSeconds(500));
139 CheckValues(rtt, Seconds(3), Seconds(1), MilliSeconds(500));
140 rtt->Reset();
141 ok = rtt->SetAttributeFailSafe("Alpha", DoubleValue(1));
142 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
143 ok = rtt->SetAttributeFailSafe("Beta", DoubleValue(1));
144 NS_TEST_EXPECT_MSG_EQ(ok, true, "Attribute should be settable");
145 CheckValues(rtt, Seconds(1), Seconds(1), MilliSeconds(500));
146 CheckValues(rtt, Seconds(2.5), Seconds(2.5), Seconds(1.5));
147 CheckValues(rtt, Seconds(7), Seconds(7), Seconds(4.5));
148
149 // recheck initial values
150 rtt->Reset();
151 NS_TEST_EXPECT_MSG_EQ(rtt->GetEstimate(), Seconds(1), "Incorrect initial estimate");
152 NS_TEST_EXPECT_MSG_EQ(rtt->GetVariation(), Seconds(0), "Incorrect initial variation");
153 NS_TEST_EXPECT_MSG_EQ(rtt->GetNSamples(), 0, "Incorrect initial estimate");
154}
155
156void
160
161/**
162 * @ingroup internet-test
163 *
164 * @brief RTT estimator TestSuite
165 */
167{
168 public:
170 : TestSuite("rtt-estimator", Type::UNIT)
171 {
172 AddTestCase(new RttEstimatorTestCase, TestCase::Duration::QUICK);
173 }
174};
175
176static 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:157
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:167
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
AttributeValue implementation for Time.
Definition nstime.h:1431
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:1368
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1380
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1356
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:176