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{
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
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");
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 */
211{
212 public:
214
215 /**
216 * Ref and Unref - only here for testing of Ptr<>
217 *
218 * @{
219 */
220 void Ref() const
221 {
222 }
223
224 void Unref() const
225 {
226 }
227
228 /** @} */
229
230 private:
231 void DoRun() override;
232
233 /**
234 * Function used for scheduling.
235 *
236 * @{
237 */
238 void bar0(){};
239 void bar1(int){};
240 void bar2(int, int){};
241 void bar3(int, int, int){};
242 void bar4(int, int, int, int){};
243 void bar5(int, int, int, int, int){};
244 void baz1(int&){};
245 void baz2(int&, int&){};
246 void baz3(int&, int&, int&){};
247 void baz4(int&, int&, int&, int&){};
248 void baz5(int&, int&, int&, int&, int&){};
249 void cbaz1(const int&){};
250 void cbaz2(const int&, const int&){};
251 void cbaz3(const int&, const int&, const int&){};
252 void cbaz4(const int&, const int&, const int&, const int&){};
253 void cbaz5(const int&, const int&, const int&, const int&, const int&){};
254
255 void bar0c() const {};
256 void bar1c(int) const {};
257 void bar2c(int, int) const {};
258 void bar3c(int, int, int) const {};
259 void bar4c(int, int, int, int) const {};
260 void bar5c(int, int, int, int, int) const {};
261 void baz1c(int&) const {};
262 void baz2c(int&, int&) const {};
263 void baz3c(int&, int&, int&) const {};
264 void baz4c(int&, int&, int&, int&) const {};
265 void baz5c(int&, int&, int&, int&, int&) const {};
266 void cbaz1c(const int&) const {};
267 void cbaz2c(const int&, const int&) const {};
268 void cbaz3c(const int&, const int&, const int&) const {};
269 void cbaz4c(const int&, const int&, const int&, const int&) const {};
270 void cbaz5c(const int&, const int&, const int&, const int&, const int&) const {};
271 /** @} */
272};
273
274/**
275 * Function used for scheduling.
276 *
277 * @{
278 */
279static void
281{
282}
283
284static void
286{
287}
288
289static void
290foo2(int, int)
291{
292}
293
294static void
295foo3(int, int, int)
296{
297}
298
299static void
300foo4(int, int, int, int)
301{
302}
303
304static void
305foo5(int, int, int, int, int)
306{
307}
308
309static void
310ber1(int&)
311{
312}
313
314static void
315ber2(int&, int&)
316{
317}
318
319static void
320ber3(int&, int&, int&)
321{
322}
323
324static void
325ber4(int&, int&, int&, int&)
326{
327}
328
329static void
330ber5(int&, int&, int&, int&, int&)
331{
332}
333
334static void
335cber1(const int&)
336{
337}
338
339static void
340cber2(const int&, const int&)
341{
342}
343
344static void
345cber3(const int&, const int&, const int&)
346{
347}
348
349static void
350cber4(const int&, const int&, const int&, const int&)
351{
352}
353
354static void
355cber5(const int&, const int&, const int&, const int&, const int&)
356{
357}
358
359/** @} */
360
362 : TestCase("Check that all templates are instantiated correctly. This is a compilation test, "
363 "it cannot fail at runtime.")
364{
365}
366
367void
369{
370 // Test schedule of const methods
419
420 // Test of schedule const methods with Ptr<> pointers
427 0);
431 0,
432 0);
436 0,
437 0,
438 0);
442 0,
443 0,
444 0,
445 0);
449 0,
450 0,
451 0,
452 0,
453 0);
458 0);
461 0,
462 0);
465 0,
466 0,
467 0);
470 0,
471 0,
472 0,
473 0);
476 0,
477 0,
478 0,
479 0,
480 0);
485 0);
488 0,
489 0);
492 0,
493 0,
494 0);
497 0,
498 0,
499 0,
500 0);
503 0,
504 0,
505 0,
506 0,
507 0);
508
509 // Test schedule of raw functions
512 Simulator::Schedule(Seconds(0), &foo2, 0, 0);
513 Simulator::Schedule(Seconds(0), &foo3, 0, 0, 0);
514 Simulator::Schedule(Seconds(0), &foo4, 0, 0, 0, 0);
515 Simulator::Schedule(Seconds(0), &foo5, 0, 0, 0, 0, 0);
518 Simulator::Schedule(Seconds(0), &cber3, 0, 0, 0);
519 Simulator::Schedule(Seconds(0), &cber4, 0, 0, 0, 0);
520 Simulator::Schedule(Seconds(0), &cber5, 0, 0, 0, 0, 0);
524 Simulator::ScheduleNow(&foo3, 0, 0, 0);
525 Simulator::ScheduleNow(&foo4, 0, 0, 0, 0);
526 Simulator::ScheduleNow(&foo5, 0, 0, 0, 0, 0);
529 Simulator::ScheduleNow(&cber3, 0, 0, 0);
530 Simulator::ScheduleNow(&cber4, 0, 0, 0, 0);
531 Simulator::ScheduleNow(&cber5, 0, 0, 0, 0, 0);
536 Simulator::ScheduleDestroy(&foo4, 0, 0, 0, 0);
537 Simulator::ScheduleDestroy(&foo5, 0, 0, 0, 0, 0);
541 Simulator::ScheduleDestroy(&cber4, 0, 0, 0, 0);
542 Simulator::ScheduleDestroy(&cber5, 0, 0, 0, 0, 0);
543
544 // Test schedule of normal member methods
578
579 // test schedule of normal methods with Ptr<> pointers
586 0);
590 0,
591 0);
595 0,
596 0,
597 0);
601 0,
602 0,
603 0,
604 0);
608 0,
609 0,
610 0,
611 0,
612 0);
616 0);
619 0,
620 0);
623 0,
624 0,
625 0);
628 0,
629 0,
630 0,
631 0);
634 0,
635 0,
636 0,
637 0,
638 0);
643 0);
646 0,
647 0);
650 0,
651 0,
652 0);
655 0,
656 0,
657 0,
658 0);
661 0,
662 0,
663 0,
664 0,
665 0);
666
667 // the code below does not compile, as expected.
668 // Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
669
670 // This code appears to be duplicate test code.
672 Simulator::Schedule(Seconds(0), &ber2, 0, 0);
673 Simulator::Schedule(Seconds(0), &ber3, 0, 0, 0);
674 Simulator::Schedule(Seconds(0), &ber4, 0, 0, 0, 0);
675 Simulator::Schedule(Seconds(0), &ber5, 0, 0, 0, 0, 0);
683 Simulator::ScheduleNow(&ber3, 0, 0, 0);
684 Simulator::ScheduleNow(&ber4, 0, 0, 0, 0);
685 Simulator::ScheduleNow(&ber5, 0, 0, 0, 0, 0);
694 Simulator::ScheduleDestroy(&ber4, 0, 0, 0, 0);
695 Simulator::ScheduleDestroy(&ber5, 0, 0, 0, 0, 0);
701
704}
705
706/**
707 * @ingroup simulator-tests
708 *
709 * @brief The simulator Test Suite.
710 */
712{
713 public:
715 : TestSuite("simulator")
716 {
717 ObjectFactory factory;
719
720 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
722 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
724 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
726 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
728 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
729 }
730};
731
732static 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.
Check that all templates are instantiated correctly.
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
Implementation to actually run this TestCase.
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:45
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc: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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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:274
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
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:611
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition simulator.cc:153
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
static void Remove(const EventId &id)
Remove an event from the event list.
Definition simulator.cc:264
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
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:407
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:294
#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:241
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1368
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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.