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
14using namespace ns3;
15
16/**
17 * @ingroup network-test
18 * @ingroup tests
19 *
20 * @brief Test Data rate
21 *
22 */
24{
25 public:
26 /**
27 * Constructor
28 * @param name test name
29 */
30 DataRateTestCase(std::string name);
31 ~DataRateTestCase() override;
32
33 /**
34 * Checks if two time values are equal
35 * @param t1 first time to check
36 * @param t2 second time to check
37 * @param msg check output message
38 */
39 void CheckTimesEqual(Time t1, Time t2, const std::string msg);
40 /**
41 * Checks if two data rates values are equal
42 * @param d1 first data rate to check
43 * @param d2 second data rate to check
44 * @param msg check output message
45 */
46 void CheckDataRateEqual(DataRate d1, DataRate d2, const std::string msg);
47
48 protected:
49 void DoRun() override = 0;
50};
51
53 : TestCase(name)
54{
55}
56
60
61void
62DataRateTestCase::CheckTimesEqual(Time actual, Time correct, const std::string msg)
63{
64 int64x64_t actualFemtos = actual.GetFemtoSeconds();
65 int64x64_t correctFemtos = correct.GetFemtoSeconds();
66 NS_TEST_EXPECT_MSG_EQ(actualFemtos, correctFemtos, msg);
67}
68
69void
71{
72 NS_TEST_EXPECT_MSG_EQ(d1, d2, msg);
73}
74
75/**
76 * @ingroup network-test
77 * @ingroup tests
78 *
79 * @brief Test Data rate
80 *
81 */
83{
84 public:
86
87 /**
88 * Checks that a given number of bits, at a specified datarate, are
89 * corresponding to a given time
90 * @param rate the DataRate
91 * @param nBits number of bits
92 * @param correctTime expected time
93 */
94 void SingleTest(std::string rate, size_t nBits, Time correctTime);
95
96 private:
97 void DoRun() override;
98};
99
101 : DataRateTestCase("Test rounding of conversion from DataRate to time")
102{
103}
104
105void
106DataRateTestCase1::SingleTest(std::string rate, size_t nBits, Time correctTime)
107{
108 DataRate dr(rate);
109 Time bitsTime = dr.CalculateBitsTxTime(nBits);
110 CheckTimesEqual(bitsTime, correctTime, "CalculateBitsTxTime returned incorrect value");
111 if ((nBits % 8) == 0)
112 {
113 Time bytesTime = dr.CalculateBytesTxTime(nBits / 8);
114 CheckTimesEqual(bytesTime, correctTime, "CalculateBytesTxTime returned incorrect value");
115 }
116}
117
118void
120{
122 {
124 }
125 SingleTest("1GB/s", 512, NanoSeconds(64));
126 SingleTest("8Gb/s", 512, NanoSeconds(64));
127 SingleTest("1Gb/s", 512, NanoSeconds(512));
128 SingleTest("8GB/s", 512, NanoSeconds(8));
129 size_t nBits;
130 for (nBits = 0; nBits <= 512; nBits++)
131 {
132 SingleTest("1Mb/s", nBits, MicroSeconds(nBits));
133 SingleTest("10Mb/s", nBits, NanoSeconds(nBits * 100));
134 SingleTest("100Mb/s", nBits, NanoSeconds(nBits * 10));
135 SingleTest("1Gb/s", nBits, NanoSeconds(nBits));
136 SingleTest("10Gb/s", nBits, PicoSeconds(nBits * 100));
137 SingleTest("25Gb/s", nBits, PicoSeconds(nBits * 40));
138 SingleTest("40Gb/s", nBits, PicoSeconds(nBits * 25));
139 SingleTest("100Gb/s", nBits, PicoSeconds(nBits * 10));
140 SingleTest("200Gb/s", nBits, PicoSeconds(nBits * 5));
141 SingleTest("400Gb/s", nBits, FemtoSeconds(nBits * 2500));
142 }
143}
144
145/**
146 * @ingroup network-test
147 * @ingroup tests
148 *
149 * @brief Test Data rate
150 *
151 */
153{
154 public:
156 /**
157 * Checks data rate addition
158 * @param rate1 first data rate
159 * @param rate2 second data rate
160 * @param rate3 third data rate (first plus second)
161 */
162 void AdditionTest(std::string rate1, std::string rate2, std::string rate3);
163 /**
164 * Checks data rate subtraction
165 * @param rate1 first data rate
166 * @param rate2 second data rate
167 * @param rate3 third data rate (first minus second)
168 */
169 void SubtractionTest(std::string rate1, std::string rate2, std::string rate3);
170 /**
171 * Checks data rate integer multiplication
172 * @param rate1 first data rate
173 * @param factor multiplication factor
174 * @param rate2 second data rate (first multiplied by factor)
175 */
176 void MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2);
177 /**
178 * Checks data rate floating point multiplication
179 * @param rate1 first data rate
180 * @param factor multiplication factor
181 * @param rate2 second data rate (first multiplied by factor)
182 */
183 void MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2);
184
185 private:
186 void DoRun() override;
187};
188
190 : DataRateTestCase("Test arithmetic on DateRate")
191{
192}
193
194void
195DataRateTestCase2::AdditionTest(std::string rate1, std::string rate2, std::string rate3)
196{
197 DataRate dr1(rate1);
198 DataRate dr2(rate2);
199 DataRate dr3(rate3);
200
201 CheckDataRateEqual(dr1 + dr2, dr3, "DataRate Addition returned incorrect value");
202
203 dr1 += dr2;
204 CheckDataRateEqual(dr1, dr3, "DataRate Addition returned incorrect value");
205}
206
207void
208DataRateTestCase2::SubtractionTest(std::string rate1, std::string rate2, std::string rate3)
209{
210 DataRate dr1(rate1);
211 DataRate dr2(rate2);
212 DataRate dr3(rate3);
213
214 CheckDataRateEqual(dr1 - dr2, dr3, "DataRate Subtraction returned incorrect value");
215
216 dr1 -= dr2;
217 CheckDataRateEqual(dr1, dr3, "DataRate Subtraction returned incorrect value");
218}
219
220void
221DataRateTestCase2::MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2)
222{
223 DataRate dr1(rate1);
224 DataRate dr2(rate2);
225
226 CheckDataRateEqual(dr1 * factor,
227 dr2,
228 "DataRate Multiplication with Int returned incorrect value");
229
230 dr1 *= factor;
231 CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Int returned incorrect value");
232}
233
234void
235DataRateTestCase2::MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2)
236{
237 DataRate dr1(rate1);
238 DataRate dr2(rate2);
239
240 CheckDataRateEqual(dr1 * factor,
241 dr2,
242 "DataRate Multiplication with Double returned incorrect value");
243
244 dr1 *= factor;
245 CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Double returned incorrect value");
246}
247
248void
250{
251 AdditionTest("1Mb/s", "3Mb/s", "4Mb/s");
252 AdditionTest("1Gb/s", "1b/s", "1000000001b/s");
253 SubtractionTest("1Mb/s", "1b/s", "999999b/s");
254 SubtractionTest("2Gb/s", "2Gb/s", "0Gb/s");
255 MultiplicationIntTest("5Gb/s", 2, "10Gb/s");
256 MultiplicationIntTest("4Mb/s", 1000, "4Gb/s");
257 MultiplicationDoubleTest("1Gb/s", 0.001, "1Mb/s");
258 MultiplicationDoubleTest("6Gb/s", 1.0 / 7.0, "857142857.14b/s");
259}
260
261/**
262 * @ingroup network-test
263 * @ingroup tests
264 *
265 * @brief DataRate TestSuite
266 */
268{
269 public:
271};
272
274 : TestSuite("data-rate", Type::UNIT)
275{
276 AddTestCase(new DataRateTestCase1(), TestCase::Duration::QUICK);
277 AddTestCase(new DataRateTestCase2(), TestCase::Duration::QUICK);
278}
279
280static 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.
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
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:397
@ FS
femtosecond
Definition nstime.h:110
static void SetResolution(Unit resolution)
Definition time.cc:202
High precision numerical type, implementing Q64.64 fixed precision.
#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:1368
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1380
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1392
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1404
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static DataRateTestSuite sDataRateTestSuite
Static variable for test initialization.