A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
test-data-rate.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Greg Steinbrecher <grs@fb.com>
7 */
8
9#include "ns3/data-rate.h"
10#include "ns3/log.h"
11#include "ns3/simulator.h"
12#include "ns3/test.h"
13#include "ns3/tuple.h"
14
15using namespace ns3;
16
17/**
18 * @ingroup network-test
19 * @ingroup tests
20 *
21 * @brief Test Data rate
22 *
23 */
25{
26 public:
27 /**
28 * Constructor
29 * @param name test name
30 */
31 DataRateTestCase(std::string name);
32 ~DataRateTestCase() override;
33
34 /**
35 * Checks if two time values are equal
36 * @param t1 first time to check
37 * @param t2 second time to check
38 * @param msg check output message
39 */
40 void CheckTimesEqual(Time t1, Time t2, const std::string msg);
41 /**
42 * Checks if two data rates values are equal
43 * @param d1 first data rate to check
44 * @param d2 second data rate to check
45 * @param msg check output message
46 */
47 void CheckDataRateEqual(DataRate d1, DataRate d2, const std::string msg);
48
49 protected:
50 void DoRun() override = 0;
51};
52
54 : TestCase(name)
55{
56}
57
61
62void
63DataRateTestCase::CheckTimesEqual(Time actual, Time correct, const std::string msg)
64{
65 int64x64_t actualFemtos = actual.GetFemtoSeconds();
66 int64x64_t correctFemtos = correct.GetFemtoSeconds();
67 NS_TEST_EXPECT_MSG_EQ(actualFemtos, correctFemtos, msg);
68}
69
70void
72{
73 NS_TEST_EXPECT_MSG_EQ(d1, d2, msg);
74}
75
76/**
77 * @ingroup network-test
78 * @ingroup tests
79 *
80 * @brief Test Data rate
81 *
82 */
84{
85 public:
87
88 /**
89 * Checks that a given number of bits, at a specified datarate, are
90 * corresponding to a given time
91 * @param rate the DataRate
92 * @param nBits number of bits
93 * @param correctTime expected time
94 */
95 void SingleTest(std::string rate, size_t nBits, Time correctTime);
96
97 private:
98 void DoRun() override;
99};
100
102 : DataRateTestCase("Test rounding of conversion from DataRate to time")
103{
104}
105
106void
107DataRateTestCase1::SingleTest(std::string rate, size_t nBits, Time correctTime)
108{
109 DataRate dr(rate);
110 Time bitsTime = dr.CalculateBitsTxTime(nBits);
111 CheckTimesEqual(bitsTime, correctTime, "CalculateBitsTxTime returned incorrect value");
112 if ((nBits % 8) == 0)
113 {
114 Time bytesTime = dr.CalculateBytesTxTime(nBits / 8);
115 CheckTimesEqual(bytesTime, correctTime, "CalculateBytesTxTime returned incorrect value");
116 }
117}
118
119void
121{
123 {
125 }
126 SingleTest("1GB/s", 512, NanoSeconds(64));
127 SingleTest("8Gb/s", 512, NanoSeconds(64));
128 SingleTest("1Gb/s", 512, NanoSeconds(512));
129 SingleTest("8GB/s", 512, NanoSeconds(8));
130 size_t nBits;
131 for (nBits = 0; nBits <= 512; nBits++)
132 {
133 SingleTest("1Mb/s", nBits, MicroSeconds(nBits));
134 SingleTest("10Mb/s", nBits, NanoSeconds(nBits * 100));
135 SingleTest("100Mb/s", nBits, NanoSeconds(nBits * 10));
136 SingleTest("1Gb/s", nBits, NanoSeconds(nBits));
137 SingleTest("10Gb/s", nBits, PicoSeconds(nBits * 100));
138 SingleTest("25Gb/s", nBits, PicoSeconds(nBits * 40));
139 SingleTest("40Gb/s", nBits, PicoSeconds(nBits * 25));
140 SingleTest("100Gb/s", nBits, PicoSeconds(nBits * 10));
141 SingleTest("200Gb/s", nBits, PicoSeconds(nBits * 5));
142 SingleTest("400Gb/s", nBits, FemtoSeconds(nBits * 2500));
143 }
144}
145
146/**
147 * @ingroup network-test
148 * @ingroup tests
149 *
150 * @brief Test Data rate
151 *
152 */
154{
155 public:
157 /**
158 * Checks data rate addition
159 * @param rate1 first data rate
160 * @param rate2 second data rate
161 * @param rate3 third data rate (first plus second)
162 */
163 void AdditionTest(std::string rate1, std::string rate2, std::string rate3);
164 /**
165 * Checks data rate subtraction
166 * @param rate1 first data rate
167 * @param rate2 second data rate
168 * @param rate3 third data rate (first minus second)
169 */
170 void SubtractionTest(std::string rate1, std::string rate2, std::string rate3);
171 /**
172 * Checks data rate integer multiplication
173 * @param rate1 first data rate
174 * @param factor multiplication factor
175 * @param rate2 second data rate (first multiplied by factor)
176 */
177 void MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2);
178 /**
179 * Checks data rate floating point multiplication
180 * @param rate1 first data rate
181 * @param factor multiplication factor
182 * @param rate2 second data rate (first multiplied by factor)
183 */
184 void MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2);
185
186 private:
187 void DoRun() override;
188};
189
191 : DataRateTestCase("Test arithmetic on DateRate")
192{
193}
194
195void
196DataRateTestCase2::AdditionTest(std::string rate1, std::string rate2, std::string rate3)
197{
198 DataRate dr1(rate1);
199 DataRate dr2(rate2);
200 DataRate dr3(rate3);
201
202 CheckDataRateEqual(dr1 + dr2, dr3, "DataRate Addition returned incorrect value");
203
204 dr1 += dr2;
205 CheckDataRateEqual(dr1, dr3, "DataRate Addition returned incorrect value");
206}
207
208void
209DataRateTestCase2::SubtractionTest(std::string rate1, std::string rate2, std::string rate3)
210{
211 DataRate dr1(rate1);
212 DataRate dr2(rate2);
213 DataRate dr3(rate3);
214
215 CheckDataRateEqual(dr1 - dr2, dr3, "DataRate Subtraction returned incorrect value");
216
217 dr1 -= dr2;
218 CheckDataRateEqual(dr1, dr3, "DataRate Subtraction returned incorrect value");
219}
220
221void
222DataRateTestCase2::MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2)
223{
224 DataRate dr1(rate1);
225 DataRate dr2(rate2);
226
227 CheckDataRateEqual(dr1 * factor,
228 dr2,
229 "DataRate Multiplication with Int returned incorrect value");
230
231 dr1 *= factor;
232 CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Int returned incorrect value");
233}
234
235void
236DataRateTestCase2::MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2)
237{
238 DataRate dr1(rate1);
239 DataRate dr2(rate2);
240
241 CheckDataRateEqual(dr1 * factor,
242 dr2,
243 "DataRate Multiplication with Double returned incorrect value");
244
245 dr1 *= factor;
246 CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Double returned incorrect value");
247}
248
249void
251{
252 AdditionTest("1Mb/s", "3Mb/s", "4Mb/s");
253 AdditionTest("1Gb/s", "1b/s", "1000000001b/s");
254 SubtractionTest("1Mb/s", "1b/s", "999999b/s");
255 SubtractionTest("2Gb/s", "2Gb/s", "0Gb/s");
256 MultiplicationIntTest("5Gb/s", 2, "10Gb/s");
257 MultiplicationIntTest("4Mb/s", 1000, "4Gb/s");
258 MultiplicationDoubleTest("1Gb/s", 0.001, "1Mb/s");
259 MultiplicationDoubleTest("6Gb/s", 1.0 / 7.0, "857142857.14b/s");
260}
261
262/**
263 * @ingroup network-test
264 * @ingroup tests
265 *
266 * Object with an attribute that is a tuple of data rates and a string.
267 */
269{
270 public:
271 ~DataRateTupleObject() override = default;
272
273 /**
274 * @brief Get the type ID.
275 * @return The object TypeId.
276 */
277 static TypeId GetTypeId();
278
279 std::tuple<DataRate, DataRate, std::string> m_tupleRatesString; //!< tuple of two data rates
280 //!< and a string
281};
282
283TypeId
285{
286 static TypeId tid =
287 TypeId("ns3::DataRateTupleObject")
288 .SetParent<Object>()
289 .SetGroupName("Test")
290 .AddConstructor<DataRateTupleObject>()
291 .AddAttribute(
292 "TupleRatesString",
293 "An example of tuple of two data rates (comprising value and unit, possibly "
294 "separated by a white space) and a string (possibly containing a white space).",
295 StringValue("{1 Mb/s, 10kb/s, test string}"),
301 return tid;
302}
303
304/**
305 * @ingroup network-test
306 * @ingroup tests
307 *
308 * Attribute set and get TestCase.
309 */
311{
312 public:
314 ~DataRateTupleSetGetTestCase() override = default;
315
316 private:
317 void DoRun() override;
318};
319
321 : TestCase("test attribute set and get")
322{
323}
324
325void
327{
329
330 // check that the tuple of two data rates and a string was correctly initialized
331 NS_TEST_EXPECT_MSG_EQ(std::get<0>(obj->m_tupleRatesString),
332 DataRate(1e6),
333 "Unexpected value for the first data rate");
334 NS_TEST_EXPECT_MSG_EQ(std::get<1>(obj->m_tupleRatesString),
335 DataRate(1e4),
336 "Unexpected value for the second data rate");
337 NS_TEST_EXPECT_MSG_EQ(std::get<2>(obj->m_tupleRatesString),
338 "test string",
339 "Unexpected value for the string");
340}
341
342/**
343 * @ingroup network-test
344 * @ingroup tests
345 *
346 * @brief DataRate TestSuite
347 */
349{
350 public:
352};
353
355 : TestSuite("data-rate", Type::UNIT)
356{
357 AddTestCase(new DataRateTestCase1(), TestCase::Duration::QUICK);
358 AddTestCase(new DataRateTestCase2(), TestCase::Duration::QUICK);
359 AddTestCase(new DataRateTupleSetGetTestCase(), TestCase::Duration::QUICK);
360}
361
362static DataRateTestSuite sDataRateTestSuite; //!< Static variable for test initialization
Test Data rate.
void DoRun() override
Implementation to actually run this TestCase.
void SingleTest(std::string rate, size_t nBits, Time correctTime)
Checks that a given number of bits, at a specified datarate, are corresponding to a given time.
Test Data rate.
void MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2)
Checks data rate integer multiplication.
void DoRun() override
Implementation to actually run this TestCase.
void MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2)
Checks data rate floating point multiplication.
void SubtractionTest(std::string rate1, std::string rate2, std::string rate3)
Checks data rate subtraction.
void AdditionTest(std::string rate1, std::string rate2, std::string rate3)
Checks data rate addition.
Test Data rate.
~DataRateTestCase() override
void CheckTimesEqual(Time t1, Time t2, const std::string msg)
Checks if two time values are equal.
void CheckDataRateEqual(DataRate d1, DataRate d2, const std::string msg)
Checks if two data rates values are equal.
void DoRun() override=0
Implementation to actually run this TestCase.
DataRateTestCase(std::string name)
Constructor.
DataRate TestSuite.
Object with an attribute that is a tuple of data rates and a string.
~DataRateTupleObject() override=default
static TypeId GetTypeId()
Get the type ID.
std::tuple< DataRate, DataRate, std::string > m_tupleRatesString
tuple of two data rates and a string
Attribute set and get TestCase.
~DataRateTupleSetGetTestCase() override=default
void DoRun() override
Implementation to actually run this TestCase.
Class for representing data rates.
Definition data-rate.h:78
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition data-rate.cc:227
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition data-rate.cc:220
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
Definition string.h:45
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
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetFemtoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:417
static Unit GetResolution()
Definition time.cc:396
@ FS
femtosecond
Definition nstime.h:110
static void SetResolution(Unit resolution)
Definition time.cc:202
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
High precision numerical type, implementing Q64.64 fixed precision.
Ptr< const AttributeChecker > MakeDataRateChecker()
Definition data-rate.cc:20
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
Ptr< const AttributeChecker > MakeTupleChecker(Ts... checkers)
Create a TupleChecker from AttributeCheckers associated with TupleValue elements.
Definition tuple.h:532
Ptr< const AttributeAccessor > MakeTupleAccessor(T1 a1)
Create an AttributeAccessor for a class data member of type tuple, or a lone class get functor or set...
Definition tuple.h:539
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
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1369
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1393
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1405
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static DataRateTestSuite sDataRateTestSuite
Static variable for test initialization.