00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "test.h"
00022 #include "callback.h"
00023 #include <stdint.h>
00024
00025 #ifdef RUN_SELF_TESTS
00026
00027 namespace ns3 {
00028
00029 static bool gTest5 = false;
00030 static bool gTest6 = false;
00031 static bool gTest7 = false;
00032
00033 void Test5 (void)
00034 {
00035 gTest5 = true;
00036 }
00037
00038 void Test6 (int)
00039 {
00040 gTest6 = true;
00041 }
00042
00043 int Test7 (int a)
00044 {
00045 gTest7 = true;
00046 return a;
00047 }
00048
00049 void *Test9 (bool *a)
00050 {
00051 return a;
00052 }
00053
00054 void *Test10 (bool *a, int const & b)
00055 {
00056 return a;
00057 }
00058
00059 void TestFZero (void) {}
00060 void TestFOne (int) {}
00061 void TestFTwo (int, int) {}
00062 void TestFThree (int, int, int) {}
00063 void TestFFour (int, int, int, int) {}
00064 void TestFFive (int, int, int, int, int) {}
00065 void TestFSix (int, int, int, int, int, int) {}
00066
00067 void TestFROne (int &) {}
00068 void TestFRTwo (int &, int &) {}
00069 void TestFRThree (int &, int &, int &) {}
00070 void TestFRFour (int &, int &, int &, int &) {}
00071 void TestFRFive (int &, int &, int &, int &, int &) {}
00072 void TestFRSix (int &, int &, int &, int &, int &, int &) {}
00073
00074 class X : public ns3::Test
00075 {
00076 public:
00077 X () : Test ("Callback") {}
00078 void PublicParent (void) {}
00079 protected:
00080 static void StaticProtectedParent (void) {}
00081 void ProtectedParent (void) {}
00082 private:
00083 void PrivateParent (void) {}
00084 };
00085
00086 class CallbackTest : public X
00087 {
00088 private:
00089 bool m_test1;
00090 bool m_test2;
00091 bool m_test3;
00092 bool m_test4;
00093 public:
00094 CallbackTest ();
00095 virtual bool RunTests (void);
00096 void Reset (void);
00097 bool IsWrong (void);
00098 void Test1 (void);
00099 int Test2 (void);
00100 void Test3 (double a);
00101 int Test4 (double a, int b);
00102 void Test8 (Callback<void, int> callback);
00103
00104 void TestZero (void) {}
00105 void TestOne (int) {}
00106 void TestTwo (int, int) {}
00107 void TestThree (int, int, int) {}
00108 void TestFour (int, int, int, int) {}
00109 void TestFive (int, int, int, int, int) {}
00110 void TestSix (int, int, int, int, int, int) {}
00111
00112 void TestCZero (void) const {}
00113 void TestCOne (int) const {}
00114 void TestCTwo (int, int) const {}
00115 void TestCThree (int, int, int) const {}
00116 void TestCFour (int, int, int, int) const {}
00117 void TestCFive (int, int, int, int, int) const {}
00118 void TestCSix (int, int, int, int, int, int) const {}
00119 };
00120
00121 CallbackTest::CallbackTest ()
00122 : m_test1 (false),
00123 m_test2 (false),
00124 m_test3 (false),
00125 m_test4 (false)
00126 {}
00127
00128 void
00129 CallbackTest::Test1 (void)
00130 {
00131 m_test1 = true;
00132 }
00133 int
00134 CallbackTest::Test2 (void)
00135 {
00136 m_test2 = true;
00137 return 2;
00138 }
00139 void
00140 CallbackTest::Test3 (double a)
00141 {
00142 m_test3 = true;
00143 }
00144 int
00145 CallbackTest::Test4 (double a, int b)
00146 {
00147 m_test4 = true;
00148 return 4;
00149 }
00150 void
00151 CallbackTest::Test8 (Callback<void,int> callback)
00152 {
00153 callback (3);
00154 }
00155
00156 bool
00157 CallbackTest::IsWrong (void)
00158 {
00159 if (!m_test1 ||
00160 !m_test2 ||
00161 !m_test3 ||
00162 !m_test4 ||
00163 !gTest5 ||
00164 !gTest6 ||
00165 !gTest7)
00166 {
00167 return true;
00168 }
00169 return false;
00170 }
00171
00172 void
00173 CallbackTest::Reset (void)
00174 {
00175 m_test1 = false;
00176 m_test2 = false;
00177 m_test3 = false;
00178 m_test4 = false;
00179 gTest5 = false;
00180 gTest6 = false;
00181 gTest7 = false;
00182 }
00183
00184
00185 bool
00186 CallbackTest::RunTests (void)
00187 {
00188 bool ok = true;
00189
00190 typedef ns3::Callback<void> A;
00191 typedef ns3::Callback<int> B;
00192 typedef ns3::Callback<void, double> C;
00193 typedef ns3::Callback<int, double, int> D;
00194 typedef ns3::Callback<void> E;
00195 typedef ns3::Callback<void,int> F;
00196 typedef ns3::Callback<int,int> G;
00197
00198 A a0 (this, &CallbackTest::Test1);
00199 B b0;
00200 b0 = B (this, &CallbackTest::Test2);
00201 C c0 = C (this, &CallbackTest::Test3);
00202 D d0 = D (this, &CallbackTest::Test4);
00203 E e0 = E (&Test5, true, true);
00204 F f0 = F (&Test6, true, true);
00205 G g0 = G (&Test7, true, true);
00206
00207 a0 ();
00208 b0 ();
00209 c0 (0.0);
00210 d0 (0.0, 1);
00211 e0 ();
00212 f0 (1);
00213 g0 (1);
00214
00215 if (IsWrong ())
00216 {
00217 ok = false;
00218 }
00219
00220 Reset ();
00221
00222 A a1 = ns3::MakeCallback (&CallbackTest::Test1, this);
00223 B b1 = ns3::MakeCallback (&CallbackTest::Test2, this);
00224 C c1 = ns3::MakeCallback (&CallbackTest::Test3, this);
00225 D d1 = ns3::MakeCallback (&CallbackTest::Test4, this);
00226 E e1 = ns3::MakeCallback (&Test5);
00227 F f1 = ns3::MakeCallback (&Test6);
00228 G g1 = ns3::MakeCallback (&Test7);
00229
00230 a1 ();
00231 b1 ();
00232 c1 (0.0);
00233 d1 (0.0, 1);
00234 e1 ();
00235 f1 (1);
00236 g1 (2);
00237
00238 a1.Nullify ();
00239 b1.Nullify ();
00240 c1.Nullify ();
00241 d1.Nullify ();
00242 e1.Nullify ();
00243 g1.Nullify ();
00244
00245 Test8 (f1);
00246
00247 f1.Nullify ();
00248
00249 Callback<void, int64_t,int64_t> a2;
00250
00251 if (IsWrong ())
00252 {
00253 ok = false;
00254 }
00255
00256
00257 MakeBoundCallback (&Test7, 0);
00258 bool v;
00259 MakeBoundCallback (&Test9, &v);
00260 MakeBoundCallback (&Test10, &v);
00261
00262
00263 MakeCallback (&CallbackTest::TestZero, this);
00264 MakeCallback (&CallbackTest::TestOne, this);
00265 MakeCallback (&CallbackTest::TestTwo, this);
00266 MakeCallback (&CallbackTest::TestThree, this);
00267 MakeCallback (&CallbackTest::TestFour, this);
00268 MakeCallback (&CallbackTest::TestFive, this);
00269 MakeCallback (&CallbackTest::TestSix, this);
00270
00271 MakeCallback (&CallbackTest::TestCZero, this);
00272 MakeCallback (&CallbackTest::TestCOne, this);
00273 MakeCallback (&CallbackTest::TestCTwo, this);
00274 MakeCallback (&CallbackTest::TestCThree, this);
00275 MakeCallback (&CallbackTest::TestCFour, this);
00276 MakeCallback (&CallbackTest::TestCFive, this);
00277 MakeCallback (&CallbackTest::TestCSix, this);
00278
00279 MakeCallback (&TestFZero);
00280 MakeCallback (&TestFOne);
00281 MakeCallback (&TestFTwo);
00282 MakeCallback (&TestFThree);
00283 MakeCallback (&TestFFour);
00284 MakeCallback (&TestFFive);
00285 MakeCallback (&TestFSix);
00286
00287 MakeCallback (&TestFROne);
00288 MakeCallback (&TestFRTwo);
00289 MakeCallback (&TestFRThree);
00290 MakeCallback (&TestFRFour);
00291 MakeCallback (&TestFRFive);
00292 MakeCallback (&TestFRSix);
00293
00294
00295 MakeBoundCallback (&TestFOne, 1);
00296 MakeBoundCallback (&TestFTwo, 1);
00297 MakeBoundCallback (&TestFThree, 1);
00298 MakeBoundCallback (&TestFFour, 1);
00299 MakeBoundCallback (&TestFFive, 1);
00300
00301 MakeBoundCallback (&TestFROne, 1);
00302 MakeBoundCallback (&TestFRTwo, 1);
00303 MakeBoundCallback (&TestFRThree, 1);
00304 MakeBoundCallback (&TestFRFour, 1);
00305 MakeBoundCallback (&TestFRFive, 1);
00306
00307
00308 MakeCallback (&X::StaticProtectedParent);
00309 MakeCallback (&X::PublicParent, this);
00310 MakeCallback (&CallbackTest::ProtectedParent, this);
00311
00312
00313
00314
00315
00316
00317 return ok;
00318 }
00319
00320 static CallbackTest gCallbackTest;
00321
00322 };
00323
00324 #endif