A Discrete-Event Network Simulator
API
timer-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) 2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/timer.h"
21 #include "ns3/test.h"
22 #include "ns3/simulator.h"
23 #include "ns3/nstime.h"
24 
25 namespace {
26 
27 /* *NS_CHECK_STYLE_OFF* */
28 void bari (int) {}
29 void bar2i (int, int) {}
30 void bar3i (int, int, int) {}
31 void bar4i (int, int, int, int) {}
32 void bar5i (int, int, int, int, int) {}
33 void barcir (const int &) {}
34 void barir (int &) {}
35 /* *NS_CHECK_STYLE_ON* */
36 
37 } // anonymous namespace
38 
39 using namespace ns3;
40 
42 {
43 public:
45  virtual void DoRun (void);
46 };
47 
49  : TestCase ("Check correct state transitions")
50 {}
51 void
53 {
54  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
55 
56  timer.SetFunction (&bari);
57  timer.SetArguments (1);
58  timer.SetDelay (Seconds (10.0));
59  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
60  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
61  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
62  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
63  timer.Schedule ();
64  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
65  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
66  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
67  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
68  timer.Suspend ();
69  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
70  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
71  NS_TEST_ASSERT_MSG_EQ (timer.IsSuspended (), true, "");
72  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::SUSPENDED, "");
73  timer.Resume ();
74  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
75  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
76  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
77  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
78  timer.Cancel ();
79  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
80  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
81  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
82  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
83 }
84 
86 {
87 public:
89  virtual void DoRun (void);
90  virtual void DoTeardown (void);
91 
92  /* *NS_CHECK_STYLE_OFF* */
93  void bazi (int) {}
94  void baz2i (int, int) {}
95  void baz3i (int, int, int) {}
96  void baz4i (int, int, int, int) {}
97  void baz5i (int, int, int, int, int) {}
98  void baz6i (int, int, int, int, int, int) {}
99  void bazcir (const int&) {}
100  void bazir (int&) {}
101  void bazip (int *) {}
102  void bazcip (const int *) {}
103  /* *NS_CHECK_STYLE_ON* */
104 };
105 
107  : TestCase ("Check that template magic is working")
108 {}
109 
110 void
112 {
113  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
114 
115  int a = 0;
116  int &b = a;
117  const int &c = a;
118 
119  timer.SetFunction (&bari);
120  timer.SetArguments (2);
121  timer.SetArguments (a);
122  timer.SetArguments (b);
123  timer.SetArguments (c);
124  timer.SetFunction (&barir);
125  timer.SetArguments (2);
126  timer.SetArguments (a);
127  timer.SetArguments (b);
128  timer.SetArguments (c);
129  timer.SetFunction (&barcir);
130  timer.SetArguments (2);
131  timer.SetArguments (a);
132  timer.SetArguments (b);
133  timer.SetArguments (c);
134  // the following call cannot possibly work and is flagged by
135  // a runtime error.
136  // timer.SetArguments (0.0);
137  timer.SetDelay (Seconds (1.0));
138  timer.Schedule ();
139 
141  timer.SetArguments (3);
143  timer.SetArguments (3);
145  timer.SetArguments (3);
146 
147  timer.SetFunction (&bar2i);
148  timer.SetArguments (1, 1);
149  timer.SetFunction (&bar3i);
150  timer.SetArguments (1, 1, 1);
151  timer.SetFunction (&bar4i);
152  timer.SetArguments (1, 1, 1, 1);
153  timer.SetFunction (&bar5i);
154  timer.SetArguments (1, 1, 1, 1, 1);
155  // unsupported in simulator class
156  // timer.SetFunction (&bar6i);
157  // timer.SetArguments (1, 1, 1, 1, 1, 1);
158 
160  timer.SetArguments (1, 1);
162  timer.SetArguments (1, 1, 1);
164  timer.SetArguments (1, 1, 1, 1);
166  timer.SetArguments (1, 1, 1, 1, 1);
167  // unsupported in simulator class
168  // timer.SetFunction (&TimerTemplateTestCase::baz6i, this);
169  // timer.SetArguments (1, 1, 1, 1, 1, 1);
170 
171  Simulator::Run ();
172  Simulator::Destroy ();
173 }
174 
175 void
177 {
178  Simulator::Run ();
179  Simulator::Destroy ();
180 }
181 
182 static class TimerTestSuite : public TestSuite
183 {
184 public:
186  : TestSuite ("timer", UNIT)
187  {
188  AddTestCase (new TimerStateTestCase (), TestCase::QUICK);
189  AddTestCase (new TimerTemplateTestCase (), TestCase::QUICK);
190  }
A simple Timer class.
Definition: timer.h:73
void bazcir(const int &)
A suite of tests to run.
Definition: test.h:1343
void baz4i(int, int, int, int)
bool IsSuspended(void) const
Definition: timer.cc:133
encapsulates test code
Definition: test.h:1153
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void SetFunction(FN fn)
Definition: timer.h:309
#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
void bar5i(int, int, int, int, int)
void SetDelay(const Time &delay)
Definition: timer.cc:75
void SetArguments(T1 a1)
Definition: timer.h:324
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:187
void bazcip(const int *)
bool IsRunning(void) const
Definition: timer.cc:127
void baz5i(int, int, int, int, int)
enum Timer::State GetState(void) const
Definition: timer.cc:139
void baz6i(int, int, int, int, int, int)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
void Suspend(void)
Cancel the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
TimerTestSuite g_timerTestSuite
This test suite implements a Unit Test.
Definition: test.h:1353
void baz3i(int, int, int)
bool IsExpired(void) const
Definition: timer.cc:121