A Discrete-Event Network Simulator
API
timer-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #include "ns3/nstime.h"
20 #include "ns3/simulator.h"
21 #include "ns3/test.h"
22 #include "ns3/timer.h"
23 
35 namespace
36 {
37 
38 // clang-format off
39 
41 void bari (int) {};
43 void bar2i (int, int) {};
45 void bar3i (int, int, int) {};
47 void bar4i (int, int, int, int) {};
49 void bar5i (int, int, int, int, int) {};
51 void barcir (const int &) {};
53 void barir (int &) {};
54 
55 // clang-format on
56 
57 } // anonymous namespace
58 
59 using namespace ns3;
60 
67 {
68  public:
70  void DoRun() override;
71 };
72 
74  : TestCase("Check correct state transitions")
75 {
76 }
77 
78 void
80 {
81  Timer timer = Timer(Timer::CANCEL_ON_DESTROY);
82 
83  timer.SetFunction(&bari);
84  timer.SetArguments(1);
85  timer.SetDelay(Seconds(10.0));
86  NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
87  NS_TEST_ASSERT_MSG_EQ(timer.IsExpired(), true, "");
88  NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
89  NS_TEST_ASSERT_MSG_EQ(timer.GetState(), Timer::EXPIRED, "");
90  timer.Schedule();
91  NS_TEST_ASSERT_MSG_EQ(timer.IsRunning(), true, "");
92  NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
93  NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
94  NS_TEST_ASSERT_MSG_EQ(timer.GetState(), Timer::RUNNING, "");
95  timer.Suspend();
96  NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
97  NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
98  NS_TEST_ASSERT_MSG_EQ(timer.IsSuspended(), true, "");
99  NS_TEST_ASSERT_MSG_EQ(timer.GetState(), Timer::SUSPENDED, "");
100  timer.Resume();
101  NS_TEST_ASSERT_MSG_EQ(timer.IsRunning(), true, "");
102  NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
103  NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
104  NS_TEST_ASSERT_MSG_EQ(timer.GetState(), Timer::RUNNING, "");
105  timer.Cancel();
106  NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
107  NS_TEST_ASSERT_MSG_EQ(timer.IsExpired(), true, "");
108  NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
109  NS_TEST_ASSERT_MSG_EQ(timer.GetState(), Timer::EXPIRED, "");
110 }
111 
118 {
119  public:
121  void DoRun() override;
122  void DoTeardown() override;
123 
125  void bazi(int){};
127  void baz2i(int, int){};
129  void baz3i(int, int, int){};
131  void baz4i(int, int, int, int){};
133  void baz5i(int, int, int, int, int){};
135  void baz6i(int, int, int, int, int, int){};
137  void bazcir(const int&){};
139  void bazir(int&){};
141  void bazip(int*){};
143  void bazcip(const int*){};
144 };
145 
147  : TestCase("Check that template magic is working")
148 {
149 }
150 
151 void
153 {
154  Timer timer = Timer(Timer::CANCEL_ON_DESTROY);
155 
156  int a = 0;
157  int& b = a;
158  const int& c = a;
159 
160  timer.SetFunction(&bari);
161  timer.SetArguments(2);
162  timer.SetArguments(a);
163  timer.SetArguments(b);
164  timer.SetArguments(c);
165  timer.SetFunction(&barir);
166  timer.SetArguments(2);
167  timer.SetArguments(a);
168  timer.SetArguments(b);
169  timer.SetArguments(c);
170  timer.SetFunction(&barcir);
171  timer.SetArguments(2);
172  timer.SetArguments(a);
173  timer.SetArguments(b);
174  timer.SetArguments(c);
175  // the following call cannot possibly work and is flagged by
176  // a runtime error.
177  // timer.SetArguments (0.0);
178  timer.SetDelay(Seconds(1.0));
179  timer.Schedule();
180 
182  timer.SetArguments(3);
184  timer.SetArguments(3);
186  timer.SetArguments(3);
187 
188  timer.SetFunction(&bar2i);
189  timer.SetArguments(1, 1);
190  timer.SetFunction(&bar3i);
191  timer.SetArguments(1, 1, 1);
192  timer.SetFunction(&bar4i);
193  timer.SetArguments(1, 1, 1, 1);
194  timer.SetFunction(&bar5i);
195  timer.SetArguments(1, 1, 1, 1, 1);
196  // unsupported in simulator class
197  // timer.SetFunction (&bar6i);
198  // timer.SetArguments (1, 1, 1, 1, 1, 1);
199 
201  timer.SetArguments(1, 1);
203  timer.SetArguments(1, 1, 1);
205  timer.SetArguments(1, 1, 1, 1);
207  timer.SetArguments(1, 1, 1, 1, 1);
208  // unsupported in simulator class
209  // timer.SetFunction (&TimerTemplateTestCase::baz6i, this);
210  // timer.SetArguments (1, 1, 1, 1, 1, 1);
211 
212  Simulator::Run();
213  Simulator::Destroy();
214 }
215 
216 void
218 {
219  Simulator::Run();
220  Simulator::Destroy();
221 }
222 
228 class TimerTestSuite : public TestSuite
229 {
230  public:
232  : TestSuite("timer", UNIT)
233  {
234  AddTestCase(new TimerStateTestCase(), TestCase::QUICK);
235  AddTestCase(new TimerTemplateTestCase(), TestCase::QUICK);
236  }
237 };
238 
Check correct state transitions.
void DoRun() override
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.
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
A simple virtual Timer class.
Definition: timer.h:74
void SetDelay(const Time &delay)
Definition: timer.cc:76
void SetFunction(FN fn)
Definition: timer.h:275
bool IsExpired() const
Definition: timer.cc:122
Timer::State GetState() const
Definition: timer.cc:143
void SetArguments(Ts... args)
Definition: timer.h:291
void Cancel()
Cancel the currently-running event if there is one.
Definition: timer.cc:108
bool IsSuspended() const
Definition: timer.cc:136
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:162
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:198
bool IsRunning() const
Definition: timer.cc:129
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:181
#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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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.