A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
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
/* -*- 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/traced-callback.h"
21
22
using namespace
ns3
;
23
40
class
BasicTracedCallbackTestCase
:
public
TestCase
41
{
42
public
:
43
BasicTracedCallbackTestCase
();
44
virtual
~BasicTracedCallbackTestCase
()
45
{}
46
47
private
:
48
virtual
void
DoRun (
void
);
49
55
void
CbOne (uint8_t a,
double
b);
61
void
CbTwo (uint8_t a,
double
b);
62
63
bool
m_one
;
64
bool
m_two
;
65
};
66
67
BasicTracedCallbackTestCase::BasicTracedCallbackTestCase
()
68
:
TestCase
(
"Check basic TracedCallback operation"
)
69
{}
70
71
void
72
BasicTracedCallbackTestCase::CbOne
([[maybe_unused]] uint8_t a, [[maybe_unused]]
double
b)
73
{
74
m_one
=
true
;
75
}
76
77
void
78
BasicTracedCallbackTestCase::CbTwo
([[maybe_unused]] uint8_t a, [[maybe_unused]]
double
b)
79
{
80
m_two
=
true
;
81
}
82
83
void
84
BasicTracedCallbackTestCase::DoRun
(
void
)
85
{
86
//
87
// Create a traced callback and connect it up to our target methods. All that
88
// these methods do is to set corresponding member variables m_one and m_two.
89
//
90
TracedCallback<uint8_t, double>
trace;
91
92
//
93
// Connect both callbacks to their respective test methods. If we hit the
94
// trace, both callbacks should be called and the two variables should be set
95
// to true.
96
//
97
trace.
ConnectWithoutContext
(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
98
trace.
ConnectWithoutContext
(
MakeCallback
(&
BasicTracedCallbackTestCase::CbTwo
,
this
));
99
m_one
=
false
;
100
m_two
=
false
;
101
trace (1, 2);
102
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
true
,
"Callback CbOne not called"
);
103
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
104
105
//
106
// If we now disconnect callback one then only callback two should be called.
107
//
108
trace.
DisconnectWithoutContext
(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
109
m_one
=
false
;
110
m_two
=
false
;
111
trace (1, 2);
112
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
false
,
"Callback CbOne unexpectedly called"
);
113
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
114
115
//
116
// If we now disconnect callback two then neither callback should be called.
117
//
118
trace.
DisconnectWithoutContext
(
MakeCallback
(&
BasicTracedCallbackTestCase::CbTwo
,
this
));
119
m_one
=
false
;
120
m_two
=
false
;
121
trace (1, 2);
122
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
false
,
"Callback CbOne unexpectedly called"
);
123
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
false
,
"Callback CbTwo unexpectedly called"
);
124
125
//
126
// If we connect them back up, then both callbacks should be called.
127
//
128
trace.
ConnectWithoutContext
(
MakeCallback
(&
BasicTracedCallbackTestCase::CbOne
,
this
));
129
trace.
ConnectWithoutContext
(
MakeCallback
(&
BasicTracedCallbackTestCase::CbTwo
,
this
));
130
m_one
=
false
;
131
m_two
=
false
;
132
trace (1, 2);
133
NS_TEST_ASSERT_MSG_EQ
(
m_one
,
true
,
"Callback CbOne not called"
);
134
NS_TEST_ASSERT_MSG_EQ
(
m_two
,
true
,
"Callback CbTwo not called"
);
135
}
136
142
class
TracedCallbackTestSuite
:
public
TestSuite
143
{
144
public
:
145
TracedCallbackTestSuite
();
146
};
147
148
TracedCallbackTestSuite::TracedCallbackTestSuite
()
149
:
TestSuite
(
"traced-callback"
, UNIT)
150
{
151
AddTestCase
(
new
BasicTracedCallbackTestCase
, TestCase::QUICK);
152
}
153
154
static
TracedCallbackTestSuite
g_tracedCallbackTestSuite
;
BasicTracedCallbackTestCase
TracedCallback Test case, check basic TracedCallback operation.
Definition:
traced-callback-test-suite.cc:41
BasicTracedCallbackTestCase::BasicTracedCallbackTestCase
BasicTracedCallbackTestCase()
Definition:
traced-callback-test-suite.cc:67
BasicTracedCallbackTestCase::m_one
bool m_one
Variable set by the first callback.
Definition:
traced-callback-test-suite.cc:63
BasicTracedCallbackTestCase::~BasicTracedCallbackTestCase
virtual ~BasicTracedCallbackTestCase()
Definition:
traced-callback-test-suite.cc:44
BasicTracedCallbackTestCase::CbOne
void CbOne(uint8_t a, double b)
First callback.
Definition:
traced-callback-test-suite.cc:72
BasicTracedCallbackTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
traced-callback-test-suite.cc:84
BasicTracedCallbackTestCase::CbTwo
void CbTwo(uint8_t a, double b)
Second callback.
Definition:
traced-callback-test-suite.cc:78
BasicTracedCallbackTestCase::m_two
bool m_two
Variable set by the second callback.
Definition:
traced-callback-test-suite.cc:64
TracedCallbackTestSuite
The traced callback Test Suite.
Definition:
traced-callback-test-suite.cc:143
TracedCallbackTestSuite::TracedCallbackTestSuite
TracedCallbackTestSuite()
Definition:
traced-callback-test-suite.cc:148
ns3::TestCase
encapsulates test code
Definition:
test.h:994
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1188
ns3::TracedCallback
Forward calls to a chain of Callback.
Definition:
traced-callback.h:53
ns3::TracedCallback::ConnectWithoutContext
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Definition:
traced-callback.h:135
ns3::TracedCallback::DisconnectWithoutContext
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
Definition:
traced-callback.h:158
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:141
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition:
callback.h:1648
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 Tue Feb 6 2024 19:21:17 for ns-3 by
1.9.1