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 
28 class DataRateTestCase : public TestCase
29 {
30 public:
31  DataRateTestCase (std::string name);
32  virtual ~DataRateTestCase ();
33  void CheckTimesEqual (Time t1, Time t2, const std::string msg);
34 
35 protected:
36  virtual void DoRun (void) = 0;
37 };
38 
39 DataRateTestCase::DataRateTestCase (std::string name) : TestCase (name)
40 {
41 }
42 
44 {
45 }
46 
47 void
48 DataRateTestCase::CheckTimesEqual (Time actual, Time correct, const std::string msg)
49 {
50  int64x64_t actualFemtos = actual.GetFemtoSeconds ();
51  int64x64_t correctFemtos = correct.GetFemtoSeconds ();
52  NS_TEST_EXPECT_MSG_EQ (actualFemtos, correctFemtos, msg);
53 }
54 
56 {
57 public:
59  void SingleTest (std::string rate, size_t nBits, Time correctTime);
60 
61 private:
62  virtual void DoRun (void);
63 };
64 
66  : DataRateTestCase ("Test rounding of conversion from DataRate to time")
67 {
68 }
69 
70 void
71 DataRateTestCase1::SingleTest (std::string rate, size_t nBits, Time correctTime)
72 {
73  DataRate dr (rate);
74  Time bitsTime = dr.CalculateBitsTxTime (nBits);
75  CheckTimesEqual (bitsTime, correctTime, "CalculateBitsTxTime returned incorrect value");
76  if ((nBits % 8) == 0)
77  {
78  Time bytesTime = dr.CalculateBytesTxTime (nBits / 8);
79  CheckTimesEqual (bytesTime, correctTime, "CalculateBytesTxTime returned incorrect value");
80  }
81 }
82 
83 void
85 {
86  if (Time::GetResolution () != Time::FS)
87  {
88  Time::SetResolution (Time::FS);
89  }
90  SingleTest ("1GB/s", 512, Time (NanoSeconds (64)));
91  SingleTest ("8Gb/s", 512, Time (NanoSeconds (64)));
92  SingleTest ("1Gb/s", 512, Time (NanoSeconds (512)));
93  SingleTest ("8GB/s", 512, Time (NanoSeconds (8)));
94  size_t nBits;
95  for (nBits = 0; nBits <= 512; nBits++)
96  {
97  SingleTest ("1Mb/s", nBits, Time (MicroSeconds (nBits)));
98  SingleTest ("10Mb/s", nBits, Time (NanoSeconds (nBits * 100)));
99  SingleTest ("100Mb/s", nBits, Time (NanoSeconds (nBits * 10)));
100  SingleTest ("1Gb/s", nBits, Time (NanoSeconds (nBits)));
101  SingleTest ("10Gb/s", nBits, Time (PicoSeconds (nBits * 100)));
102  SingleTest ("25Gb/s", nBits, Time (PicoSeconds (nBits * 40)));
103  SingleTest ("40Gb/s", nBits, Time (PicoSeconds (nBits * 25)));
104  SingleTest ("100Gb/s", nBits, Time (PicoSeconds (nBits * 10)));
105  SingleTest ("200Gb/s", nBits, Time (PicoSeconds (nBits * 5)));
106  SingleTest ("400Gb/s", nBits, Time (FemtoSeconds (nBits * 2500)));
107  }
108 }
109 
111 {
112 public:
114 };
115 
117 {
118  AddTestCase (new DataRateTestCase1 (), TestCase::QUICK);
119 }
120 
121 // Do not forget to allocate an instance of this TestSuite
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
DataRateTestCase(std::string name)
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:45
A suite of tests to run.
Definition: test.h:1343
#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)
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1310
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:1302
virtual ~DataRateTestCase()
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition: data-rate.cc:235
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1294
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1318
int64_t GetFemtoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:399
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:229
static DataRateTestSuite sDataRateTestSuite
void SingleTest(std::string rate, size_t nBits, Time correctTime)
virtual void DoRun(void)
Implementation to actually run this TestCase.