A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
simulator-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/test.h"
21 #include "ns3/simulator.h"
22 #include "ns3/list-scheduler.h"
23 #include "ns3/heap-scheduler.h"
24 #include "ns3/map-scheduler.h"
25 #include "ns3/calendar-scheduler.h"
26 
27 namespace ns3 {
28 
30 {
31 public:
32  SimulatorEventsTestCase (ObjectFactory schedulerFactory);
33  virtual void DoRun (void);
34  void A (int a);
35  void B (int b);
36  void C (int c);
37  void D (int d);
38  void foo0 (void);
39  uint64_t NowUs (void);
40  void destroy (void);
41  bool m_b;
42  bool m_a;
43  bool m_c;
44  bool m_d;
46  bool m_destroy;
49 };
50 
52  : TestCase ("Check that basic event handling is working with " +
53  schedulerFactory.GetTypeId ().GetName ()),
54  m_schedulerFactory (schedulerFactory)
55 {
56 }
57 uint64_t
59 {
60  uint64_t ns = Now ().GetNanoSeconds ();
61  return ns / 1000;
62 }
63 
64 void
66 {
67  m_a = false;
68 }
69 
70 void
72 {
73  if (b != 2 || NowUs () != 11)
74  {
75  m_b = false;
76  }
77  else
78  {
79  m_b = true;
80  }
83 }
84 
85 void
87 {
88  m_c = false;
89 }
90 
91 void
93 {
94  if (d != 4 || NowUs () != (11+10))
95  {
96  m_d = false;
97  }
98  else
99  {
100  m_d = true;
101  }
102 }
103 
104 void
106 {}
107 
108 void
110 {
111  if (m_destroyId.IsExpired ())
112  {
113  m_destroy = true;
114  }
115 }
116 void
118 {
119  m_a = true;
120  m_b = false;
121  m_c = true;
122  m_d = false;
123 
125 
129 
130  NS_TEST_EXPECT_MSG_EQ (!m_idC.IsExpired (), true, "");
131  NS_TEST_EXPECT_MSG_EQ (!a.IsExpired (), true, "");
132  Simulator::Cancel (a);
133  NS_TEST_EXPECT_MSG_EQ (a.IsExpired (), true, "");
134  Simulator::Run ();
135  NS_TEST_EXPECT_MSG_EQ (m_a, true, "Event A did not run ?");
136  NS_TEST_EXPECT_MSG_EQ (m_b, true, "Event B did not run ?");
137  NS_TEST_EXPECT_MSG_EQ (m_c, true, "Event C did not run ?");
138  NS_TEST_EXPECT_MSG_EQ (m_d, true, "Event D did not run ?");
139 
141  EventId anotherId = anId;
142  NS_TEST_EXPECT_MSG_EQ (!(anId.IsExpired () || anotherId.IsExpired ()), true, "Event should not have expired yet.");
143 
144  Simulator::Remove (anId);
145  NS_TEST_EXPECT_MSG_EQ (anId.IsExpired (), true, "Event was removed: it is now expired");
146  NS_TEST_EXPECT_MSG_EQ (anotherId.IsExpired (), true, "Event was removed: it is now expired");
147 
148  m_destroy = false;
150  NS_TEST_EXPECT_MSG_EQ (!m_destroyId.IsExpired (), true, "Event should not have expired yet");
151  m_destroyId.Cancel ();
152  NS_TEST_EXPECT_MSG_EQ (m_destroyId.IsExpired (), true, "Event was canceled: should have expired now");
153 
155  NS_TEST_EXPECT_MSG_EQ (!m_destroyId.IsExpired (), true, "Event should not have expired yet");
156  Simulator::Remove (m_destroyId);
157  NS_TEST_EXPECT_MSG_EQ (m_destroyId.IsExpired (), true, "Event was canceled: should have expired now");
158 
160  NS_TEST_EXPECT_MSG_EQ (!m_destroyId.IsExpired (), true, "Event should not have expired yet");
161 
162  Simulator::Run ();
163  NS_TEST_EXPECT_MSG_EQ (!m_destroyId.IsExpired (), true, "Event should not have expired yet");
164  NS_TEST_EXPECT_MSG_EQ (!m_destroy, true, "Event should not have run");
165 
167  NS_TEST_EXPECT_MSG_EQ (m_destroyId.IsExpired (), true, "Event should have expired now");
168  NS_TEST_EXPECT_MSG_EQ (m_destroy, true, "Event should have run");
169 }
170 
172 {
173 public:
175  // only here for testing of Ptr<>
176  void Ref (void) const {}
177  void Unref (void) const {}
178 private:
179  virtual void DoRun (void);
180 
181  void bar0 (void) {}
182  void bar1 (int) {}
183  void bar2 (int, int) {}
184  void bar3 (int, int, int) {}
185  void bar4 (int, int, int, int) {}
186  void bar5 (int, int, int, int, int) {}
187  void baz1 (int &) {}
188  void baz2 (int &, int &) {}
189  void baz3 (int &, int &, int &) {}
190  void baz4 (int &, int &, int &, int &) {}
191  void baz5 (int &, int &, int &, int &, int &) {}
192  void cbaz1 (const int &) {}
193  void cbaz2 (const int &, const int &) {}
194  void cbaz3 (const int &, const int &, const int &) {}
195  void cbaz4 (const int &, const int &, const int &, const int &) {}
196  void cbaz5 (const int &, const int &, const int &, const int &, const int &) {}
197 
198  void bar0c (void) const {}
199  void bar1c (int) const {}
200  void bar2c (int, int) const {}
201  void bar3c (int, int, int) const {}
202  void bar4c (int, int, int, int) const {}
203  void bar5c (int, int, int, int, int) const {}
204  void baz1c (int &) const {}
205  void baz2c (int &, int &) const {}
206  void baz3c (int &, int &, int &) const {}
207  void baz4c (int &, int &, int &, int &) const {}
208  void baz5c (int &, int &, int &, int &, int &) const {}
209  void cbaz1c (const int &) const {}
210  void cbaz2c (const int &, const int &) const {}
211  void cbaz3c (const int &, const int &, const int &) const {}
212  void cbaz4c (const int &, const int &, const int &, const int &) const {}
213  void cbaz5c (const int &, const int &, const int &, const int &, const int &) const {}
214 
215 };
216 
217 static void foo0 (void)
218 {}
219 static void foo1 (int)
220 {}
221 static void foo2 (int, int)
222 {}
223 static void foo3 (int, int, int)
224 {}
225 static void foo4 (int, int, int, int)
226 {}
227 static void foo5 (int, int, int, int, int)
228 {}
229 static void ber1 (int &)
230 {}
231 static void ber2 (int &, int &)
232 {}
233 static void ber3 (int &, int &, int &)
234 {}
235 static void ber4 (int &, int &, int &, int &)
236 {}
237 static void ber5 (int &, int &, int &, int &, int &)
238 {}
239 static void cber1 (const int &)
240 {}
241 static void cber2 (const int &, const int &)
242 {}
243 static void cber3 (const int &, const int &, const int &)
244 {}
245 static void cber4 (const int &, const int &, const int &, const int &)
246 {}
247 static void cber5 (const int &, const int &, const int &, const int &, const int &)
248 {}
249 
251  : TestCase ("Check that all templates are instanciated correctly. This is a compilation test, it cannot fail at runtime.")
252 {
253 }
254 void
256 {
257  // Test schedule of const methods
263  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::bar5c, this, 0, 0, 0, 0, 0);
268  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::cbaz5c, this, 0, 0, 0, 0, 0);
295  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::baz5c, this, 0, 0, 0, 0, 0);
306 
307  // Test of schedule const methods with Ptr<> pointers
326 
327 
328  // Test schedule of raw functions
329  Simulator::Schedule (Seconds (0.0), &foo0);
330  Simulator::Schedule (Seconds (0.0), &foo1, 0);
331  Simulator::Schedule (Seconds (0.0), &foo2, 0, 0);
332  Simulator::Schedule (Seconds (0.0), &foo3, 0, 0, 0);
333  Simulator::Schedule (Seconds (0.0), &foo4, 0, 0, 0, 0);
334  Simulator::Schedule (Seconds (0.0), &foo5, 0, 0, 0, 0, 0);
335  Simulator::Schedule (Seconds (0.0), &cber1, 0);
336  Simulator::Schedule (Seconds (0.0), &cber2, 0, 0);
337  Simulator::Schedule (Seconds (0.0), &cber3, 0, 0, 0);
338  Simulator::Schedule (Seconds (0.0), &cber4, 0, 0, 0, 0);
339  Simulator::Schedule (Seconds (0.0), &cber5, 0, 0, 0, 0, 0);
342  Simulator::ScheduleNow (&foo2, 0, 0);
343  Simulator::ScheduleNow (&foo3, 0, 0, 0);
344  Simulator::ScheduleNow (&foo4, 0, 0, 0, 0);
345  Simulator::ScheduleNow (&foo5, 0, 0, 0, 0, 0);
347  Simulator::ScheduleNow (&cber2, 0, 0);
348  Simulator::ScheduleNow (&cber3, 0, 0, 0);
349  Simulator::ScheduleNow (&cber4, 0, 0, 0, 0);
350  Simulator::ScheduleNow (&cber5, 0, 0, 0, 0, 0);
354  Simulator::ScheduleDestroy (&foo3, 0, 0, 0);
355  Simulator::ScheduleDestroy (&foo4, 0, 0, 0, 0);
356  Simulator::ScheduleDestroy (&foo5, 0, 0, 0, 0, 0);
359  Simulator::ScheduleDestroy (&cber3, 0, 0, 0);
360  Simulator::ScheduleDestroy (&cber4, 0, 0, 0, 0);
361  Simulator::ScheduleDestroy (&cber5, 0, 0, 0, 0, 0);
362 
363 
364  // Test schedule of normal member methods
369  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::bar4, this, 0, 0, 0, 0);
370  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::bar5, this, 0, 0, 0, 0, 0);
375  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::cbaz5, this, 0, 0, 0, 0, 0);
398 
399 
400  // test schedule of normal methods with Ptr<> pointers
419 
420 
421  // the code below does not compile, as expected.
422  //Simulator::Schedule (Seconds (0.0), &cber1, 0.0);
423 
424 
425  // This code appears to be duplicate test code.
426  Simulator::Schedule (Seconds (0.0), &ber1, 0);
427  Simulator::Schedule (Seconds (0.0), &ber2, 0, 0);
428  Simulator::Schedule (Seconds (0.0), &ber3, 0, 0, 0);
429  Simulator::Schedule (Seconds (0.0), &ber4, 0, 0, 0, 0);
430  Simulator::Schedule (Seconds (0.0), &ber5, 0, 0, 0, 0, 0);
434  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::baz4, this, 0, 0, 0, 0);
435  Simulator::Schedule (Seconds (0.0), &SimulatorTemplateTestCase::baz5, this, 0, 0, 0, 0, 0);
437  Simulator::ScheduleNow (&ber2, 0, 0);
438  Simulator::ScheduleNow (&ber3, 0, 0, 0);
439  Simulator::ScheduleNow (&ber4, 0, 0, 0, 0);
440  Simulator::ScheduleNow (&ber5, 0, 0, 0, 0, 0);
448  Simulator::ScheduleDestroy (&ber3, 0, 0, 0);
449  Simulator::ScheduleDestroy (&ber4, 0, 0, 0, 0);
450  Simulator::ScheduleDestroy (&ber5, 0, 0, 0, 0, 0);
456 
457 
458  Simulator::Run ();
460 }
461 
463 {
464 public:
466  : TestSuite ("simulator")
467  {
468  ObjectFactory factory;
469  factory.SetTypeId (ListScheduler::GetTypeId ());
470 
471  AddTestCase (new SimulatorEventsTestCase (factory));
472  factory.SetTypeId (MapScheduler::GetTypeId ());
473  AddTestCase (new SimulatorEventsTestCase (factory));
474  factory.SetTypeId (HeapScheduler::GetTypeId ());
475  AddTestCase (new SimulatorEventsTestCase (factory));
477  AddTestCase (new SimulatorEventsTestCase (factory));
478  }
480 
481 } // namespace ns3