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