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 
303 
304 void
306 {
308 }
309 
310 void
312 {
314 }
315 
316 int
317 MakeBoundCallbackTarget3 (bool *a, int b)
318 {
321  return 1234;
322 }
323 
325  : TestCase ("Check MakeBoundCallback() mechanism")
326 {
327 }
328 
329 void
331 {
336 }
337 
338 void
340 {
341  //
342  // This is slightly tricky to explain. A bound Callback allows us to package
343  // up arguments for use later. The arguments are bound when the callback is
344  // created and the code that fires the Callback does not know they are there.
345  //
346  // Since the callback is *declared* according to the way it will be used, the
347  // arguments are not seen there. However, the target function of the callback
348  // will have the provided arguments present. The MakeBoundCallback template
349  // function is what connects the two together and where you provide the
350  // arguments to be bound.
351  //
352  // Here we declare a Callback that returns a void and takes no parameters.
353  // MakeBoundCallback connects this Callback to a target function that returns
354  // void and takes an integer argument. That integer argument is bound to the
355  // value 1234. When the Callback is fired, no integer argument is provided
356  // directly. The argument is provided by bound Callback mechanism.
357  //
359  target1 ();
360  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest1, 1234, "Callback did not fire or binding not correct");
361 
362  //
363  // Make sure we can bind a pointer value (a common use case).
364  //
365  bool a;
367  target2 ();
368  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest2, &a, "Callback did not fire or binding not correct");
369 
370  //
371  // Make sure we can mix and match bound and unbound arguments. This callback
372  // returns an integer so we should see that appear.
373  //
375  int result = target3 (2468);
376  NS_TEST_ASSERT_MSG_EQ (result, 1234, "Return value of callback not correct");
377  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest3a, &a, "Callback did not fire or binding not correct");
378  NS_TEST_ASSERT_MSG_EQ (gMakeBoundCallbackTest3b, 2468, "Callback did not fire or argument not correct");
379 }
380 
381 // ===========================================================================
382 // Test the Nullify mechanism
383 // ===========================================================================
385 {
386 public:
389 
390  void Target1 (void) { m_test1 = true; }
391 
392 private:
393  virtual void DoRun (void);
394  virtual void DoSetup (void);
395 
396  bool m_test1;
397 };
398 
400  : TestCase ("Check Nullify() and IsNull()")
401 {
402 }
403 
404 void
406 {
407  m_test1 = false;
408 }
409 
410 void
412 {
413  //
414  // Make sure we can declare and make a Callback pointing to a member
415  // function returning void and execute it.
416  //
418  target1 ();
419  NS_TEST_ASSERT_MSG_EQ (m_test1, true, "Callback did not fire");
420 
421  NS_TEST_ASSERT_MSG_EQ (target1.IsNull (), false, "Working Callback reports IsNull()");
422 
423  target1.Nullify ();
424 
425  NS_TEST_ASSERT_MSG_EQ (target1.IsNull (), true, "Nullified Callback reports not IsNull()");
426 }
427 
428 // ===========================================================================
429 // Make sure that various MakeCallback template functions compile and execute.
430 // Doesn't check an results of the execution.
431 // ===========================================================================
433 {
434 public:
437 
438  void Target1 (void) { m_test1 = true; }
439 
440 private:
441  virtual void DoRun (void);
442 
443  bool m_test1;
444 };
445 
446 void TestFZero (void) {}
447 void TestFOne (int) {}
448 void TestFTwo (int, int) {}
449 void TestFThree (int, int, int) {}
450 void TestFFour (int, int, int, int) {}
451 void TestFFive (int, int, int, int, int) {}
452 void TestFSix (int, int, int, int, int, int) {}
453 
454 void TestFROne (int &) {}
455 void TestFRTwo (int &, int &) {}
456 void TestFRThree (int &, int &, int &) {}
457 void TestFRFour (int &, int &, int &, int &) {}
458 void TestFRFive (int &, int &, int &, int &, int &) {}
459 void TestFRSix (int &, int &, int &, int &, int &, int &) {}
460 
462 {
463 public:
464  void PublicParent (void) {}
465 protected:
466  void ProtectedParent (void) {}
467  static void StaticProtectedParent (void) {}
468 private:
469  void PrivateParent (void) {}
470 };
471 
473 {
474 public:
475  void TestZero (void) {}
476  void TestOne (int) {}
477  void TestTwo (int, int) {}
478  void TestThree (int, int, int) {}
479  void TestFour (int, int, int, int) {}
480  void TestFive (int, int, int, int, int) {}
481  void TestSix (int, int, int, int, int, int) {}
482  void TestCZero (void) const {}
483  void TestCOne (int) const {}
484  void TestCTwo (int, int) const {}
485  void TestCThree (int, int, int) const {}
486  void TestCFour (int, int, int, int) const {}
487  void TestCFive (int, int, int, int, int) const {}
488  void TestCSix (int, int, int, int, int, int) const {}
489 
491  {
495  // as expected, fails.
496  // MakeCallback (&CallbackTestParent::PrivateParent, this);
497  // unexpected, but fails too. It does fumble me.
498  // MakeCallback (&CallbackTestParent::ProtectedParent, this);
499  }
500 
501 };
502 
504  : TestCase ("Check various MakeCallback() template functions")
505 {
506 }
507 
508 void
510 {
511  CallbackTestClass that;
512 
520 
528 
536 
543 
549 
555 
556  that.CheckParentalRights ();
557 }
558 
559 // ===========================================================================
560 // The Test Suite that glues all of the Test Cases together.
561 // ===========================================================================
563 {
564 public:
566 };
567 
569  : TestSuite ("callback", UNIT)
570 {
576 }
577