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/// Function with one int parameter.
28void bari(int){};
29/// Function with two int parameters.
30void bar2i(int, int){};
31/// Function with three int parameters.
32void bar3i(int, int, int){};
33/// Function with four int parameters.
34void bar4i(int, int, int, int){};
35/// Function with five int parameters.
36void bar5i(int, int, int, int, int){};
37/// Function with one const int reference parameter.
38void barcir(const int&){};
39/// Function with one int reference parameter.
40void barir(int&){};
41
42} // anonymous namespace
43
44using namespace ns3;
45
46/**
47 * \ingroup timer-tests
48 *
49 * \brief Check correct state transitions.
50 */
52{
53 public:
55 void DoRun() override;
56};
57
59 : TestCase("Check correct state transitions")
60{
61}
62
63void
65{
67
68 timer.SetFunction(&bari);
69 timer.SetArguments(1);
70 timer.SetDelay(Seconds(10.0));
71 NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
72 NS_TEST_ASSERT_MSG_EQ(timer.IsExpired(), true, "");
73 NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
75 timer.Schedule();
76 NS_TEST_ASSERT_MSG_EQ(timer.IsRunning(), true, "");
77 NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
78 NS_TEST_ASSERT_MSG_EQ(!timer.IsSuspended(), true, "");
80 timer.Suspend();
81 NS_TEST_ASSERT_MSG_EQ(!timer.IsRunning(), true, "");
82 NS_TEST_ASSERT_MSG_EQ(!timer.IsExpired(), true, "");
83 NS_TEST_ASSERT_MSG_EQ(timer.IsSuspended(), true, "");
85 timer.Resume();
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.Cancel();
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}
96
97/**
98 * \ingroup timer-tests
99 *
100 * \brief Check that Timer template magic is working.
101 */
103{
104 public:
106 void DoRun() override;
107 void DoTeardown() override;
108
109 /// Member function with one int parameter.
110 void bazi(int){};
111 /// Member function with two int parameters.
112 void baz2i(int, int){};
113 /// Member function with three int parameters.
114 void baz3i(int, int, int){};
115 /// Member function with four int parameters.
116 void baz4i(int, int, int, int){};
117 /// Member function with five int parameters.
118 void baz5i(int, int, int, int, int){};
119 /// Member function with six int parameters.
120 void baz6i(int, int, int, int, int, int){};
121 /// Member function with one const int reference parameter.
122 void bazcir(const int&){};
123 /// Member function with one int reference parameter.
124 void bazir(int&){};
125 /// Member function with one int pointer parameter.
126 void bazip(int*){};
127 /// Member function with one const int pointer parameter.
128 void bazcip(const int*){};
129};
130
132 : TestCase("Check that template magic is working")
133{
134}
135
136void
138{
140
141 int a = 0;
142 int& b = a;
143 const int& c = a;
144
145 timer.SetFunction(&bari);
146 timer.SetArguments(2);
147 timer.SetArguments(a);
148 timer.SetArguments(b);
149 timer.SetArguments(c);
150 timer.SetFunction(&barir);
151 timer.SetArguments(2);
152 timer.SetArguments(a);
153 timer.SetArguments(b);
154 timer.SetArguments(c);
155 timer.SetFunction(&barcir);
156 timer.SetArguments(2);
157 timer.SetArguments(a);
158 timer.SetArguments(b);
159 timer.SetArguments(c);
160 // the following call cannot possibly work and is flagged by
161 // a runtime error.
162 // timer.SetArguments (0.0);
163 timer.SetDelay(Seconds(1.0));
164 timer.Schedule();
165
167 timer.SetArguments(3);
169 timer.SetArguments(3);
171 timer.SetArguments(3);
172
173 timer.SetFunction(&bar2i);
174 timer.SetArguments(1, 1);
175 timer.SetFunction(&bar3i);
176 timer.SetArguments(1, 1, 1);
177 timer.SetFunction(&bar4i);
178 timer.SetArguments(1, 1, 1, 1);
179 timer.SetFunction(&bar5i);
180 timer.SetArguments(1, 1, 1, 1, 1);
181 // unsupported in simulator class
182 // timer.SetFunction (&bar6i);
183 // timer.SetArguments (1, 1, 1, 1, 1, 1);
184
186 timer.SetArguments(1, 1);
188 timer.SetArguments(1, 1, 1);
190 timer.SetArguments(1, 1, 1, 1);
192 timer.SetArguments(1, 1, 1, 1, 1);
193 // unsupported in simulator class
194 // timer.SetFunction (&TimerTemplateTestCase::baz6i, this);
195 // timer.SetArguments (1, 1, 1, 1, 1, 1);
196
199}
200
201void
207
208/**
209 * \ingroup timer-tests
210 *
211 * \brief The timer Test Suite.
212 */
214{
215 public:
217 : TestSuite("timer", Type::UNIT)
218 {
219 AddTestCase(new TimerStateTestCase(), TestCase::Duration::QUICK);
220 AddTestCase(new TimerTemplateTestCase(), TestCase::Duration::QUICK);
221 }
222};
223
224static 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.