A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
traced-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/test.h"
19
#include "ns3/traced-callback.h"
20
21
using namespace
ns3
;
22
39
class
BasicTracedCallbackTestCase
:
public
TestCase
40
{
41
public
:
42
BasicTracedCallbackTestCase
();
43
44
~BasicTracedCallbackTestCase
()
override
45
{
46
}
47
48
private
:
49
void
DoRun()
override
;
50
56
void
CbOne(uint8_t a,
double
b);
57
58
CallbackBase
m_cbTwo
;
59
bool
m_one
;
60
bool
m_two
;
61
};
62
63
BasicTracedCallbackTestCase::BasicTracedCallbackTestCase
()
64
:
TestCase
(
"Check basic TracedCallback operation"
)
65
{
66
}
67
68
void
69
BasicTracedCallbackTestCase::CbOne
(uint8_t
/* a */
,
double
/* b */
)
70
{
71
m_one
=
true
;
72
}
73
74
void
75
BasicTracedCallbackTestCase::DoRun
()
76
{
77
//
78
// Disconnecting callbacks from a traced callback is based on the ability to
79
// compare callbacks. Given that lambdas cannot be compared, a callback
80
// pointing to a lambda needs to be stored to allow it to be disconnected
81
// later. Here we check that it is enough to store the callback as a CallbackBase.
82
//
83
m_cbTwo
=
Callback<void, uint8_t, double>
([
this
](uint8_t,
double
) {
m_two
=
true
; });
84
85
//
86
// Create a traced callback and connect it up to our target methods. All that
87
// these methods do is to set corresponding member variables m_one and m_two.
88
//
89
TracedCallback<uint8_t, double>
trace;
90
91
//
92
// Connect both callbacks to their respective test methods. If we hit the
93
// trace, both callbacks should be called and the two variables should be set
94
// to true.
95
//
96
trace.ConnectWithoutContext(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
97
trace.ConnectWithoutContext(
m_cbTwo
);
98
m_one
=
false
;
99
m_two
=
false
;
100
trace(1, 2);
101
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
true
,
"Callback CbOne not called"
);
102
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
103
104
//
105
// If we now disconnect callback one then only callback two should be called.
106
//
107
trace.DisconnectWithoutContext(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
108
m_one
=
false
;
109
m_two
=
false
;
110
trace(1, 2);
111
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
false
,
"Callback CbOne unexpectedly called"
);
112
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
113
114
//
115
// If we now disconnect callback two then neither callback should be called.
116
//
117
trace.DisconnectWithoutContext(
m_cbTwo
);
118
m_one
=
false
;
119
m_two
=
false
;
120
trace(1, 2);
121
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
false
,
"Callback CbOne unexpectedly called"
);
122
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
false
,
"Callback CbTwo unexpectedly called"
);
123
124
//
125
// If we connect them back up, then both callbacks should be called.
126
//
127
trace.ConnectWithoutContext(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
128
trace.ConnectWithoutContext(
m_cbTwo
);
129
m_one
=
false
;
130
m_two
=
false
;
131
trace(1, 2);
132
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
true
,
"Callback CbOne not called"
);
133
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
134
}
135
141
class
TracedCallbackTestSuite
:
public
TestSuite
142
{
143
public
:
144
TracedCallbackTestSuite
();
145
};
146
147
TracedCallbackTestSuite::TracedCallbackTestSuite
()
148
:
TestSuite
(
"traced-callback"
, UNIT)
149
{
150
AddTestCase
(
new
BasicTracedCallbackTestCase
, TestCase::QUICK);
151
}
152
153
static
TracedCallbackTestSuite
154
g_tracedCallbackTestSuite
;
BasicTracedCallbackTestCase
TracedCallback Test case, check basic TracedCallback operation.
Definition:
traced-callback-test-suite.cc:40
BasicTracedCallbackTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
traced-callback-test-suite.cc:75
BasicTracedCallbackTestCase::BasicTracedCallbackTestCase
BasicTracedCallbackTestCase()
Definition:
traced-callback-test-suite.cc:63
BasicTracedCallbackTestCase::m_one
bool m_one
Variable set by the first callback.
Definition:
traced-callback-test-suite.cc:59
BasicTracedCallbackTestCase::m_cbTwo
CallbackBase m_cbTwo
second callback
Definition:
traced-callback-test-suite.cc:58
BasicTracedCallbackTestCase::CbOne
void CbOne(uint8_t a, double b)
First callback.
Definition:
traced-callback-test-suite.cc:69
BasicTracedCallbackTestCase::~BasicTracedCallbackTestCase
~BasicTracedCallbackTestCase() override
Definition:
traced-callback-test-suite.cc:44
BasicTracedCallbackTestCase::m_two
bool m_two
Variable set by the second callback.
Definition:
traced-callback-test-suite.cc:60
TracedCallbackTestSuite
The traced callback Test Suite.
Definition:
traced-callback-test-suite.cc:142
TracedCallbackTestSuite::TracedCallbackTestSuite
TracedCallbackTestSuite()
Definition:
traced-callback-test-suite.cc:147
ns3::CallbackBase
Base class for Callback class.
Definition:
callback.h:360
ns3::Callback
Callback template class.
Definition:
callback.h:438
ns3::TestCase
encapsulates test code
Definition:
test.h:1060
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:301
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1256
ns3::TracedCallback
Forward calls to a chain of Callback.
Definition:
traced-callback.h:54
NS_TEST_ASSERT_MSG_EQ
#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:144
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeCallback
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:704
g_tracedCallbackTestSuite
static TracedCallbackTestSuite g_tracedCallbackTestSuite
Static variable for test initialization.
Definition:
traced-callback-test-suite.cc:154
src
core
test
traced-callback-test-suite.cc
Generated on Sun Mar 3 2024 17:10:56 for ns-3 by
1.9.1