A Discrete-Event Network Simulator
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 * 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
21using namespace ns3;
22
23/**
24 * \file
25 * \ingroup tracedcallback-tests
26 * TracedCallback test suite
27 */
28
29/**
30 * \ingroup core-tests
31 * \defgroup tracedcallback-tests TracedCallback class tests
32 */
33
34/**
35 * \ingroup tracedcallback-tests
36 *
37 * TracedCallback Test case, check basic TracedCallback operation.
38 */
40{
41 public:
43
45 {
46 }
47
48 private:
49 void DoRun() override;
50
51 /**
52 * First callback.
53 * \param a First parameter.
54 * \param b Second parameter.
55 */
56 void CbOne(uint8_t a, double b);
57
58 CallbackBase m_cbTwo; //!< second callback
59 bool m_one; //!< Variable set by the first callback.
60 bool m_two; //!< Variable set by the second callback.
61};
62
64 : TestCase("Check basic TracedCallback operation")
65{
66}
67
68void
69BasicTracedCallbackTestCase::CbOne(uint8_t /* a */, double /* b */)
70{
71 m_one = true;
72}
73
74void
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 //
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
136/**
137 * \ingroup tracedcallback-tests
138 *
139 * \brief The traced callback Test Suite.
140 */
142{
143 public:
145};
146
148 : TestSuite("traced-callback", Type::UNIT)
149{
150 AddTestCase(new BasicTracedCallbackTestCase, TestCase::Duration::QUICK);
151}
152
154 g_tracedCallbackTestSuite; //!< Static variable for test initialization
TracedCallback Test case, check basic TracedCallback operation.
void DoRun() override
Implementation to actually run this TestCase.
bool m_one
Variable set by the first callback.
CallbackBase m_cbTwo
second callback
void CbOne(uint8_t a, double b)
First callback.
bool m_two
Variable set by the second callback.
The traced callback Test Suite.
Base class for Callback class.
Definition: callback.h:360
Callback template class.
Definition: callback.h:438
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
Forward calls to a chain of Callback.
#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:145
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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
static TracedCallbackTestSuite g_tracedCallbackTestSuite
Static variable for test initialization.