A Discrete-Event Network Simulator
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 
41 {
42 public:
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 
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
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  //
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  //
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  //
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  //
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  //
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 
143 {
144 public:
146 };
147 
149  : TestSuite ("traced-callback", UNIT)
150 {
151  AddTestCase (new BasicTracedCallbackTestCase, TestCase::QUICK);
152 }
153 
TracedCallback Test case, check basic TracedCallback operation.
bool m_one
Variable set by the first callback.
void CbOne(uint8_t a, double b)
First callback.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void CbTwo(uint8_t a, double b)
Second callback.
bool m_two
Variable set by the second callback.
The traced callback Test Suite.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Forward calls to a chain of Callback.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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
static TracedCallbackTestSuite g_tracedCallbackTestSuite
Static variable for test initialization.