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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include "ns3/callback.h"
19#include "ns3/test.h"
20
21#include <stdint.h>
22
23using namespace ns3;
24
25/**
26 * \file
27 * \ingroup callback-tests
28 * Callback test suite
29 */
30
31/**
32 * \ingroup core-tests
33 * \defgroup callback-tests Callback tests
34 */
35
36/**
37 * \ingroup callback-tests
38 *
39 * Test the basic Callback mechanism.
40 */
42{
43 public:
45
47 {
48 }
49
50 /**
51 * Callback 1 target function.
52 */
53 void Target1()
54 {
55 m_test1 = true;
56 }
57
58 /**
59 * Callback 2 target function.
60 * \return two.
61 */
62 int Target2()
63 {
64 m_test2 = true;
65 return 2;
66 }
67
68 /**
69 * Callback 3 target function.
70 * \param a A parameter (unused).
71 */
72 void Target3(double a [[maybe_unused]])
73 {
74 m_test3 = true;
75 }
76
77 /**
78 * Callback 4 target function.
79 * \param a A parameter (unused).
80 * \param b Another parameter (unused).
81 * \return four.
82 */
83 int Target4(double a [[maybe_unused]], int b [[maybe_unused]])
84 {
85 m_test4 = true;
86 return 4;
87 }
88
89 private:
90 void DoRun() override;
91 void DoSetup() override;
92
93 bool m_test1; //!< true if Target1 has been called, false otherwise.
94 bool m_test2; //!< true if Target2 has been called, false otherwise.
95 bool m_test3; //!< true if Target3 has been called, false otherwise.
96 bool m_test4; //!< true if Target4 has been called, false otherwise.
97};
98
99/**
100 * Variable to verify that a callback has been called.
101 * @{
102 */
107
108/** @} */
109
110/**
111 * Callback 5 target function.
112 */
113void
115{
116 gBasicCallbackTest5 = true;
117}
118
119/**
120 * Callback 6 target function.
121 */
122void
124{
125 gBasicCallbackTest6 = true;
126}
127
128/**
129 * Callback 6 target function.
130 * \param a The value passed by the callback.
131 * \return the value of the calling function.
132 */
133int
135{
136 gBasicCallbackTest7 = true;
137 return a;
138}
139
141 : TestCase("Check basic Callback mechanism")
142{
143}
144
145void
147{
148 m_test1 = false;
149 m_test2 = false;
150 m_test3 = false;
151 m_test4 = false;
152 gBasicCallbackTest5 = false;
153 gBasicCallbackTest6 = false;
154 gBasicCallbackTest7 = false;
155 gBasicCallbackTest8 = false;
156}
157
158void
160{
161 //
162 // Make sure we can declare and compile a Callback pointing to a member
163 // function returning void and execute it.
164 //
166 target1();
167 NS_TEST_ASSERT_MSG_EQ(m_test1, true, "Callback did not fire");
168
169 //
170 // Make sure we can declare and compile a Callback pointing to a member
171 // function that returns an int and execute it.
172 //
173 Callback<int> target2;
175 target2();
176 NS_TEST_ASSERT_MSG_EQ(m_test2, true, "Callback did not fire");
177
178 //
179 // Make sure we can declare and compile a Callback pointing to a member
180 // function that returns void, takes a double parameter, and execute it.
181 //
183 target3(0.0);
184 NS_TEST_ASSERT_MSG_EQ(m_test3, true, "Callback did not fire");
185
186 //
187 // Make sure we can declare and compile a Callback pointing to a member
188 // function that returns void, takes two parameters, and execute it.
189 //
192 target4(0.0, 1);
193 NS_TEST_ASSERT_MSG_EQ(m_test4, 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, and execute it. This is a lower level call
198 // than MakeCallback so we have got to include at least two arguments to make
199 // sure that the constructor is properly disambiguated. If the arguments are
200 // not needed, we just pass in dummy values.
201 //
203 target5();
204 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest5, true, "Callback did not fire");
205
206 //
207 // Make sure we can declare and compile a Callback pointing to a non-member
208 // function that returns void, takes one integer argument and execute it.
209 //
211 target6(1);
212 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest6, true, "Callback did not fire");
213
214 //
215 // Make sure we can declare and compile a Callback pointing to a non-member
216 // function that returns int, takes one integer argument and execute it.
217 //
219 target7(1);
220 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest7, true, "Callback did not fire");
221
222 //
223 // Make sure we can create a callback pointing to a lambda.
224 //
225 Callback<double, int> target8([](int p) {
226 gBasicCallbackTest8 = true;
227 return p / 2.;
228 });
229 target8(5);
230 NS_TEST_ASSERT_MSG_EQ(gBasicCallbackTest8, true, "Callback did not fire");
231}
232
233/**
234 * \ingroup callback-tests
235 *
236 * Test the MakeCallback mechanism.
237 */
239{
240 public:
242
244 {
245 }
246
247 /**
248 * Callback 1 target function.
249 */
250 void Target1()
251 {
252 m_test1 = true;
253 }
254
255 /**
256 * Callback 2 target function.
257 * \return two.
258 */
260 {
261 m_test2 = true;
262 return 2;
263 }
264
265 /**
266 * Callback 3 target function.
267 * \param a A parameter (unused).
268 */
269 void Target3(double a [[maybe_unused]])
270 {
271 m_test3 = true;
272 }
273
274 /**
275 * Callback 4 target function.
276 * \param a A parameter (unused).
277 * \param b Another parameter (unused).
278 * \return four.
279 */
280 int Target4(double a [[maybe_unused]], int b [[maybe_unused]])
281 {
282 m_test4 = true;
283 return 4;
284 }
285
286 private:
287 void DoRun() override;
288 void DoSetup() override;
289
290 bool m_test1; //!< true if Target1 has been called, false otherwise.
291 bool m_test2; //!< true if Target2 has been called, false otherwise.
292 bool m_test3; //!< true if Target3 has been called, false otherwise.
293 bool m_test4; //!< true if Target4 has been called, false otherwise.
294};
295
296/**
297 * Variable to verify that a callback has been called.
298 * @{
299 */
303
304/** @} */
305
306/**
307 * MakeCallback 5 target function.
308 */
309void
311{
312 gMakeCallbackTest5 = true;
313}
314
315/**
316 * MakeCallback 6 target function.
317 */
318void
320{
321 gMakeCallbackTest6 = true;
322}
323
324/**
325 * MakeCallback 7 target function.
326 * \param a The value passed by the callback.
327 * \return the value of the calling function.
328 */
329int
331{
332 gMakeCallbackTest7 = true;
333 return a;
334}
335
337 : TestCase("Check MakeCallback() mechanism")
338{
339}
340
341void
343{
344 m_test1 = false;
345 m_test2 = false;
346 m_test3 = false;
347 m_test4 = false;
348 gMakeCallbackTest5 = false;
349 gMakeCallbackTest6 = false;
350 gMakeCallbackTest7 = false;
351}
352
353void
355{
356 //
357 // Make sure we can declare and make a Callback pointing to a member
358 // function returning void and execute it.
359 //
361 target1();
362 NS_TEST_ASSERT_MSG_EQ(m_test1, true, "Callback did not fire");
363
364 //
365 // Make sure we can declare and make a Callback pointing to a member
366 // function that returns an int and execute it.
367 //
369 target2();
370 NS_TEST_ASSERT_MSG_EQ(m_test2, true, "Callback did not fire");
371
372 //
373 // Make sure we can declare and make a Callback pointing to a member
374 // function that returns void, takes a double parameter, and execute it.
375 //
377 target3(0.0);
378 NS_TEST_ASSERT_MSG_EQ(m_test3, true, "Callback did not fire");
379
380 //
381 // Make sure we can declare and make a Callback pointing to a member
382 // function that returns void, takes two parameters, and execute it.
383 //
385 target4(0.0, 1);
386 NS_TEST_ASSERT_MSG_EQ(m_test4, true, "Callback did not fire");
387
388 //
389 // Make sure we can declare and make a Callback pointing to a non-member
390 // function that returns void, and execute it. This uses a higher level call
391 // than in the basic tests so we do not need to include any dummy arguments
392 // here.
393 //
395 target5();
396 NS_TEST_ASSERT_MSG_EQ(gMakeCallbackTest5, true, "Callback did not fire");
397
398 //
399 // Make sure we can declare and compile a Callback pointing to a non-member
400 // function that returns void, takes one integer argument and execute it.
401 // This uses a higher level call than in the basic tests so we do not need to
402 // include any dummy arguments here.
403 //
405 target6(1);
406 NS_TEST_ASSERT_MSG_EQ(gMakeCallbackTest6, true, "Callback did not fire");
407
408 //
409 // Make sure we can declare and compile a Callback pointing to a non-member
410 // function that returns int, takes one integer argument and execute it.
411 // This uses a higher level call than in the basic tests so we do not need to
412 // include any dummy arguments here.
413 //
415 target7(1);
416 NS_TEST_ASSERT_MSG_EQ(gMakeCallbackTest7, true, "Callback did not fire");
417}
418
419/**
420 * \ingroup callback-tests
421 *
422 * Test the MakeBoundCallback mechanism.
423 */
425{
426 public:
428
430 {
431 }
432
433 /**
434 * Member function to test the creation of a bound callback pointing to a member function
435 *
436 * \param a first argument
437 * \param b second argument
438 * \param c third argument
439 * \return the sum of the arguments
440 */
441 int BoundTarget(int a, int b, int c)
442 {
443 return a + b + c;
444 }
445
446 private:
447 void DoRun() override;
448 void DoSetup() override;
449};
450
451/**
452 * Variable to verify that a callback has been called.
453 * @{
454 */
477
478/** @} */
479
480// Note: doxygen compounds don not work due to params / return variability.
481
482/**
483 * MakeBoundCallback 1 target function.
484 * \param a The value passed by the callback.
485 */
486void
488{
490}
491
492/**
493 * MakeBoundCallback 2 target function.
494 * \param a The value passed by the callback.
495 */
496void
498{
500}
501
502/**
503 * MakeBoundCallback 3 target function.
504 * \param a The value passed by the callback.
505 * \param b The value passed by the callback.
506 * \return the value 1234.
507 */
508int
510{
513 return 1234;
514}
515
516/**
517 * MakeBoundCallback 4 target function.
518 * \param a The value passed by the callback.
519 * \param b The value passed by the callback.
520 */
521void
523{
526}
527
528/**
529 * MakeBoundCallback 5 target function.
530 * \param a The value passed by the callback.
531 * \param b The value passed by the callback.
532 * \return the value 1234.
533 */
534int
536{
539 return 1234;
540}
541
542/**
543 * MakeBoundCallback 5 target function.
544 * \param a The value passed by the callback.
545 * \param b The value passed by the callback.
546 * \param c The value passed by the callback.
547 * \return the value 1234.
548 */
549int
550MakeBoundCallbackTarget6(int a, int b, int c)
551{
555 return 1234;
556}
557
558/**
559 * MakeBoundCallback 7 target function.
560 * \param a The value passed by the callback.
561 * \param b The value passed by the callback.
562 * \param c The value passed by the callback.
563 */
564void
565MakeBoundCallbackTarget7(int a, int b, int c)
566{
570}
571
572/**
573 * MakeBoundCallback 8 target function.
574 * \param a The value passed by the callback.
575 * \param b The value passed by the callback.
576 * \param c The value passed by the callback.
577 * \return the value 1234.
578 */
579int
580MakeBoundCallbackTarget8(int a, int b, int c)
581{
585 return 1234;
586}
587
588/**
589 * MakeBoundCallback 5 target function.
590 * \param a The value passed by the callback.
591 * \param b The value passed by the callback.
592 * \param c The value passed by the callback.
593 * \param d The value passed by the callback.
594 * \return the value 1234.
595 */
596int
597MakeBoundCallbackTarget9(int a, int b, int c, int d)
598{
603 return 1234;
604}
605
607 : TestCase("Check MakeBoundCallback() mechanism")
608{
609}
610
611void
613{
615 gMakeBoundCallbackTest2 = nullptr;
616 gMakeBoundCallbackTest3a = nullptr;
636}
637
638void
640{
641 //
642 // This is slightly tricky to explain. A bound Callback allows us to package
643 // up arguments for use later. The arguments are bound when the callback is
644 // created and the code that fires the Callback does not know they are there.
645 //
646 // Since the callback is *declared* according to the way it will be used, the
647 // arguments are not seen there. However, the target function of the callback
648 // will have the provided arguments present. The MakeBoundCallback template
649 // function is what connects the two together and where you provide the
650 // arguments to be bound.
651 //
652 // Here we declare a Callback that returns a void and takes no parameters.
653 // MakeBoundCallback connects this Callback to a target function that returns
654 // void and takes an integer argument. That integer argument is bound to the
655 // value 1234. When the Callback is fired, no integer argument is provided
656 // directly. The argument is provided by bound Callback mechanism.
657 //
659 target1();
661 1234,
662 "Callback did not fire or binding not correct");
663
664 //
665 // Make sure we can bind a pointer value (a common use case).
666 //
667 bool a;
669 target2();
671 &a,
672 "Callback did not fire or binding not correct");
673
674 //
675 // Make sure we can mix and match bound and unbound arguments. This callback
676 // returns an integer so we should see that appear.
677 //
679 int result = target3(2468);
680 NS_TEST_ASSERT_MSG_EQ(result, 1234, "Return value of callback not correct");
682 &a,
683 "Callback did not fire or binding not correct");
685 2468,
686 "Callback did not fire or argument not correct");
687
688 //
689 // Test the TwoBound variant
690 //
692 target4();
694 3456,
695 "Callback did not fire or binding not correct");
697 5678,
698 "Callback did not fire or binding not correct");
699
701 int resultTwoA = target5();
702 NS_TEST_ASSERT_MSG_EQ(resultTwoA, 1234, "Return value of callback not correct");
704 3456,
705 "Callback did not fire or binding not correct");
707 5678,
708 "Callback did not fire or binding not correct");
709
711 int resultTwoB = target6(6789);
712 NS_TEST_ASSERT_MSG_EQ(resultTwoB, 1234, "Return value of callback not correct");
714 3456,
715 "Callback did not fire or binding not correct");
717 5678,
718 "Callback did not fire or binding not correct");
720 6789,
721 "Callback did not fire or argument not correct");
722
723 //
724 // Test the ThreeBound variant
725 //
726 Callback<void> target7 = MakeBoundCallback(&MakeBoundCallbackTarget7, 2345, 3456, 4567);
727 target7();
729 2345,
730 "Callback did not fire or binding not correct");
732 3456,
733 "Callback did not fire or binding not correct");
735 4567,
736 "Callback did not fire or binding not correct");
737
738 Callback<int> target8 = MakeBoundCallback(&MakeBoundCallbackTarget8, 2345, 3456, 4567);
739 int resultThreeA = target8();
740 NS_TEST_ASSERT_MSG_EQ(resultThreeA, 1234, "Return value of callback not correct");
742 2345,
743 "Callback did not fire or binding not correct");
745 3456,
746 "Callback did not fire or binding not correct");
748 4567,
749 "Callback did not fire or binding not correct");
750
752 int resultThreeB = target9(5678);
753 NS_TEST_ASSERT_MSG_EQ(resultThreeB, 1234, "Return value of callback not correct");
755 2345,
756 "Callback did not fire or binding not correct");
758 3456,
759 "Callback did not fire or binding not correct");
761 4567,
762 "Callback did not fire or binding not correct");
764 5678,
765 "Callback did not fire or binding not correct");
766
767 //
768 // Test creating a bound callback pointing to a member function. Also, make sure that
769 // an argument can be bound to a reference.
770 //
771 int b = 1;
772 Callback<int> target10 =
773 Callback<int>(&MakeBoundCallbackTestCase::BoundTarget, this, std::ref(b), 5, 2);
774 NS_TEST_ASSERT_MSG_EQ(target10(), 8, "Bound callback returned an unexpected value");
775 b = 3;
776 NS_TEST_ASSERT_MSG_EQ(target10(), 10, "Bound callback returned an unexpected value");
777}
778
779/**
780 * \ingroup callback-tests
781 *
782 * Test the callback equality implementation.
783 */
785{
786 public:
788
790 {
791 }
792
793 /**
794 * Member function used to test equality of callbacks.
795 *
796 * \param a first argument
797 * \param b second argument
798 * \return the sum of the arguments
799 */
800 int TargetMember(double a, int b)
801 {
802 return static_cast<int>(a) + b;
803 }
804
805 private:
806 void DoRun() override;
807 void DoSetup() override;
808};
809
810/**
811 * Non-member function used to test equality of callbacks.
812 *
813 * \param a first argument
814 * \param b second argument
815 * \return the sum of the arguments
816 */
817int
819{
820 return static_cast<int>(a) + b;
821}
822
824 : TestCase("Check Callback equality test")
825{
826}
827
828void
830{
831}
832
833void
835{
836 //
837 // Make sure that two callbacks pointing to the same member function
838 // compare equal.
839 //
843 NS_TEST_ASSERT_MSG_EQ(target1a.IsEqual(target1b), true, "Equality test failed");
844
845 //
846 // Make sure that two callbacks pointing to the same member function
847 // compare equal, after binding the first argument.
848 //
850 Callback<int, int> target2b = target1b.Bind(1.5);
851 NS_TEST_ASSERT_MSG_EQ(target2a.IsEqual(target2b), true, "Equality test failed");
852
853 //
854 // Make sure that two callbacks pointing to the same member function
855 // compare equal, after binding the first two arguments.
856 //
857 Callback<int> target3a(target2a, 2);
858 Callback<int> target3b = target1b.Bind(1.5, 2);
859 NS_TEST_ASSERT_MSG_EQ(target3a.IsEqual(target3b), true, "Equality test failed");
860
861 //
862 // Make sure that two callbacks pointing to the same member function do
863 // not compare equal if they are bound to different arguments.
864 //
865 Callback<int> target3c = target1b.Bind(1.5, 3);
866 NS_TEST_ASSERT_MSG_EQ(target3c.IsEqual(target3b), false, "Equality test failed");
867
868 //
869 // Make sure that two callbacks pointing to the same non-member function
870 // compare equal.
871 //
874 NS_TEST_ASSERT_MSG_EQ(target4a.IsEqual(target4b), true, "Equality test failed");
875
876 //
877 // Make sure that two callbacks pointing to the same non-member function
878 // compare equal, after binding the first argument.
879 //
881 Callback<int, int> target5b = target4b.Bind(1.5);
882 NS_TEST_ASSERT_MSG_EQ(target5a.IsEqual(target5b), true, "Equality test failed");
883
884 //
885 // Make sure that two callbacks pointing to the same non-member function
886 // compare equal, after binding the first two arguments.
887 //
888 Callback<int> target6a(target5a, 2);
889 Callback<int> target6b = target4b.Bind(1.5, 2);
890 NS_TEST_ASSERT_MSG_EQ(target6a.IsEqual(target6b), true, "Equality test failed");
891
892 //
893 // Make sure that two callbacks pointing to the same non-member function do
894 // not compare equal if they are bound to different arguments.
895 //
896 Callback<int> target6c = target4b.Bind(1.5, 3);
897 NS_TEST_ASSERT_MSG_EQ(target6c.IsEqual(target6b), false, "Equality test failed");
898
899 //
900 // Check that we cannot compare lambdas.
901 //
902 Callback<double, int, double> target7a([](int p, double d) { return d + p / 2.; });
903 Callback<double, int, double> target7b([](int p, double d) { return d + p / 2.; });
904 NS_TEST_ASSERT_MSG_EQ(target7a.IsEqual(target7b), false, "Compared lambdas?");
905
906 //
907 // Make sure that a callback pointing to a lambda and a copy of it compare equal.
908 //
909 Callback<double, int, double> target7c(target7b);
910 NS_TEST_ASSERT_MSG_EQ(target7c.IsEqual(target7b), true, "Equality test failed");
911
912 //
913 // Make sure that a callback pointing to a lambda and a copy of it compare equal,
914 // after binding the first argument.
915 //
916 Callback<double, double> target8b = target7b.Bind(1);
917 Callback<double, double> target8c(target7c, 1);
918 NS_TEST_ASSERT_MSG_EQ(target8b.IsEqual(target8c), true, "Equality test failed");
919
920 //
921 // Make sure that a callback pointing to a lambda and a copy of it compare equal,
922 // after binding the first two arguments.
923 //
924 Callback<double> target9b = target8b.Bind(2.0);
925 Callback<double> target9c(target8c, 2.0);
926 NS_TEST_ASSERT_MSG_EQ(target9b.IsEqual(target9c), true, "Equality test failed");
927
928 //
929 // Make sure that a callback pointing to a lambda and a copy of it do not compare
930 // equal if they are bound to different arguments.
931 //
932 Callback<double> target9d = target8b.Bind(4);
933 NS_TEST_ASSERT_MSG_EQ(target9d.IsEqual(target9c), false, "Equality test failed");
934}
935
936/**
937 * \ingroup callback-tests
938 *
939 * Test the Nullify mechanism.
940 */
942{
943 public:
945
947 {
948 }
949
950 /**
951 * Callback 1 target function.
952 */
953 void Target1()
954 {
955 m_test1 = true;
956 }
957
958 private:
959 void DoRun() override;
960 void DoSetup() override;
961
962 bool m_test1; //!< true if Target1 has been called, false otherwise.
963};
964
966 : TestCase("Check Nullify() and IsNull()")
967{
968}
969
970void
972{
973 m_test1 = false;
974}
975
976void
978{
979 //
980 // Make sure we can declare and make a Callback pointing to a member
981 // function returning void and execute it.
982 //
984 target1();
985 NS_TEST_ASSERT_MSG_EQ(m_test1, true, "Callback did not fire");
986
987 NS_TEST_ASSERT_MSG_EQ(target1.IsNull(), false, "Working Callback reports IsNull()");
988
989 target1.Nullify();
990
991 NS_TEST_ASSERT_MSG_EQ(target1.IsNull(), true, "Nullified Callback reports not IsNull()");
992}
993
994/**
995 * \ingroup callback-tests
996 *
997 * Make sure that various MakeCallback template functions compile and execute;
998 * doesn't check an results of the execution.
999 */
1001{
1002 public:
1004
1006 {
1007 }
1008
1009 /**
1010 * Callback 1 target function.
1011 */
1012 void Target1()
1013 {
1014 m_test1 = true;
1015 }
1016
1017 private:
1018 void DoRun() override;
1019
1020 bool m_test1; //!< true if Target1 has been called, false otherwise.
1021};
1022
1023/**
1024 * Test function - does nothing.
1025 * @{
1026 */
1027void TestFZero(){};
1028void TestFOne(int){};
1029void TestFTwo(int, int){};
1030void TestFThree(int, int, int){};
1031void TestFFour(int, int, int, int){};
1032void TestFFive(int, int, int, int, int){};
1033void TestFSix(int, int, int, int, int, int){};
1034
1035void TestFROne(int&){};
1036void TestFRTwo(int&, int&){};
1037void TestFRThree(int&, int&, int&){};
1038void TestFRFour(int&, int&, int&, int&){};
1039void TestFRFive(int&, int&, int&, int&, int&){};
1040void TestFRSix(int&, int&, int&, int&, int&, int&){};
1041
1042/** @} */
1043
1044/**
1045 * \ingroup callback-tests
1046 *
1047 * Class used to check the capability of callbacks to call
1048 * public, protected, and private functions.
1049 */
1051{
1052 public:
1053 /// A public function.
1055 {
1056 }
1057
1058 protected:
1059 /// A protected function.
1061 {
1062 }
1063
1064 /// A static protected function.
1066 {
1067 }
1068
1069 private:
1070 /// A private function.
1072 {
1073 }
1074};
1075
1076/**
1077 * \ingroup callback-tests
1078 *
1079 * Derived class used to check the capability of callbacks to call
1080 * public, protected, and private functions.
1081 */
1083{
1084 public:
1085 /**
1086 * Test function - does nothing.
1087 * @{
1088 */
1089 void TestZero(){};
1090 void TestOne(int){};
1091 void TestTwo(int, int){};
1092 void TestThree(int, int, int){};
1093 void TestFour(int, int, int, int){};
1094 void TestFive(int, int, int, int, int){};
1095 void TestSix(int, int, int, int, int, int){};
1096 void TestCZero() const {};
1097 void TestCOne(int) const {};
1098 void TestCTwo(int, int) const {};
1099 void TestCThree(int, int, int) const {};
1100 void TestCFour(int, int, int, int) const {};
1101 void TestCFive(int, int, int, int, int) const {};
1102 void TestCSix(int, int, int, int, int, int) const {};
1103
1104 /** @} */
1105
1106 /**
1107 * Tries to make a callback to public and protected functions of a class.
1108 * Private are not tested because, as expected, the compilation fails.
1109 */
1111 {
1115 // as expected, fails.
1116 // MakeCallback (&CallbackTestParent::PrivateParent, this);
1117 // as expected, fails.
1118 // Pointers do not carry the access restriction info, so it is forbidden
1119 // to generate a pointer to a parent's protected function, because
1120 // this could lead to un-protect them, e.g., by making it public.
1121 // MakeCallback (&CallbackTestParent::ProtectedParent, this);
1122 }
1123};
1124
1126 : TestCase("Check various MakeCallback() template functions")
1127{
1128}
1129
1130void
1132{
1133 CallbackTestClass that;
1134
1142
1150
1158
1165
1171
1177
1178 that.CheckParentalRights();
1179}
1180
1181/**
1182 * \ingroup callback-tests
1183 *
1184 * \brief The callback Test Suite.
1185 */
1187{
1188 public:
1190};
1191
1193 : TestSuite("callback", Type::UNIT)
1194{
1195 AddTestCase(new BasicCallbackTestCase, TestCase::Duration::QUICK);
1196 AddTestCase(new MakeCallbackTestCase, TestCase::Duration::QUICK);
1197 AddTestCase(new MakeBoundCallbackTestCase, TestCase::Duration::QUICK);
1198 AddTestCase(new CallbackEqualityTestCase, TestCase::Duration::QUICK);
1199 AddTestCase(new NullifyCallbackTestCase, TestCase::Duration::QUICK);
1200 AddTestCase(new MakeCallbackTemplatesTestCase, TestCase::Duration::QUICK);
1201}
1202
1203static 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:438
bool IsEqual(const CallbackBase &other) const
Equality test.
Definition: callback.h:599
void Nullify()
Discard the implementation, set it to null.
Definition: callback.h:577
bool IsNull() const
Check for null implementation.
Definition: callback.h:571
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition: callback.h:559
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:767
#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:145
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:706