A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
callback-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "ns3/callback.h"
8#include "ns3/test.h"
9
10#include <stdint.h>
11
12using namespace ns3;
13
14/**
15 * @file
16 * @ingroup callback-tests
17 * Callback test suite
18 */
19
20/**
21 * @ingroup core-tests
22 * @defgroup callback-tests Callback tests
23 */
24
25/**
26 * @ingroup callback-tests
27 *
28 * Test the basic Callback mechanism.
29 */
31{
32 public:
34
36 {
37 }
38
39 /**
40 * Callback 1 target function.
41 */
42 void Target1()
43 {
44 m_test1 = true;
45 }
46
47 /**
48 * Callback 2 target function.
49 * @return two.
50 */
51 int Target2()
52 {
53 m_test2 = true;
54 return 2;
55 }
56
57 /**
58 * Callback 3 target function.
59 * @param a A parameter (unused).
60 */
61 void Target3(double a [[maybe_unused]])
62 {
63 m_test3 = true;
64 }
65
66 /**
67 * Callback 4 target function.
68 * @param a A parameter (unused).
69 * @param b Another parameter (unused).
70 * @return four.
71 */
72 int Target4(double a [[maybe_unused]], int b [[maybe_unused]])
73 {
74 m_test4 = true;
75 return 4;
76 }
77
78 private:
79 void DoRun() override;
80 void DoSetup() override;
81
82 bool m_test1; //!< true if Target1 has been called, false otherwise.
83 bool m_test2; //!< true if Target2 has been called, false otherwise.
84 bool m_test3; //!< true if Target3 has been called, false otherwise.
85 bool m_test4; //!< true if Target4 has been called, false otherwise.
86};
87
88/**
89 * Variable to verify that a callback has been called.
90 * @{
91 */
96
97/** @} */
98
99/**
100 * Callback 5 target function.
101 */
102void
107
108/**
109 * Callback 6 target function.
110 */
111void
113{
114 gBasicCallbackTest6 = true;
115}
116
117/**
118 * Callback 6 target function.
119 * @param a The value passed by the callback.
120 * @return the value of the calling function.
121 */
122int
124{
125 gBasicCallbackTest7 = true;
126 return a;
127}
128
130 : TestCase("Check basic Callback mechanism")
131{
132}
133
134void
136{
137 m_test1 = false;
138 m_test2 = false;
139 m_test3 = false;
140 m_test4 = false;
141 gBasicCallbackTest5 = false;
142 gBasicCallbackTest6 = false;
143 gBasicCallbackTest7 = false;
144 gBasicCallbackTest8 = false;
145}
146
147void
149{
150 //
151 // Make sure we can declare and compile a Callback pointing to a member
152 // function returning void and execute it.
153 //
155 target1();
156 NS_TEST_ASSERT_MSG_EQ(m_test1, true, "Callback did not fire");
157
158 //
159 // Make sure we can declare and compile a Callback pointing to a member
160 // function that returns an int and execute it.
161 //
162 Callback<int> target2;
164 target2();
165 NS_TEST_ASSERT_MSG_EQ(m_test2, true, "Callback did not fire");
166
167 //
168 // Make sure we can declare and compile a Callback pointing to a member
169 // function that returns void, takes a double parameter, and execute it.
170 //
172 target3(0.0);
173 NS_TEST_ASSERT_MSG_EQ(m_test3, true, "Callback did not fire");
174
175 //
176 // Make sure we can declare and compile a Callback pointing to a member
177 // function that returns void, takes two parameters, and execute it.
178 //
181 target4(0.0, 1);
182 NS_TEST_ASSERT_MSG_EQ(m_test4, true, "Callback did not fire");
183
184 //
185 // Make sure we can declare and compile a Callback pointing to a non-member
186 // function that returns void, and execute it. This is a lower level call
187 // than MakeCallback so we have got to include at least two arguments to make
188 // sure that the constructor is properly disambiguated. If the arguments are
189 // not needed, we just pass in dummy values.
190 //
192 target5();
193 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest5, true, "Callback did not fire");
194
195 //
196 // Make sure we can declare and compile a Callback pointing to a non-member
197 // function that returns void, takes one integer argument and execute it.
198 //
200 target6(1);
201 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest6, true, "Callback did not fire");
202
203 //
204 // Make sure we can declare and compile a Callback pointing to a non-member
205 // function that returns int, takes one integer argument and execute it.
206 //
208 target7(1);
209 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest7, true, "Callback did not fire");
210
211 //
212 // Make sure we can create a callback pointing to a lambda.
213 //
214 Callback<double, int> target8([](int p) {
215 gBasicCallbackTest8 = true;
216 return p / 2.;
217 });
218 target8(5);
219 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest8, true, "Callback did not fire");
220}
221
222/**
223 * @ingroup callback-tests
224 *
225 * Test the MakeCallback mechanism.
226 */
228{
229 public:
231
233 {
234 }
235
236 /**
237 * Callback 1 target function.
238 */
239 void Target1()
240 {
241 m_test1 = true;
242 }
243
244 /**
245 * Callback 2 target function.
246 * @return two.
247 */
249 {
250 m_test2 = true;
251 return 2;
252 }
253
254 /**
255 * Callback 3 target function.
256 * @param a A parameter (unused).
257 */
258 void Target3(double a [[maybe_unused]])
259 {
260 m_test3 = true;
261 }
262
263 /**
264 * Callback 4 target function.
265 * @param a A parameter (unused).
266 * @param b Another parameter (unused).
267 * @return four.
268 */
269 int Target4(double a [[maybe_unused]], int b [[maybe_unused]])
270 {
271 m_test4 = true;
272 return 4;
273 }
274
275 private:
276 void DoRun() override;
277 void DoSetup() override;
278
279 bool m_test1; //!< true if Target1 has been called, false otherwise.
280 bool m_test2; //!< true if Target2 has been called, false otherwise.
281 bool m_test3; //!< true if Target3 has been called, false otherwise.
282 bool m_test4; //!< true if Target4 has been called, false otherwise.
283};
284
285/**
286 * Variable to verify that a callback has been called.
287 * @{
288 */
292
293/** @} */
294
295/**
296 * MakeCallback 5 target function.
297 */
298void
303
304/**
305 * MakeCallback 6 target function.
306 */
307void
309{
310 gMakeCallbackTest6 = true;
311}
312
313/**
314 * MakeCallback 7 target function.
315 * @param a The value passed by the callback.
316 * @return the value of the calling function.
317 */
318int
320{
321 gMakeCallbackTest7 = true;
322 return a;
323}
324
326 : TestCase("Check MakeCallback() mechanism")
327{
328}
329
330void
332{
333 m_test1 = false;
334 m_test2 = false;
335 m_test3 = false;
336 m_test4 = false;
337 gMakeCallbackTest5 = false;
338 gMakeCallbackTest6 = false;
339 gMakeCallbackTest7 = false;
340}
341
342void
344{
345 //
346 // Make sure we can declare and make a Callback pointing to a member
347 // function returning void and execute it.
348 //
350 target1();
351 NS_TEST_ASSERT_MSG_EQ(m_test1, true, "Callback did not fire");
352
353 //
354 // Make sure we can declare and make a Callback pointing to a member
355 // function that returns an int and execute it.
356 //
358 target2();
359 NS_TEST_ASSERT_MSG_EQ(m_test2, true, "Callback did not fire");
360
361 //
362 // Make sure we can declare and make a Callback pointing to a member
363 // function that returns void, takes a double parameter, and execute it.
364 //
366 target3(0.0);
367 NS_TEST_ASSERT_MSG_EQ(m_test3, true, "Callback did not fire");
368
369 //
370 // Make sure we can declare and make a Callback pointing to a member
371 // function that returns void, takes two parameters, and execute it.
372 //
374 target4(0.0, 1);
375 NS_TEST_ASSERT_MSG_EQ(m_test4, true, "Callback did not fire");
376
377 //
378 // Make sure we can declare and make a Callback pointing to a non-member
379 // function that returns void, and execute it. This uses a higher level call
380 // than in the basic tests so we do not need to include any dummy arguments
381 // here.
382 //
384 target5();
385 NS_TEST_ASSERT_MSG_EQ(gMakeCallbackTest5, true, "Callback did not fire");
386
387 //
388 // Make sure we can declare and compile a Callback pointing to a non-member
389 // function that returns void, takes one integer argument and execute it.
390 // This uses a higher level call than in the basic tests so we do not need to
391 // include any dummy arguments here.
392 //
394 target6(1);
395 NS_TEST_ASSERT_MSG_EQ(gMakeCallbackTest6, true, "Callback did not fire");
396
397 //
398 // Make sure we can declare and compile a Callback pointing to a non-member
399 // function that returns int, takes one integer argument and execute it.
400 // This uses a higher level call than in the basic tests so we do not need to
401 // include any dummy arguments here.
402 //
404 target7(1);
405 NS_TEST_ASSERT_MSG_EQ(gMakeCallbackTest7, true, "Callback did not fire");
406}
407
408/**
409 * @ingroup callback-tests
410 *
411 * Test the MakeBoundCallback mechanism.
412 */
414{
415 public:
417
419 {
420 }
421
422 /**
423 * Member function to test the creation of a bound callback pointing to a member function
424 *
425 * @param a first argument
426 * @param b second argument
427 * @param c third argument
428 * @return the sum of the arguments
429 */
430 int BoundTarget(int a, int b, int c)
431 {
432 return a + b + c;
433 }
434
435 private:
436 void DoRun() override;
437 void DoSetup() override;
438};
439
440/**
441 * Variable to verify that a callback has been called.
442 * @{
443 */
466
467/** @} */
468
469// Note: doxygen compounds don not work due to params / return variability.
470
471/**
472 * MakeBoundCallback 1 target function.
473 * @param a The value passed by the callback.
474 */
475void
480
481/**
482 * MakeBoundCallback 2 target function.
483 * @param a The value passed by the callback.
484 */
485void
490
491/**
492 * MakeBoundCallback 3 target function.
493 * @param a The value passed by the callback.
494 * @param b The value passed by the callback.
495 * @return the value 1234.
496 */
497int
499{
502 return 1234;
503}
504
505/**
506 * MakeBoundCallback 4 target function.
507 * @param a The value passed by the callback.
508 * @param b The value passed by the callback.
509 */
510void
516
517/**
518 * MakeBoundCallback 5 target function.
519 * @param a The value passed by the callback.
520 * @param b The value passed by the callback.
521 * @return the value 1234.
522 */
523int
525{
528 return 1234;
529}
530
531/**
532 * MakeBoundCallback 5 target function.
533 * @param a The value passed by the callback.
534 * @param b The value passed by the callback.
535 * @param c The value passed by the callback.
536 * @return the value 1234.
537 */
538int
539MakeBoundCallbackTarget6(int a, int b, int c)
540{
544 return 1234;
545}
546
547/**
548 * MakeBoundCallback 7 target function.
549 * @param a The value passed by the callback.
550 * @param b The value passed by the callback.
551 * @param c The value passed by the callback.
552 */
553void
560
561/**
562 * MakeBoundCallback 8 target function.
563 * @param a The value passed by the callback.
564 * @param b The value passed by the callback.
565 * @param c The value passed by the callback.
566 * @return the value 1234.
567 */
568int
569MakeBoundCallbackTarget8(int a, int b, int c)
570{
574 return 1234;
575}
576
577/**
578 * MakeBoundCallback 5 target function.
579 * @param a The value passed by the callback.
580 * @param b The value passed by the callback.
581 * @param c The value passed by the callback.
582 * @param d The value passed by the callback.
583 * @return the value 1234.
584 */
585int
586MakeBoundCallbackTarget9(int a, int b, int c, int d)
587{
592 return 1234;
593}
594
596 : TestCase("Check MakeBoundCallback() mechanism")
597{
598}
599
600void
626
627void
629{
630 //
631 // This is slightly tricky to explain. A bound Callback allows us to package
632 // up arguments for use later. The arguments are bound when the callback is
633 // created and the code that fires the Callback does not know they are there.
634 //
635 // Since the callback is *declared* according to the way it will be used, the
636 // arguments are not seen there. However, the target function of the callback
637 // will have the provided arguments present. The MakeBoundCallback template
638 // function is what connects the two together and where you provide the
639 // arguments to be bound.
640 //
641 // Here we declare a Callback that returns a void and takes no parameters.
642 // MakeBoundCallback connects this Callback to a target function that returns
643 // void and takes an integer argument. That integer argument is bound to the
644 // value 1234. When the Callback is fired, no integer argument is provided
645 // directly. The argument is provided by bound Callback mechanism.
646 //
648 target1();
650 1234,
651 "Callback did not fire or binding not correct");
652
653 //
654 // Make sure we can bind a pointer value (a common use case).
655 //
656 bool a;
658 target2();
660 &a,
661 "Callback did not fire or binding not correct");
662
663 //
664 // Make sure we can mix and match bound and unbound arguments. This callback
665 // returns an integer so we should see that appear.
666 //
668 int result = target3(2468);
669 NS_TEST_ASSERT_MSG_EQ(result, 1234, "Return value of callback not correct");
671 &a,
672 "Callback did not fire or binding not correct");
674 2468,
675 "Callback did not fire or argument not correct");
676
677 //
678 // Test the TwoBound variant
679 //
681 target4();
683 3456,
684 "Callback did not fire or binding not correct");
686 5678,
687 "Callback did not fire or binding not correct");
688
690 int resultTwoA = target5();
691 NS_TEST_ASSERT_MSG_EQ(resultTwoA, 1234, "Return value of callback not correct");
693 3456,
694 "Callback did not fire or binding not correct");
696 5678,
697 "Callback did not fire or binding not correct");
698
700 int resultTwoB = target6(6789);
701 NS_TEST_ASSERT_MSG_EQ(resultTwoB, 1234, "Return value of callback not correct");
703 3456,
704 "Callback did not fire or binding not correct");
706 5678,
707 "Callback did not fire or binding not correct");
709 6789,
710 "Callback did not fire or argument not correct");
711
712 //
713 // Test the ThreeBound variant
714 //
715 Callback<void> target7 = MakeBoundCallback(&MakeBoundCallbackTarget7, 2345, 3456, 4567);
716 target7();
718 2345,
719 "Callback did not fire or binding not correct");
721 3456,
722 "Callback did not fire or binding not correct");
724 4567,
725 "Callback did not fire or binding not correct");
726
727 Callback<int> target8 = MakeBoundCallback(&MakeBoundCallbackTarget8, 2345, 3456, 4567);
728 int resultThreeA = target8();
729 NS_TEST_ASSERT_MSG_EQ(resultThreeA, 1234, "Return value of callback not correct");
731 2345,
732 "Callback did not fire or binding not correct");
734 3456,
735 "Callback did not fire or binding not correct");
737 4567,
738 "Callback did not fire or binding not correct");
739
741 int resultThreeB = target9(5678);
742 NS_TEST_ASSERT_MSG_EQ(resultThreeB, 1234, "Return value of callback not correct");
744 2345,
745 "Callback did not fire or binding not correct");
747 3456,
748 "Callback did not fire or binding not correct");
750 4567,
751 "Callback did not fire or binding not correct");
753 5678,
754 "Callback did not fire or binding not correct");
755
756 //
757 // Test creating a bound callback pointing to a member function. Also, make sure that
758 // an argument can be bound to a reference.
759 //
760 int b = 1;
761 Callback<int> target10 =
762 Callback<int>(&MakeBoundCallbackTestCase::BoundTarget, this, std::ref(b), 5, 2);
763 NS_TEST_ASSERT_MSG_EQ(target10(), 8, "Bound callback returned an unexpected value");
764 b = 3;
765 NS_TEST_ASSERT_MSG_EQ(target10(), 10, "Bound callback returned an unexpected value");
766}
767
768/**
769 * @ingroup callback-tests
770 *
771 * Test the callback equality implementation.
772 */
774{
775 public:
777
779 {
780 }
781
782 /**
783 * Member function used to test equality of callbacks.
784 *
785 * @param a first argument
786 * @param b second argument
787 * @return the sum of the arguments
788 */
789 int TargetMember(double a, int b)
790 {
791 return static_cast<int>(a) + b;
792 }
793
794 private:
795 void DoRun() override;
796 void DoSetup() override;
797};
798
799/**
800 * Non-member function used to test equality of callbacks.
801 *
802 * @param a first argument
803 * @param b second argument
804 * @return the sum of the arguments
805 */
806int
808{
809 return static_cast<int>(a) + b;
810}
811
813 : TestCase("Check Callback equality test")
814{
815}
816
817void
821
822void
824{
825 //
826 // Make sure that two callbacks pointing to the same member function
827 // compare equal.
828 //
832 NS_TEST_ASSERT_MSG_EQ(target1a.IsEqual(target1b), true, "Equality test failed");
833
834 //
835 // Make sure that two callbacks pointing to the same member function
836 // compare equal, after binding the first argument.
837 //
839 Callback<int, int> target2b = target1b.Bind(1.5);
840 NS_TEST_ASSERT_MSG_EQ(target2a.IsEqual(target2b), true, "Equality test failed");
841
842 //
843 // Make sure that two callbacks pointing to the same member function
844 // compare equal, after binding the first two arguments.
845 //
846 Callback<int> target3a(target2a, 2);
847 Callback<int> target3b = target1b.Bind(1.5, 2);
848 NS_TEST_ASSERT_MSG_EQ(target3a.IsEqual(target3b), true, "Equality test failed");
849
850 //
851 // Make sure that two callbacks pointing to the same member function do
852 // not compare equal if they are bound to different arguments.
853 //
854 Callback<int> target3c = target1b.Bind(1.5, 3);
855 NS_TEST_ASSERT_MSG_EQ(target3c.IsEqual(target3b), false, "Equality test failed");
856
857 //
858 // Make sure that two callbacks pointing to the same non-member function
859 // compare equal.
860 //
863 NS_TEST_ASSERT_MSG_EQ(target4a.IsEqual(target4b), true, "Equality test failed");
864
865 //
866 // Make sure that two callbacks pointing to the same non-member function
867 // compare equal, after binding the first argument.
868 //
870 Callback<int, int> target5b = target4b.Bind(1.5);
871 NS_TEST_ASSERT_MSG_EQ(target5a.IsEqual(target5b), true, "Equality test failed");
872
873 //
874 // Make sure that two callbacks pointing to the same non-member function
875 // compare equal, after binding the first two arguments.
876 //
877 Callback<int> target6a(target5a, 2);
878 Callback<int> target6b = target4b.Bind(1.5, 2);
879 NS_TEST_ASSERT_MSG_EQ(target6a.IsEqual(target6b), true, "Equality test failed");
880
881 //
882 // Make sure that two callbacks pointing to the same non-member function do
883 // not compare equal if they are bound to different arguments.
884 //
885 Callback<int> target6c = target4b.Bind(1.5, 3);
886 NS_TEST_ASSERT_MSG_EQ(target6c.IsEqual(target6b), false, "Equality test failed");
887
888 //
889 // Check that we cannot compare lambdas.
890 //
891 Callback<double, int, double> target7a([](int p, double d) { return d + p / 2.; });
892 Callback<double, int, double> target7b([](int p, double d) { return d + p / 2.; });
893 NS_TEST_ASSERT_MSG_EQ(target7a.IsEqual(target7b), false, "Compared lambdas?");
894
895 //
896 // Make sure that a callback pointing to a lambda and a copy of it compare equal.
897 //
898 Callback<double, int, double> target7c(target7b);
899 NS_TEST_ASSERT_MSG_EQ(target7c.IsEqual(target7b), true, "Equality test failed");
900
901 //
902 // Make sure that a callback pointing to a lambda and a copy of it compare equal,
903 // after binding the first argument.
904 //
905 Callback<double, double> target8b = target7b.Bind(1);
906 Callback<double, double> target8c(target7c, 1);
907 NS_TEST_ASSERT_MSG_EQ(target8b.IsEqual(target8c), true, "Equality test failed");
908
909 //
910 // Make sure that a callback pointing to a lambda and a copy of it compare equal,
911 // after binding the first two arguments.
912 //
913 Callback<double> target9b = target8b.Bind(2.0);
914 Callback<double> target9c(target8c, 2.0);
915 NS_TEST_ASSERT_MSG_EQ(target9b.IsEqual(target9c), true, "Equality test failed");
916
917 //
918 // Make sure that a callback pointing to a lambda and a copy of it do not compare
919 // equal if they are bound to different arguments.
920 //
921 Callback<double> target9d = target8b.Bind(4);
922 NS_TEST_ASSERT_MSG_EQ(target9d.IsEqual(target9c), false, "Equality test failed");
923}
924
925/**
926 * @ingroup callback-tests
927 *
928 * Test the Nullify mechanism.
929 */
931{
932 public:
934
936 {
937 }
938
939 /**
940 * Callback 1 target function.
941 */
942 void Target1()
943 {
944 m_test1 = true;
945 }
946
947 private:
948 void DoRun() override;
949 void DoSetup() override;
950
951 bool m_test1; //!< true if Target1 has been called, false otherwise.
952};
953
955 : TestCase("Check Nullify() and IsNull()")
956{
957}
958
959void
964
965void
967{
968 //
969 // Make sure we can declare and make a Callback pointing to a member
970 // function returning void and execute it.
971 //
973 target1();
974 NS_TEST_ASSERT_MSG_EQ(m_test1, true, "Callback did not fire");
975
976 NS_TEST_ASSERT_MSG_EQ(target1.IsNull(), false, "Working Callback reports IsNull()");
977
978 target1.Nullify();
979
980 NS_TEST_ASSERT_MSG_EQ(target1.IsNull(), true, "Nullified Callback reports not IsNull()");
981}
982
983/**
984 * @ingroup callback-tests
985 *
986 * Make sure that various MakeCallback template functions compile and execute;
987 * doesn't check an results of the execution.
988 */
990{
991 public:
993
995 {
996 }
997
998 /**
999 * Callback 1 target function.
1000 */
1001 void Target1()
1002 {
1003 m_test1 = true;
1004 }
1005
1006 private:
1007 void DoRun() override;
1008
1009 bool m_test1; //!< true if Target1 has been called, false otherwise.
1010};
1011
1012/**
1013 * Test function - does nothing.
1014 * @{
1015 */
1016void TestFZero(){};
1017void TestFOne(int){};
1018void TestFTwo(int, int){};
1019void TestFThree(int, int, int){};
1020void TestFFour(int, int, int, int){};
1021void TestFFive(int, int, int, int, int){};
1022void TestFSix(int, int, int, int, int, int){};
1023
1024void TestFROne(int&){};
1025void TestFRTwo(int&, int&){};
1026void TestFRThree(int&, int&, int&){};
1027void TestFRFour(int&, int&, int&, int&){};
1028void TestFRFive(int&, int&, int&, int&, int&){};
1029void TestFRSix(int&, int&, int&, int&, int&, int&){};
1030
1031/** @} */
1032
1033/**
1034 * @ingroup callback-tests
1035 *
1036 * Class used to check the capability of callbacks to call
1037 * public, protected, and private functions.
1038 */
1040{
1041 public:
1042 /// A public function.
1044 {
1045 }
1046
1047 protected:
1048 /// A protected function.
1050 {
1051 }
1052
1053 /// A static protected function.
1055 {
1056 }
1057
1058 private:
1059 /// A private function.
1061 {
1062 }
1063};
1064
1065/**
1066 * @ingroup callback-tests
1067 *
1068 * Derived class used to check the capability of callbacks to call
1069 * public, protected, and private functions.
1070 */
1072{
1073 public:
1074 /**
1075 * Test function - does nothing.
1076 * @{
1077 */
1078 void TestZero(){};
1079 void TestOne(int){};
1080 void TestTwo(int, int){};
1081 void TestThree(int, int, int){};
1082 void TestFour(int, int, int, int){};
1083 void TestFive(int, int, int, int, int){};
1084 void TestSix(int, int, int, int, int, int){};
1085 void TestCZero() const {};
1086 void TestCOne(int) const {};
1087 void TestCTwo(int, int) const {};
1088 void TestCThree(int, int, int) const {};
1089 void TestCFour(int, int, int, int) const {};
1090 void TestCFive(int, int, int, int, int) const {};
1091 void TestCSix(int, int, int, int, int, int) const {};
1092
1093 /** @} */
1094
1095 /**
1096 * Tries to make a callback to public and protected functions of a class.
1097 * Private are not tested because, as expected, the compilation fails.
1098 */
1100 {
1104 // as expected, fails.
1105 // MakeCallback (&CallbackTestParent::PrivateParent, this);
1106 // as expected, fails.
1107 // Pointers do not carry the access restriction info, so it is forbidden
1108 // to generate a pointer to a parent's protected function, because
1109 // this could lead to un-protect them, e.g., by making it public.
1110 // MakeCallback (&CallbackTestParent::ProtectedParent, this);
1111 }
1112};
1113
1115 : TestCase("Check various MakeCallback() template functions")
1116{
1117}
1118
1119void
1121{
1122 CallbackTestClass that;
1123
1131
1139
1147
1154
1160
1166
1167 that.CheckParentalRights();
1168}
1169
1170/**
1171 * @ingroup callback-tests
1172 *
1173 * @brief The callback Test Suite.
1174 */
1176{
1177 public:
1179};
1180
1182 : TestSuite("callback", Type::UNIT)
1183{
1184 AddTestCase(new BasicCallbackTestCase, TestCase::Duration::QUICK);
1185 AddTestCase(new MakeCallbackTestCase, TestCase::Duration::QUICK);
1186 AddTestCase(new MakeBoundCallbackTestCase, TestCase::Duration::QUICK);
1187 AddTestCase(new CallbackEqualityTestCase, TestCase::Duration::QUICK);
1188 AddTestCase(new NullifyCallbackTestCase, TestCase::Duration::QUICK);
1189 AddTestCase(new MakeCallbackTemplatesTestCase, TestCase::Duration::QUICK);
1190}
1191
1192static CallbackTestSuite g_gallbackTestSuite; //!< Static variable for test initialization
static bool gBasicCallbackTest7
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest5a
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest7c
Variable to verify that a callback has been called.
int MakeBoundCallbackTarget5(int a, int b)
MakeBoundCallback 5 target function.
static bool * gMakeBoundCallbackTest2
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest6b
Variable to verify that a callback has been called.
void TestFSix(int, int, int, int, int, int)
Test function - does nothing.
int MakeCallbackTarget7(int a)
MakeCallback 7 target function.
int CallbackEqualityTarget(double a, int b)
Non-member function used to test equality of callbacks.
static int gMakeBoundCallbackTest7b
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest9a
Variable to verify that a callback has been called.
static CallbackTestSuite g_gallbackTestSuite
Static variable for test initialization.
int MakeBoundCallbackTarget8(int a, int b, int c)
MakeBoundCallback 8 target function.
static bool gMakeCallbackTest5
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest1
Variable to verify that a callback has been called.
static bool gBasicCallbackTest5
Variable to verify that a callback has been called.
void MakeCallbackTarget5()
MakeCallback 5 target function.
static bool * gMakeBoundCallbackTest3a
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest6c
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest9b
Variable to verify that a callback has been called.
int BasicCallbackTarget7(int a)
Callback 6 target function.
void MakeBoundCallbackTarget2(bool *a)
MakeBoundCallback 2 target function.
void BasicCallbackTarget5()
Callback 5 target function.
void MakeBoundCallbackTarget7(int a, int b, int c)
MakeBoundCallback 7 target function.
static int gMakeBoundCallbackTest7a
Variable to verify that a callback has been called.
void BasicCallbackTarget6(int)
Callback 6 target function.
void TestFROne(int &)
Test function - does nothing.
void TestFRTwo(int &, int &)
Test function - does nothing.
static int gMakeBoundCallbackTest4b
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest5b
Variable to verify that a callback has been called.
int MakeBoundCallbackTarget6(int a, int b, int c)
MakeBoundCallback 5 target function.
static int gMakeBoundCallbackTest6a
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest5c
Variable to verify that a callback has been called.
void TestFFive(int, int, int, int, int)
Test function - does nothing.
static bool gMakeCallbackTest7
Variable to verify that a callback has been called.
int MakeBoundCallbackTarget3(bool *a, int b)
MakeBoundCallback 3 target function.
void MakeBoundCallbackTarget4(int a, int b)
MakeBoundCallback 4 target function.
static int gMakeBoundCallbackTest3b
Variable to verify that a callback has been called.
void TestFRFive(int &, int &, int &, int &, int &)
Test function - does nothing.
void TestFFour(int, int, int, int)
Test function - does nothing.
void TestFRThree(int &, int &, int &)
Test function - does nothing.
static int gMakeBoundCallbackTest8b
Variable to verify that a callback has been called.
void TestFOne(int)
Test function - does nothing.
int MakeBoundCallbackTarget9(int a, int b, int c, int d)
MakeBoundCallback 5 target function.
void TestFThree(int, int, int)
Test function - does nothing.
static bool gBasicCallbackTest8
Variable to verify that a callback has been called.
void TestFTwo(int, int)
Test function - does nothing.
static int gMakeBoundCallbackTest9d
Variable to verify that a callback has been called.
void TestFZero()
Test function - does nothing.
static int gMakeBoundCallbackTest4a
Variable to verify that a callback has been called.
static bool gMakeCallbackTest6
Variable to verify that a callback has been called.
void TestFRSix(int &, int &, int &, int &, int &, int &)
Test function - does nothing.
void TestFRFour(int &, int &, int &, int &)
Test function - does nothing.
static int gMakeBoundCallbackTest8c
Variable to verify that a callback has been called.
void MakeCallbackTarget6(int)
MakeCallback 6 target function.
void MakeBoundCallbackTarget1(int a)
MakeBoundCallback 1 target function.
static int gMakeBoundCallbackTest9c
Variable to verify that a callback has been called.
static int gMakeBoundCallbackTest8a
Variable to verify that a callback has been called.
static bool gBasicCallbackTest6
Variable to verify that a callback has been called.
Test the basic Callback mechanism.
bool m_test2
true if Target2 has been called, false otherwise.
bool m_test3
true if Target3 has been called, false otherwise.
bool m_test1
true if Target1 has been called, false otherwise.
void Target3(double a)
Callback 3 target function.
void Target1()
Callback 1 target function.
bool m_test4
true if Target4 has been called, false otherwise.
void DoRun() override
Implementation to actually run this TestCase.
int Target2()
Callback 2 target function.
int Target4(double a, int b)
Callback 4 target function.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
Test the callback equality implementation.
void DoRun() override
Implementation to actually run this TestCase.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
int TargetMember(double a, int b)
Member function used to test equality of callbacks.
Derived class used to check the capability of callbacks to call public, protected,...
void TestSix(int, int, int, int, int, int)
Test function - does nothing.
void CheckParentalRights()
Tries to make a callback to public and protected functions of a class.
void TestCThree(int, int, int) const
Test function - does nothing.
void TestCSix(int, int, int, int, int, int) const
Test function - does nothing.
void TestZero()
Test function - does nothing.
void TestOne(int)
Test function - does nothing.
void TestThree(int, int, int)
Test function - does nothing.
void TestCFour(int, int, int, int) const
Test function - does nothing.
void TestCOne(int) const
Test function - does nothing.
void TestCFive(int, int, int, int, int) const
Test function - does nothing.
void TestFour(int, int, int, int)
Test function - does nothing.
void TestCZero() const
Test function - does nothing.
void TestCTwo(int, int) const
Test function - does nothing.
void TestFive(int, int, int, int, int)
Test function - does nothing.
void TestTwo(int, int)
Test function - does nothing.
Class used to check the capability of callbacks to call public, protected, and private functions.
void PrivateParent()
A private function.
void PublicParent()
A public function.
void ProtectedParent()
A protected function.
static void StaticProtectedParent()
A static protected function.
The callback Test Suite.
Test the MakeBoundCallback mechanism.
void DoRun() override
Implementation to actually run this TestCase.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
int BoundTarget(int a, int b, int c)
Member function to test the creation of a bound callback pointing to a member function.
Make sure that various MakeCallback template functions compile and execute; doesn't check an results ...
bool m_test1
true if Target1 has been called, false otherwise.
void DoRun() override
Implementation to actually run this TestCase.
void Target1()
Callback 1 target function.
Test the MakeCallback mechanism.
int Target4(double a, int b)
Callback 4 target function.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
bool m_test1
true if Target1 has been called, false otherwise.
void Target1()
Callback 1 target function.
bool m_test2
true if Target2 has been called, false otherwise.
bool m_test4
true if Target4 has been called, false otherwise.
int Target2()
Callback 2 target function.
void Target3(double a)
Callback 3 target function.
bool m_test3
true if Target3 has been called, false otherwise.
void DoRun() override
Implementation to actually run this TestCase.
Test the Nullify mechanism.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
void Target1()
Callback 1 target function.
bool m_test1
true if Target1 has been called, false otherwise.
void DoRun() override
Implementation to actually run this TestCase.
Callback template class.
Definition callback.h:422
bool IsEqual(const CallbackBase &other) const
Equality test.
Definition callback.h:583
void Nullify()
Discard the implementation, set it to null.
Definition callback.h:561
bool IsNull() const
Check for null implementation.
Definition callback.h:555
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition callback.h:543
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
#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:134
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684