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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "ns3/calendar-scheduler.h"
20#include "ns3/heap-scheduler.h"
21#include "ns3/list-scheduler.h"
22#include "ns3/map-scheduler.h"
23#include "ns3/priority-queue-scheduler.h"
24#include "ns3/simulator.h"
25#include "ns3/test.h"
26
27using namespace ns3;
28
29/**
30 * \file
31 * \ingroup simulator-tests
32 * Simulator class test suite
33 */
34
35/**
36 * \ingroup core-tests
37 * \defgroup simulator-tests Simulator class tests
38 */
39
40/**
41 * \ingroup simulator-tests
42 *
43 * \brief Check that basic event handling is working with different Simulator implementations.
44 */
46{
47 public:
48 /**
49 * Constructor.
50 * \param schedulerFactory Scheduler factory.
51 */
52 SimulatorEventsTestCase(ObjectFactory schedulerFactory);
53 void DoRun() override;
54 /**
55 * Test Event.
56 * \param value Event parameter.
57 * @{
58 */
59 void EventA(int value);
60 void EventB(int value);
61 void EventC(int value);
62 void EventD(int value);
63 /** @} */
64
65 /**
66 * Test Event.
67 */
68 void Eventfoo0();
69
70 /**
71 * Get the simulator time.
72 * \return The actual time [ms].
73 */
74 uint64_t NowUs();
75 /**
76 * Checks that the events has been destroyed.
77 */
78 void Destroy();
79 /**
80 * Checks that events are properly handled.
81 * @{
82 */
83 bool m_a;
84 bool m_b;
85 bool m_c;
86 bool m_d;
88 /** @} */
89
90 EventId m_idC; //!< Event C.
91 EventId m_destroyId; //!< Event to check event lifetime.
92 ObjectFactory m_schedulerFactory; //!< Scheduler factory.
93};
94
96 : TestCase("Check that basic event handling is working with " +
97 schedulerFactory.GetTypeId().GetName()),
98 m_schedulerFactory(schedulerFactory)
99{
100}
101
102uint64_t
104{
105 uint64_t ns = Now().GetNanoSeconds();
106 return ns / 1000;
107}
108
109void
111{
112 m_a = false;
113}
114
115void
117{
118 m_b = !(b != 2 || NowUs() != 11);
121}
122
123void
125{
126 m_c = false;
127}
128
129void
131{
132 m_d = !(d != 4 || NowUs() != (11 + 10));
133}
134
135void
137{
138}
139
140void
142{
144 {
145 m_destroy = true;
146 }
147}
148
149void
151{
152 m_a = true;
153 m_b = false;
154 m_c = true;
155 m_d = false;
156
158
162
164 NS_TEST_EXPECT_MSG_EQ(!a.IsExpired(), true, "");
166 NS_TEST_EXPECT_MSG_EQ(a.IsExpired(), true, "");
168 NS_TEST_EXPECT_MSG_EQ(m_a, true, "Event A did not run ?");
169 NS_TEST_EXPECT_MSG_EQ(m_b, true, "Event B did not run ?");
170 NS_TEST_EXPECT_MSG_EQ(m_c, true, "Event C did not run ?");
171 NS_TEST_EXPECT_MSG_EQ(m_d, true, "Event D did not run ?");
172
174
175 // Test copy assignment operator
176 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
177 EventId anotherId = anId;
178
179 NS_TEST_EXPECT_MSG_EQ(!(anId.IsExpired() || anotherId.IsExpired()),
180 true,
181 "Event should not have expired yet.");
182
183 Simulator::Remove(anId);
184 NS_TEST_EXPECT_MSG_EQ(anId.IsExpired(), true, "Event was removed: it is now expired");
185 NS_TEST_EXPECT_MSG_EQ(anotherId.IsExpired(), true, "Event was removed: it is now expired");
186
187 m_destroy = false;
189 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
192 true,
193 "Event was canceled: should have expired now");
194
196 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
199 true,
200 "Event was canceled: should have expired now");
201
203 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
204
206 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
207 NS_TEST_EXPECT_MSG_EQ(!m_destroy, true, "Event should not have run");
208
210 NS_TEST_EXPECT_MSG_EQ(m_destroyId.IsExpired(), true, "Event should have expired now");
211 NS_TEST_EXPECT_MSG_EQ(m_destroy, true, "Event should have run");
212}
213
214/**
215 * \ingroup simulator-tests
216 *
217 * \brief Check that all templates are instantiated correctly.
218 *
219 * This is a compilation test, it cannot fail at runtime.
220 */
222{
223 public:
225 /**
226 * Ref and Unref - only here for testing of Ptr<>
227 *
228 * @{
229 */
230 void Ref() const {};
231 void Unref() const {};
232 /** @} */
233
234 private:
235 void DoRun() override;
236
237 /**
238 * Function used for scheduling.
239 *
240 * @{
241 */
242 void bar0(){};
243 void bar1(int){};
244 void bar2(int, int){};
245 void bar3(int, int, int){};
246 void bar4(int, int, int, int){};
247 void bar5(int, int, int, int, int){};
248 void baz1(int&){};
249 void baz2(int&, int&){};
250 void baz3(int&, int&, int&){};
251 void baz4(int&, int&, int&, int&){};
252 void baz5(int&, int&, int&, int&, int&){};
253 void cbaz1(const int&){};
254 void cbaz2(const int&, const int&){};
255 void cbaz3(const int&, const int&, const int&){};
256 void cbaz4(const int&, const int&, const int&, const int&){};
257 void cbaz5(const int&, const int&, const int&, const int&, const int&){};
258
259 void bar0c() const {};
260 void bar1c(int) const {};
261 void bar2c(int, int) const {};
262 void bar3c(int, int, int) const {};
263 void bar4c(int, int, int, int) const {};
264 void bar5c(int, int, int, int, int) const {};
265 void baz1c(int&) const {};
266 void baz2c(int&, int&) const {};
267 void baz3c(int&, int&, int&) const {};
268 void baz4c(int&, int&, int&, int&) const {};
269 void baz5c(int&, int&, int&, int&, int&) const {};
270 void cbaz1c(const int&) const {};
271 void cbaz2c(const int&, const int&) const {};
272 void cbaz3c(const int&, const int&, const int&) const {};
273 void cbaz4c(const int&, const int&, const int&, const int&) const {};
274 void cbaz5c(const int&, const int&, const int&, const int&, const int&) const {};
275 /** @} */
276};
277
278/**
279 * Function used for scheduling.
280 *
281 * @{
282 */
283static void
285{
286}
287
288static void
290{
291}
292
293static void
294foo2(int, int)
295{
296}
297
298static void
299foo3(int, int, int)
300{
301}
302
303static void
304foo4(int, int, int, int)
305{
306}
307
308static void
309foo5(int, int, int, int, int)
310{
311}
312
313static void
314ber1(int&)
315{
316}
317
318static void
319ber2(int&, int&)
320{
321}
322
323static void
324ber3(int&, int&, int&)
325{
326}
327
328static void
329ber4(int&, int&, int&, int&)
330{
331}
332
333static void
334ber5(int&, int&, int&, int&, int&)
335{
336}
337
338static void
339cber1(const int&)
340{
341}
342
343static void
344cber2(const int&, const int&)
345{
346}
347
348static void
349cber3(const int&, const int&, const int&)
350{
351}
352
353static void
354cber4(const int&, const int&, const int&, const int&)
355{
356}
357
358static void
359cber5(const int&, const int&, const int&, const int&, const int&)
360{
361}
362
363/** @} */
364
366 : TestCase("Check that all templates are instantiated correctly. This is a compilation test, "
367 "it cannot fail at runtime.")
368{
369}
370
371void
373{
374 // Test schedule of const methods
423
424 // Test of schedule const methods with Ptr<> pointers
431 0);
435 0,
436 0);
440 0,
441 0,
442 0);
446 0,
447 0,
448 0,
449 0);
453 0,
454 0,
455 0,
456 0,
457 0);
462 0);
465 0,
466 0);
469 0,
470 0,
471 0);
474 0,
475 0,
476 0,
477 0);
480 0,
481 0,
482 0,
483 0,
484 0);
489 0);
492 0,
493 0);
496 0,
497 0,
498 0);
501 0,
502 0,
503 0,
504 0);
507 0,
508 0,
509 0,
510 0,
511 0);
512
513 // Test schedule of raw functions
516 Simulator::Schedule(Seconds(0.0), &foo2, 0, 0);
517 Simulator::Schedule(Seconds(0.0), &foo3, 0, 0, 0);
518 Simulator::Schedule(Seconds(0.0), &foo4, 0, 0, 0, 0);
519 Simulator::Schedule(Seconds(0.0), &foo5, 0, 0, 0, 0, 0);
521 Simulator::Schedule(Seconds(0.0), &cber2, 0, 0);
522 Simulator::Schedule(Seconds(0.0), &cber3, 0, 0, 0);
523 Simulator::Schedule(Seconds(0.0), &cber4, 0, 0, 0, 0);
524 Simulator::Schedule(Seconds(0.0), &cber5, 0, 0, 0, 0, 0);
528 Simulator::ScheduleNow(&foo3, 0, 0, 0);
529 Simulator::ScheduleNow(&foo4, 0, 0, 0, 0);
530 Simulator::ScheduleNow(&foo5, 0, 0, 0, 0, 0);
533 Simulator::ScheduleNow(&cber3, 0, 0, 0);
534 Simulator::ScheduleNow(&cber4, 0, 0, 0, 0);
535 Simulator::ScheduleNow(&cber5, 0, 0, 0, 0, 0);
540 Simulator::ScheduleDestroy(&foo4, 0, 0, 0, 0);
541 Simulator::ScheduleDestroy(&foo5, 0, 0, 0, 0, 0);
545 Simulator::ScheduleDestroy(&cber4, 0, 0, 0, 0);
546 Simulator::ScheduleDestroy(&cber5, 0, 0, 0, 0, 0);
547
548 // Test schedule of normal member methods
582
583 // test schedule of normal methods with Ptr<> pointers
590 0);
594 0,
595 0);
599 0,
600 0,
601 0);
605 0,
606 0,
607 0,
608 0);
612 0,
613 0,
614 0,
615 0,
616 0);
620 0);
623 0,
624 0);
627 0,
628 0,
629 0);
632 0,
633 0,
634 0,
635 0);
638 0,
639 0,
640 0,
641 0,
642 0);
647 0);
650 0,
651 0);
654 0,
655 0,
656 0);
659 0,
660 0,
661 0,
662 0);
665 0,
666 0,
667 0,
668 0,
669 0);
670
671 // the code below does not compile, as expected.
672 // Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
673
674 // This code appears to be duplicate test code.
676 Simulator::Schedule(Seconds(0.0), &ber2, 0, 0);
677 Simulator::Schedule(Seconds(0.0), &ber3, 0, 0, 0);
678 Simulator::Schedule(Seconds(0.0), &ber4, 0, 0, 0, 0);
679 Simulator::Schedule(Seconds(0.0), &ber5, 0, 0, 0, 0, 0);
687 Simulator::ScheduleNow(&ber3, 0, 0, 0);
688 Simulator::ScheduleNow(&ber4, 0, 0, 0, 0);
689 Simulator::ScheduleNow(&ber5, 0, 0, 0, 0, 0);
698 Simulator::ScheduleDestroy(&ber4, 0, 0, 0, 0);
699 Simulator::ScheduleDestroy(&ber5, 0, 0, 0, 0, 0);
705
708}
709
710/**
711 * \ingroup simulator-tests
712 *
713 * \brief The simulator Test Suite.
714 */
716{
717 public:
719 : TestSuite("simulator")
720 {
721 ObjectFactory factory;
723
724 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
726 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
728 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
730 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
732 AddTestCase(new SimulatorEventsTestCase(factory), TestCase::Duration::QUICK);
733 }
734};
735
736static 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:55
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
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:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
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:285
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
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:622
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:164
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:275
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:418
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
#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:252
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1343
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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.