A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simulator-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "ns3/calendar-scheduler.h"
9#include "ns3/heap-scheduler.h"
10#include "ns3/list-scheduler.h"
11#include "ns3/map-scheduler.h"
12#include "ns3/priority-queue-scheduler.h"
13#include "ns3/simulator.h"
14#include "ns3/test.h"
15
16using namespace ns3;
17
18/**
19 * @file
20 * @ingroup simulator-tests
21 * Simulator class test suite
22 */
23
24/**
25 * @ingroup core-tests
26 * @defgroup simulator-tests Simulator class tests
27 */
28
29/**
30 * @ingroup simulator-tests
31 *
32 * @brief Check that basic event handling is working with different Simulator implementations.
33 */
35{
36 public:
37 /**
38 * Constructor.
39 * @param schedulerFactory Scheduler factory.
40 */
41 SimulatorEventsTestCase(ObjectFactory schedulerFactory);
42 void DoRun() override;
43 /**
44 * Test Event.
45 * @param value Event parameter.
46 * @{
47 */
48 void EventA(int value);
49 void EventB(int value);
50 void EventC(int value);
51 void EventD(int value);
52 /** @} */
53
54 /**
55 * Test Event.
56 */
57 void Eventfoo0();
58
59 /**
60 * Get the simulator time.
61 * @return The actual time [ms].
62 */
63 uint64_t NowUs();
64 /**
65 * Checks that the events has been destroyed.
66 */
67 void Destroy();
68 /**
69 * Checks that events are properly handled.
70 * @{
71 */
72 bool m_a;
73 bool m_b;
74 bool m_c;
75 bool m_d;
77 /** @} */
78
79 EventId m_idC; //!< Event C.
80 EventId m_destroyId; //!< Event to check event lifetime.
81 ObjectFactory m_schedulerFactory; //!< Scheduler factory.
82};
83
85 : TestCase("Check that basic event handling is working with " +
86 schedulerFactory.GetTypeId().GetName()),
87 m_schedulerFactory(schedulerFactory)
88{
89}
90
91uint64_t
93{
94 uint64_t ns = Now().GetNanoSeconds();
95 return ns / 1000;
96}
97
98void
100{
101 m_a = false;
102}
103
104void
111
112void
114{
115 m_c = false;
116}
117
118void
120{
121 m_d = !(d != 4 || NowUs() != (11 + 10));
122}
123
124void
128
129void
131{
132 if (m_destroyId.IsExpired())
133 {
134 m_destroy = true;
135 }
136}
137
138void
140{
141 m_a = true;
142 m_b = false;
143 m_c = true;
144 m_d = false;
145
147
151
152 NS_TEST_EXPECT_MSG_EQ(!m_idC.IsExpired(), true, "");
153 NS_TEST_EXPECT_MSG_EQ(!a.IsExpired(), true, "");
155 NS_TEST_EXPECT_MSG_EQ(a.IsExpired(), true, "");
157 NS_TEST_EXPECT_MSG_EQ(m_a, true, "Event A did not run ?");
158 NS_TEST_EXPECT_MSG_EQ(m_b, true, "Event B did not run ?");
159 NS_TEST_EXPECT_MSG_EQ(m_c, true, "Event C did not run ?");
160 NS_TEST_EXPECT_MSG_EQ(m_d, true, "Event D did not run ?");
161
163
164 // Test copy assignment operator
165 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
166 EventId anotherId = anId;
167
168 NS_TEST_EXPECT_MSG_EQ(!(anId.IsExpired() || anotherId.IsExpired()),
169 true,
170 "Event should not have expired yet.");
171
172 Simulator::Remove(anId);
173 NS_TEST_EXPECT_MSG_EQ(anId.IsExpired(), true, "Event was removed: it is now expired");
174 NS_TEST_EXPECT_MSG_EQ(anotherId.IsExpired(), true, "Event was removed: it is now expired");
175
176 m_destroy = false;
178 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
179 m_destroyId.Cancel();
181 true,
182 "Event was canceled: should have expired now");
183
185 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
188 true,
189 "Event was canceled: should have expired now");
190
192 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
193
195 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
196 NS_TEST_EXPECT_MSG_EQ(!m_destroy, true, "Event should not have run");
197
199 NS_TEST_EXPECT_MSG_EQ(m_destroyId.IsExpired(), true, "Event should have expired now");
200 NS_TEST_EXPECT_MSG_EQ(m_destroy, true, "Event should have run");
201}
202
203/**
204 * @ingroup simulator-tests
205 *
206 * @brief Check that all templates are instantiated correctly.
207 *
208 * This is a compilation test, it cannot fail at runtime.
209 *
210 * Call graph was not generated because of its size.
211 * @hidecallgraph
212 */
214{
215 public:
217
218 /**
219 * Ref and Unref - only here for testing of Ptr<>
220 *
221 * @{
222 */
223 void Ref() const
224 {
225 }
226
227 void Unref() const
228 {
229 }
230
231 /** @} */
232
233 private:
234 /**
235 * Call graph was not generated because of its size.
236 * @hidecallgraph
237 */
238 void DoRun() override;
239
240 /**
241 * Function used for scheduling.
242 *
243 * @{
244 */
245 void bar0() {};
246 void bar1(int) {};
247 void bar2(int, int) {};
248 void bar3(int, int, int) {};
249 void bar4(int, int, int, int) {};
250 void bar5(int, int, int, int, int) {};
251 void baz1(int&) {};
252 void baz2(int&, int&) {};
253 void baz3(int&, int&, int&) {};
254 void baz4(int&, int&, int&, int&) {};
255 void baz5(int&, int&, int&, int&, int&) {};
256 void cbaz1(const int&) {};
257 void cbaz2(const int&, const int&) {};
258 void cbaz3(const int&, const int&, const int&) {};
259 void cbaz4(const int&, const int&, const int&, const int&) {};
260 void cbaz5(const int&, const int&, const int&, const int&, const int&) {};
261
262 void bar0c() const {};
263 void bar1c(int) const {};
264 void bar2c(int, int) const {};
265 void bar3c(int, int, int) const {};
266 void bar4c(int, int, int, int) const {};
267 void bar5c(int, int, int, int, int) const {};
268 void baz1c(int&) const {};
269 void baz2c(int&, int&) const {};
270 void baz3c(int&, int&, int&) const {};
271 void baz4c(int&, int&, int&, int&) const {};
272 void baz5c(int&, int&, int&, int&, int&) const {};
273 void cbaz1c(const int&) const {};
274 void cbaz2c(const int&, const int&) const {};
275 void cbaz3c(const int&, const int&, const int&) const {};
276 void cbaz4c(const int&, const int&, const int&, const int&) const {};
277 void cbaz5c(const int&, const int&, const int&, const int&, const int&) const {};
278 /** @} */
279};
280
281/**
282 * Function used for scheduling.
283 *
284 * @{
285 */
286static void foo0() {};
287static void foo1(int) {};
288static void foo2(int, int) {};
289static void foo3(int, int, int) {};
290static void foo4(int, int, int, int) {};
291static void foo5(int, int, int, int, int) {};
292static void ber1(int&) {};
293static void ber2(int&, int&) {};
294static void ber3(int&, int&, int&) {};
295static void ber4(int&, int&, int&, int&) {};
296static void ber5(int&, int&, int&, int&, int&) {};
297static void cber1(const int&) {};
298static void cber2(const int&, const int&) {};
299static void cber3(const int&, const int&, const int&) {};
300static void cber4(const int&, const int&, const int&, const int&) {};
301static void cber5(const int&, const int&, const int&, const int&, const int&) {};
302
303/** @} */
304
306 : TestCase("Check that all templates are instantiated correctly. This is a compilation test, "
307 "it cannot fail at runtime.")
308{
309}
310
311void
313{
314 // Test schedule of const methods
363
364 // Test of schedule const methods with Ptr<> pointers
371 0);
375 0,
376 0);
380 0,
381 0,
382 0);
386 0,
387 0,
388 0,
389 0);
393 0,
394 0,
395 0,
396 0,
397 0);
402 0);
405 0,
406 0);
409 0,
410 0,
411 0);
414 0,
415 0,
416 0,
417 0);
420 0,
421 0,
422 0,
423 0,
424 0);
429 0);
432 0,
433 0);
436 0,
437 0,
438 0);
441 0,
442 0,
443 0,
444 0);
447 0,
448 0,
449 0,
450 0,
451 0);
452
453 // Test schedule of raw functions
456 Simulator::Schedule(Seconds(0), &foo2, 0, 0);
457 Simulator::Schedule(Seconds(0), &foo3, 0, 0, 0);
458 Simulator::Schedule(Seconds(0), &foo4, 0, 0, 0, 0);
459 Simulator::Schedule(Seconds(0), &foo5, 0, 0, 0, 0, 0);
462 Simulator::Schedule(Seconds(0), &cber3, 0, 0, 0);
463 Simulator::Schedule(Seconds(0), &cber4, 0, 0, 0, 0);
464 Simulator::Schedule(Seconds(0), &cber5, 0, 0, 0, 0, 0);
468 Simulator::ScheduleNow(&foo3, 0, 0, 0);
469 Simulator::ScheduleNow(&foo4, 0, 0, 0, 0);
470 Simulator::ScheduleNow(&foo5, 0, 0, 0, 0, 0);
473 Simulator::ScheduleNow(&cber3, 0, 0, 0);
474 Simulator::ScheduleNow(&cber4, 0, 0, 0, 0);
475 Simulator::ScheduleNow(&cber5, 0, 0, 0, 0, 0);
480 Simulator::ScheduleDestroy(&foo4, 0, 0, 0, 0);
481 Simulator::ScheduleDestroy(&foo5, 0, 0, 0, 0, 0);
485 Simulator::ScheduleDestroy(&cber4, 0, 0, 0, 0);
486 Simulator::ScheduleDestroy(&cber5, 0, 0, 0, 0, 0);
487
488 // Test schedule of normal member methods
522
523 // test schedule of normal methods with Ptr<> pointers
530 0);
534 0,
535 0);
539 0,
540 0,
541 0);
545 0,
546 0,
547 0,
548 0);
552 0,
553 0,
554 0,
555 0,
556 0);
560 0);
563 0,
564 0);
567 0,
568 0,
569 0);
572 0,
573 0,
574 0,
575 0);
578 0,
579 0,
580 0,
581 0,
582 0);
587 0);
590 0,
591 0);
594 0,
595 0,
596 0);
599 0,
600 0,
601 0,
602 0);
605 0,
606 0,
607 0,
608 0,
609 0);
610
611 // the code below does not compile, as expected.
612 // Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
613
614 // This code appears to be duplicate test code.
616 Simulator::Schedule(Seconds(0), &ber2, 0, 0);
617 Simulator::Schedule(Seconds(0), &ber3, 0, 0, 0);
618 Simulator::Schedule(Seconds(0), &ber4, 0, 0, 0, 0);
619 Simulator::Schedule(Seconds(0), &ber5, 0, 0, 0, 0, 0);
627 Simulator::ScheduleNow(&ber3, 0, 0, 0);
628 Simulator::ScheduleNow(&ber4, 0, 0, 0, 0);
629 Simulator::ScheduleNow(&ber5, 0, 0, 0, 0, 0);
638 Simulator::ScheduleDestroy(&ber4, 0, 0, 0, 0);
639 Simulator::ScheduleDestroy(&ber5, 0, 0, 0, 0, 0);
645
648}
649
650/**
651 * @ingroup simulator-tests
652 *
653 * @brief The simulator Test Suite.
654 */
675
676static SimulatorTestSuite g_simulatorTestSuite; //!< Static variable for test initialization
Check that basic event handling is working with different Simulator implementations.
void EventD(int value)
Test Event.
void Destroy()
Checks that the events has been destroyed.
uint64_t NowUs()
Get the simulator time.
void DoRun() override
Implementation to actually run this TestCase.
void EventC(int value)
Test Event.
bool m_c
Checks that events are properly handled.
SimulatorEventsTestCase(ObjectFactory schedulerFactory)
Constructor.
bool m_a
Checks that events are properly handled.
bool m_destroy
Checks that events are properly handled.
bool m_b
Checks that events are properly handled.
void EventB(int value)
Test Event.
ObjectFactory m_schedulerFactory
Scheduler factory.
EventId m_destroyId
Event to check event lifetime.
void EventA(int value)
Test Event.
bool m_d
Checks that events are properly handled.
void bar2(int, int)
Function used for scheduling.
void bar4(int, int, int, int)
Function used for scheduling.
void bar1c(int) const
Function used for scheduling.
void bar0()
Function used for scheduling.
void cbaz2(const int &, const int &)
Function used for scheduling.
void Ref() const
Ref and Unref - only here for testing of Ptr<>.
void Unref() const
Ref and Unref - only here for testing of Ptr<>.
void baz5c(int &, int &, int &, int &, int &) const
Function used for scheduling.
void baz1(int &)
Function used for scheduling.
void cbaz4c(const int &, const int &, const int &, const int &) const
Function used for scheduling.
void baz3c(int &, int &, int &) const
Function used for scheduling.
void baz4c(int &, int &, int &, int &) const
Function used for scheduling.
void cbaz2c(const int &, const int &) const
Function used for scheduling.
void baz4(int &, int &, int &, int &)
Function used for scheduling.
void baz1c(int &) const
Function used for scheduling.
void cbaz5c(const int &, const int &, const int &, const int &, const int &) const
Function used for scheduling.
void cbaz5(const int &, const int &, const int &, const int &, const int &)
Function used for scheduling.
void bar5(int, int, int, int, int)
Function used for scheduling.
void baz2c(int &, int &) const
Function used for scheduling.
void baz5(int &, int &, int &, int &, int &)
Function used for scheduling.
void bar1(int)
Function used for scheduling.
void cbaz1(const int &)
Function used for scheduling.
void cbaz3(const int &, const int &, const int &)
Function used for scheduling.
void bar4c(int, int, int, int) const
Function used for scheduling.
void bar3c(int, int, int) const
Function used for scheduling.
void bar0c() const
Function used for scheduling.
void DoRun() override
Call graph was not generated because of its size.
void cbaz3c(const int &, const int &, const int &) const
Function used for scheduling.
void cbaz1c(const int &) const
Function used for scheduling.
void cbaz4(const int &, const int &, const int &, const int &)
Function used for scheduling.
void baz2(int &, int &)
Function used for scheduling.
void bar5c(int, int, int, int, int) const
Function used for scheduling.
void bar2c(int, int) const
Function used for scheduling.
void bar3(int, int, int)
Function used for scheduling.
void baz3(int &, int &, int &)
Function used for scheduling.
The simulator Test Suite.
static TypeId GetTypeId()
Register this type.
An identifier for simulation events.
Definition event-id.h:44
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
static TypeId GetTypeId()
Register this type.
static TypeId GetTypeId()
Register this type.
static TypeId GetTypeId()
Register this type.
Instantiate subclasses of ns3::Object.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
static TypeId GetTypeId()
Register this type.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:268
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition simulator.h:631
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition simulator.cc:147
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:614
static void Remove(const EventId &id)
Remove an event from the event list.
Definition simulator.cc:258
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
std::string GetName() const
Definition test.cc:371
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:414
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:288
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:240
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1415
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static void ber5(int &, int &, int &, int &, int &)
Function used for scheduling.
static void foo3(int, int, int)
Function used for scheduling.
static void ber4(int &, int &, int &, int &)
Function used for scheduling.
static void cber5(const int &, const int &, const int &, const int &, const int &)
Function used for scheduling.
static void cber1(const int &)
Function used for scheduling.
static void ber3(int &, int &, int &)
Function used for scheduling.
static SimulatorTestSuite g_simulatorTestSuite
Static variable for test initialization.
static void cber3(const int &, const int &, const int &)
Function used for scheduling.
static void foo4(int, int, int, int)
Function used for scheduling.
static void foo2(int, int)
Function used for scheduling.
static void cber2(const int &, const int &)
Function used for scheduling.
static void cber4(const int &, const int &, const int &, const int &)
Function used for scheduling.
static void ber1(int &)
Function used for scheduling.
static void foo5(int, int, int, int, int)
Function used for scheduling.
static void foo1(int)
Function used for scheduling.
static void foo0()
Function used for scheduling.
static void ber2(int &, int &)
Function used for scheduling.