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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "ns3/nstime.h"
9#include "ns3/simulator.h"
10#include "ns3/test.h"
11#include "ns3/timer.h"
12
13/**
14 * \file
15 * \ingroup timer-tests
16 * Timer test suite
17 */
18
19/**
20 * \ingroup core-tests
21 * \defgroup timer-tests Timer tests
22 */
23
24namespace
25{
26
27// clang-format off
28
29/// Function with one int parameter.
30void bari (int) {};
31/// Function with two int parameters.
32void bar2i (int, int) {};
33/// Function with three int parameters.
34void bar3i (int, int, int) {};
35/// Function with four int parameters.
36void bar4i (int, int, int, int) {};
37/// Function with five int parameters.
38void bar5i (int, int, int, int, int) {};
39/// Function with one const int reference parameter.
40void barcir (const int &) {};
41/// Function with one int reference parameter.
42void barir (int &) {};
43
44// clang-format on
45
46} // anonymous namespace
47
48using namespace ns3;
49
50/**
51 * \ingroup timer-tests
52 *
53 * \brief Check correct state transitions.
54 */
56{
57 public:
59 void DoRun() override;
60};
61
63 : TestCase("Check correct state transitions")
64{
65}
66
67void
69{
71
72 timer.SetFunction(&bari);
73 timer.SetArguments(1);
74 timer.SetDelay(Seconds(10.0));
75 NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
76 NS_TEST_ASSERT_MSG_EQ(timer.IsExpired(), true, "");
77 NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
79 timer.Schedule();
80 NS_TEST_ASSERT_MSG_EQ(timer.IsRunning(), true, "");
81 NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
82 NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
84 timer.Suspend();
85 NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
86 NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
87 NS_TEST_ASSERT_MSG_EQ(timer.IsSuspended(), true, "");
89 timer.Resume();
90 NS_TEST_ASSERT_MSG_EQ(timer.IsRunning(), true, "");
91 NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
92 NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
94 timer.Cancel();
95 NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
96 NS_TEST_ASSERT_MSG_EQ(timer.IsExpired(), true, "");
97 NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
99}
100
101/**
102 * \ingroup timer-tests
103 *
104 * \brief Check that Timer template magic is working.
105 */
107{
108 public:
110 void DoRun() override;
111 void DoTeardown() override;
112
113 /// Member function with one int parameter.
114 void bazi(int){};
115 /// Member function with two int parameters.
116 void baz2i(int, int){};
117 /// Member function with three int parameters.
118 void baz3i(int, int, int){};
119 /// Member function with four int parameters.
120 void baz4i(int, int, int, int){};
121 /// Member function with five int parameters.
122 void baz5i(int, int, int, int, int){};
123 /// Member function with six int parameters.
124 void baz6i(int, int, int, int, int, int){};
125 /// Member function with one const int reference parameter.
126 void bazcir(const int&){};
127 /// Member function with one int reference parameter.
128 void bazir(int&){};
129 /// Member function with one int pointer parameter.
130 void bazip(int*){};
131 /// Member function with one const int pointer parameter.
132 void bazcip(const int*){};
133};
134
136 : TestCase("Check that template magic is working")
137{
138}
139
140void
142{
144
145 int a = 0;
146 int& b = a;
147 const int& c = a;
148
149 timer.SetFunction(&bari);
150 timer.SetArguments(2);
151 timer.SetArguments(a);
152 timer.SetArguments(b);
153 timer.SetArguments(c);
154 timer.SetFunction(&barir);
155 timer.SetArguments(2);
156 timer.SetArguments(a);
157 timer.SetArguments(b);
158 timer.SetArguments(c);
159 timer.SetFunction(&barcir);
160 timer.SetArguments(2);
161 timer.SetArguments(a);
162 timer.SetArguments(b);
163 timer.SetArguments(c);
164 // the following call cannot possibly work and is flagged by
165 // a runtime error.
166 // timer.SetArguments (0.0);
167 timer.SetDelay(Seconds(1.0));
168 timer.Schedule();
169
171 timer.SetArguments(3);
173 timer.SetArguments(3);
175 timer.SetArguments(3);
176
177 timer.SetFunction(&bar2i);
178 timer.SetArguments(1, 1);
179 timer.SetFunction(&bar3i);
180 timer.SetArguments(1, 1, 1);
181 timer.SetFunction(&bar4i);
182 timer.SetArguments(1, 1, 1, 1);
183 timer.SetFunction(&bar5i);
184 timer.SetArguments(1, 1, 1, 1, 1);
185 // unsupported in simulator class
186 // timer.SetFunction (&bar6i);
187 // timer.SetArguments (1, 1, 1, 1, 1, 1);
188
190 timer.SetArguments(1, 1);
192 timer.SetArguments(1, 1, 1);
194 timer.SetArguments(1, 1, 1, 1);
196 timer.SetArguments(1, 1, 1, 1, 1);
197 // unsupported in simulator class
198 // timer.SetFunction (&TimerTemplateTestCase::baz6i, this);
199 // timer.SetArguments (1, 1, 1, 1, 1, 1);
200
203}
204
205void
211
212/**
213 * \ingroup timer-tests
214 *
215 * \brief The timer Test Suite.
216 */
218{
219 public:
221 : TestSuite("timer", Type::UNIT)
222 {
223 AddTestCase(new TimerStateTestCase(), TestCase::Duration::QUICK);
224 AddTestCase(new TimerTemplateTestCase(), TestCase::Duration::QUICK);
225 }
226};
227
228static 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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
A simple virtual Timer class.
Definition timer.h:67
void SetDelay(const Time &delay)
Definition timer.cc:65
void SetFunction(FN fn)
Definition timer.h:268
bool IsExpired() const
Definition timer.cc:111
Timer::State GetState() const
Definition timer.cc:132
void SetArguments(Ts... args)
Definition timer.h:284
@ CANCEL_ON_DESTROY
This policy cancels the event from the destructor of the Timer or from Suspend().
Definition timer.h:86
@ RUNNING
Timer is currently running.
Definition timer.h:103
@ EXPIRED
Timer has already expired.
Definition timer.h:104
@ SUSPENDED
Timer is suspended.
Definition timer.h:105
void Cancel()
Cancel the currently-running event if there is one.
Definition timer.cc:97
bool IsSuspended() const
Definition timer.cc:125
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition timer.cc:151
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition timer.cc:187
bool IsRunning() const
Definition timer.cc:118
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition timer.cc:170
#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:134
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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.