A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
24/**
25 * \file
26 * \ingroup timer-tests
27 * Timer test suite
28 */
29
30/**
31 * \ingroup core-tests
32 * \defgroup timer-tests Timer tests
33 */
34
35namespace
36{
37
38// clang-format off
39
40/// Function with one int parameter.
41void bari (int) {};
42/// Function with two int parameters.
43void bar2i (int, int) {};
44/// Function with three int parameters.
45void bar3i (int, int, int) {};
46/// Function with four int parameters.
47void bar4i (int, int, int, int) {};
48/// Function with five int parameters.
49void bar5i (int, int, int, int, int) {};
50/// Function with one const int reference parameter.
51void barcir (const int &) {};
52/// Function with one int reference parameter.
53void barir (int &) {};
54
55// clang-format on
56
57} // anonymous namespace
58
59using namespace ns3;
60
61/**
62 * \ingroup timer-tests
63 *
64 * \brief Check correct state transitions.
65 */
67{
68 public:
70 void DoRun() override;
71};
72
74 : TestCase("Check correct state transitions")
75{
76}
77
78void
80{
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, "");
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, "");
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, "");
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, "");
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, "");
110}
111
112/**
113 * \ingroup timer-tests
114 *
115 * \brief Check that Timer template magic is working.
116 */
118{
119 public:
121 void DoRun() override;
122 void DoTeardown() override;
123
124 /// Member function with one int parameter.
125 void bazi(int){};
126 /// Member function with two int parameters.
127 void baz2i(int, int){};
128 /// Member function with three int parameters.
129 void baz3i(int, int, int){};
130 /// Member function with four int parameters.
131 void baz4i(int, int, int, int){};
132 /// Member function with five int parameters.
133 void baz5i(int, int, int, int, int){};
134 /// Member function with six int parameters.
135 void baz6i(int, int, int, int, int, int){};
136 /// Member function with one const int reference parameter.
137 void bazcir(const int&){};
138 /// Member function with one int reference parameter.
139 void bazir(int&){};
140 /// Member function with one int pointer parameter.
141 void bazip(int*){};
142 /// Member function with one const int pointer parameter.
143 void bazcip(const int*){};
144};
145
147 : TestCase("Check that template magic is working")
148{
149}
150
151void
153{
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
214}
215
216void
218{
221}
222
223/**
224 * \ingroup timer-tests
225 *
226 * \brief The timer Test Suite.
227 */
229{
230 public:
232 : TestSuite("timer", Type::UNIT)
233 {
234 AddTestCase(new TimerStateTestCase(), TestCase::Duration::QUICK);
235 AddTestCase(new TimerTemplateTestCase(), TestCase::Duration::QUICK);
236 }
237};
238
239static TimerTestSuite g_timerTestSuite; //!< Static variable for test initialization
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
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
@ CANCEL_ON_DESTROY
This policy cancels the event from the destructor of the Timer or from Suspend().
Definition: timer.h:93
@ RUNNING
Timer is currently running.
Definition: timer.h:110
@ EXPIRED
Timer has already expired.
Definition: timer.h:111
@ SUSPENDED
Timer is suspended.
Definition: timer.h:112
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:145
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.