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 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "ns3/test.h"
8#include "ns3/traced-callback.h"
9
10using 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 */
29{
30 public:
32
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
53 : TestCase("Check basic TracedCallback operation")
54{
55}
56
57void
58BasicTracedCallbackTestCase::CbOne(uint8_t /* a */, double /* b */)
59{
60 m_one = true;
61}
62
63void
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 //
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 */
131{
132 public:
134};
135
137 : TestSuite("traced-callback", Type::UNIT)
138{
139 AddTestCase(new BasicTracedCallbackTestCase, TestCase::Duration::QUICK);
140}
141
143 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:344
Callback template class.
Definition callback.h:422
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
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:134
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:684
static TracedCallbackTestSuite g_tracedCallbackTestSuite
Static variable for test initialization.