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
46{
47 public:
52 SimulatorEventsTestCase(ObjectFactory schedulerFactory);
53 void DoRun() override;
59 void EventA(int value);
60 void EventB(int value);
61 void EventC(int value);
62 void EventD(int value);
68 void Eventfoo0();
69
74 uint64_t NowUs();
78 void Destroy();
83 bool m_a;
84 bool m_b;
85 bool m_c;
86 bool m_d;
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 if (b != 2 || NowUs() != 11)
119 {
120 m_b = false;
121 }
122 else
123 {
124 m_b = true;
125 }
128}
129
130void
132{
133 m_c = false;
134}
135
136void
138{
139 if (d != 4 || NowUs() != (11 + 10))
140 {
141 m_d = false;
142 }
143 else
144 {
145 m_d = true;
146 }
147}
148
149void
151{
152}
153
154void
156{
158 {
159 m_destroy = true;
160 }
161}
162
163void
165{
166 m_a = true;
167 m_b = false;
168 m_c = true;
169 m_d = false;
170
172
176
178 NS_TEST_EXPECT_MSG_EQ(!a.IsExpired(), true, "");
180 NS_TEST_EXPECT_MSG_EQ(a.IsExpired(), true, "");
182 NS_TEST_EXPECT_MSG_EQ(m_a, true, "Event A did not run ?");
183 NS_TEST_EXPECT_MSG_EQ(m_b, true, "Event B did not run ?");
184 NS_TEST_EXPECT_MSG_EQ(m_c, true, "Event C did not run ?");
185 NS_TEST_EXPECT_MSG_EQ(m_d, true, "Event D did not run ?");
186
188 EventId anotherId = anId;
189 NS_TEST_EXPECT_MSG_EQ(!(anId.IsExpired() || anotherId.IsExpired()),
190 true,
191 "Event should not have expired yet.");
192
193 Simulator::Remove(anId);
194 NS_TEST_EXPECT_MSG_EQ(anId.IsExpired(), true, "Event was removed: it is now expired");
195 NS_TEST_EXPECT_MSG_EQ(anotherId.IsExpired(), true, "Event was removed: it is now expired");
196
197 m_destroy = false;
199 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
202 true,
203 "Event was canceled: should have expired now");
204
206 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
209 true,
210 "Event was canceled: should have expired now");
211
213 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
214
216 NS_TEST_EXPECT_MSG_EQ(!m_destroyId.IsExpired(), true, "Event should not have expired yet");
217 NS_TEST_EXPECT_MSG_EQ(!m_destroy, true, "Event should not have run");
218
220 NS_TEST_EXPECT_MSG_EQ(m_destroyId.IsExpired(), true, "Event should have expired now");
221 NS_TEST_EXPECT_MSG_EQ(m_destroy, true, "Event should have run");
222}
223
232{
233 public:
240 void Ref() const {};
241 void Unref() const {};
244 private:
245 void DoRun() override;
246
252 void bar0(){};
253 void bar1(int){};
254 void bar2(int, int){};
255 void bar3(int, int, int){};
256 void bar4(int, int, int, int){};
257 void bar5(int, int, int, int, int){};
258 void baz1(int&){};
259 void baz2(int&, int&){};
260 void baz3(int&, int&, int&){};
261 void baz4(int&, int&, int&, int&){};
262 void baz5(int&, int&, int&, int&, int&){};
263 void cbaz1(const int&){};
264 void cbaz2(const int&, const int&){};
265 void cbaz3(const int&, const int&, const int&){};
266 void cbaz4(const int&, const int&, const int&, const int&){};
267 void cbaz5(const int&, const int&, const int&, const int&, const int&){};
268
269 void bar0c() const {};
270 void bar1c(int) const {};
271 void bar2c(int, int) const {};
272 void bar3c(int, int, int) const {};
273 void bar4c(int, int, int, int) const {};
274 void bar5c(int, int, int, int, int) const {};
275 void baz1c(int&) const {};
276 void baz2c(int&, int&) const {};
277 void baz3c(int&, int&, int&) const {};
278 void baz4c(int&, int&, int&, int&) const {};
279 void baz5c(int&, int&, int&, int&, int&) const {};
280 void cbaz1c(const int&) const {};
281 void cbaz2c(const int&, const int&) const {};
282 void cbaz3c(const int&, const int&, const int&) const {};
283 void cbaz4c(const int&, const int&, const int&, const int&) const {};
284 void cbaz5c(const int&, const int&, const int&, const int&, const int&) const {};
286};
287
293static void
295{
296}
297
298static void
300{
301}
302
303static void
304foo2(int, int)
305{
306}
307
308static void
309foo3(int, int, int)
310{
311}
312
313static void
314foo4(int, int, int, int)
315{
316}
317
318static void
319foo5(int, int, int, int, int)
320{
321}
322
323static void
324ber1(int&)
325{
326}
327
328static void
329ber2(int&, int&)
330{
331}
332
333static void
334ber3(int&, int&, int&)
335{
336}
337
338static void
339ber4(int&, int&, int&, int&)
340{
341}
342
343static void
344ber5(int&, int&, int&, int&, int&)
345{
346}
347
348static void
349cber1(const int&)
350{
351}
352
353static void
354cber2(const int&, const int&)
355{
356}
357
358static void
359cber3(const int&, const int&, const int&)
360{
361}
362
363static void
364cber4(const int&, const int&, const int&, const int&)
365{
366}
367
368static void
369cber5(const int&, const int&, const int&, const int&, const int&)
370{
371}
372
376 : TestCase("Check that all templates are instantiated correctly. This is a compilation test, "
377 "it cannot fail at runtime.")
378{
379}
380
381void
383{
384 // Test schedule of const methods
433
434 // Test of schedule const methods with Ptr<> pointers
441 0);
445 0,
446 0);
450 0,
451 0,
452 0);
456 0,
457 0,
458 0,
459 0);
463 0,
464 0,
465 0,
466 0,
467 0);
472 0);
475 0,
476 0);
479 0,
480 0,
481 0);
484 0,
485 0,
486 0,
487 0);
490 0,
491 0,
492 0,
493 0,
494 0);
499 0);
502 0,
503 0);
506 0,
507 0,
508 0);
511 0,
512 0,
513 0,
514 0);
517 0,
518 0,
519 0,
520 0,
521 0);
522
523 // Test schedule of raw functions
526 Simulator::Schedule(Seconds(0.0), &foo2, 0, 0);
527 Simulator::Schedule(Seconds(0.0), &foo3, 0, 0, 0);
528 Simulator::Schedule(Seconds(0.0), &foo4, 0, 0, 0, 0);
529 Simulator::Schedule(Seconds(0.0), &foo5, 0, 0, 0, 0, 0);
531 Simulator::Schedule(Seconds(0.0), &cber2, 0, 0);
532 Simulator::Schedule(Seconds(0.0), &cber3, 0, 0, 0);
533 Simulator::Schedule(Seconds(0.0), &cber4, 0, 0, 0, 0);
534 Simulator::Schedule(Seconds(0.0), &cber5, 0, 0, 0, 0, 0);
538 Simulator::ScheduleNow(&foo3, 0, 0, 0);
539 Simulator::ScheduleNow(&foo4, 0, 0, 0, 0);
540 Simulator::ScheduleNow(&foo5, 0, 0, 0, 0, 0);
543 Simulator::ScheduleNow(&cber3, 0, 0, 0);
544 Simulator::ScheduleNow(&cber4, 0, 0, 0, 0);
545 Simulator::ScheduleNow(&cber5, 0, 0, 0, 0, 0);
550 Simulator::ScheduleDestroy(&foo4, 0, 0, 0, 0);
551 Simulator::ScheduleDestroy(&foo5, 0, 0, 0, 0, 0);
555 Simulator::ScheduleDestroy(&cber4, 0, 0, 0, 0);
556 Simulator::ScheduleDestroy(&cber5, 0, 0, 0, 0, 0);
557
558 // Test schedule of normal member methods
592
593 // test schedule of normal methods with Ptr<> pointers
600 0);
604 0,
605 0);
609 0,
610 0,
611 0);
615 0,
616 0,
617 0,
618 0);
622 0,
623 0,
624 0,
625 0,
626 0);
630 0);
633 0,
634 0);
637 0,
638 0,
639 0);
642 0,
643 0,
644 0,
645 0);
648 0,
649 0,
650 0,
651 0,
652 0);
657 0);
660 0,
661 0);
664 0,
665 0,
666 0);
669 0,
670 0,
671 0,
672 0);
675 0,
676 0,
677 0,
678 0,
679 0);
680
681 // the code below does not compile, as expected.
682 // Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
683
684 // This code appears to be duplicate test code.
686 Simulator::Schedule(Seconds(0.0), &ber2, 0, 0);
687 Simulator::Schedule(Seconds(0.0), &ber3, 0, 0, 0);
688 Simulator::Schedule(Seconds(0.0), &ber4, 0, 0, 0, 0);
689 Simulator::Schedule(Seconds(0.0), &ber5, 0, 0, 0, 0, 0);
697 Simulator::ScheduleNow(&ber3, 0, 0, 0);
698 Simulator::ScheduleNow(&ber4, 0, 0, 0, 0);
699 Simulator::ScheduleNow(&ber5, 0, 0, 0, 0, 0);
708 Simulator::ScheduleDestroy(&ber4, 0, 0, 0, 0);
709 Simulator::ScheduleDestroy(&ber5, 0, 0, 0, 0, 0);
715
718}
719
726{
727 public:
729 : TestSuite("simulator")
730 {
731 ObjectFactory factory;
733
743 }
744};
745
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:78
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
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:276
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
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:625
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:162
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:266
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:417
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
#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:251
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1360
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
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.