A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
callback-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) 2009 University of Washington
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 
19 #include "ns3/test.h"
20 #include "ns3/callback.h"
21 #include <stdint.h>
22 
23 using namespace ns3;
24 
25 // ===========================================================================
26 // Test the basic Callback mechanism
27 // ===========================================================================
29 {
30 public:
32  virtual ~BasicCallbackTestCase () {}
33 
34  void Target1 (void) { m_test1 = true; }
35  int Target2 (void) { m_test2 = true; return 2; }
36  void Target3 (double a) { m_test3 = true; }
37  int Target4 (double a, int b) { m_test4 = true; return 4; }
38 
39 private:
40  virtual void DoRun (void);
41  virtual void DoSetup (void);
42 
43  bool m_test1;
44  bool m_test2;
45  bool m_test3;
46  bool m_test4;
47 };
48 
49 static bool gBasicCallbackTest5;
50 static bool gBasicCallbackTest6;
51 static bool gBasicCallbackTest7;
52 
53 void
55 {
56  gBasicCallbackTest5 = true;
57 }
58 
59 void
61 {
62  gBasicCallbackTest6 = true;
63 }
64 
65 int
67 {
68  gBasicCallbackTest7 = true;
69  return a;
70 }
71 
73  : TestCase ("Check basic Callback mechansim")
74 {
75 }
76 
77 void
79 {
80  m_test1 = false;
81  m_test2 = false;
82  m_test3 = false;
83  m_test4 = false;
84  gBasicCallbackTest5 = false;
85  gBasicCallbackTest6 = false;
86  gBasicCallbackTest7 = false;
87 }
88 
89 void
91 {
92  //
93  // Make sure we can declare and compile a Callback pointing to a member
94  // function returning void and execute it.
95  //
97  target1 ();
98  NS_TEST_ASSERT_MSG_EQ (m_test1, true, "Callback did not fire");
99 
100  //
101  // Make sure we can declare and compile a Callback pointing to a member
102  // function that returns an int and execute it.
103  //
104  Callback<int> target2;
106  target2 ();
107  NS_TEST_ASSERT_MSG_EQ (m_test2, true, "Callback did not fire");
108 
109  //
110  // Make sure we can declare and compile a Callback pointing to a member
111  // function that returns void, takes a double parameter, and execute it.
112  //
114  target3 (0.0);
115  NS_TEST_ASSERT_MSG_EQ (m_test3, true, "Callback did not fire");
116 
117  //
118  // Make sure we can declare and compile a Callback pointing to a member
119  // function that returns void, takes two parameters, and execute it.
120  //
122  target4 (0.0, 1);
123  NS_TEST_ASSERT_MSG_EQ (m_test4, true, "Callback did not fire");
124 
125  //
126  // Make sure we can declare and compile a Callback pointing to a non-member
127  // function that returns void, and execute it. This is a lower level call
128  // than MakeCallback so we have got to include at least two arguments to make
129  // sure that the constructor is properly disambiguated. If the arguments are
130  // not needed, we just pass in dummy values.
131  //
132  Callback<void> target5 = Callback<void> (&BasicCallbackTarget5, true, true);
133  target5 ();
134  NS_TEST_ASSERT_MSG_EQ (gBasicCallbackTest5, true, "Callback did not fire");
135 
136  //
137  // Make sure we can declare and compile a Callback pointing to a non-member
138  // function that returns void, takes one integer argument and execute it.
139  // We also need to provide two dummy arguments to the constructor here.
140  //
142  target6 (1);
143  NS_TEST_ASSERT_MSG_EQ (gBasicCallbackTest6, true, "Callback did not fire");
144 
145  //
146  // Make sure we can declare and compile a Callback pointing to a non-member
147  // function that returns int, takes one integer argument and execute it.
148  // We also need to provide two dummy arguments to the constructor here.
149  //
151  target7 (1);
152  NS_TEST_ASSERT_MSG_EQ (gBasicCallbackTest7, true, "Callback did not fire");
153 }
154 
155 // ===========================================================================
156 // Test the MakeCallback mechanism
157 // ===========================================================================
159 {
160 public:
162  virtual ~MakeCallbackTestCase () {}
163 
164  void Target1 (void) { m_test1 = true; }
165  int Target2 (void) { m_test2 = true; return 2; }
166  void Target3 (double a) { m_test3 = true; }
167  int Target4 (double a, int b) { m_test4 = true; return 4; }
168 
169 private:
170  virtual void DoRun (void);
171  virtual void DoSetup (void);
172 
173  bool m_test1;
174  bool m_test2;
175  bool m_test3;
176  bool m_test4;
177 };
178 
179 static bool gMakeCallbackTest5;
180 static bool gMakeCallbackTest6;
181 static bool gMakeCallbackTest7;
182 
183 void
185 {
186  gMakeCallbackTest5 = true;
187 }
188 
189 void
191 {
192  gMakeCallbackTest6 = true;
193 }
194 
195 int
197 {
198  gMakeCallbackTest7 = true;
199  return a;
200 }
201 
203  : TestCase ("Check MakeCallback() mechanism")
204 {
205 }
206 
207 void
209 {
210  m_test1 = false;
211  m_test2 = false;
212  m_test3 = false;
213  m_test4 = false;
214  gMakeCallbackTest5 = false;
215  gMakeCallbackTest6 = false;
216  gMakeCallbackTest7 = false;
217 }
218 
219 void
221 {
222  //
223  // Make sure we can declare and make a Callback pointing to a member
224  // function returning void and execute it.
225  //
227  target1 ();
228  NS_TEST_ASSERT_MSG_EQ (m_test1, true, "Callback did not fire");
229 
230  //
231  // Make sure we can declare and make a Callback pointing to a member
232  // function that returns an int and execute it.
233  //
235  target2 ();
236  NS_TEST_ASSERT_MSG_EQ (m_test2, true, "Callback did not fire");
237 
238  //
239  // Make sure we can declare and make a Callback pointing to a member
240  // function that returns void, takes a double parameter, and execute it.
241  //
243  target3 (0.0);
244  NS_TEST_ASSERT_MSG_EQ (m_test3, true, "Callback did not fire");
245 
246  //
247  // Make sure we can declare and make a Callback pointing to a member
248  // function that returns void, takes two parameters, and execute it.
249  //
251  target4 (0.0, 1);
252  NS_TEST_ASSERT_MSG_EQ (m_test4, true, "Callback did not fire");
253 
254  //
255  // Make sure we can declare and make a Callback pointing to a non-member
256  // function that returns void, and execute it. This uses a higher level call
257  // than in the basic tests so we do not need to include any dummy arguments
258  // here.
259  //
261  target5 ();
262  NS_TEST_ASSERT_MSG_EQ (gMakeCallbackTest5, true, "Callback did not fire");
263 
264  //
265  // Make sure we can declare and compile a Callback pointing to a non-member
266  // function that returns void, takes one integer argument and execute it.
267  // This uses a higher level call than in the basic tests so we do not need to
268  // include any dummy arguments here.
269  //
271  target6 (1);
272  NS_TEST_ASSERT_MSG_EQ (gMakeCallbackTest6, true, "Callback did not fire");
273 
274  //
275  // Make sure we can declare and compile a Callback pointing to a non-member
276  // function that returns int, takes one integer argument and execute it.
277  // This uses a higher level call than in the basic tests so we do not need to
278  // include any dummy arguments here.
279  //
281  target7 (1);
282  NS_TEST_ASSERT_MSG_EQ (gMakeCallbackTest7, true, "Callback did not fire");
283 }
284 
285 // ===========================================================================
286 // Test the MakeBoundCallback mechanism
287 // ===========================================================================
289 {
290 public:
293 
294 private:
295  virtual void DoRun (void);
296  virtual void DoSetup (void);
297 };
298 
321 
322 void
324 {
326 }
327 
328 void
330 {
332 }
333 
334 int
335 MakeBoundCallbackTarget3 (bool *a, int b)
336 {
339  return 1234;
340 }
341 
342 void
344 {
347 }
348 
349 int
351 {
354  return 1234;
355 }
356 
357 int
358 MakeBoundCallbackTarget6 (int a, int b, int c)
359 {
363  return 1234;
364 }
365 
366 void
367 MakeBoundCallbackTarget7 (int a, int b, int c)
368 {
372 }
373 
374 int
375 MakeBoundCallbackTarget8 (int a, int b, int c)
376 {
380  return 1234;
381 }
382 
383 int
384 MakeBoundCallbackTarget9 (int a, int b, int c, int d)
385 {
390  return 1234;
391 }
392 
394  : TestCase ("Check MakeBoundCallback() mechanism")
395 {
396 }
397 
398 void
400 {
423 }
424 
425 void
427 {
428  //
429  // This is slightly tricky to explain. A bound Callback allows us to package
430  // up arguments for use later. The arguments are bound when the callback is
431  // created and the code that fires the Callback does not know they are there.
432  //
433  // Since the callback is *declared* according to the way it will be used, the
434  // arguments are not seen there. However, the target function of the callback
435  // will have the provided arguments present. The MakeBoundCallback template
436  // function is what connects the two together and where you provide the
437  // arguments to be bound.
438  //
439  // Here we declare a Callback that returns a void and takes no parameters.
440  // MakeBoundCallback connects this Callback to a target function that returns
441  // void and takes an integer argument. That integer argument is bound to the
442  // value 1234. When the Callback is fired, no integer argument is provided
443  // directly. The argument is provided by bound Callback mechanism.
444  //
446  target1 ();
447  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest1, 1234, "Callback did not fire or binding not correct");
448 
449  //
450  // Make sure we can bind a pointer value (a common use case).
451  //
452  bool a;
454  target2 ();
455  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest2, &a, "Callback did not fire or binding not correct");
456 
457  //
458  // Make sure we can mix and match bound and unbound arguments. This callback
459  // returns an integer so we should see that appear.
460  //
462  int result = target3 (2468);
463  NS_TEST_ASSERT_MSG_EQ (result, 1234, "Return value of callback not correct");
464  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest3a, &a, "Callback did not fire or binding not correct");
465  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest3b, 2468, "Callback did not fire or argument not correct");
466 
467  //
468  // Test the TwoBound variant
469  //
471  target4 ();
472  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest4a, 3456, "Callback did not fire or binding not correct");
473  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest4b, 5678, "Callback did not fire or binding not correct");
474 
476  int resultTwoA = target5 ();
477  NS_TEST_ASSERT_MSG_EQ (resultTwoA, 1234, "Return value of callback not correct");
478  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest5a, 3456, "Callback did not fire or binding not correct");
479  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest5b, 5678, "Callback did not fire or binding not correct");
480 
482  int resultTwoB = target6 (6789);
483  NS_TEST_ASSERT_MSG_EQ (resultTwoB, 1234, "Return value of callback not correct");
484  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest6a, 3456, "Callback did not fire or binding not correct");
485  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest6b, 5678, "Callback did not fire or binding not correct");
486  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest6c, 6789, "Callback did not fire or argument not correct");
487 
488  //
489  // Test the ThreeBound variant
490  //
491  Callback<void> target7 = MakeBoundCallback (&MakeBoundCallbackTarget7, 2345, 3456, 4567);
492  target7 ();
493  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest7a, 2345, "Callback did not fire or binding not correct");
494  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest7b, 3456, "Callback did not fire or binding not correct");
495  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest7c, 4567, "Callback did not fire or binding not correct");
496 
497  Callback<int> target8 = MakeBoundCallback (&MakeBoundCallbackTarget8, 2345, 3456, 4567);
498  int resultThreeA = target8 ();
499  NS_TEST_ASSERT_MSG_EQ (resultThreeA, 1234, "Return value of callback not correct");
500  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest8a, 2345, "Callback did not fire or binding not correct");
501  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest8b, 3456, "Callback did not fire or binding not correct");
502  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest8c, 4567, "Callback did not fire or binding not correct");
503 
504  Callback<int, int> target9 = MakeBoundCallback (&MakeBoundCallbackTarget9, 2345, 3456, 4567);
505  int resultThreeB = target9 (5678);
506  NS_TEST_ASSERT_MSG_EQ (resultThreeB, 1234, "Return value of callback not correct");
507  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9a, 2345, "Callback did not fire or binding not correct");
508  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9b, 3456, "Callback did not fire or binding not correct");
509  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9c, 4567, "Callback did not fire or binding not correct");
510  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9d, 5678, "Callback did not fire or binding not correct");
511 }
512 
513 // ===========================================================================
514 // Test the Nullify mechanism
515 // ===========================================================================
517 {
518 public:
521 
522  void Target1 (void) { m_test1 = true; }
523 
524 private:
525  virtual void DoRun (void);
526  virtual void DoSetup (void);
527 
528  bool m_test1;
529 };
530 
532  : TestCase ("Check Nullify() and IsNull()")
533 {
534 }
535 
536 void
538 {
539  m_test1 = false;
540 }
541 
542 void
544 {
545  //
546  // Make sure we can declare and make a Callback pointing to a member
547  // function returning void and execute it.
548  //
550  target1 ();
551  NS_TEST_ASSERT_MSG_EQ (m_test1, true, "Callback did not fire");
552 
553  NS_TEST_ASSERT_MSG_EQ (target1.IsNull (), false, "Working Callback reports IsNull()");
554 
555  target1.Nullify ();
556 
557  NS_TEST_ASSERT_MSG_EQ (target1.IsNull (), true, "Nullified Callback reports not IsNull()");
558 }
559 
560 // ===========================================================================
561 // Make sure that various MakeCallback template functions compile and execute.
562 // Doesn't check an results of the execution.
563 // ===========================================================================
565 {
566 public:
569 
570  void Target1 (void) { m_test1 = true; }
571 
572 private:
573  virtual void DoRun (void);
574 
575  bool m_test1;
576 };
577 
578 void TestFZero (void) {}
579 void TestFOne (int) {}
580 void TestFTwo (int, int) {}
581 void TestFThree (int, int, int) {}
582 void TestFFour (int, int, int, int) {}
583 void TestFFive (int, int, int, int, int) {}
584 void TestFSix (int, int, int, int, int, int) {}
585 
586 void TestFROne (int &) {}
587 void TestFRTwo (int &, int &) {}
588 void TestFRThree (int &, int &, int &) {}
589 void TestFRFour (int &, int &, int &, int &) {}
590 void TestFRFive (int &, int &, int &, int &, int &) {}
591 void TestFRSix (int &, int &, int &, int &, int &, int &) {}
592 
594 {
595 public:
596  void PublicParent (void) {}
597 protected:
598  void ProtectedParent (void) {}
599  static void StaticProtectedParent (void) {}
600 private:
601  void PrivateParent (void) {}
602 };
603 
605 {
606 public:
607  void TestZero (void) {}
608  void TestOne (int) {}
609  void TestTwo (int, int) {}
610  void TestThree (int, int, int) {}
611  void TestFour (int, int, int, int) {}
612  void TestFive (int, int, int, int, int) {}
613  void TestSix (int, int, int, int, int, int) {}
614  void TestCZero (void) const {}
615  void TestCOne (int) const {}
616  void TestCTwo (int, int) const {}
617  void TestCThree (int, int, int) const {}
618  void TestCFour (int, int, int, int) const {}
619  void TestCFive (int, int, int, int, int) const {}
620  void TestCSix (int, int, int, int, int, int) const {}
621 
623  {
627  // as expected, fails.
628  // MakeCallback (&CallbackTestParent::PrivateParent, this);
629  // unexpected, but fails too. It does fumble me.
630  // MakeCallback (&CallbackTestParent::ProtectedParent, this);
631  }
632 
633 };
634 
636  : TestCase ("Check various MakeCallback() template functions")
637 {
638 }
639 
640 void
642 {
643  CallbackTestClass that;
644 
652 
660 
668 
675 
681 
687 
688  that.CheckParentalRights ();
689 }
690 
691 // ===========================================================================
692 // The Test Suite that glues all of the Test Cases together.
693 // ===========================================================================
695 {
696 public:
698 };
699 
701  : TestSuite ("callback", UNIT)
702 {
703  AddTestCase (new BasicCallbackTestCase, TestCase::QUICK);
704  AddTestCase (new MakeCallbackTestCase, TestCase::QUICK);
705  AddTestCase (new MakeBoundCallbackTestCase, TestCase::QUICK);
706  AddTestCase (new NullifyCallbackTestCase, TestCase::QUICK);
707  AddTestCase (new MakeCallbackTemplatesTestCase, TestCase::QUICK);
708 }
709