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 
36 namespace {
37 
38 /* *NS_CHECK_STYLE_OFF* */
40 void bari (int) {}
42 void bar2i (int, int) {}
44 void bar3i (int, int, int) {}
46 void bar4i (int, int, int, int) {}
48 void bar5i (int, int, int, int, int) {}
50 void barcir (const int &) {}
52 void barir (int &) {}
53 /* *NS_CHECK_STYLE_ON* */
54 
55 } // anonymous namespace
56 
57 using namespace ns3;
58 
65 {
66 public:
68  virtual void DoRun (void);
69 };
70 
72  : TestCase ("Check correct state transitions")
73 {}
74 void
76 {
77  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
78 
79  timer.SetFunction (&bari);
80  timer.SetArguments (1);
81  timer.SetDelay (Seconds (10.0));
82  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
83  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
84  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
85  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
86  timer.Schedule ();
87  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
88  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
89  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
90  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
91  timer.Suspend ();
92  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
93  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
94  NS_TEST_ASSERT_MSG_EQ (timer.IsSuspended (), true, "");
95  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::SUSPENDED, "");
96  timer.Resume ();
97  NS_TEST_ASSERT_MSG_EQ (timer.IsRunning (), true, "");
98  NS_TEST_ASSERT_MSG_EQ (!timer.IsExpired (), true, "");
99  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
100  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::RUNNING, "");
101  timer.Cancel ();
102  NS_TEST_ASSERT_MSG_EQ (!timer.IsRunning (), true, "");
103  NS_TEST_ASSERT_MSG_EQ (timer.IsExpired (), true, "");
104  NS_TEST_ASSERT_MSG_EQ (!timer.IsSuspended (), true, "");
105  NS_TEST_ASSERT_MSG_EQ (timer.GetState (), Timer::EXPIRED, "");
106 }
107 
114 {
115 public:
117  virtual void DoRun (void);
118  virtual void DoTeardown (void);
119 
120  /* *NS_CHECK_STYLE_OFF* */
122  void bazi (int) {}
124  void baz2i (int, int) {}
126  void baz3i (int, int, int) {}
128  void baz4i (int, int, int, int) {}
130  void baz5i (int, int, int, int, int) {}
132  void baz6i (int, int, int, int, int, int) {}
134  void bazcir (const int&) {}
136  void bazir (int&) {}
138  void bazip (int *) {}
140  void bazcip (const int *) {}
141  /* *NS_CHECK_STYLE_ON* */
142 };
143 
145  : TestCase ("Check that template magic is working")
146 {}
147 
148 void
150 {
151  Timer timer = Timer (Timer::CANCEL_ON_DESTROY);
152 
153  int a = 0;
154  int &b = a;
155  const int &c = a;
156 
157  timer.SetFunction (&bari);
158  timer.SetArguments (2);
159  timer.SetArguments (a);
160  timer.SetArguments (b);
161  timer.SetArguments (c);
162  timer.SetFunction (&barir);
163  timer.SetArguments (2);
164  timer.SetArguments (a);
165  timer.SetArguments (b);
166  timer.SetArguments (c);
167  timer.SetFunction (&barcir);
168  timer.SetArguments (2);
169  timer.SetArguments (a);
170  timer.SetArguments (b);
171  timer.SetArguments (c);
172  // the following call cannot possibly work and is flagged by
173  // a runtime error.
174  // timer.SetArguments (0.0);
175  timer.SetDelay (Seconds (1.0));
176  timer.Schedule ();
177 
179  timer.SetArguments (3);
181  timer.SetArguments (3);
183  timer.SetArguments (3);
184 
185  timer.SetFunction (&bar2i);
186  timer.SetArguments (1, 1);
187  timer.SetFunction (&bar3i);
188  timer.SetArguments (1, 1, 1);
189  timer.SetFunction (&bar4i);
190  timer.SetArguments (1, 1, 1, 1);
191  timer.SetFunction (&bar5i);
192  timer.SetArguments (1, 1, 1, 1, 1);
193  // unsupported in simulator class
194  // timer.SetFunction (&bar6i);
195  // timer.SetArguments (1, 1, 1, 1, 1, 1);
196 
198  timer.SetArguments (1, 1);
200  timer.SetArguments (1, 1, 1);
202  timer.SetArguments (1, 1, 1, 1);
204  timer.SetArguments (1, 1, 1, 1, 1);
205  // unsupported in simulator class
206  // timer.SetFunction (&TimerTemplateTestCase::baz6i, this);
207  // timer.SetArguments (1, 1, 1, 1, 1, 1);
208 
209  Simulator::Run ();
210  Simulator::Destroy ();
211 }
212 
213 void
215 {
216  Simulator::Run ();
217  Simulator::Destroy ();
218 }
219 
225 class TimerTestSuite : public TestSuite
226 {
227 public:
229  : TestSuite ("timer", UNIT)
230  {
231  AddTestCase (new TimerStateTestCase (), TestCase::QUICK);
232  AddTestCase (new TimerTemplateTestCase (), TestCase::QUICK);
233  }
234 };
235 
Check correct state transitions.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Check that Timer template magic is working.
void bazi(int)
Member function with one int parameter.
void bazcir(const int &)
Member function with one const int reference parameter.
void bazip(int *)
Member function with one int pointer parameter.
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 baz6i(int, int, int, int, int, int)
Member function with six int parameters.
void bazir(int &)
Member function with one int reference parameter.
void baz3i(int, int, int)
Member function with three int parameters.
void bazcip(const int *)
Member function with one const int pointer parameter.
void baz4i(int, int, int, int)
Member function with four int parameters.
void baz2i(int, int)
Member function with two int parameters.
void baz5i(int, int, int, int, int)
Member function with five int parameters.
The timer Test Suite.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
A simple virtual Timer class.
Definition: timer.h:74
void SetDelay(const Time &delay)
Definition: timer.cc:75
void SetFunction(FN fn)
Definition: timer.h:278
void Suspend(void)
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
bool IsExpired(void) const
Definition: timer.cc:121
bool IsRunning(void) const
Definition: timer.cc:127
void SetArguments(Ts... args)
Definition: timer.h:293
enum Timer::State GetState(void) const
Definition: timer.cc:139
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:194
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
bool IsSuspended(void) const
Definition: timer.cc:133
#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:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
void bar2i(int, int)
Function with two int parameters.
void bar4i(int, int, int, int)
Function with four int parameters.
void bar5i(int, int, int, int, int)
Function with five int parameters.
void barir(int &)
Function with one int reference parameter.
void bar3i(int, int, int)
Function with three int parameters.
void bari(int)
Function with one int parameter.
void barcir(const int &)
Function with one const int reference parameter.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TimerTestSuite g_timerTestSuite
Static variable for test initialization.