A Discrete-Event Network Simulator
API
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 
25 using namespace ns3;
26 
34 class DataRateTestCase : public TestCase
35 {
36  public:
41  DataRateTestCase(std::string name);
42  ~DataRateTestCase() override;
43 
50  void CheckTimesEqual(Time t1, Time t2, const std::string msg);
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 
72 void
73 DataRateTestCase::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 
80 void
82 {
83  NS_TEST_EXPECT_MSG_EQ(d1, d2, msg);
84 }
85 
94 {
95  public:
97 
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 
116 void
117 DataRateTestCase1::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 
129 void
131 {
132  if (Time::GetResolution() != Time::FS)
133  {
134  Time::SetResolution(Time::FS);
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 
164 {
165  public:
173  void AdditionTest(std::string rate1, std::string rate2, std::string rate3);
180  void SubtractionTest(std::string rate1, std::string rate2, std::string rate3);
187  void MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2);
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 
205 void
206 DataRateTestCase2::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 
218 void
219 DataRateTestCase2::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 
231 void
232 DataRateTestCase2::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 
245 void
246 DataRateTestCase2::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 
259 void
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 
279 {
280  public:
282 };
283 
285  : TestSuite("data-rate", UNIT)
286 {
287  AddTestCase(new DataRateTestCase1(), TestCase::QUICK);
288  AddTestCase(new DataRateTestCase2(), TestCase::QUICK);
289 }
290 
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
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
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:251
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1374
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1386
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static DataRateTestSuite sDataRateTestSuite
Static variable for test initialization.