A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #include "ns3/nstime.h"
23 #include "ns3/test.h"
24 
25 using namespace ns3;
26 
28 {
29 public:
31 private:
32  virtual void DoSetup (void);
33  virtual void DoRun (void);
34  virtual void DoTeardown (void);
35 };
36 
38  : TestCase ("Sanity check of common time operations")
39 {
40 }
41 
42 void
44 {
45 }
46 
47 void
49 {
50  NS_TEST_ASSERT_MSG_EQ_TOL (Years (1.0).GetYears (), 1.0, Years (1).GetYears (),
51  "is 1 really 1 ?");
52  NS_TEST_ASSERT_MSG_EQ_TOL (Years (10.0).GetYears (), 10.0, Years (1).GetYears (),
53  "is 10 really 10 ?");
54  NS_TEST_ASSERT_MSG_EQ_TOL (Days (1.0).GetDays (), 1.0, Days (1).GetDays (),
55  "is 1 really 1 ?");
56  NS_TEST_ASSERT_MSG_EQ_TOL (Days (10.0).GetDays (), 10.0, Days (1).GetDays (),
57  "is 10 really 10 ?");
58  NS_TEST_ASSERT_MSG_EQ_TOL (Hours (1.0).GetHours (), 1.0, Hours (1).GetHours (),
59  "is 1 really 1 ?");
60  NS_TEST_ASSERT_MSG_EQ_TOL (Hours (10.0).GetHours (), 10.0, Hours (1).GetHours (),
61  "is 10 really 10 ?");
62  NS_TEST_ASSERT_MSG_EQ_TOL (Minutes (1.0).GetMinutes (), 1.0, Minutes (1).GetMinutes (),
63  "is 1 really 1 ?");
64  NS_TEST_ASSERT_MSG_EQ_TOL (Minutes (10.0).GetMinutes (), 10.0, Minutes (1).GetMinutes (),
65  "is 10 really 10 ?");
66  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (1.0).GetSeconds (), 1.0, TimeStep (1).GetSeconds (),
67  "is 1 really 1 ?");
68  NS_TEST_ASSERT_MSG_EQ_TOL (Seconds (10.0).GetSeconds (), 10.0, TimeStep (1).GetSeconds (),
69  "is 10 really 10 ?");
70  NS_TEST_ASSERT_MSG_EQ (MilliSeconds (1).GetMilliSeconds (), 1,
71  "is 1ms really 1ms ?");
72  NS_TEST_ASSERT_MSG_EQ (MicroSeconds (1).GetMicroSeconds (), 1,
73  "is 1us really 1us ?");
74 #if 0
75  Time ns = NanoSeconds (1);
76  ns.GetNanoSeconds ();
77  NS_TEST_ASSERT_MSG_EQ (NanoSeconds (1).GetNanoSeconds (), 1,
78  "is 1ns really 1ns ?");
79  NS_TEST_ASSERT_MSG_EQ (PicoSeconds (1).GetPicoSeconds (), 1,
80  "is 1ps really 1ps ?");
81  NS_TEST_ASSERT_MSG_EQ (FemtoSeconds (1).GetFemtoSeconds (), 1,
82  "is 1fs really 1fs ?");
83 #endif
84 
85  Time ten = NanoSeconds (10);
86  int64_t tenValue = ten.GetInteger ();
87  Time::SetResolution (Time::PS);
88  int64_t tenKValue = ten.GetInteger ();
89  NS_TEST_ASSERT_MSG_EQ (tenValue * 1000, tenKValue,
90  "change resolution to PS");
91 }
92 
93 void
95 {
96 }
97 
99 {
100 public:
102 private:
103  virtual void DoSetup (void);
104  virtual void DoRun (void);
105  virtual void DoTeardown (void);
106 };
107 
109  : TestCase ("Checks times that have plus or minus signs")
110 {
111 }
112 
113 void
115 {
116 }
117 
118 void
120 {
121  Time timePositive ("+1000.0");
122  Time timePositiveWithUnits ("+1000.0ms");
123 
124  Time timeNegative ("-1000.0");
125  Time timeNegativeWithUnits ("-1000.0ms");
126 
127  NS_TEST_ASSERT_MSG_EQ_TOL (timePositive.GetSeconds (),
128  +1000.0,
129  1.0e-8,
130  "Positive time not parsed correctly.");
131 
132  NS_TEST_ASSERT_MSG_EQ_TOL (timePositiveWithUnits.GetSeconds (),
133  +1.0,
134  1.0e-8,
135  "Positive time with units not parsed correctly.");
136 
137  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegative.GetSeconds (),
138  -1000.0,
139  1.0e-8,
140  "Negative time not parsed correctly.");
141 
142  NS_TEST_ASSERT_MSG_EQ_TOL (timeNegativeWithUnits.GetSeconds (),
143  -1.0,
144  1.0e-8,
145  "Negative time with units not parsed correctly.");
146 }
147 
148 void
150 {
151 }
152 
153 static class TimeTestSuite : public TestSuite
154 {
155 public:
157  : TestSuite ("time", UNIT)
158  {
159  AddTestCase (new TimeSimpleTestCase (), TestCase::QUICK);
160  AddTestCase (new TimesWithSignsTestCase (), TestCase::QUICK);
161  }
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Time TimeStep(uint64_t ts)
Definition: nstime.h:950
#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:326
A suite of tests to run.
Definition: test.h:1025
TimeTestSuite g_timeTestSuite
virtual void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:849
double GetSeconds(void) const
Definition: nstime.h:274
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
int64_t GetInteger(void) const
Definition: nstime.h:364
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
int64_t GetNanoSeconds(void) const
Definition: nstime.h:299
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
This test suite implements a Unit Test.
Definition: test.h:1035
#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:137