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 #include "ns3/unused.h"
22 
23 using namespace ns3;
24 
26 {
27 public:
30  {}
31 
32 private:
33  virtual void DoRun (void);
34 
35  void CbOne (uint8_t a, double b);
36  void CbTwo (uint8_t a, double b);
37 
38  bool m_one;
39  bool m_two;
40 };
41 
43  : TestCase ("Check basic TracedCallback operation")
44 {}
45 
46 void
48 {
49  NS_UNUSED (a);
50  NS_UNUSED (b);
51  m_one = true;
52 }
53 
54 void
56 {
57  NS_UNUSED (a);
58  NS_UNUSED (b);
59  m_two = true;
60 }
61 
62 void
64 {
65  //
66  // Create a traced callback and connect it up to our target methods. All that
67  // these methods do is to set corresponding member variables m_one and m_two.
68  //
70 
71  //
72  // Connect both callbacks to their respective test methods. If we hit the
73  // trace, both callbacks should be called and the two variables should be set
74  // to true.
75  //
78  m_one = false;
79  m_two = false;
80  trace (1, 2);
81  NS_TEST_ASSERT_MSG_EQ (m_one, true, "Callback CbOne not called");
82  NS_TEST_ASSERT_MSG_EQ (m_two, true, "Callback CbTwo not called");
83 
84  //
85  // If we now disconnect callback one then only callback two should be called.
86  //
88  m_one = false;
89  m_two = false;
90  trace (1, 2);
91  NS_TEST_ASSERT_MSG_EQ (m_one, false, "Callback CbOne unexpectedly called");
92  NS_TEST_ASSERT_MSG_EQ (m_two, true, "Callback CbTwo not called");
93 
94  //
95  // If we now disconnect callback two then neither callback should be called.
96  //
98  m_one = false;
99  m_two = false;
100  trace (1, 2);
101  NS_TEST_ASSERT_MSG_EQ (m_one, false, "Callback CbOne unexpectedly called");
102  NS_TEST_ASSERT_MSG_EQ (m_two, false, "Callback CbTwo unexpectedly called");
103 
104  //
105  // If we connect them back up, then both callbacks should be called.
106  //
109  m_one = false;
110  m_two = false;
111  trace (1, 2);
112  NS_TEST_ASSERT_MSG_EQ (m_one, true, "Callback CbOne not called");
113  NS_TEST_ASSERT_MSG_EQ (m_two, true, "Callback CbTwo not called");
114 }
115 
117 {
118 public:
120 };
121 
123  : TestSuite ("traced-callback", UNIT)
124 {
125  AddTestCase (new BasicTracedCallbackTestCase, TestCase::QUICK);
126 }
127 
A suite of tests to run.
Definition: test.h:1343
Forward calls to a chain of Callback.
#define NS_UNUSED(x)
Mark a local variable as unused.
Definition: unused.h:36
static TracedCallbackTestSuite tracedCallbackTestSuite
encapsulates test code
Definition: test.h:1153
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#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:166
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoRun(void)
Implementation to actually run this TestCase.
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:1642
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).