A Discrete-Event Network Simulator
API
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 "ns3/unused.h"
22 #include <stdint.h>
23 
24 using namespace ns3;
25 
26 // ===========================================================================
27 // Test the basic Callback mechanism
28 // ===========================================================================
30 {
31 public:
34  {}
35 
36  void Target1 (void)
37  {
38  m_test1 = true;
39  }
40  int Target2 (void)
41  {
42  m_test2 = true;
43  return 2;
44  }
45  void Target3 (double a)
46  {
47  NS_UNUSED (a);
48  m_test3 = true;
49  }
50  int Target4 (double a, int b)
51  {
52  NS_UNUSED (a);
53  NS_UNUSED (b);
54  m_test4 = true;
55  return 4;
56  }
57 
58 private:
59  virtual void DoRun (void);
60  virtual void DoSetup (void);
61 
62  bool m_test1;
63  bool m_test2;
64  bool m_test3;
65  bool m_test4;
66 };
67 
68 static bool gBasicCallbackTest5;
69 static bool gBasicCallbackTest6;
70 static bool gBasicCallbackTest7;
71 
72 void
74 {
75  gBasicCallbackTest5 = true;
76 }
77 
78 void
80 {
81  gBasicCallbackTest6 = true;
82 }
83 
84 int
86 {
87  gBasicCallbackTest7 = true;
88  return a;
89 }
90 
92  : TestCase ("Check basic Callback mechansim")
93 {}
94 
95 void
97 {
98  m_test1 = false;
99  m_test2 = false;
100  m_test3 = false;
101  m_test4 = false;
102  gBasicCallbackTest5 = false;
103  gBasicCallbackTest6 = false;
104  gBasicCallbackTest7 = false;
105 }
106 
107 void
109 {
110  //
111  // Make sure we can declare and compile a Callback pointing to a member
112  // function returning void and execute it.
113  //
115  target1 ();
116  NS_TEST_ASSERT_MSG_EQ (m_test1, true, "Callback did not fire");
117 
118  //
119  // Make sure we can declare and compile a Callback pointing to a member
120  // function that returns an int and execute it.
121  //
122  Callback<int> target2;
124  target2 ();
125  NS_TEST_ASSERT_MSG_EQ (m_test2, true, "Callback did not fire");
126 
127  //
128  // Make sure we can declare and compile a Callback pointing to a member
129  // function that returns void, takes a double parameter, and execute it.
130  //
132  target3 (0.0);
133  NS_TEST_ASSERT_MSG_EQ (m_test3, true, "Callback did not fire");
134 
135  //
136  // Make sure we can declare and compile a Callback pointing to a member
137  // function that returns void, takes two parameters, and execute it.
138  //
140  target4 (0.0, 1);
141  NS_TEST_ASSERT_MSG_EQ (m_test4, true, "Callback did not fire");
142 
143  //
144  // Make sure we can declare and compile a Callback pointing to a non-member
145  // function that returns void, and execute it. This is a lower level call
146  // than MakeCallback so we have got to include at least two arguments to make
147  // sure that the constructor is properly disambiguated. If the arguments are
148  // not needed, we just pass in dummy values.
149  //
150  Callback<void> target5 = Callback<void> (&BasicCallbackTarget5, true, true);
151  target5 ();
152  NS_TEST_ASSERT_MSG_EQ (gBasicCallbackTest5, true, "Callback did not fire");
153 
154  //
155  // Make sure we can declare and compile a Callback pointing to a non-member
156  // function that returns void, takes one integer argument and execute it.
157  // We also need to provide two dummy arguments to the constructor here.
158  //
160  target6 (1);
161  NS_TEST_ASSERT_MSG_EQ (gBasicCallbackTest6, true, "Callback did not fire");
162 
163  //
164  // Make sure we can declare and compile a Callback pointing to a non-member
165  // function that returns int, takes one integer argument and execute it.
166  // We also need to provide two dummy arguments to the constructor here.
167  //
169  target7 (1);
170  NS_TEST_ASSERT_MSG_EQ (gBasicCallbackTest7, true, "Callback did not fire");
171 }
172 
173 // ===========================================================================
174 // Test the MakeCallback mechanism
175 // ===========================================================================
177 {
178 public:
181  {}
182 
183  void Target1 (void)
184  {
185  m_test1 = true;
186  }
187  int Target2 (void)
188  {
189  m_test2 = true;
190  return 2;
191  }
192  void Target3 (double a)
193  {
194  NS_UNUSED (a);
195  m_test3 = true;
196  }
197  int Target4 (double a, int b)
198  {
199  NS_UNUSED (a);
200  NS_UNUSED (b);
201  m_test4 = true;
202  return 4;
203  }
204 
205 private:
206  virtual void DoRun (void);
207  virtual void DoSetup (void);
208 
209  bool m_test1;
210  bool m_test2;
211  bool m_test3;
212  bool m_test4;
213 };
214 
215 static bool gMakeCallbackTest5;
216 static bool gMakeCallbackTest6;
217 static bool gMakeCallbackTest7;
218 
219 void
221 {
222  gMakeCallbackTest5 = true;
223 }
224 
225 void
227 {
228  gMakeCallbackTest6 = true;
229 }
230 
231 int
233 {
234  gMakeCallbackTest7 = true;
235  return a;
236 }
237 
239  : TestCase ("Check MakeCallback() mechanism")
240 {}
241 
242 void
244 {
245  m_test1 = false;
246  m_test2 = false;
247  m_test3 = false;
248  m_test4 = false;
249  gMakeCallbackTest5 = false;
250  gMakeCallbackTest6 = false;
251  gMakeCallbackTest7 = false;
252 }
253 
254 void
256 {
257  //
258  // Make sure we can declare and make a Callback pointing to a member
259  // function returning void and execute it.
260  //
262  target1 ();
263  NS_TEST_ASSERT_MSG_EQ (m_test1, true, "Callback did not fire");
264 
265  //
266  // Make sure we can declare and make a Callback pointing to a member
267  // function that returns an int and execute it.
268  //
270  target2 ();
271  NS_TEST_ASSERT_MSG_EQ (m_test2, true, "Callback did not fire");
272 
273  //
274  // Make sure we can declare and make a Callback pointing to a member
275  // function that returns void, takes a double parameter, and execute it.
276  //
278  target3 (0.0);
279  NS_TEST_ASSERT_MSG_EQ (m_test3, true, "Callback did not fire");
280 
281  //
282  // Make sure we can declare and make a Callback pointing to a member
283  // function that returns void, takes two parameters, and execute it.
284  //
286  target4 (0.0, 1);
287  NS_TEST_ASSERT_MSG_EQ (m_test4, true, "Callback did not fire");
288 
289  //
290  // Make sure we can declare and make a Callback pointing to a non-member
291  // function that returns void, and execute it. This uses a higher level call
292  // than in the basic tests so we do not need to include any dummy arguments
293  // here.
294  //
296  target5 ();
297  NS_TEST_ASSERT_MSG_EQ (gMakeCallbackTest5, true, "Callback did not fire");
298 
299  //
300  // Make sure we can declare and compile a Callback pointing to a non-member
301  // function that returns void, takes one integer argument and execute it.
302  // This uses a higher level call than in the basic tests so we do not need to
303  // include any dummy arguments here.
304  //
306  target6 (1);
307  NS_TEST_ASSERT_MSG_EQ (gMakeCallbackTest6, true, "Callback did not fire");
308 
309  //
310  // Make sure we can declare and compile a Callback pointing to a non-member
311  // function that returns int, takes one integer argument and execute it.
312  // This uses a higher level call than in the basic tests so we do not need to
313  // include any dummy arguments here.
314  //
316  target7 (1);
317  NS_TEST_ASSERT_MSG_EQ (gMakeCallbackTest7, true, "Callback did not fire");
318 }
319 
320 // ===========================================================================
321 // Test the MakeBoundCallback mechanism
322 // ===========================================================================
324 {
325 public:
328  {}
329 
330 private:
331  virtual void DoRun (void);
332  virtual void DoSetup (void);
333 };
334 
357 
358 void
360 {
362 }
363 
364 void
366 {
368 }
369 
370 int
371 MakeBoundCallbackTarget3 (bool *a, int b)
372 {
375  return 1234;
376 }
377 
378 void
380 {
383 }
384 
385 int
387 {
390  return 1234;
391 }
392 
393 int
394 MakeBoundCallbackTarget6 (int a, int b, int c)
395 {
399  return 1234;
400 }
401 
402 void
403 MakeBoundCallbackTarget7 (int a, int b, int c)
404 {
408 }
409 
410 int
411 MakeBoundCallbackTarget8 (int a, int b, int c)
412 {
416  return 1234;
417 }
418 
419 int
420 MakeBoundCallbackTarget9 (int a, int b, int c, int d)
421 {
426  return 1234;
427 }
428 
430  : TestCase ("Check MakeBoundCallback() mechanism")
431 {}
432 
433 void
435 {
458 }
459 
460 void
462 {
463  //
464  // This is slightly tricky to explain. A bound Callback allows us to package
465  // up arguments for use later. The arguments are bound when the callback is
466  // created and the code that fires the Callback does not know they are there.
467  //
468  // Since the callback is *declared* according to the way it will be used, the
469  // arguments are not seen there. However, the target function of the callback
470  // will have the provided arguments present. The MakeBoundCallback template
471  // function is what connects the two together and where you provide the
472  // arguments to be bound.
473  //
474  // Here we declare a Callback that returns a void and takes no parameters.
475  // MakeBoundCallback connects this Callback to a target function that returns
476  // void and takes an integer argument. That integer argument is bound to the
477  // value 1234. When the Callback is fired, no integer argument is provided
478  // directly. The argument is provided by bound Callback mechanism.
479  //
481  target1 ();
482  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest1, 1234, "Callback did not fire or binding not correct");
483 
484  //
485  // Make sure we can bind a pointer value (a common use case).
486  //
487  bool a;
489  target2 ();
490  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest2, &a, "Callback did not fire or binding not correct");
491 
492  //
493  // Make sure we can mix and match bound and unbound arguments. This callback
494  // returns an integer so we should see that appear.
495  //
497  int result = target3 (2468);
498  NS_TEST_ASSERT_MSG_EQ (result, 1234, "Return value of callback not correct");
499  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest3a, &a, "Callback did not fire or binding not correct");
500  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest3b, 2468, "Callback did not fire or argument not correct");
501 
502  //
503  // Test the TwoBound variant
504  //
506  target4 ();
507  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest4a, 3456, "Callback did not fire or binding not correct");
508  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest4b, 5678, "Callback did not fire or binding not correct");
509 
511  int resultTwoA = target5 ();
512  NS_TEST_ASSERT_MSG_EQ (resultTwoA, 1234, "Return value of callback not correct");
513  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest5a, 3456, "Callback did not fire or binding not correct");
514  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest5b, 5678, "Callback did not fire or binding not correct");
515 
517  int resultTwoB = target6 (6789);
518  NS_TEST_ASSERT_MSG_EQ (resultTwoB, 1234, "Return value of callback not correct");
519  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest6a, 3456, "Callback did not fire or binding not correct");
520  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest6b, 5678, "Callback did not fire or binding not correct");
521  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest6c, 6789, "Callback did not fire or argument not correct");
522 
523  //
524  // Test the ThreeBound variant
525  //
526  Callback<void> target7 = MakeBoundCallback (&MakeBoundCallbackTarget7, 2345, 3456, 4567);
527  target7 ();
528  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest7a, 2345, "Callback did not fire or binding not correct");
529  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest7b, 3456, "Callback did not fire or binding not correct");
530  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest7c, 4567, "Callback did not fire or binding not correct");
531 
532  Callback<int> target8 = MakeBoundCallback (&MakeBoundCallbackTarget8, 2345, 3456, 4567);
533  int resultThreeA = target8 ();
534  NS_TEST_ASSERT_MSG_EQ (resultThreeA, 1234, "Return value of callback not correct");
535  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest8a, 2345, "Callback did not fire or binding not correct");
536  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest8b, 3456, "Callback did not fire or binding not correct");
537  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest8c, 4567, "Callback did not fire or binding not correct");
538 
539  Callback<int, int> target9 = MakeBoundCallback (&MakeBoundCallbackTarget9, 2345, 3456, 4567);
540  int resultThreeB = target9 (5678);
541  NS_TEST_ASSERT_MSG_EQ (resultThreeB, 1234, "Return value of callback not correct");
542  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9a, 2345, "Callback did not fire or binding not correct");
543  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9b, 3456, "Callback did not fire or binding not correct");
544  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9c, 4567, "Callback did not fire or binding not correct");
545  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest9d, 5678, "Callback did not fire or binding not correct");
546 }
547 
548 // ===========================================================================
549 // Test the Nullify mechanism
550 // ===========================================================================
552 {
553 public:
556  {}
557 
558  void Target1 (void)
559  {
560  m_test1 = true;
561  }
562 
563 private:
564  virtual void DoRun (void);
565  virtual void DoSetup (void);
566 
567  bool m_test1;
568 };
569 
571  : TestCase ("Check Nullify() and IsNull()")
572 {}
573 
574 void
576 {
577  m_test1 = false;
578 }
579 
580 void
582 {
583  //
584  // Make sure we can declare and make a Callback pointing to a member
585  // function returning void and execute it.
586  //
588  target1 ();
589  NS_TEST_ASSERT_MSG_EQ (m_test1, true, "Callback did not fire");
590 
591  NS_TEST_ASSERT_MSG_EQ (target1.IsNull (), false, "Working Callback reports IsNull()");
592 
593  target1.Nullify ();
594 
595  NS_TEST_ASSERT_MSG_EQ (target1.IsNull (), true, "Nullified Callback reports not IsNull()");
596 }
597 
598 // ===========================================================================
599 // Make sure that various MakeCallback template functions compile and execute.
600 // Doesn't check an results of the execution.
601 // ===========================================================================
603 {
604 public:
607  {}
608 
609  void Target1 (void)
610  {
611  m_test1 = true;
612  }
613 
614 private:
615  virtual void DoRun (void);
616 
617  bool m_test1;
618 };
619 
620 /* *NS_CHECK_STYLE_OFF* */
621 void TestFZero (void) {}
622 void TestFOne (int) {}
623 void TestFTwo (int, int) {}
624 void TestFThree (int, int, int) {}
625 void TestFFour (int, int, int, int) {}
626 void TestFFive (int, int, int, int, int) {}
627 void TestFSix (int, int, int, int, int, int) {}
628 
629 void TestFROne (int &) {}
630 void TestFRTwo (int &, int &) {}
631 void TestFRThree (int &, int &, int &) {}
632 void TestFRFour (int &, int &, int &, int &) {}
633 void TestFRFive (int &, int &, int &, int &, int &) {}
634 void TestFRSix (int &, int &, int &, int &, int &, int &) {}
635 /* *NS_CHECK_STYLE_ON* */
636 
638 {
639 public:
640  void PublicParent (void)
641  {}
642 
643 protected:
644  void ProtectedParent (void)
645  {}
646  static void StaticProtectedParent (void)
647  {}
648 
649 private:
650  void PrivateParent (void)
651  {}
652 };
653 
655 {
656 public:
657  /* *NS_CHECK_STYLE_OFF* */
658  void TestZero (void) {}
659  void TestOne (int) {}
660  void TestTwo (int, int) {}
661  void TestThree (int, int, int) {}
662  void TestFour (int, int, int, int) {}
663  void TestFive (int, int, int, int, int) {}
664  void TestSix (int, int, int, int, int, int) {}
665  void TestCZero (void) const {}
666  void TestCOne (int) const {}
667  void TestCTwo (int, int) const {}
668  void TestCThree (int, int, int) const {}
669  void TestCFour (int, int, int, int) const {}
670  void TestCFive (int, int, int, int, int) const {}
671  void TestCSix (int, int, int, int, int, int) const {}
672  /* *NS_CHECK_STYLE_ON* */
673 
675  {
679  // as expected, fails.
680  // MakeCallback (&CallbackTestParent::PrivateParent, this);
681  // unexpected, but fails too. It does fumble me.
682  // MakeCallback (&CallbackTestParent::ProtectedParent, this);
683  }
684 
685 };
686 
688  : TestCase ("Check various MakeCallback() template functions")
689 {}
690 
691 void
693 {
694  CallbackTestClass that;
695 
703 
711 
719 
726 
732 
738 
739  that.CheckParentalRights ();
740 }
741 
742 // ===========================================================================
743 // The Test Suite that glues all of the Test Cases together.
744 // ===========================================================================
746 {
747 public:
749 };
750 
752  : TestSuite ("callback", UNIT)
753 {
754  AddTestCase (new BasicCallbackTestCase, TestCase::QUICK);
755  AddTestCase (new MakeCallbackTestCase, TestCase::QUICK);
756  AddTestCase (new MakeBoundCallbackTestCase, TestCase::QUICK);
757  AddTestCase (new NullifyCallbackTestCase, TestCase::QUICK);
758  AddTestCase (new MakeCallbackTemplatesTestCase, TestCase::QUICK);
759 }
760 
void MakeBoundCallbackTarget2(bool *a)
static int gMakeBoundCallbackTest5a
static int gMakeBoundCallbackTest5b
void MakeBoundCallbackTarget4(int a, int b)
void TestCThree(int, int, int) const
void TestFThree(int, int, int)
void TestFRThree(int &, int &, int &)
static int gMakeBoundCallbackTest9c
void TestFROne(int &)
A suite of tests to run.
Definition: test.h:1343
static int gMakeBoundCallbackTest8a
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1703
void TestFRFive(int &, int &, int &, int &, int &)
#define NS_UNUSED(x)
Mark a local variable as unused.
Definition: unused.h:36
static int gMakeBoundCallbackTest8b
void TestFOne(int)
void TestCTwo(int, int) const
encapsulates test code
Definition: test.h:1153
static int gMakeBoundCallbackTest9b
int MakeBoundCallbackTarget8(int a, int b, int c)
virtual void DoRun(void)
Implementation to actually run this TestCase.
static bool * gMakeBoundCallbackTest2
void TestFFive(int, int, int, int, int)
virtual void DoRun(void)
Implementation to actually run this TestCase.
int MakeBoundCallbackTarget5(int a, int b)
static int gMakeBoundCallbackTest1
static int gMakeBoundCallbackTest6a
static bool gBasicCallbackTest5
static int gMakeBoundCallbackTest8c
int Target4(double a, int b)
void TestSix(int, int, int, int, int, int)
static int gMakeBoundCallbackTest5c
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
static int gMakeBoundCallbackTest9d
void MakeBoundCallbackTarget1(int a)
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:166
void TestFSix(int, int, int, int, int, int)
static int gMakeBoundCallbackTest4a
void TestFZero(void)
void MakeCallbackTarget6(int)
int MakeBoundCallbackTarget6(int a, int b, int c)
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
static CallbackTestSuite CallbackTestSuite
static bool gBasicCallbackTest6
void BasicCallbackTarget6(int)
void MakeBoundCallbackTarget7(int a, int b, int c)
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
static int gMakeBoundCallbackTest7a
static bool gBasicCallbackTest7
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int BasicCallbackTarget7(int a)
void TestCOne(int) const
void TestFive(int, int, int, int, int)
static int gMakeBoundCallbackTest7c
void TestFTwo(int, int)
static bool gMakeCallbackTest5
static int gMakeBoundCallbackTest9a
void TestCSix(int, int, int, int, int, int) const
void TestCZero(void) const
static void StaticProtectedParent(void)
int MakeCallbackTarget7(int a)
void TestFRSix(int &, int &, int &, int &, int &, int &)
void BasicCallbackTarget5(void)
int MakeBoundCallbackTarget9(int a, int b, int c, int d)
void TestFRFour(int &, int &, int &, int &)
void TestFour(int, int, int, int)
static int gMakeBoundCallbackTest7b
void TestFRTwo(int &, int &)
static int gMakeBoundCallbackTest6b
void TestFFour(int, int, int, int)
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1391
static bool gMakeCallbackTest6
static int gMakeBoundCallbackTest6c
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
static int gMakeBoundCallbackTest4b
virtual void DoRun(void)
Implementation to actually run this TestCase.
static bool gMakeCallbackTest7
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
void TestCFour(int, int, int, int) const
int Target4(double a, int b)
static int gMakeBoundCallbackTest3b
void MakeCallbackTarget5(void)
int MakeBoundCallbackTarget3(bool *a, int b)
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
void TestCFive(int, int, int, int, int) const
void TestThree(int, int, int)
static bool * gMakeBoundCallbackTest3a