A Discrete-Event Network Simulator
API
test-data-rate.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) Facebook, Inc. and its affiliates.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Greg Steinbrecher <grs@fb.com>
19  */
20 
21 #include "ns3/data-rate.h"
22 #include "ns3/log.h"
23 #include "ns3/test.h"
24 #include "ns3/simulator.h"
25 
26 using namespace ns3;
27 
35 class DataRateTestCase : public TestCase
36 {
37 public:
42  DataRateTestCase (std::string name);
43  virtual ~DataRateTestCase ();
44 
51  void CheckTimesEqual (Time t1, Time t2, const std::string msg);
58  void CheckDataRateEqual (DataRate d1, DataRate d2, const std::string msg);
59 
60 protected:
61  virtual void DoRun (void) = 0;
62 };
63 
64 DataRateTestCase::DataRateTestCase (std::string name) : 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  virtual void DoRun (void);
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:
166  DataRateTestCase2 ();
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  virtual void DoRun (void);
198 };
199 
201  : DataRateTestCase ("Test arithmatic 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, dr2, "DataRate Multiplication with Int returned incorrect value");
238 
239  dr1 *= factor;
240  CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Int returned incorrect value");
241 }
242 
243 void
244 DataRateTestCase2::MultiplicationDoubleTest (std::string rate1, double factor, std::string rate2)
245 {
246  DataRate dr1 (rate1);
247  DataRate dr2 (rate2);
248 
249  CheckDataRateEqual(dr1 * factor, dr2, "DataRate Multiplication with Double returned incorrect value");
250 
251  dr1 *= factor;
252  CheckDataRateEqual(dr1, dr2, "DataRate Multiplication with Double returned incorrect value");
253 }
254 
255 void
257 {
258  AdditionTest("1Mb/s", "3Mb/s", "4Mb/s");
259  AdditionTest("1Gb/s", "1b/s", "1000000001b/s");
260  SubtractionTest("1Mb/s", "1b/s", "999999b/s");
261  SubtractionTest("2Gb/s", "2Gb/s", "0Gb/s");
262  MultiplicationIntTest("5Gb/s", 2, "10Gb/s");
263  MultiplicationIntTest("4Mb/s", 1000, "4Gb/s");
264  MultiplicationDoubleTest("1Gb/s", 0.001, "1Mb/s");
265  MultiplicationDoubleTest("6Gb/s", 1.0/7.0, "857142857.14b/s");
266 }
267 
275 {
276 public:
278 };
279 
281 {
282  AddTestCase (new DataRateTestCase1 (), TestCase::QUICK);
283  AddTestCase (new DataRateTestCase2 (), TestCase::QUICK);
284 }
285 
void SubtractionTest(std::string rate1, std::string rate2, std::string rate3)
Checks data rate subtraction.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
DataRateTestCase(std::string name)
Constructor.
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:45
A suite of tests to run.
Definition: test.h:1343
Test Data rate.
#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:283
encapsulates test code
Definition: test.h:1153
void CheckTimesEqual(Time t1, Time t2, const std::string msg)
Checks if two time values are equal.
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1321
Class for representing data rates.
Definition: data-rate.h:88
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1313
Test Data rate.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void MultiplicationIntTest(std::string rate1, uint64_t factor, std::string rate2)
Checks data rate integer multiplication.
virtual ~DataRateTestCase()
DataRate TestSuite.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void AdditionTest(std::string rate1, std::string rate2, std::string rate3)
Checks data rate addition.
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition: data-rate.cc:281
void MultiplicationDoubleTest(std::string rate1, double factor, std::string rate2)
Checks data rate floating point multiplication.
void CheckDataRateEqual(DataRate d1, DataRate d2, const std::string msg)
Checks if two data rates values are equal.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1305
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1329
int64_t GetFemtoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:400
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:275
Test Data rate.
static DataRateTestSuite sDataRateTestSuite
Static variable for test initialization.
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...
virtual void DoRun(void)
Implementation to actually run this TestCase.