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 */
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 foo0() {};
280static void foo1(int) {};
281static void foo2(int, int) {};
282static void foo3(int, int, int) {};
283static void foo4(int, int, int, int) {};
284static void foo5(int, int, int, int, int) {};
285static void ber1(int&) {};
286static void ber2(int&, int&) {};
287static void ber3(int&, int&, int&) {};
288static void ber4(int&, int&, int&, int&) {};
289static void ber5(int&, int&, int&, int&, int&) {};
290static void cber1(const int&) {};
291static void cber2(const int&, const int&) {};
292static void cber3(const int&, const int&, const int&) {};
293static void cber4(const int&, const int&, const int&, const int&) {};
294static void cber5(const int&, const int&, const int&, const int&, const int&) {};
295
296/** @} */
297
299 : TestCase("Check that all templates are instantiated correctly. This is a compilation test, "
300 "it cannot fail at runtime.")
301{
302}
303
304void
306{
307 // Test schedule of const methods
356
357 // Test of schedule const methods with Ptr<> pointers
364 0);
368 0,
369 0);
373 0,
374 0,
375 0);
379 0,
380 0,
381 0,
382 0);
386 0,
387 0,
388 0,
389 0,
390 0);
395 0);
398 0,
399 0);
402 0,
403 0,
404 0);
407 0,
408 0,
409 0,
410 0);
413 0,
414 0,
415 0,
416 0,
417 0);
422 0);
425 0,
426 0);
429 0,
430 0,
431 0);
434 0,
435 0,
436 0,
437 0);
440 0,
441 0,
442 0,
443 0,
444 0);
445
446 // Test schedule of raw functions
449 Simulator::Schedule(Seconds(0), &foo2, 0, 0);
450 Simulator::Schedule(Seconds(0), &foo3, 0, 0, 0);
451 Simulator::Schedule(Seconds(0), &foo4, 0, 0, 0, 0);
452 Simulator::Schedule(Seconds(0), &foo5, 0, 0, 0, 0, 0);
455 Simulator::Schedule(Seconds(0), &cber3, 0, 0, 0);
456 Simulator::Schedule(Seconds(0), &cber4, 0, 0, 0, 0);
457 Simulator::Schedule(Seconds(0), &cber5, 0, 0, 0, 0, 0);
461 Simulator::ScheduleNow(&foo3, 0, 0, 0);
462 Simulator::ScheduleNow(&foo4, 0, 0, 0, 0);
463 Simulator::ScheduleNow(&foo5, 0, 0, 0, 0, 0);
466 Simulator::ScheduleNow(&cber3, 0, 0, 0);
467 Simulator::ScheduleNow(&cber4, 0, 0, 0, 0);
468 Simulator::ScheduleNow(&cber5, 0, 0, 0, 0, 0);
473 Simulator::ScheduleDestroy(&foo4, 0, 0, 0, 0);
474 Simulator::ScheduleDestroy(&foo5, 0, 0, 0, 0, 0);
478 Simulator::ScheduleDestroy(&cber4, 0, 0, 0, 0);
479 Simulator::ScheduleDestroy(&cber5, 0, 0, 0, 0, 0);
480
481 // Test schedule of normal member methods
515
516 // test schedule of normal methods with Ptr<> pointers
523 0);
527 0,
528 0);
532 0,
533 0,
534 0);
538 0,
539 0,
540 0,
541 0);
545 0,
546 0,
547 0,
548 0,
549 0);
553 0);
556 0,
557 0);
560 0,
561 0,
562 0);
565 0,
566 0,
567 0,
568 0);
571 0,
572 0,
573 0,
574 0,
575 0);
580 0);
583 0,
584 0);
587 0,
588 0,
589 0);
592 0,
593 0,
594 0,
595 0);
598 0,
599 0,
600 0,
601 0,
602 0);
603
604 // the code below does not compile, as expected.
605 // Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
606
607 // This code appears to be duplicate test code.
609 Simulator::Schedule(Seconds(0), &ber2, 0, 0);
610 Simulator::Schedule(Seconds(0), &ber3, 0, 0, 0);
611 Simulator::Schedule(Seconds(0), &ber4, 0, 0, 0, 0);
612 Simulator::Schedule(Seconds(0), &ber5, 0, 0, 0, 0, 0);
620 Simulator::ScheduleNow(&ber3, 0, 0, 0);
621 Simulator::ScheduleNow(&ber4, 0, 0, 0, 0);
622 Simulator::ScheduleNow(&ber5, 0, 0, 0, 0, 0);
631 Simulator::ScheduleDestroy(&ber4, 0, 0, 0, 0);
632 Simulator::ScheduleDestroy(&ber5, 0, 0, 0, 0, 0);
638
641}
642
643/**
644 * @ingroup simulator-tests
645 *
646 * @brief The simulator Test Suite.
647 */
668
669static 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
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: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:67
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
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
@ QUICK
Fast test.
Definition test.h:1054
TestCase(const TestCase &)=delete
std::string GetName() const
Definition test.cc:367
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:490
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:409
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:240
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1393
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1369
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.