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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Greg Steinbrecher <grs@fb.com>
18 */
19
20#include "ns3/data-rate.h"
21#include "ns3/log.h"
22#include "ns3/simulator.h"
23#include "ns3/test.h"
24
25using namespace ns3;
26
27/**
28 * \ingroup network-test
29 * \ingroup tests
30 *
31 * \brief Test Data rate
32 *
33 */
35{
36 public:
37 /**
38 * Constructor
39 * \param name test name
40 */
41 DataRateTestCase(std::string name);
42 ~DataRateTestCase() override;
43
44 /**
45 * Checks if two time values are equal
46 * \param t1 first time to check
47 * \param t2 second time to check
48 * \param msg check output message
49 */
50 void CheckTimesEqual(Time t1, Time t2, const std::string msg);
51 /**
52 * Checks if two data rates values are equal
53 * \param d1 first data rate to check
54 * \param d2 second data rate to check
55 * \param msg check output message
56 */
57 void CheckDataRateEqual(DataRate d1, DataRate d2, const std::string msg);
58
59 protected:
60 void DoRun() override = 0;
61};
62
64 : TestCase(name)
65{
66}
67
69{
70}
71
72void
73DataRateTestCase::CheckTimesEqual(Time actual, Time correct, const std::string msg)
74{
75 int64x64_t actualFemtos = actual.GetFemtoSeconds();
76 int64x64_t correctFemtos = correct.GetFemtoSeconds();
77 NS_TEST_EXPECT_MSG_EQ(actualFemtos, correctFemtos, msg);
78}
79
80void
82{
83 NS_TEST_EXPECT_MSG_EQ(d1, d2, msg);
84}
85
86/**
87 * \ingroup network-test
88 * \ingroup tests
89 *
90 * \brief Test Data rate
91 *
92 */
94{
95 public:
97
98 /**
99 * Checks that a given number of bits, at a specified datarate, are
100 * corresponding to a given time
101 * \param rate the DataRate
102 * \param nBits number of bits
103 * \param correctTime expected time
104 */
105 void SingleTest(std::string rate, size_t nBits, Time correctTime);
106
107 private:
108 void DoRun() override;
109};
110
112 : DataRateTestCase("Test rounding of conversion from DataRate to time")
113{
114}
115
116void
117DataRateTestCase1::SingleTest(std::string rate, size_t nBits, Time correctTime)
118{
119 DataRate dr(rate);
120 Time bitsTime = dr.CalculateBitsTxTime(nBits);
121 CheckTimesEqual(bitsTime, correctTime, "CalculateBitsTxTime returned incorrect value");
122 if ((nBits % 8) == 0)
123 {
124 Time bytesTime = dr.CalculateBytesTxTime(nBits / 8);
125 CheckTimesEqual(bytesTime, correctTime, "CalculateBytesTxTime returned incorrect value");
126 }
127}
128
129void
131{
133 {
135 }
136 SingleTest("1GB/s", 512, Time(NanoSeconds(64)));
137 SingleTest("8Gb/s", 512, Time(NanoSeconds(64)));
138 SingleTest("1Gb/s", 512, Time(NanoSeconds(512)));
139 SingleTest("8GB/s", 512, Time(NanoSeconds(8)));
140 size_t nBits;
141 for (nBits = 0; nBits <= 512; nBits++)
142 {
143 SingleTest("1Mb/s", nBits, Time(MicroSeconds(nBits)));
144 SingleTest("10Mb/s", nBits, Time(NanoSeconds(nBits * 100)));
145 SingleTest("100Mb/s", nBits, Time(NanoSeconds(nBits * 10)));
146 SingleTest("1Gb/s", nBits, Time(NanoSeconds(nBits)));
147 SingleTest("10Gb/s", nBits, Time(PicoSeconds(nBits * 100)));
148 SingleTest("25Gb/s", nBits, Time(PicoSeconds(nBits * 40)));
149 SingleTest("40Gb/s", nBits, Time(PicoSeconds(nBits * 25)));
150 SingleTest("100Gb/s", nBits, Time(PicoSeconds(nBits * 10)));
151 SingleTest("200Gb/s", nBits, Time(PicoSeconds(nBits * 5)));
152 SingleTest("400Gb/s", nBits, Time(FemtoSeconds(nBits * 2500)));
153 }
154}
155
156/**
157 * \ingroup network-test
158 * \ingroup tests
159 *
160 * \brief Test Data rate
161 *
162 */
164{
165 public:
167 /**
168 * Checks data rate addition
169 * \param rate1 first data rate
170 * \param rate2 second data rate
171 * \param rate3 third data rate (first plus second)
172 */
173 void AdditionTest(std::string rate1, std::string rate2, std::string rate3);
174 /**
175 * Checks data rate subtraction
176 * \param rate1 first data rate
177 * \param rate2 second data rate
178 * \param rate3 third data rate (first minus second)
179 */
180 void SubtractionTest(std::string rate1, std::string rate2, std::string rate3);
181 /**
182 * Checks data rate integer multiplication
183 * \param rate1 first data rate
184 * \param factor multiplication factor
185 * \param rate2 second data rate (first multiplied by factor)
186 */
187 void MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2);
188 /**
189 * Checks data rate floating point multiplication
190 * \param rate1 first data rate
191 * \param factor multiplication factor
192 * \param rate2 second data rate (first multiplied by factor)
193 */
194 void MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2);
195
196 private:
197 void DoRun() override;
198};
199
201 : DataRateTestCase("Test arithmetic on DateRate")
202{
203}
204
205void
206DataRateTestCase2::AdditionTest(std::string rate1, std::string rate2, std::string rate3)
207{
208 DataRate dr1(rate1);
209 DataRate dr2(rate2);
210 DataRate dr3(rate3);
211
212 CheckDataRateEqual(dr1 + dr2, dr3, "DataRate Addition returned incorrect value");
213
214 dr1 += dr2;
215 CheckDataRateEqual(dr1, dr3, "DataRate Addition returned incorrect value");
216}
217
218void
219DataRateTestCase2::SubtractionTest(std::string rate1, std::string rate2, std::string rate3)
220{
221 DataRate dr1(rate1);
222 DataRate dr2(rate2);
223 DataRate dr3(rate3);
224
225 CheckDataRateEqual(dr1 - dr2, dr3, "DataRate Subtraction returned incorrect value");
226
227 dr1 -= dr2;
228 CheckDataRateEqual(dr1, dr3, "DataRate Subtraction returned incorrect value");
229}
230
231void
232DataRateTestCase2::MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2)
233{
234 DataRate dr1(rate1);
235 DataRate dr2(rate2);
236
237 CheckDataRateEqual(dr1 * factor,
238 dr2,
239 "DataRate Multiplication with Int returned incorrect value");
240
241 dr1 *= factor;
242 CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Int returned incorrect value");
243}
244
245void
246DataRateTestCase2::MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2)
247{
248 DataRate dr1(rate1);
249 DataRate dr2(rate2);
250
251 CheckDataRateEqual(dr1 * factor,
252 dr2,
253 "DataRate Multiplication with Double returned incorrect value");
254
255 dr1 *= factor;
256 CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Double returned incorrect value");
257}
258
259void
261{
262 AdditionTest("1Mb/s", "3Mb/s", "4Mb/s");
263 AdditionTest("1Gb/s", "1b/s", "1000000001b/s");
264 SubtractionTest("1Mb/s", "1b/s", "999999b/s");
265 SubtractionTest("2Gb/s", "2Gb/s", "0Gb/s");
266 MultiplicationIntTest("5Gb/s", 2, "10Gb/s");
267 MultiplicationIntTest("4Mb/s", 1000, "4Gb/s");
268 MultiplicationDoubleTest("1Gb/s", 0.001, "1Mb/s");
269 MultiplicationDoubleTest("6Gb/s", 1.0 / 7.0, "857142857.14b/s");
270}
271
272/**
273 * \ingroup network-test
274 * \ingroup tests
275 *
276 * \brief DataRate TestSuite
277 */
279{
280 public:
282};
283
285 : TestSuite("data-rate", Type::UNIT)
286{
287 AddTestCase(new DataRateTestCase1(), TestCase::Duration::QUICK);
288 AddTestCase(new DataRateTestCase2(), TestCase::Duration::QUICK);
289}
290
291static 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:89
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition: data-rate.cc:298
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:291
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
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetFemtoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:428
static Unit GetResolution()
Definition: time.cc:408
@ FS
femtosecond
Definition: nstime.h:121
static void SetResolution(Unit resolution)
Definition: time.cc:213
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
#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
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1343
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1355
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1367
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1379
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static DataRateTestSuite sDataRateTestSuite
Static variable for test initialization.