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 * Ref and Unref - only here for testing of Ptr<>
216 *
217 * @{
218 */
219 void Ref() const {};
220 void Unref() const {};
221 /** @} */
222
223 private:
224 void DoRun() override;
225
226 /**
227 * Function used for scheduling.
228 *
229 * @{
230 */
231 void bar0(){};
232 void bar1(int){};
233 void bar2(int, int){};
234 void bar3(int, int, int){};
235 void bar4(int, int, int, int){};
236 void bar5(int, int, int, int, int){};
237 void baz1(int&){};
238 void baz2(int&, int&){};
239 void baz3(int&, int&, int&){};
240 void baz4(int&, int&, int&, int&){};
241 void baz5(int&, int&, int&, int&, int&){};
242 void cbaz1(const int&){};
243 void cbaz2(const int&, const int&){};
244 void cbaz3(const int&, const int&, const int&){};
245 void cbaz4(const int&, const int&, const int&, const int&){};
246 void cbaz5(const int&, const int&, const int&, const int&, const int&){};
247
248 void bar0c() const {};
249 void bar1c(int) const {};
250 void bar2c(int, int) const {};
251 void bar3c(int, int, int) const {};
252 void bar4c(int, int, int, int) const {};
253 void bar5c(int, int, int, int, int) const {};
254 void baz1c(int&) const {};
255 void baz2c(int&, int&) const {};
256 void baz3c(int&, int&, int&) const {};
257 void baz4c(int&, int&, int&, int&) const {};
258 void baz5c(int&, int&, int&, int&, int&) const {};
259 void cbaz1c(const int&) const {};
260 void cbaz2c(const int&, const int&) const {};
261 void cbaz3c(const int&, const int&, const int&) const {};
262 void cbaz4c(const int&, const int&, const int&, const int&) const {};
263 void cbaz5c(const int&, const int&, const int&, const int&, const int&) const {};
264 /** @} */
265};
266
267/**
268 * Function used for scheduling.
269 *
270 * @{
271 */
272static void
274{
275}
276
277static void
279{
280}
281
282static void
283foo2(int, int)
284{
285}
286
287static void
288foo3(int, int, int)
289{
290}
291
292static void
293foo4(int, int, int, int)
294{
295}
296
297static void
298foo5(int, int, int, int, int)
299{
300}
301
302static void
303ber1(int&)
304{
305}
306
307static void
308ber2(int&, int&)
309{
310}
311
312static void
313ber3(int&, int&, int&)
314{
315}
316
317static void
318ber4(int&, int&, int&, int&)
319{
320}
321
322static void
323ber5(int&, int&, int&, int&, int&)
324{
325}
326
327static void
328cber1(const int&)
329{
330}
331
332static void
333cber2(const int&, const int&)
334{
335}
336
337static void
338cber3(const int&, const int&, const int&)
339{
340}
341
342static void
343cber4(const int&, const int&, const int&, const int&)
344{
345}
346
347static void
348cber5(const int&, const int&, const int&, const int&, const int&)
349{
350}
351
352/** @} */
353
355 : TestCase("Check that all templates are instantiated correctly. This is a compilation test, "
356 "it cannot fail at runtime.")
357{
358}
359
360void
362{
363 // Test schedule of const methods
412
413 // Test of schedule const methods with Ptr<> pointers
420 0);
424 0,
425 0);
429 0,
430 0,
431 0);
435 0,
436 0,
437 0,
438 0);
442 0,
443 0,
444 0,
445 0,
446 0);
451 0);
454 0,
455 0);
458 0,
459 0,
460 0);
463 0,
464 0,
465 0,
466 0);
469 0,
470 0,
471 0,
472 0,
473 0);
478 0);
481 0,
482 0);
485 0,
486 0,
487 0);
490 0,
491 0,
492 0,
493 0);
496 0,
497 0,
498 0,
499 0,
500 0);
501
502 // Test schedule of raw functions
505 Simulator::Schedule(Seconds(0.0), &foo2, 0, 0);
506 Simulator::Schedule(Seconds(0.0), &foo3, 0, 0, 0);
507 Simulator::Schedule(Seconds(0.0), &foo4, 0, 0, 0, 0);
508 Simulator::Schedule(Seconds(0.0), &foo5, 0, 0, 0, 0, 0);
510 Simulator::Schedule(Seconds(0.0), &cber2, 0, 0);
511 Simulator::Schedule(Seconds(0.0), &cber3, 0, 0, 0);
512 Simulator::Schedule(Seconds(0.0), &cber4, 0, 0, 0, 0);
513 Simulator::Schedule(Seconds(0.0), &cber5, 0, 0, 0, 0, 0);
517 Simulator::ScheduleNow(&foo3, 0, 0, 0);
518 Simulator::ScheduleNow(&foo4, 0, 0, 0, 0);
519 Simulator::ScheduleNow(&foo5, 0, 0, 0, 0, 0);
522 Simulator::ScheduleNow(&cber3, 0, 0, 0);
523 Simulator::ScheduleNow(&cber4, 0, 0, 0, 0);
524 Simulator::ScheduleNow(&cber5, 0, 0, 0, 0, 0);
529 Simulator::ScheduleDestroy(&foo4, 0, 0, 0, 0);
530 Simulator::ScheduleDestroy(&foo5, 0, 0, 0, 0, 0);
534 Simulator::ScheduleDestroy(&cber4, 0, 0, 0, 0);
535 Simulator::ScheduleDestroy(&cber5, 0, 0, 0, 0, 0);
536
537 // Test schedule of normal member methods
571
572 // test schedule of normal methods with Ptr<> pointers
579 0);
583 0,
584 0);
588 0,
589 0,
590 0);
594 0,
595 0,
596 0,
597 0);
601 0,
602 0,
603 0,
604 0,
605 0);
609 0);
612 0,
613 0);
616 0,
617 0,
618 0);
621 0,
622 0,
623 0,
624 0);
627 0,
628 0,
629 0,
630 0,
631 0);
636 0);
639 0,
640 0);
643 0,
644 0,
645 0);
648 0,
649 0,
650 0,
651 0);
654 0,
655 0,
656 0,
657 0,
658 0);
659
660 // the code below does not compile, as expected.
661 // Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
662
663 // This code appears to be duplicate test code.
665 Simulator::Schedule(Seconds(0.0), &ber2, 0, 0);
666 Simulator::Schedule(Seconds(0.0), &ber3, 0, 0, 0);
667 Simulator::Schedule(Seconds(0.0), &ber4, 0, 0, 0, 0);
668 Simulator::Schedule(Seconds(0.0), &ber5, 0, 0, 0, 0, 0);
676 Simulator::ScheduleNow(&ber3, 0, 0, 0);
677 Simulator::ScheduleNow(&ber4, 0, 0, 0, 0);
678 Simulator::ScheduleNow(&ber5, 0, 0, 0, 0, 0);
687 Simulator::ScheduleDestroy(&ber4, 0, 0, 0, 0);
688 Simulator::ScheduleDestroy(&ber5, 0, 0, 0, 0, 0);
694
697}
698
699/**
700 * \ingroup simulator-tests
701 *
702 * \brief The simulator Test Suite.
703 */
705{
706 public:
708 : TestSuite("simulator")
709 {
710 ObjectFactory factory;
712
713 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
715 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
717 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
719 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
721 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
722 }
723};
724
725static 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:1332
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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.