A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
callback-test-suite.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2009 University of Washington
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
19
#include "ns3/test.h"
20
#include "ns3/callback.h"
21
#include <stdint.h>
22
23
using namespace
ns3;
24
25
// ===========================================================================
26
// Test the basic Callback mechanism
27
// ===========================================================================
28
class
BasicCallbackTestCase
:
public
TestCase
29
{
30
public
:
31
BasicCallbackTestCase
();
32
virtual
~BasicCallbackTestCase
() {}
33
34
void
Target1
(
void
) { m_test1 =
true
; }
35
int
Target2
(
void
) { m_test2 =
true
;
return
2; }
36
void
Target3
(
double
a) { m_test3 =
true
; }
37
int
Target4
(
double
a,
int
b) { m_test4 =
true
;
return
4; }
38
39
private
:
40
virtual
void
DoRun (
void
);
41
virtual
void
DoSetup (
void
);
42
43
bool
m_test1
;
44
bool
m_test2
;
45
bool
m_test3
;
46
bool
m_test4
;
47
};
48
49
static
bool
gBasicCallbackTest5
;
50
static
bool
gBasicCallbackTest6
;
51
static
bool
gBasicCallbackTest7
;
52
53
void
54
BasicCallbackTarget5
(
void
)
55
{
56
gBasicCallbackTest5
=
true
;
57
}
58
59
void
60
BasicCallbackTarget6
(
int
)
61
{
62
gBasicCallbackTest6
=
true
;
63
}
64
65
int
66
BasicCallbackTarget7
(
int
a)
67
{
68
gBasicCallbackTest7
=
true
;
69
return
a;
70
}
71
72
BasicCallbackTestCase::BasicCallbackTestCase
()
73
:
TestCase
(
"Check basic Callback mechansim"
)
74
{
75
}
76
77
void
78
BasicCallbackTestCase::DoSetup
(
void
)
79
{
80
m_test1
=
false
;
81
m_test2
=
false
;
82
m_test3
=
false
;
83
m_test4
=
false
;
84
gBasicCallbackTest5
=
false
;
85
gBasicCallbackTest6
=
false
;
86
gBasicCallbackTest7
=
false
;
87
}
88
89
void
90
BasicCallbackTestCase::DoRun
(
void
)
91
{
92
//
93
// Make sure we can declare and compile a Callback pointing to a member
94
// function returning void and execute it.
95
//
96
Callback<void>
target1 (
this
, &
BasicCallbackTestCase::Target1
);
97
target1 ();
98
NS_TEST_ASSERT_MSG_EQ
(
m_test1
,
true
,
"Callback did not fire"
);
99
100
//
101
// Make sure we can declare and compile a Callback pointing to a member
102
// function that returns an int and execute it.
103
//
104
Callback<int>
target2;
105
target2 =
Callback<int>
(
this
, &
BasicCallbackTestCase::Target2
);
106
target2 ();
107
NS_TEST_ASSERT_MSG_EQ
(
m_test2
,
true
,
"Callback did not fire"
);
108
109
//
110
// Make sure we can declare and compile a Callback pointing to a member
111
// function that returns void, takes a double parameter, and execute it.
112
//
113
Callback<void, double>
target3 =
Callback<void, double>
(
this
, &
BasicCallbackTestCase::Target3
);
114
target3 (0.0);
115
NS_TEST_ASSERT_MSG_EQ
(
m_test3
,
true
,
"Callback did not fire"
);
116
117
//
118
// Make sure we can declare and compile a Callback pointing to a member
119
// function that returns void, takes two parameters, and execute it.
120
//
121
Callback<int, double, int>
target4 =
Callback<int, double, int>
(
this
, &
BasicCallbackTestCase::Target4
);
122
target4 (0.0, 1);
123
NS_TEST_ASSERT_MSG_EQ
(
m_test4
,
true
,
"Callback did not fire"
);
124
125
//
126
// Make sure we can declare and compile a Callback pointing to a non-member
127
// function that returns void, and execute it. This is a lower level call
128
// than MakeCallback so we have got to include at least two arguments to make
129
// sure that the constructor is properly disambiguated. If the arguments are
130
// not needed, we just pass in dummy values.
131
//
132
Callback<void>
target5 =
Callback<void>
(&
BasicCallbackTarget5
,
true
,
true
);
133
target5 ();
134
NS_TEST_ASSERT_MSG_EQ
(
gBasicCallbackTest5
,
true
,
"Callback did not fire"
);
135
136
//
137
// Make sure we can declare and compile a Callback pointing to a non-member
138
// function that returns void, takes one integer argument and execute it.
139
// We also need to provide two dummy arguments to the constructor here.
140
//
141
Callback<void, int>
target6 =
Callback<void, int>
(&
BasicCallbackTarget6
,
true
,
true
);
142
target6 (1);
143
NS_TEST_ASSERT_MSG_EQ
(
gBasicCallbackTest6
,
true
,
"Callback did not fire"
);
144
145
//
146
// Make sure we can declare and compile a Callback pointing to a non-member
147
// function that returns int, takes one integer argument and execute it.
148
// We also need to provide two dummy arguments to the constructor here.
149
//
150
Callback<int, int>
target7 =
Callback<int, int>
(&
BasicCallbackTarget7
,
true
,
true
);
151
target7 (1);
152
NS_TEST_ASSERT_MSG_EQ
(
gBasicCallbackTest7
,
true
,
"Callback did not fire"
);
153
}
154
155
// ===========================================================================
156
// Test the MakeCallback mechanism
157
// ===========================================================================
158
class
MakeCallbackTestCase
:
public
TestCase
159
{
160
public
:
161
MakeCallbackTestCase
();
162
virtual
~MakeCallbackTestCase
() {}
163
164
void
Target1
(
void
) {
m_test1
=
true
; }
165
int
Target2
(
void
) {
m_test2
=
true
;
return
2; }
166
void
Target3
(
double
a) {
m_test3
=
true
; }
167
int
Target4
(
double
a,
int
b) {
m_test4
=
true
;
return
4; }
168
169
private
:
170
virtual
void
DoRun
(
void
);
171
virtual
void
DoSetup
(
void
);
172
173
bool
m_test1
;
174
bool
m_test2
;
175
bool
m_test3
;
176
bool
m_test4
;
177
};
178
179
static
bool
gMakeCallbackTest5
;
180
static
bool
gMakeCallbackTest6
;
181
static
bool
gMakeCallbackTest7
;
182
183
void
184
MakeCallbackTarget5
(
void
)
185
{
186
gMakeCallbackTest5
=
true
;
187
}
188
189
void
190
MakeCallbackTarget6
(
int
)
191
{
192
gMakeCallbackTest6
=
true
;
193
}
194
195
int
196
MakeCallbackTarget7
(
int
a)
197
{
198
gMakeCallbackTest7
=
true
;
199
return
a;
200
}
201
202
MakeCallbackTestCase::MakeCallbackTestCase
()
203
:
TestCase
(
"Check MakeCallback() mechanism"
)
204
{
205
}
206
207
void
208
MakeCallbackTestCase::DoSetup
(
void
)
209
{
210
m_test1
=
false
;
211
m_test2
=
false
;
212
m_test3
=
false
;
213
m_test4
=
false
;
214
gMakeCallbackTest5
=
false
;
215
gMakeCallbackTest6
=
false
;
216
gMakeCallbackTest7
=
false
;
217
}
218
219
void
220
MakeCallbackTestCase::DoRun
(
void
)
221
{
222
//
223
// Make sure we can declare and make a Callback pointing to a member
224
// function returning void and execute it.
225
//
226
Callback<void>
target1 =
MakeCallback
(&
MakeCallbackTestCase::Target1
,
this
);
227
target1 ();
228
NS_TEST_ASSERT_MSG_EQ
(
m_test1
,
true
,
"Callback did not fire"
);
229
230
//
231
// Make sure we can declare and make a Callback pointing to a member
232
// function that returns an int and execute it.
233
//
234
Callback<int>
target2 =
MakeCallback
(&
MakeCallbackTestCase::Target2
,
this
);
235
target2 ();
236
NS_TEST_ASSERT_MSG_EQ
(
m_test2
,
true
,
"Callback did not fire"
);
237
238
//
239
// Make sure we can declare and make a Callback pointing to a member
240
// function that returns void, takes a double parameter, and execute it.
241
//
242
Callback<void, double>
target3 =
MakeCallback
(&
MakeCallbackTestCase::Target3
,
this
);
243
target3 (0.0);
244
NS_TEST_ASSERT_MSG_EQ
(
m_test3
,
true
,
"Callback did not fire"
);
245
246
//
247
// Make sure we can declare and make a Callback pointing to a member
248
// function that returns void, takes two parameters, and execute it.
249
//
250
Callback<int, double, int>
target4 =
MakeCallback
(&
MakeCallbackTestCase::Target4
,
this
);
251
target4 (0.0, 1);
252
NS_TEST_ASSERT_MSG_EQ
(
m_test4
,
true
,
"Callback did not fire"
);
253
254
//
255
// Make sure we can declare and make a Callback pointing to a non-member
256
// function that returns void, and execute it. This uses a higher level call
257
// than in the basic tests so we do not need to include any dummy arguments
258
// here.
259
//
260
Callback<void>
target5 =
MakeCallback
(&
MakeCallbackTarget5
);
261
target5 ();
262
NS_TEST_ASSERT_MSG_EQ
(
gMakeCallbackTest5
,
true
,
"Callback did not fire"
);
263
264
//
265
// Make sure we can declare and compile a Callback pointing to a non-member
266
// function that returns void, takes one integer argument and execute it.
267
// This uses a higher level call than in the basic tests so we do not need to
268
// include any dummy arguments here.
269
//
270
Callback<void, int>
target6 =
MakeCallback
(&
MakeCallbackTarget6
);
271
target6 (1);
272
NS_TEST_ASSERT_MSG_EQ
(
gMakeCallbackTest6
,
true
,
"Callback did not fire"
);
273
274
//
275
// Make sure we can declare and compile a Callback pointing to a non-member
276
// function that returns int, takes one integer argument and execute it.
277
// This uses a higher level call than in the basic tests so we do not need to
278
// include any dummy arguments here.
279
//
280
Callback<int, int>
target7 =
MakeCallback
(&
MakeCallbackTarget7
);
281
target7 (1);
282
NS_TEST_ASSERT_MSG_EQ
(
gMakeCallbackTest7
,
true
,
"Callback did not fire"
);
283
}
284
285
// ===========================================================================
286
// Test the MakeBoundCallback mechanism
287
// ===========================================================================
288
class
MakeBoundCallbackTestCase
:
public
TestCase
289
{
290
public
:
291
MakeBoundCallbackTestCase
();
292
virtual
~MakeBoundCallbackTestCase
() {}
293
294
private
:
295
virtual
void
DoRun
(
void
);
296
virtual
void
DoSetup
(
void
);
297
};
298
299
static
int
gMakeBoundCallbackTest1
;
300
static
bool
*
gMakeBoundCallbackTest2
;
301
static
bool
*
gMakeBoundCallbackTest3a
;
302
static
int
gMakeBoundCallbackTest3b
;
303
304
void
305
MakeBoundCallbackTarget1
(
int
a)
306
{
307
gMakeBoundCallbackTest1
= a;
308
}
309
310
void
311
MakeBoundCallbackTarget2
(
bool
*a)
312
{
313
gMakeBoundCallbackTest2
= a;
314
}
315
316
int
317
MakeBoundCallbackTarget3
(
bool
*a,
int
b)
318
{
319
gMakeBoundCallbackTest3a
= a;
320
gMakeBoundCallbackTest3b
= b;
321
return
1234;
322
}
323
324
MakeBoundCallbackTestCase::MakeBoundCallbackTestCase
()
325
:
TestCase
(
"Check MakeBoundCallback() mechanism"
)
326
{
327
}
328
329
void
330
MakeBoundCallbackTestCase::DoSetup
(
void
)
331
{
332
gMakeBoundCallbackTest1
= 0;
333
gMakeBoundCallbackTest2
= 0;
334
gMakeBoundCallbackTest3a
= 0;
335
gMakeBoundCallbackTest3b
= 0;
336
}
337
338
void
339
MakeBoundCallbackTestCase::DoRun
(
void
)
340
{
341
//
342
// This is slightly tricky to explain. A bound Callback allows us to package
343
// up arguments for use later. The arguments are bound when the callback is
344
// created and the code that fires the Callback does not know they are there.
345
//
346
// Since the callback is *declared* according to the way it will be used, the
347
// arguments are not seen there. However, the target function of the callback
348
// will have the provided arguments present. The MakeBoundCallback template
349
// function is what connects the two together and where you provide the
350
// arguments to be bound.
351
//
352
// Here we declare a Callback that returns a void and takes no parameters.
353
// MakeBoundCallback connects this Callback to a target function that returns
354
// void and takes an integer argument. That integer argument is bound to the
355
// value 1234. When the Callback is fired, no integer argument is provided
356
// directly. The argument is provided by bound Callback mechanism.
357
//
358
Callback<void>
target1 =
MakeBoundCallback
(&
MakeBoundCallbackTarget1
, 1234);
359
target1 ();
360
NS_TEST_ASSERT_MSG_EQ
(
gMakeBoundCallbackTest1
, 1234,
"Callback did not fire or binding not correct"
);
361
362
//
363
// Make sure we can bind a pointer value (a common use case).
364
//
365
bool
a;
366
Callback<void>
target2 =
MakeBoundCallback
(&
MakeBoundCallbackTarget2
, &a);
367
target2 ();
368
NS_TEST_ASSERT_MSG_EQ
(
gMakeBoundCallbackTest2
, &a,
"Callback did not fire or binding not correct"
);
369
370
//
371
// Make sure we can mix and match bound and unbound arguments. This callback
372
// returns an integer so we should see that appear.
373
//
374
Callback<int, int>
target3 =
MakeBoundCallback
(&
MakeBoundCallbackTarget3
, &a);
375
int
result = target3 (2468);
376
NS_TEST_ASSERT_MSG_EQ
(result, 1234,
"Return value of callback not correct"
);
377
NS_TEST_ASSERT_MSG_EQ
(
gMakeBoundCallbackTest3a
, &a,
"Callback did not fire or binding not correct"
);
378
NS_TEST_ASSERT_MSG_EQ
(
gMakeBoundCallbackTest3b
, 2468,
"Callback did not fire or argument not correct"
);
379
}
380
381
// ===========================================================================
382
// Test the Nullify mechanism
383
// ===========================================================================
384
class
NullifyCallbackTestCase
:
public
TestCase
385
{
386
public
:
387
NullifyCallbackTestCase
();
388
virtual
~NullifyCallbackTestCase
() {}
389
390
void
Target1
(
void
) {
m_test1
=
true
; }
391
392
private
:
393
virtual
void
DoRun
(
void
);
394
virtual
void
DoSetup
(
void
);
395
396
bool
m_test1
;
397
};
398
399
NullifyCallbackTestCase::NullifyCallbackTestCase
()
400
:
TestCase
(
"Check Nullify() and IsNull()"
)
401
{
402
}
403
404
void
405
NullifyCallbackTestCase::DoSetup
(
void
)
406
{
407
m_test1
=
false
;
408
}
409
410
void
411
NullifyCallbackTestCase::DoRun
(
void
)
412
{
413
//
414
// Make sure we can declare and make a Callback pointing to a member
415
// function returning void and execute it.
416
//
417
Callback<void>
target1 =
MakeCallback
(&
NullifyCallbackTestCase::Target1
,
this
);
418
target1 ();
419
NS_TEST_ASSERT_MSG_EQ
(
m_test1
,
true
,
"Callback did not fire"
);
420
421
NS_TEST_ASSERT_MSG_EQ
(target1.
IsNull
(),
false
,
"Working Callback reports IsNull()"
);
422
423
target1.
Nullify
();
424
425
NS_TEST_ASSERT_MSG_EQ
(target1.
IsNull
(),
true
,
"Nullified Callback reports not IsNull()"
);
426
}
427
428
// ===========================================================================
429
// Make sure that various MakeCallback template functions compile and execute.
430
// Doesn't check an results of the execution.
431
// ===========================================================================
432
class
MakeCallbackTemplatesTestCase
:
public
TestCase
433
{
434
public
:
435
MakeCallbackTemplatesTestCase
();
436
virtual
~MakeCallbackTemplatesTestCase
() {}
437
438
void
Target1
(
void
) {
m_test1
=
true
; }
439
440
private
:
441
virtual
void
DoRun
(
void
);
442
443
bool
m_test1
;
444
};
445
446
void
TestFZero
(
void
) {}
447
void
TestFOne
(
int
) {}
448
void
TestFTwo
(
int
,
int
) {}
449
void
TestFThree
(
int
,
int
,
int
) {}
450
void
TestFFour
(
int
,
int
,
int
,
int
) {}
451
void
TestFFive
(
int
,
int
,
int
,
int
,
int
) {}
452
void
TestFSix
(
int
,
int
,
int
,
int
,
int
,
int
) {}
453
454
void
TestFROne
(
int
&) {}
455
void
TestFRTwo
(
int
&,
int
&) {}
456
void
TestFRThree
(
int
&,
int
&,
int
&) {}
457
void
TestFRFour
(
int
&,
int
&,
int
&,
int
&) {}
458
void
TestFRFive
(
int
&,
int
&,
int
&,
int
&,
int
&) {}
459
void
TestFRSix
(
int
&,
int
&,
int
&,
int
&,
int
&,
int
&) {}
460
461
class
CallbackTestParent
462
{
463
public
:
464
void
PublicParent
(
void
) {}
465
protected
:
466
void
ProtectedParent
(
void
) {}
467
static
void
StaticProtectedParent
(
void
) {}
468
private
:
469
void
PrivateParent
(
void
) {}
470
};
471
472
class
CallbackTestClass
:
public
CallbackTestParent
473
{
474
public
:
475
void
TestZero
(
void
) {}
476
void
TestOne
(
int
) {}
477
void
TestTwo
(
int
,
int
) {}
478
void
TestThree
(
int
,
int
,
int
) {}
479
void
TestFour
(
int
,
int
,
int
,
int
) {}
480
void
TestFive
(
int
,
int
,
int
,
int
,
int
) {}
481
void
TestSix
(
int
,
int
,
int
,
int
,
int
,
int
) {}
482
void
TestCZero
(
void
)
const
{}
483
void
TestCOne
(
int
)
const
{}
484
void
TestCTwo
(
int
,
int
)
const
{}
485
void
TestCThree
(
int
,
int
,
int
)
const
{}
486
void
TestCFour
(
int
,
int
,
int
,
int
)
const
{}
487
void
TestCFive
(
int
,
int
,
int
,
int
,
int
)
const
{}
488
void
TestCSix
(
int
,
int
,
int
,
int
,
int
,
int
)
const
{}
489
490
void
CheckParentalRights
(
void
)
491
{
492
MakeCallback
(&
CallbackTestParent::StaticProtectedParent
);
493
MakeCallback
(&
CallbackTestParent::PublicParent
,
this
);
494
MakeCallback
(&
CallbackTestClass::ProtectedParent
,
this
);
495
// as expected, fails.
496
// MakeCallback (&CallbackTestParent::PrivateParent, this);
497
// unexpected, but fails too. It does fumble me.
498
// MakeCallback (&CallbackTestParent::ProtectedParent, this);
499
}
500
501
};
502
503
MakeCallbackTemplatesTestCase::MakeCallbackTemplatesTestCase
()
504
:
TestCase
(
"Check various MakeCallback() template functions"
)
505
{
506
}
507
508
void
509
MakeCallbackTemplatesTestCase::DoRun
(
void
)
510
{
511
CallbackTestClass
that;
512
513
MakeCallback
(&
CallbackTestClass::TestZero
, &that);
514
MakeCallback
(&
CallbackTestClass::TestOne
, &that);
515
MakeCallback
(&
CallbackTestClass::TestTwo
, &that);
516
MakeCallback
(&
CallbackTestClass::TestThree
, &that);
517
MakeCallback
(&
CallbackTestClass::TestFour
, &that);
518
MakeCallback
(&
CallbackTestClass::TestFive
, &that);
519
MakeCallback
(&
CallbackTestClass::TestSix
, &that);
520
521
MakeCallback
(&
CallbackTestClass::TestCZero
, &that);
522
MakeCallback
(&
CallbackTestClass::TestCOne
, &that);
523
MakeCallback
(&
CallbackTestClass::TestCTwo
, &that);
524
MakeCallback
(&
CallbackTestClass::TestCThree
, &that);
525
MakeCallback
(&
CallbackTestClass::TestCFour
, &that);
526
MakeCallback
(&
CallbackTestClass::TestCFive
, &that);
527
MakeCallback
(&
CallbackTestClass::TestCSix
, &that);
528
529
MakeCallback
(&
TestFZero
);
530
MakeCallback
(&
TestFOne
);
531
MakeCallback
(&
TestFTwo
);
532
MakeCallback
(&
TestFThree
);
533
MakeCallback
(&
TestFFour
);
534
MakeCallback
(&
TestFFive
);
535
MakeCallback
(&
TestFSix
);
536
537
MakeCallback
(&
TestFROne
);
538
MakeCallback
(&
TestFRTwo
);
539
MakeCallback
(&
TestFRThree
);
540
MakeCallback
(&
TestFRFour
);
541
MakeCallback
(&
TestFRFive
);
542
MakeCallback
(&
TestFRSix
);
543
544
MakeBoundCallback
(&
TestFOne
, 1);
545
MakeBoundCallback
(&
TestFTwo
, 1);
546
MakeBoundCallback
(&
TestFThree
, 1);
547
MakeBoundCallback
(&
TestFFour
, 1);
548
MakeBoundCallback
(&
TestFFive
, 1);
549
550
MakeBoundCallback
(&
TestFROne
, 1);
551
MakeBoundCallback
(&
TestFRTwo
, 1);
552
MakeBoundCallback
(&
TestFRThree
, 1);
553
MakeBoundCallback
(&
TestFRFour
, 1);
554
MakeBoundCallback
(&
TestFRFive
, 1);
555
556
that.
CheckParentalRights
();
557
}
558
559
// ===========================================================================
560
// The Test Suite that glues all of the Test Cases together.
561
// ===========================================================================
562
class
CallbackTestSuite
:
public
TestSuite
563
{
564
public
:
565
CallbackTestSuite
();
566
};
567
568
CallbackTestSuite::CallbackTestSuite
()
569
:
TestSuite
(
"callback"
, UNIT)
570
{
571
AddTestCase
(
new
BasicCallbackTestCase
, TestCase::QUICK);
572
AddTestCase
(
new
MakeCallbackTestCase
, TestCase::QUICK);
573
AddTestCase
(
new
MakeBoundCallbackTestCase
, TestCase::QUICK);
574
AddTestCase
(
new
NullifyCallbackTestCase
, TestCase::QUICK);
575
AddTestCase
(
new
MakeCallbackTemplatesTestCase
, TestCase::QUICK);
576
}
577
578
static
CallbackTestSuite
CallbackTestSuite
;
src
core
test
callback-test-suite.cc
Generated on Tue May 14 2013 11:08:19 for ns-3 by
1.8.1.2