A Discrete-Event Network Simulator
API
time-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  * Copyright (c) 2007 Emmanuelle Laprise
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * TimeStep support by Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
21  */
22 
23 #include <iomanip>
24 #include <iostream>
25 #include <string>
26 #include <sstream>
27 
28 #include "ns3/nstime.h"
29 #include "ns3/int64x64.h"
30 #include "ns3/test.h"
31 
32 using namespace ns3;
33 
35 {
36 public:
38 private:
39  virtual void DoSetup (void);
40  virtual void DoRun (void);
41  virtual void DoTeardown (void);
42 };
43 
45  : TestCase ("Sanity check of common time operations")
46 {
47 }
48 
49 void
51 {
52 }
53 
54 void
56 {
57  NS_TEST_ASSERT_MSG_EQ_TOL (Years (1.0).GetYears (), 1.0, Years (1).GetYears (),
58  "is 1 really 1 ?");
59  NS_TEST_ASSERT_MSG_EQ_TOL (Years (10.0).GetYears (), 10.0, Years (1).GetYears (),
60  "is 10 really 10 ?");
61  NS_TEST_ASSERT_MSG_EQ_TOL (Days (1.0).GetDays (), 1.0, Days (1).GetDays (),
62  "is 1 really 1 ?");
63  NS_TEST_ASSERT_MSG_EQ_TOL (Days (10.0).GetDays (), 10.0, Days (1).GetDays (),
64  "is 10 really 10 ?");
65  NS_TEST_ASSERT_MSG_EQ_TOL (Hours (1.0).GetHours (), 1.0, Hours (1).GetHours (),
66  "is 1 really 1 ?");
67  NS_TEST_ASSERT_MSG_EQ_TOL (Hours (10.0).GetHours (), 10.0, Hours (1).GetHours (),
68  "is 10 really 10 ?");
69  NS_TEST_ASSERT_MSG_EQ_TOL (Minutes (1.0).GetMinutes (), 1.0, Minutes (1).GetMinutes (),
70  "is 1 really 1 ?");
71  NS_TEST_ASSERT_MSG_EQ_TOL (Minutes (10.0).GetMinutes (), 10.0, Minutes (1).GetMinutes (),
72  "is 10 really 10 ?");
73  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (1.0).GetSeconds (), 1.0, TimeStep (1).GetSeconds (),
74  "is 1 really 1 ?");
75  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (10.0).GetSeconds (), 10.0, TimeStep (1).GetSeconds (),
76  "is 10 really 10 ?");
77  NS_TEST_ASSERT_MSG_EQ (MilliSeconds (1).GetMilliSeconds (), 1,
78  "is 1ms really 1ms ?");
79  NS_TEST_ASSERT_MSG_EQ (MicroSeconds (1).GetMicroSeconds (), 1,
80  "is 1us really 1us ?");
81 #if 0
82  Time ns = NanoSeconds (1);
83  ns.GetNanoSeconds ();
84  NS_TEST_ASSERT_MSG_EQ (NanoSeconds (1).GetNanoSeconds (), 1,
85  "is 1ns really 1ns ?");
86  NS_TEST_ASSERT_MSG_EQ (PicoSeconds (1).GetPicoSeconds (), 1,
87  "is 1ps really 1ps ?");
88  NS_TEST_ASSERT_MSG_EQ (FemtoSeconds (1).GetFemtoSeconds (), 1,
89  "is 1fs really 1fs ?");
90 #endif
91 
92  Time ten = NanoSeconds (10);
93  int64_t tenValue = ten.GetInteger ();
94  Time::SetResolution (Time::PS);
95  int64_t tenKValue = ten.GetInteger ();
96  NS_TEST_ASSERT_MSG_EQ (tenValue * 1000, tenKValue,
97  "change resolution to PS");
98 }
99 
100 void
102 {
103 }
104 
106 {
107 public:
109 private:
110  virtual void DoSetup (void);
111  virtual void DoRun (void);
112  virtual void DoTeardown (void);
113 };
114 
116  : TestCase ("Checks times that have plus or minus signs")
117 {
118 }
119 
120 void
122 {
123 }
124 
125 void
127 {
128  Time timePositive ("+1000.0");
129  Time timePositiveWithUnits ("+1000.0ms");
130 
131  Time timeNegative ("-1000.0");
132  Time timeNegativeWithUnits ("-1000.0ms");
133 
134  NS_TEST_ASSERT_MSG_EQ_TOL (timePositive.GetSeconds (),
135  +1000.0,
136  1.0e-8,
137  "Positive time not parsed correctly.");
138 
139  NS_TEST_ASSERT_MSG_EQ_TOL (timePositiveWithUnits.GetSeconds (),
140  +1.0,
141  1.0e-8,
142  "Positive time with units not parsed correctly.");
143 
144  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegative.GetSeconds (),
145  -1000.0,
146  1.0e-8,
147  "Negative time not parsed correctly.");
148 
149  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegativeWithUnits.GetSeconds (),
150  -1.0,
151  1.0e-8,
152  "Negative time with units not parsed correctly.");
153 }
154 
155 void
157 {
158 }
159 
160 
162 {
163 public:
165 private:
166  virtual void DoRun (void);
167  void Check (const std::string & str);
168 };
169 
171  : TestCase ("Input,output from,to strings")
172 {
173 }
174 
175 void
176 TimeInputOutputTestCase::Check (const std::string & str)
177 {
178  std::stringstream ss (str);
179  Time time;
180  ss >> time;
181  ss << time;
182  bool pass = (str == ss.str ());
183 
184  std::cout << GetParent ()->GetName () << " InputOutput: "
185  << (pass ? "pass " : "FAIL ")
186  << "\"" << str << "\"";
187  if (!pass)
188  {
189  std::cout << ", got " << ss.str ();
190  }
191  std::cout << std::endl;
192 }
193 
194 void
196 {
197  std::cout << std::endl;
198  std::cout << GetParent ()->GetName () << " InputOutput: " << GetName ()
199  << std::endl;
200 
201  Check ("2ns");
202  Check ("+3.1us");
203  Check ("-4.2ms");
204  Check ("5.3s");
205  Check ("6.4min");
206  Check ("7.5h");
207  Check ("8.6d");
208  Check ("10.8y");
209 
210  Time t (3.141592654e9); // Pi seconds
211 
212  std::cout << GetParent ()->GetName () << " InputOutput: "
213  << "example: raw: " << t
214  << std::endl;
215 
216  std::cout << GetParent ()->GetName () << " InputOutput: "
217  << std::fixed << std::setprecision (9)
218  << "example: in s: " << t.As (Time::S)
219  << std::endl;
220 
221  std::cout << GetParent ()->GetName () << " InputOutput: "
222  << std::setprecision (6)
223  << "example: in ms: " << t.As (Time::MS)
224  << std::endl;
225 
226  std::cout << GetParent ()->GetName () << " InputOutput: "
227  << "example: Get ns: " << t.GetNanoSeconds ()
228  << std::endl;
229 
230  std::cout << std::endl;
231 }
232 
233 static class TimeTestSuite : public TestSuite
234 {
235 public:
237  : TestSuite ("time", UNIT)
238  {
239  AddTestCase (new TimeWithSignTestCase (), TestCase::QUICK);
240  AddTestCase (new TimeInputOutputTestCase (), TestCase::QUICK);
241  // This should be last, since it changes the resolution
242  AddTestCase (new TimeSimpleTestCase (), TestCase::QUICK);
243  }
245 
246 
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
A suite of tests to run.
Definition: test.h:1333
TimeTestSuite g_timeTestSuite
void Check(const std::string &str)
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:927
virtual void DoRun(void)
Implementation to actually run this TestCase.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
Time Years(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:863
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:919
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:161
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:373
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TestCase * GetParent() const
Get the parent of this TestCsse.
Definition: test.cc:375
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:879
Time TimeStep(uint64_t ts)
Definition: nstime.h:952
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:385
Time Minutes(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:887
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:353
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
TimeWithUnit As(const enum Unit unit) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:388
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
std::string GetName(void) const
Definition: test.cc:369
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:911
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:871
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:935