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 
39 private:
40  virtual void DoSetup (void);
41  virtual void DoRun (void);
42  virtual void DoTeardown (void);
43 };
44 
46  : TestCase ("Sanity check of common time operations")
47 {}
48 
49 void
51 {}
52 
53 void
55 {
56  NS_TEST_ASSERT_MSG_EQ_TOL (Years (1.0).GetYears (), 1.0, Years (1).GetYears (),
57  "is 1 really 1 ?");
58  NS_TEST_ASSERT_MSG_EQ_TOL (Years (10.0).GetYears (), 10.0, Years (1).GetYears (),
59  "is 10 really 10 ?");
60  NS_TEST_ASSERT_MSG_EQ_TOL (Days (1.0).GetDays (), 1.0, Days (1).GetDays (),
61  "is 1 really 1 ?");
62  NS_TEST_ASSERT_MSG_EQ_TOL (Days (10.0).GetDays (), 10.0, Days (1).GetDays (),
63  "is 10 really 10 ?");
64  NS_TEST_ASSERT_MSG_EQ_TOL (Hours (1.0).GetHours (), 1.0, Hours (1).GetHours (),
65  "is 1 really 1 ?");
66  NS_TEST_ASSERT_MSG_EQ_TOL (Hours (10.0).GetHours (), 10.0, Hours (1).GetHours (),
67  "is 10 really 10 ?");
68  NS_TEST_ASSERT_MSG_EQ_TOL (Minutes (1.0).GetMinutes (), 1.0, Minutes (1).GetMinutes (),
69  "is 1 really 1 ?");
70  NS_TEST_ASSERT_MSG_EQ_TOL (Minutes (10.0).GetMinutes (), 10.0, Minutes (1).GetMinutes (),
71  "is 10 really 10 ?");
72  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (1.0).GetSeconds (), 1.0, TimeStep (1).GetSeconds (),
73  "is 1 really 1 ?");
74  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (10.0).GetSeconds (), 10.0, TimeStep (1).GetSeconds (),
75  "is 10 really 10 ?");
76  NS_TEST_ASSERT_MSG_EQ (MilliSeconds (1).GetMilliSeconds (), 1,
77  "is 1ms really 1ms ?");
78  NS_TEST_ASSERT_MSG_EQ (MicroSeconds (1).GetMicroSeconds (), 1,
79  "is 1us really 1us ?");
80 #if 0
81  Time ns = NanoSeconds (1);
82  ns.GetNanoSeconds ();
83  NS_TEST_ASSERT_MSG_EQ (NanoSeconds (1).GetNanoSeconds (), 1,
84  "is 1ns really 1ns ?");
85  NS_TEST_ASSERT_MSG_EQ (PicoSeconds (1).GetPicoSeconds (), 1,
86  "is 1ps really 1ps ?");
87  NS_TEST_ASSERT_MSG_EQ (FemtoSeconds (1).GetFemtoSeconds (), 1,
88  "is 1fs really 1fs ?");
89 #endif
90 
91  Time ten = NanoSeconds (10);
92  int64_t tenValue = ten.GetInteger ();
93  Time::SetResolution (Time::PS);
94  int64_t tenKValue = ten.GetInteger ();
95  NS_TEST_ASSERT_MSG_EQ (tenValue * 1000, tenKValue,
96  "change resolution to PS");
97 }
98 
99 void
101 {}
102 
104 {
105 public:
107 
108 private:
109  virtual void DoSetup (void);
110  virtual void DoRun (void);
111  virtual void DoTeardown (void);
112 };
113 
115  : TestCase ("Checks times that have plus or minus signs")
116 {}
117 
118 void
120 {}
121 
122 void
124 {
125  Time timePositive ("+1000.0");
126  Time timePositiveWithUnits ("+1000.0ms");
127 
128  Time timeNegative ("-1000.0");
129  Time timeNegativeWithUnits ("-1000.0ms");
130 
131  NS_TEST_ASSERT_MSG_EQ_TOL (timePositive.GetSeconds (),
132  +1000.0,
133  1.0e-8,
134  "Positive time not parsed correctly.");
135 
136  NS_TEST_ASSERT_MSG_EQ_TOL (timePositiveWithUnits.GetSeconds (),
137  +1.0,
138  1.0e-8,
139  "Positive time with units not parsed correctly.");
140 
141  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegative.GetSeconds (),
142  -1000.0,
143  1.0e-8,
144  "Negative time not parsed correctly.");
145 
146  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegativeWithUnits.GetSeconds (),
147  -1.0,
148  1.0e-8,
149  "Negative time with units not parsed correctly.");
150 }
151 
152 void
154 {}
155 
156 
158 {
159 public:
161 
162 private:
163  virtual void DoRun (void);
164  void Check (const std::string & str);
165 };
166 
168  : TestCase ("Input,output from,to strings")
169 {}
170 
171 void
172 TimeInputOutputTestCase::Check (const std::string & str)
173 {
174  std::stringstream ss (str);
175  Time time;
176  ss >> time;
177  ss << time;
178  bool pass = (str == ss.str ());
179 
180  std::cout << GetParent ()->GetName () << " InputOutput: "
181  << (pass ? "pass " : "FAIL ")
182  << "\"" << str << "\"";
183  if (!pass)
184  {
185  std::cout << ", got " << ss.str ();
186  }
187  std::cout << std::endl;
188 }
189 
190 void
192 {
193  std::cout << std::endl;
194  std::cout << GetParent ()->GetName () << " InputOutput: " << GetName ()
195  << std::endl;
196 
197  Check ("2ns");
198  Check ("+3.1us");
199  Check ("-4.2ms");
200  Check ("5.3s");
201  Check ("6.4min");
202  Check ("7.5h");
203  Check ("8.6d");
204  Check ("10.8y");
205 
206  Time t (3.141592654e9); // Pi seconds
207 
208  std::cout << GetParent ()->GetName () << " InputOutput: "
209  << "example: raw: " << t
210  << std::endl;
211 
212  std::cout << GetParent ()->GetName () << " InputOutput: "
213  << std::fixed << std::setprecision (9)
214  << "example: in s: " << t.As (Time::S)
215  << std::endl;
216 
217  std::cout << GetParent ()->GetName () << " InputOutput: "
218  << std::setprecision (6)
219  << "example: in ms: " << t.As (Time::MS)
220  << std::endl;
221 
222  std::cout << GetParent ()->GetName () << " InputOutput: "
223  << "example: Get ns: " << t.GetNanoSeconds ()
224  << std::endl;
225 
226  std::cout << std::endl;
227 }
228 
229 static class TimeTestSuite : public TestSuite
230 {
231 public:
233  : TestSuite ("time", UNIT)
234  {
235  AddTestCase (new TimeWithSignTestCase (), TestCase::QUICK);
236  AddTestCase (new TimeInputOutputTestCase (), TestCase::QUICK);
237  // This should be last, since it changes the resolution
238  AddTestCase (new TimeSimpleTestCase (), TestCase::QUICK);
239  }
241 
242 
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
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:405
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
A suite of tests to run.
Definition: test.h:1343
TimeTestSuite g_timeTestSuite
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:361
void Check(const std::string &str)
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
encapsulates test code
Definition: test.h:1153
TimeWithUnit As(const enum Unit unit) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:389
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1094
virtual void DoRun(void)
Implementation to actually run this TestCase.
Time Years(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1030
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
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:1086
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:166
#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:378
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:373
TestCase * GetParent() const
Get the parent of this TestCsse.
Definition: test.cc:376
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1046
Time TimeStep(uint64_t ts)
Definition: nstime.h:1119
Time Minutes(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1054
std::string GetName(void) const
Definition: test.cc:370
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1078
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1038
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1102
This test suite implements a Unit Test.
Definition: test.h:1353