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
239 // clang-format off
240
241 void bar0() {}
242 void bar1(int) {}
243 void bar2(int, int) {}
244 void bar3(int, int, int) {}
245 void bar4(int, int, int, int) {}
246 void bar5(int, int, int, int, int) {}
247 void baz1(int&) {}
248 void baz2(int&, int&) {}
249 void baz3(int&, int&, int&) {}
250 void baz4(int&, int&, int&, int&) {}
251 void baz5(int&, int&, int&, int&, int&) {}
252 void cbaz1(const int&) {}
253 void cbaz2(const int&, const int&) {}
254 void cbaz3(const int&, const int&, const int&) {}
255 void cbaz4(const int&, const int&, const int&, const int&) {}
256 void cbaz5(const int&, const int&, const int&, const int&, const int&) {}
257
258 void bar0c() const {}
259 void bar1c(int) const {}
260 void bar2c(int, int) const {}
261 void bar3c(int, int, int) const {}
262 void bar4c(int, int, int, int) const {}
263 void bar5c(int, int, int, int, int) const {}
264 void baz1c(int&) const {}
265 void baz2c(int&, int&) const {}
266 void baz3c(int&, int&, int&) const {}
267 void baz4c(int&, int&, int&, int&) const {}
268 void baz5c(int&, int&, int&, int&, int&) const {}
269 void cbaz1c(const int&) const {}
270 void cbaz2c(const int&, const int&) const {}
271 void cbaz3c(const int&, const int&, const int&) const {}
272 void cbaz4c(const int&, const int&, const int&, const int&) const {}
273 void cbaz5c(const int&, const int&, const int&, const int&, const int&) const {}
274
275 // clang-format on
276
277 /** @} */
278};
279
280/**
281 * Function used for scheduling.
282 *
283 * @{
284 */
285
286// clang-format off
287
288static void foo0() {}
289static void foo1(int) {}
290static void foo2(int, int) {}
291static void foo3(int, int, int) {}
292static void foo4(int, int, int, int) {}
293static void foo5(int, int, int, int, int) {}
294static void ber1(int&) {}
295static void ber2(int&, int&) {}
296static void ber3(int&, int&, int&) {}
297static void ber4(int&, int&, int&, int&) {}
298static void ber5(int&, int&, int&, int&, int&) {}
299static void cber1(const int&) {}
300static void cber2(const int&, const int&) {}
301static void cber3(const int&, const int&, const int&) {}
302static void cber4(const int&, const int&, const int&, const int&) {}
303static void cber5(const int&, const int&, const int&, const int&, const int&) {}
304
305// clang-format on
306
307/** @} */
308
310 : TestCase("Check that all templates are instantiated correctly. This is a compilation test, "
311 "it cannot fail at runtime.")
312{
313}
314
315void
317{
318 // Test schedule of const methods
367
368 // Test of schedule const methods with Ptr<> pointers
375 0);
379 0,
380 0);
384 0,
385 0,
386 0);
390 0,
391 0,
392 0,
393 0);
397 0,
398 0,
399 0,
400 0,
401 0);
406 0);
409 0,
410 0);
413 0,
414 0,
415 0);
418 0,
419 0,
420 0,
421 0);
424 0,
425 0,
426 0,
427 0,
428 0);
433 0);
436 0,
437 0);
440 0,
441 0,
442 0);
445 0,
446 0,
447 0,
448 0);
451 0,
452 0,
453 0,
454 0,
455 0);
456
457 // Test schedule of raw functions
460 Simulator::Schedule(Seconds(0), &foo2, 0, 0);
461 Simulator::Schedule(Seconds(0), &foo3, 0, 0, 0);
462 Simulator::Schedule(Seconds(0), &foo4, 0, 0, 0, 0);
463 Simulator::Schedule(Seconds(0), &foo5, 0, 0, 0, 0, 0);
466 Simulator::Schedule(Seconds(0), &cber3, 0, 0, 0);
467 Simulator::Schedule(Seconds(0), &cber4, 0, 0, 0, 0);
468 Simulator::Schedule(Seconds(0), &cber5, 0, 0, 0, 0, 0);
472 Simulator::ScheduleNow(&foo3, 0, 0, 0);
473 Simulator::ScheduleNow(&foo4, 0, 0, 0, 0);
474 Simulator::ScheduleNow(&foo5, 0, 0, 0, 0, 0);
477 Simulator::ScheduleNow(&cber3, 0, 0, 0);
478 Simulator::ScheduleNow(&cber4, 0, 0, 0, 0);
479 Simulator::ScheduleNow(&cber5, 0, 0, 0, 0, 0);
484 Simulator::ScheduleDestroy(&foo4, 0, 0, 0, 0);
485 Simulator::ScheduleDestroy(&foo5, 0, 0, 0, 0, 0);
489 Simulator::ScheduleDestroy(&cber4, 0, 0, 0, 0);
490 Simulator::ScheduleDestroy(&cber5, 0, 0, 0, 0, 0);
491
492 // Test schedule of normal member methods
526
527 // test schedule of normal methods with Ptr<> pointers
534 0);
538 0,
539 0);
543 0,
544 0,
545 0);
549 0,
550 0,
551 0,
552 0);
556 0,
557 0,
558 0,
559 0,
560 0);
564 0);
567 0,
568 0);
571 0,
572 0,
573 0);
576 0,
577 0,
578 0,
579 0);
582 0,
583 0,
584 0,
585 0,
586 0);
591 0);
594 0,
595 0);
598 0,
599 0,
600 0);
603 0,
604 0,
605 0,
606 0);
609 0,
610 0,
611 0,
612 0,
613 0);
614
615 // the code below does not compile, as expected.
616 // Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
617
618 // This code appears to be duplicate test code.
620 Simulator::Schedule(Seconds(0), &ber2, 0, 0);
621 Simulator::Schedule(Seconds(0), &ber3, 0, 0, 0);
622 Simulator::Schedule(Seconds(0), &ber4, 0, 0, 0, 0);
623 Simulator::Schedule(Seconds(0), &ber5, 0, 0, 0, 0, 0);
631 Simulator::ScheduleNow(&ber3, 0, 0, 0);
632 Simulator::ScheduleNow(&ber4, 0, 0, 0, 0);
633 Simulator::ScheduleNow(&ber5, 0, 0, 0, 0, 0);
642 Simulator::ScheduleDestroy(&ber4, 0, 0, 0, 0);
643 Simulator::ScheduleDestroy(&ber5, 0, 0, 0, 0, 0);
649
652}
653
654/**
655 * @ingroup simulator-tests
656 *
657 * @brief The simulator Test Suite.
658 */
660{
661 public:
663 : TestSuite("simulator")
664 {
665 ObjectFactory factory;
667
668 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
670 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
672 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
674 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
676 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
677 }
678};
679
680static 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:561
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:612
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:595
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:1369
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
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.