A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
time-test-suite.cc
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 #include "ns3/nstime.h"
23 #include "ns3/test.h"
24 
25 using namespace ns3;
26 
28 {
29 public:
30  TimeSimpleTestCase (enum Time::Unit resolution);
31 private:
32  virtual void DoSetup (void);
33  virtual void DoRun (void);
34  virtual void DoTeardown (void);
35  enum Time::Unit m_originalResolution;
36  enum Time::Unit m_resolution;
37 };
38 
39 TimeSimpleTestCase::TimeSimpleTestCase (enum Time::Unit resolution)
40  : TestCase ("Sanity check of common time operations"),
41  m_resolution (resolution)
42 {
43 }
44 
45 void
47 {
48  m_originalResolution = Time::GetResolution ();
49 }
50 
51 void
53 {
54  Time::SetResolution (m_resolution);
55  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (1.0).GetSeconds (), 1.0, TimeStep (1).GetSeconds (),
56  "is 1 really 1 ?");
57  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (10.0).GetSeconds (), 10.0, TimeStep (1).GetSeconds (),
58  "is 10 really 10 ?");
59  NS_TEST_ASSERT_MSG_EQ (MilliSeconds (1).GetMilliSeconds (), 1,
60  "is 1ms really 1ms ?");
61  NS_TEST_ASSERT_MSG_EQ (MicroSeconds (1).GetMicroSeconds (), 1,
62  "is 1us really 1us ?");
63 #if 0
64  Time ns = NanoSeconds (1);
65  ns.GetNanoSeconds ();
66  NS_TEST_ASSERT_MSG_EQ (NanoSeconds (1).GetNanoSeconds (), 1,
67  "is 1ns really 1ns ?");
68  NS_TEST_ASSERT_MSG_EQ (PicoSeconds (1).GetPicoSeconds (), 1,
69  "is 1ps really 1ps ?");
70  NS_TEST_ASSERT_MSG_EQ (FemtoSeconds (1).GetFemtoSeconds (), 1,
71  "is 1fs really 1fs ?");
72 #endif
73 }
74 
75 void
77 {
78  Time::SetResolution (m_originalResolution);
79 }
80 
82 {
83 public:
85 private:
86  virtual void DoSetup (void);
87  virtual void DoRun (void);
88  virtual void DoTeardown (void);
89 };
90 
91 TimesWithSignsTestCase::TimesWithSignsTestCase ()
92  : TestCase ("Checks times that have plus or minus signs")
93 {
94 }
95 
96 void
98 {
99 }
100 
101 void
103 {
104  Time timePositive ("+1000.0");
105  Time timePositiveWithUnits ("+1000.0ms");
106 
107  Time timeNegative ("-1000.0");
108  Time timeNegativeWithUnits ("-1000.0ms");
109 
110  NS_TEST_ASSERT_MSG_EQ_TOL (timePositive.GetSeconds (),
111  +1000.0,
112  1.0e-8,
113  "Positive time not parsed correctly.");
114 
115  NS_TEST_ASSERT_MSG_EQ_TOL (timePositiveWithUnits.GetSeconds (),
116  +1.0,
117  1.0e-8,
118  "Positive time with units not parsed correctly.");
119 
120  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegative.GetSeconds (),
121  -1000.0,
122  1.0e-8,
123  "Negative time not parsed correctly.");
124 
125  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegativeWithUnits.GetSeconds (),
126  -1.0,
127  1.0e-8,
128  "Negative time with units not parsed correctly.");
129 }
130 
131 void
133 {
134 }
135 
136 static class TimeTestSuite : public TestSuite
137 {
138 public:
139  TimeTestSuite ()
140  : TestSuite ("time", UNIT)
141  {
142  AddTestCase (new TimeSimpleTestCase (Time::US), TestCase::QUICK);
143  AddTestCase (new TimesWithSignsTestCase (), TestCase::QUICK);
144  }
145 } g_timeTestSuite;
Time NanoSeconds(uint64_t ns)
create ns3::Time instances in units of nanoseconds.
Definition: nstime.h:629
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
keep track of time unit.
Definition: nstime.h:149
A suite of tests to run.
Definition: test.h:962
virtual void DoRun(void)
Implementation to actually run this test case.
encapsulates test code
Definition: test.h:834
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
Definition: test.cc:354
Time PicoSeconds(uint64_t ps)
create ns3::Time instances in units of picoseconds.
Definition: nstime.h:643
double GetSeconds(void) const
Definition: nstime.h:262
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
Time FemtoSeconds(uint64_t fs)
create ns3::Time instances in units of femtoseconds.
Definition: nstime.h:657
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
int64_t GetNanoSeconds(void) const
Definition: nstime.h:287
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
Time MicroSeconds(uint64_t us)
create ns3::Time instances in units of microseconds.
Definition: nstime.h:615