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 // ===========================================================================
29 class BasicCallbackTestCase : public TestCase
30 {
31 public:
32   BasicCallbackTestCase ();
~BasicCallbackTestCase()33   virtual ~BasicCallbackTestCase ()
34   {}
35 
Target1(void)36   void Target1 (void)
37   {
38     m_test1 = true;
39   }
Target2(void)40   int Target2 (void)
41   {
42     m_test2 = true;
43     return 2;
44   }
Target3(double a)45   void Target3 (double a)
46   {
47     NS_UNUSED (a);
48     m_test3 = true;
49   }
Target4(double a,int b)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
BasicCallbackTarget5(void)73 BasicCallbackTarget5 (void)
74 {
75   gBasicCallbackTest5 = true;
76 }
77 
78 void
BasicCallbackTarget6(int)79 BasicCallbackTarget6 (int)
80 {
81   gBasicCallbackTest6 = true;
82 }
83 
84 int
BasicCallbackTarget7(int a)85 BasicCallbackTarget7 (int a)
86 {
87   gBasicCallbackTest7 = true;
88   return a;
89 }
90 
BasicCallbackTestCase()91 BasicCallbackTestCase::BasicCallbackTestCase ()
92   : TestCase ("Check basic Callback mechansim")
93 {}
94 
95 void
DoSetup(void)96 BasicCallbackTestCase::DoSetup (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
DoRun(void)108 BasicCallbackTestCase::DoRun (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   //
114   Callback<void> target1 (this, &BasicCallbackTestCase::Target1);
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;
123   target2 = Callback<int> (this, &BasicCallbackTestCase::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   //
131   Callback<void, double> target3 = Callback<void, double> (this, &BasicCallbackTestCase::Target3);
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   //
139   Callback<int, double, int> target4 = Callback<int, double, int> (this, &BasicCallbackTestCase::Target4);
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   //
159   Callback<void, int> target6 = Callback<void, int> (&BasicCallbackTarget6, true, true);
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   //
168   Callback<int, int> target7 = Callback<int, int> (&BasicCallbackTarget7, true, true);
169   target7 (1);
170   NS_TEST_ASSERT_MSG_EQ (gBasicCallbackTest7, true, "Callback did not fire");
171 }
172 
173 // ===========================================================================
174 // Test the MakeCallback mechanism
175 // ===========================================================================
176 class MakeCallbackTestCase : public TestCase
177 {
178 public:
179   MakeCallbackTestCase ();
~MakeCallbackTestCase()180   virtual ~MakeCallbackTestCase ()
181   {}
182 
Target1(void)183   void Target1 (void)
184   {
185     m_test1 = true;
186   }
Target2(void)187   int Target2 (void)
188   {
189     m_test2 = true;
190     return 2;
191   }
Target3(double a)192   void Target3 (double a)
193   {
194     NS_UNUSED (a);
195     m_test3 = true;
196   }
Target4(double a,int b)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
MakeCallbackTarget5(void)220 MakeCallbackTarget5 (void)
221 {
222   gMakeCallbackTest5 = true;
223 }
224 
225 void
MakeCallbackTarget6(int)226 MakeCallbackTarget6 (int)
227 {
228   gMakeCallbackTest6 = true;
229 }
230 
231 int
MakeCallbackTarget7(int a)232 MakeCallbackTarget7 (int a)
233 {
234   gMakeCallbackTest7 = true;
235   return a;
236 }
237 
MakeCallbackTestCase()238 MakeCallbackTestCase::MakeCallbackTestCase ()
239   : TestCase ("Check MakeCallback() mechanism")
240 {}
241 
242 void
DoSetup(void)243 MakeCallbackTestCase::DoSetup (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
DoRun(void)255 MakeCallbackTestCase::DoRun (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   //
261   Callback<void> target1 = MakeCallback (&MakeCallbackTestCase::Target1, this);
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   //
269   Callback<int> target2 = MakeCallback (&MakeCallbackTestCase::Target2, this);
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   //
277   Callback<void, double> target3 = MakeCallback (&MakeCallbackTestCase::Target3, this);
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   //
285   Callback<int, double, int> target4 = MakeCallback (&MakeCallbackTestCase::Target4, this);
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   //
295   Callback<void> target5 = MakeCallback (&MakeCallbackTarget5);
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   //
305   Callback<void, int> target6 = MakeCallback (&MakeCallbackTarget6);
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   //
315   Callback<int, int> target7 = MakeCallback (&MakeCallbackTarget7);
316   target7 (1);
317   NS_TEST_ASSERT_MSG_EQ (gMakeCallbackTest7, true, "Callback did not fire");
318 }
319 
320 // ===========================================================================
321 // Test the MakeBoundCallback mechanism
322 // ===========================================================================
323 class MakeBoundCallbackTestCase : public TestCase
324 {
325 public:
326   MakeBoundCallbackTestCase ();
~MakeBoundCallbackTestCase()327   virtual ~MakeBoundCallbackTestCase ()
328   {}
329 
330 private:
331   virtual void DoRun (void);
332   virtual void DoSetup (void);
333 };
334 
335 static int gMakeBoundCallbackTest1;
336 static bool *gMakeBoundCallbackTest2;
337 static bool *gMakeBoundCallbackTest3a;
338 static int gMakeBoundCallbackTest3b;
339 static int gMakeBoundCallbackTest4a;
340 static int gMakeBoundCallbackTest4b;
341 static int gMakeBoundCallbackTest5a;
342 static int gMakeBoundCallbackTest5b;
343 static int gMakeBoundCallbackTest5c;
344 static int gMakeBoundCallbackTest6a;
345 static int gMakeBoundCallbackTest6b;
346 static int gMakeBoundCallbackTest6c;
347 static int gMakeBoundCallbackTest7a;
348 static int gMakeBoundCallbackTest7b;
349 static int gMakeBoundCallbackTest7c;
350 static int gMakeBoundCallbackTest8a;
351 static int gMakeBoundCallbackTest8b;
352 static int gMakeBoundCallbackTest8c;
353 static int gMakeBoundCallbackTest9a;
354 static int gMakeBoundCallbackTest9b;
355 static int gMakeBoundCallbackTest9c;
356 static int gMakeBoundCallbackTest9d;
357 
358 void
MakeBoundCallbackTarget1(int a)359 MakeBoundCallbackTarget1 (int a)
360 {
361   gMakeBoundCallbackTest1 = a;
362 }
363 
364 void
MakeBoundCallbackTarget2(bool * a)365 MakeBoundCallbackTarget2 (bool *a)
366 {
367   gMakeBoundCallbackTest2 = a;
368 }
369 
370 int
MakeBoundCallbackTarget3(bool * a,int b)371 MakeBoundCallbackTarget3 (bool *a, int b)
372 {
373   gMakeBoundCallbackTest3a = a;
374   gMakeBoundCallbackTest3b = b;
375   return 1234;
376 }
377 
378 void
MakeBoundCallbackTarget4(int a,int b)379 MakeBoundCallbackTarget4 (int a, int b)
380 {
381   gMakeBoundCallbackTest4a = a;
382   gMakeBoundCallbackTest4b = b;
383 }
384 
385 int
MakeBoundCallbackTarget5(int a,int b)386 MakeBoundCallbackTarget5 (int a, int b)
387 {
388   gMakeBoundCallbackTest5a = a;
389   gMakeBoundCallbackTest5b = b;
390   return 1234;
391 }
392 
393 int
MakeBoundCallbackTarget6(int a,int b,int c)394 MakeBoundCallbackTarget6 (int a, int b, int c)
395 {
396   gMakeBoundCallbackTest6a = a;
397   gMakeBoundCallbackTest6b = b;
398   gMakeBoundCallbackTest6c = c;
399   return 1234;
400 }
401 
402 void
MakeBoundCallbackTarget7(int a,int b,int c)403 MakeBoundCallbackTarget7 (int a, int b, int c)
404 {
405   gMakeBoundCallbackTest7a = a;
406   gMakeBoundCallbackTest7b = b;
407   gMakeBoundCallbackTest7c = c;
408 }
409 
410 int
MakeBoundCallbackTarget8(int a,int b,int c)411 MakeBoundCallbackTarget8 (int a, int b, int c)
412 {
413   gMakeBoundCallbackTest8a = a;
414   gMakeBoundCallbackTest8b = b;
415   gMakeBoundCallbackTest8c = c;
416   return 1234;
417 }
418 
419 int
MakeBoundCallbackTarget9(int a,int b,int c,int d)420 MakeBoundCallbackTarget9 (int a, int b, int c, int d)
421 {
422   gMakeBoundCallbackTest9a = a;
423   gMakeBoundCallbackTest9b = b;
424   gMakeBoundCallbackTest9c = c;
425   gMakeBoundCallbackTest9d = d;
426   return 1234;
427 }
428 
MakeBoundCallbackTestCase()429 MakeBoundCallbackTestCase::MakeBoundCallbackTestCase ()
430   : TestCase ("Check MakeBoundCallback() mechanism")
431 {}
432 
433 void
DoSetup(void)434 MakeBoundCallbackTestCase::DoSetup (void)
435 {
436   gMakeBoundCallbackTest1 = 0;
437   gMakeBoundCallbackTest2 = 0;
438   gMakeBoundCallbackTest3a = 0;
439   gMakeBoundCallbackTest3b = 0;
440   gMakeBoundCallbackTest4a = 0;
441   gMakeBoundCallbackTest4b = 0;
442   gMakeBoundCallbackTest5a = 0;
443   gMakeBoundCallbackTest5b = 0;
444   gMakeBoundCallbackTest5c = 0;
445   gMakeBoundCallbackTest6a = 0;
446   gMakeBoundCallbackTest6b = 0;
447   gMakeBoundCallbackTest6c = 0;
448   gMakeBoundCallbackTest7a = 0;
449   gMakeBoundCallbackTest7b = 0;
450   gMakeBoundCallbackTest7c = 0;
451   gMakeBoundCallbackTest8a = 0;
452   gMakeBoundCallbackTest8b = 0;
453   gMakeBoundCallbackTest8c = 0;
454   gMakeBoundCallbackTest9a = 0;
455   gMakeBoundCallbackTest9b = 0;
456   gMakeBoundCallbackTest9c = 0;
457   gMakeBoundCallbackTest9d = 0;
458 }
459 
460 void
DoRun(void)461 MakeBoundCallbackTestCase::DoRun (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   //
480   Callback<void> target1 = MakeBoundCallback (&MakeBoundCallbackTarget1, 1234);
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;
488   Callback<void> target2 = MakeBoundCallback (&MakeBoundCallbackTarget2, &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   //
496   Callback<int, int> target3 = MakeBoundCallback (&MakeBoundCallbackTarget3, &a);
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   //
505   Callback<void> target4 = MakeBoundCallback (&MakeBoundCallbackTarget4, 3456, 5678);
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 
510   Callback<int> target5 = MakeBoundCallback (&MakeBoundCallbackTarget5, 3456, 5678);
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 
516   Callback<int, int> target6 = MakeBoundCallback (&MakeBoundCallbackTarget6, 3456, 5678);
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 // ===========================================================================
551 class NullifyCallbackTestCase : public TestCase
552 {
553 public:
554   NullifyCallbackTestCase ();
~NullifyCallbackTestCase()555   virtual ~NullifyCallbackTestCase ()
556   {}
557 
Target1(void)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 
NullifyCallbackTestCase()570 NullifyCallbackTestCase::NullifyCallbackTestCase ()
571   : TestCase ("Check Nullify() and IsNull()")
572 {}
573 
574 void
DoSetup(void)575 NullifyCallbackTestCase::DoSetup (void)
576 {
577   m_test1 = false;
578 }
579 
580 void
DoRun(void)581 NullifyCallbackTestCase::DoRun (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   //
587   Callback<void> target1 = MakeCallback (&NullifyCallbackTestCase::Target1, this);
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 // ===========================================================================
602 class MakeCallbackTemplatesTestCase : public TestCase
603 {
604 public:
605   MakeCallbackTemplatesTestCase ();
~MakeCallbackTemplatesTestCase()606   virtual ~MakeCallbackTemplatesTestCase ()
607   {}
608 
Target1(void)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* */
TestFZero(void)621 void TestFZero (void) {}
TestFOne(int)622 void TestFOne (int) {}
TestFTwo(int,int)623 void TestFTwo (int, int) {}
TestFThree(int,int,int)624 void TestFThree (int, int, int) {}
TestFFour(int,int,int,int)625 void TestFFour (int, int, int, int) {}
TestFFive(int,int,int,int,int)626 void TestFFive (int, int, int, int, int) {}
TestFSix(int,int,int,int,int,int)627 void TestFSix (int, int, int, int, int, int) {}
628 
TestFROne(int &)629 void TestFROne (int &) {}
TestFRTwo(int &,int &)630 void TestFRTwo (int &, int &) {}
TestFRThree(int &,int &,int &)631 void TestFRThree (int &, int &, int &) {}
TestFRFour(int &,int &,int &,int &)632 void TestFRFour (int &, int &, int &, int &) {}
TestFRFive(int &,int &,int &,int &,int &)633 void TestFRFive (int &, int &, int &, int &, int &) {}
TestFRSix(int &,int &,int &,int &,int &,int &)634 void TestFRSix (int &, int &, int &, int &, int &, int &) {}
635 /* *NS_CHECK_STYLE_ON* */
636 
637 class CallbackTestParent
638 {
639 public:
PublicParent(void)640   void PublicParent (void)
641   {}
642 
643 protected:
ProtectedParent(void)644   void ProtectedParent (void)
645   {}
StaticProtectedParent(void)646   static void StaticProtectedParent (void)
647   {}
648 
649 private:
PrivateParent(void)650   void PrivateParent (void)
651   {}
652 };
653 
654 class CallbackTestClass : public CallbackTestParent
655 {
656 public:
657   /* *NS_CHECK_STYLE_OFF* */
TestZero(void)658   void TestZero (void) {}
TestOne(int)659   void TestOne (int) {}
TestTwo(int,int)660   void TestTwo (int, int) {}
TestThree(int,int,int)661   void TestThree (int, int, int) {}
TestFour(int,int,int,int)662   void TestFour (int, int, int, int) {}
TestFive(int,int,int,int,int)663   void TestFive (int, int, int, int, int) {}
TestSix(int,int,int,int,int,int)664   void TestSix (int, int, int, int, int, int) {}
TestCZero(void) const665   void TestCZero (void) const {}
TestCOne(int) const666   void TestCOne (int) const {}
TestCTwo(int,int) const667   void TestCTwo (int, int) const {}
TestCThree(int,int,int) const668   void TestCThree (int, int, int) const {}
TestCFour(int,int,int,int) const669   void TestCFour (int, int, int, int) const {}
TestCFive(int,int,int,int,int) const670   void TestCFive (int, int, int, int, int) const {}
TestCSix(int,int,int,int,int,int) const671   void TestCSix (int, int, int, int, int, int) const {}
672   /* *NS_CHECK_STYLE_ON* */
673 
CheckParentalRights(void)674   void CheckParentalRights (void)
675   {
676     MakeCallback (&CallbackTestParent::StaticProtectedParent);
677     MakeCallback (&CallbackTestParent::PublicParent, this);
678     MakeCallback (&CallbackTestClass::ProtectedParent, this);
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 
MakeCallbackTemplatesTestCase()687 MakeCallbackTemplatesTestCase::MakeCallbackTemplatesTestCase ()
688   : TestCase ("Check various MakeCallback() template functions")
689 {}
690 
691 void
DoRun(void)692 MakeCallbackTemplatesTestCase::DoRun (void)
693 {
694   CallbackTestClass that;
695 
696   MakeCallback (&CallbackTestClass::TestZero, &that);
697   MakeCallback (&CallbackTestClass::TestOne, &that);
698   MakeCallback (&CallbackTestClass::TestTwo, &that);
699   MakeCallback (&CallbackTestClass::TestThree, &that);
700   MakeCallback (&CallbackTestClass::TestFour, &that);
701   MakeCallback (&CallbackTestClass::TestFive, &that);
702   MakeCallback (&CallbackTestClass::TestSix, &that);
703 
704   MakeCallback (&CallbackTestClass::TestCZero, &that);
705   MakeCallback (&CallbackTestClass::TestCOne, &that);
706   MakeCallback (&CallbackTestClass::TestCTwo, &that);
707   MakeCallback (&CallbackTestClass::TestCThree, &that);
708   MakeCallback (&CallbackTestClass::TestCFour, &that);
709   MakeCallback (&CallbackTestClass::TestCFive, &that);
710   MakeCallback (&CallbackTestClass::TestCSix, &that);
711 
712   MakeCallback (&TestFZero);
713   MakeCallback (&TestFOne);
714   MakeCallback (&TestFTwo);
715   MakeCallback (&TestFThree);
716   MakeCallback (&TestFFour);
717   MakeCallback (&TestFFive);
718   MakeCallback (&TestFSix);
719 
720   MakeCallback (&TestFROne);
721   MakeCallback (&TestFRTwo);
722   MakeCallback (&TestFRThree);
723   MakeCallback (&TestFRFour);
724   MakeCallback (&TestFRFive);
725   MakeCallback (&TestFRSix);
726 
727   MakeBoundCallback (&TestFOne, 1);
728   MakeBoundCallback (&TestFTwo, 1);
729   MakeBoundCallback (&TestFThree, 1);
730   MakeBoundCallback (&TestFFour, 1);
731   MakeBoundCallback (&TestFFive, 1);
732 
733   MakeBoundCallback (&TestFROne, 1);
734   MakeBoundCallback (&TestFRTwo, 1);
735   MakeBoundCallback (&TestFRThree, 1);
736   MakeBoundCallback (&TestFRFour, 1);
737   MakeBoundCallback (&TestFRFive, 1);
738 
739   that.CheckParentalRights ();
740 }
741 
742 // ===========================================================================
743 // The Test Suite that glues all of the Test Cases together.
744 // ===========================================================================
745 class CallbackTestSuite : public TestSuite
746 {
747 public:
748   CallbackTestSuite ();
749 };
750 
CallbackTestSuite()751 CallbackTestSuite::CallbackTestSuite ()
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 
761 static CallbackTestSuite CallbackTestSuite;
762