A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
traced-callback-test-suite.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2009 University of Washington
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*/
6
7
#include "ns3/test.h"
8
#include "ns3/traced-callback.h"
9
10
using namespace
ns3
;
11
12
/**
13
* \file
14
* \ingroup tracedcallback-tests
15
* TracedCallback test suite
16
*/
17
18
/**
19
* \ingroup core-tests
20
* \defgroup tracedcallback-tests TracedCallback class tests
21
*/
22
23
/**
24
* \ingroup tracedcallback-tests
25
*
26
* TracedCallback Test case, check basic TracedCallback operation.
27
*/
28
class
BasicTracedCallbackTestCase
:
public
TestCase
29
{
30
public
:
31
BasicTracedCallbackTestCase
();
32
33
~BasicTracedCallbackTestCase
()
override
34
{
35
}
36
37
private
:
38
void
DoRun
()
override
;
39
40
/**
41
* First callback.
42
* \param a First parameter.
43
* \param b Second parameter.
44
*/
45
void
CbOne
(uint8_t a,
double
b);
46
47
CallbackBase
m_cbTwo
;
//!< second callback
48
bool
m_one
;
//!< Variable set by the first callback.
49
bool
m_two
;
//!< Variable set by the second callback.
50
};
51
52
BasicTracedCallbackTestCase::BasicTracedCallbackTestCase
()
53
:
TestCase
(
"Check basic TracedCallback operation"
)
54
{
55
}
56
57
void
58
BasicTracedCallbackTestCase::CbOne
(uint8_t
/* a */
,
double
/* b */
)
59
{
60
m_one
=
true
;
61
}
62
63
void
64
BasicTracedCallbackTestCase::DoRun
()
65
{
66
//
67
// Disconnecting callbacks from a traced callback is based on the ability to
68
// compare callbacks. Given that lambdas cannot be compared, a callback
69
// pointing to a lambda needs to be stored to allow it to be disconnected
70
// later. Here we check that it is enough to store the callback as a CallbackBase.
71
//
72
m_cbTwo
=
Callback<void, uint8_t, double>
([
this
](uint8_t,
double
) {
m_two
=
true
; });
73
74
//
75
// Create a traced callback and connect it up to our target methods. All that
76
// these methods do is to set corresponding member variables m_one and m_two.
77
//
78
TracedCallback<uint8_t, double>
trace;
79
80
//
81
// Connect both callbacks to their respective test methods. If we hit the
82
// trace, both callbacks should be called and the two variables should be set
83
// to true.
84
//
85
trace.ConnectWithoutContext(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
86
trace.ConnectWithoutContext(
m_cbTwo
);
87
m_one
=
false
;
88
m_two
=
false
;
89
trace(1, 2);
90
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
true
,
"Callback CbOne not called"
);
91
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
92
93
//
94
// If we now disconnect callback one then only callback two should be called.
95
//
96
trace.DisconnectWithoutContext(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
97
m_one
=
false
;
98
m_two
=
false
;
99
trace(1, 2);
100
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
false
,
"Callback CbOne unexpectedly called"
);
101
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
102
103
//
104
// If we now disconnect callback two then neither callback should be called.
105
//
106
trace.DisconnectWithoutContext(
m_cbTwo
);
107
m_one
=
false
;
108
m_two
=
false
;
109
trace(1, 2);
110
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
false
,
"Callback CbOne unexpectedly called"
);
111
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
false
,
"Callback CbTwo unexpectedly called"
);
112
113
//
114
// If we connect them back up, then both callbacks should be called.
115
//
116
trace.ConnectWithoutContext(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
117
trace.ConnectWithoutContext(
m_cbTwo
);
118
m_one
=
false
;
119
m_two
=
false
;
120
trace(1, 2);
121
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
true
,
"Callback CbOne not called"
);
122
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
123
}
124
125
/**
126
* \ingroup tracedcallback-tests
127
*
128
* \brief The traced callback Test Suite.
129
*/
130
class
TracedCallbackTestSuite
:
public
TestSuite
131
{
132
public
:
133
TracedCallbackTestSuite
();
134
};
135
136
TracedCallbackTestSuite::TracedCallbackTestSuite
()
137
:
TestSuite
(
"traced-callback"
,
Type
::UNIT)
138
{
139
AddTestCase
(
new
BasicTracedCallbackTestCase
, TestCase::Duration::QUICK);
140
}
141
142
static
TracedCallbackTestSuite
143
g_tracedCallbackTestSuite
;
//!< Static variable for test initialization
BasicTracedCallbackTestCase
TracedCallback Test case, check basic TracedCallback operation.
Definition
traced-callback-test-suite.cc:29
BasicTracedCallbackTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
traced-callback-test-suite.cc:64
BasicTracedCallbackTestCase::BasicTracedCallbackTestCase
BasicTracedCallbackTestCase()
Definition
traced-callback-test-suite.cc:52
BasicTracedCallbackTestCase::m_one
bool m_one
Variable set by the first callback.
Definition
traced-callback-test-suite.cc:48
BasicTracedCallbackTestCase::m_cbTwo
CallbackBase m_cbTwo
second callback
Definition
traced-callback-test-suite.cc:47
BasicTracedCallbackTestCase::CbOne
void CbOne(uint8_t a, double b)
First callback.
Definition
traced-callback-test-suite.cc:58
BasicTracedCallbackTestCase::~BasicTracedCallbackTestCase
~BasicTracedCallbackTestCase() override
Definition
traced-callback-test-suite.cc:33
BasicTracedCallbackTestCase::m_two
bool m_two
Variable set by the second callback.
Definition
traced-callback-test-suite.cc:49
TracedCallbackTestSuite
The traced callback Test Suite.
Definition
traced-callback-test-suite.cc:131
TracedCallbackTestSuite::TracedCallbackTestSuite
TracedCallbackTestSuite()
Definition
traced-callback-test-suite.cc:136
ns3::CallbackBase
Base class for Callback class.
Definition
callback.h:344
ns3::Callback
Callback template class.
Definition
callback.h:422
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
ns3::TracedCallback
Forward calls to a chain of Callback.
Definition
traced-callback.h:43
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:134
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:684
g_tracedCallbackTestSuite
static TracedCallbackTestSuite g_tracedCallbackTestSuite
Static variable for test initialization.
Definition
traced-callback-test-suite.cc:143
src
core
test
traced-callback-test-suite.cc
Generated on Fri Nov 8 2024 13:59:00 for ns-3 by
1.11.0