src/core/callback-test.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 1963 007214146da7
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2005,2006 INRIA
     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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 
    21 #include "test.h"
    22 #include "callback.h"
    23 #include <stdint.h>
    24 
    25 #ifdef RUN_SELF_TESTS
    26 
    27 namespace ns3 {
    28 
    29 static bool gTest5 = false;
    30 static bool gTest6 = false;
    31 static bool gTest7 = false;
    32 
    33 void Test5 (void)
    34 {
    35   gTest5 = true;
    36 }
    37 
    38 void Test6 (int)
    39 {
    40   gTest6 = true;
    41 }
    42 
    43 int Test7 (int a)
    44 {
    45   gTest7 = true;
    46   return a;
    47 }
    48 
    49 void *Test9 (bool *a)
    50 {
    51   return a;
    52 }
    53 
    54 void *Test10 (bool *a, int const & b)
    55 {
    56   return a;
    57 }
    58 
    59 void TestFZero (void) {}
    60 void TestFOne (int) {}
    61 void TestFTwo (int, int) {}
    62 void TestFThree (int, int, int) {}
    63 void TestFFour (int, int, int, int) {}
    64 void TestFFive (int, int, int, int, int) {}
    65 void TestFSix (int, int, int, int, int, int) {}
    66 
    67 void TestFROne (int &) {}
    68 void TestFRTwo (int &, int &) {}
    69 void TestFRThree (int &, int &, int &) {}
    70 void TestFRFour (int &, int &, int &, int &) {}
    71 void TestFRFive (int &, int &, int &, int &, int &) {}
    72 void TestFRSix (int &, int &, int &, int &, int &, int &) {}
    73 
    74 class X : public ns3::Test 
    75 {
    76 public:
    77   X () : Test ("Callback") {}
    78   void PublicParent (void) {}
    79 protected:
    80   static void StaticProtectedParent (void) {}
    81   void ProtectedParent (void) {}
    82 private:
    83   void PrivateParent (void) {}
    84 };
    85 
    86 class CallbackTest : public X 
    87 {
    88 private:
    89   bool m_test1;
    90   bool m_test2;
    91   bool m_test3;
    92   bool m_test4;
    93 public:
    94   CallbackTest ();
    95   virtual bool RunTests (void);
    96   void Reset (void);
    97   bool IsWrong (void);
    98   void Test1 (void);
    99   int Test2 (void);
   100   void Test3 (double a);
   101   int Test4 (double a, int b);
   102   void Test8 (Callback<void, int> callback);
   103 
   104   void TestZero (void) {}
   105   void TestOne (int) {}
   106   void TestTwo (int, int) {}
   107   void TestThree (int, int, int) {}
   108   void TestFour (int, int, int, int) {}
   109   void TestFive (int, int, int, int, int) {}
   110   void TestSix (int, int, int, int, int, int) {}
   111 
   112   void TestCZero (void) const {}
   113   void TestCOne (int) const {}
   114   void TestCTwo (int, int) const {}
   115   void TestCThree (int, int, int) const {}
   116   void TestCFour (int, int, int, int) const {}
   117   void TestCFive (int, int, int, int, int) const {}
   118   void TestCSix (int, int, int, int, int, int) const {}
   119 };
   120 
   121 CallbackTest::CallbackTest ()
   122   : m_test1 (false),
   123     m_test2 (false),
   124     m_test3 (false),
   125     m_test4 (false)
   126 {}
   127 
   128 void 
   129 CallbackTest::Test1 (void)
   130 {
   131   m_test1 = true;
   132 }
   133 int 
   134 CallbackTest::Test2 (void)
   135 {
   136   m_test2 = true;
   137   return 2;
   138 }
   139 void 
   140 CallbackTest::Test3 (double a)
   141 {
   142   m_test3 = true;
   143 }
   144 int 
   145 CallbackTest::Test4 (double a, int b)
   146 {
   147   m_test4 = true;
   148   return 4;
   149 }
   150 void
   151 CallbackTest::Test8 (Callback<void,int> callback)
   152 {
   153   callback (3);
   154 }
   155 
   156 bool
   157 CallbackTest::IsWrong (void)
   158 {
   159   if (!m_test1 ||
   160       !m_test2 ||
   161       !m_test3 ||
   162       !m_test4 ||
   163       !gTest5 ||
   164       !gTest6 ||
   165       !gTest7) 
   166     {
   167       return true;
   168     }
   169   return false;
   170 }
   171 
   172 void
   173 CallbackTest::Reset (void)
   174 {
   175   m_test1 = false;
   176   m_test2 = false;
   177   m_test3 = false;
   178   m_test4 = false;
   179   gTest5 = false;
   180   gTest6 = false;
   181   gTest7 = false;
   182 }
   183 
   184   
   185 bool 
   186 CallbackTest::RunTests (void)
   187 {
   188   bool ok = true;
   189 
   190   typedef ns3::Callback<void> A;
   191   typedef ns3::Callback<int> B;
   192   typedef ns3::Callback<void, double> C;
   193   typedef ns3::Callback<int, double, int> D;
   194   typedef ns3::Callback<void> E;
   195   typedef ns3::Callback<void,int> F;
   196   typedef ns3::Callback<int,int> G;
   197   
   198   A a0 (this, &CallbackTest::Test1);
   199   B b0;
   200   b0 = B (this, &CallbackTest::Test2);
   201   C c0 = C (this, &CallbackTest::Test3);
   202   D d0 = D (this, &CallbackTest::Test4);
   203   E e0 = E (&Test5, true, true);
   204   F f0 = F (&Test6, true, true);
   205   G g0 = G (&Test7, true, true);
   206 
   207   a0 ();
   208   b0 ();
   209   c0 (0.0);
   210   d0 (0.0, 1);
   211   e0 ();
   212   f0 (1);
   213   g0 (1);
   214 
   215   if (IsWrong ()) 
   216     {
   217       ok = false;
   218     }
   219 
   220   Reset ();
   221 
   222   A a1 = ns3::MakeCallback (&CallbackTest::Test1, this);
   223   B b1 = ns3::MakeCallback (&CallbackTest::Test2, this);
   224   C c1 = ns3::MakeCallback (&CallbackTest::Test3, this);
   225   D d1 = ns3::MakeCallback (&CallbackTest::Test4, this);
   226   E e1 = ns3::MakeCallback (&Test5);
   227   F f1 = ns3::MakeCallback (&Test6);
   228   G g1 = ns3::MakeCallback (&Test7);
   229   
   230   a1 ();
   231   b1 ();
   232   c1 (0.0);
   233   d1 (0.0, 1);
   234   e1 ();
   235   f1 (1);
   236   g1 (2);
   237 
   238   a1.Nullify ();
   239   b1.Nullify ();
   240   c1.Nullify ();
   241   d1.Nullify ();
   242   e1.Nullify ();
   243   g1.Nullify ();
   244 
   245   Test8 (f1);
   246 
   247   f1.Nullify ();
   248 
   249   Callback<void, int64_t,int64_t> a2;
   250 
   251   if (IsWrong ()) 
   252     {
   253       ok = false;
   254     }
   255 
   256 
   257   MakeBoundCallback (&Test7, 0);
   258   bool v;
   259   MakeBoundCallback (&Test9, &v);
   260   MakeBoundCallback (&Test10, &v);
   261 
   262 
   263   MakeCallback (&CallbackTest::TestZero, this);
   264   MakeCallback (&CallbackTest::TestOne, this);
   265   MakeCallback (&CallbackTest::TestTwo, this);
   266   MakeCallback (&CallbackTest::TestThree, this);
   267   MakeCallback (&CallbackTest::TestFour, this);
   268   MakeCallback (&CallbackTest::TestFive, this);
   269   MakeCallback (&CallbackTest::TestSix, this);
   270 
   271   MakeCallback (&CallbackTest::TestCZero, this);
   272   MakeCallback (&CallbackTest::TestCOne, this);
   273   MakeCallback (&CallbackTest::TestCTwo, this);
   274   MakeCallback (&CallbackTest::TestCThree, this);
   275   MakeCallback (&CallbackTest::TestCFour, this);
   276   MakeCallback (&CallbackTest::TestCFive, this);
   277   MakeCallback (&CallbackTest::TestCSix, this);
   278 
   279   MakeCallback (&TestFZero);
   280   MakeCallback (&TestFOne);
   281   MakeCallback (&TestFTwo);
   282   MakeCallback (&TestFThree);
   283   MakeCallback (&TestFFour);
   284   MakeCallback (&TestFFive);
   285   MakeCallback (&TestFSix);
   286 
   287   MakeCallback (&TestFROne);
   288   MakeCallback (&TestFRTwo);
   289   MakeCallback (&TestFRThree);
   290   MakeCallback (&TestFRFour);
   291   MakeCallback (&TestFRFive);
   292   MakeCallback (&TestFRSix);
   293 
   294 
   295   MakeBoundCallback (&TestFOne, 1);
   296   MakeBoundCallback (&TestFTwo, 1);
   297   MakeBoundCallback (&TestFThree, 1);
   298   MakeBoundCallback (&TestFFour, 1);
   299   MakeBoundCallback (&TestFFive, 1);
   300 
   301   MakeBoundCallback (&TestFROne, 1);
   302   MakeBoundCallback (&TestFRTwo, 1);
   303   MakeBoundCallback (&TestFRThree, 1);
   304   MakeBoundCallback (&TestFRFour, 1);
   305   MakeBoundCallback (&TestFRFive, 1);
   306 
   307 
   308   MakeCallback (&X::StaticProtectedParent);
   309   MakeCallback (&X::PublicParent, this);
   310   MakeCallback (&CallbackTest::ProtectedParent, this);
   311   // as expected, fails.
   312   //MakeCallback (&X::PrivateParent, this);
   313   // unexpected, but fails too.
   314   // It does fumble me.
   315   //MakeCallback (&X::ProtectedParent, this);
   316 
   317   return ok;
   318 }
   319 
   320 static CallbackTest gCallbackTest;
   321 
   322 }; // namespace
   323 
   324 #endif /* RUN_SELF_TESTS */