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 
25 {
26 public:
29 
30 private:
31  virtual void DoRun (void);
32 
33  void CbOne (uint8_t a, double b);
34  void CbTwo (uint8_t a, double b);
35 
36  bool m_one;
37  bool m_two;
38 };
39 
41  : TestCase ("Check basic TracedCallback operation")
42 {
43 }
44 
45 void
47 {
48  m_one = true;
49 }
50 
51 void
53 {
54  m_two = true;
55 }
56 
57 void
59 {
60  //
61  // Create a traced callback and connect it up to our target methods. All that
62  // these methods do is to set corresponding member variables m_one and m_two.
63  //
65 
66  //
67  // Connect both callbacks to their respective test methods. If we hit the
68  // trace, both callbacks should be called and the two variables should be set
69  // to true.
70  //
73  m_one = false;
74  m_two = false;
75  trace (1, 2);
76  NS_TEST_ASSERT_MSG_EQ (m_one, true, "Callback CbOne not called");
77  NS_TEST_ASSERT_MSG_EQ (m_two, true, "Callback CbTwo not called");
78 
79  //
80  // If we now disconnect callback one then only callback two should be called.
81  //
83  m_one = false;
84  m_two = false;
85  trace (1, 2);
86  NS_TEST_ASSERT_MSG_EQ (m_one, false, "Callback CbOne unexpectedly called");
87  NS_TEST_ASSERT_MSG_EQ (m_two, true, "Callback CbTwo not called");
88 
89  //
90  // If we now disconnect callback two then neither callback should be called.
91  //
93  m_one = false;
94  m_two = false;
95  trace (1, 2);
96  NS_TEST_ASSERT_MSG_EQ (m_one, false, "Callback CbOne unexpectedly called");
97  NS_TEST_ASSERT_MSG_EQ (m_two, false, "Callback CbTwo unexpectedly called");
98 
99  //
100  // If we connect them back up, then both callbacks should be called.
101  //
104  m_one = false;
105  m_two = false;
106  trace (1, 2);
107  NS_TEST_ASSERT_MSG_EQ (m_one, true, "Callback CbOne not called");
108  NS_TEST_ASSERT_MSG_EQ (m_two, true, "Callback CbTwo not called");
109 }
110 
112 {
113 public:
115 };
116 
118  : TestSuite ("traced-callback", UNIT)
119 {
120  AddTestCase (new BasicTracedCallbackTestCase, TestCase::QUICK);
121 }
122 
A suite of tests to run.
Definition: test.h:1333
Forward calls to a chain of Callback.
static TracedCallbackTestSuite tracedCallbackTestSuite
encapsulates test code
Definition: test.h:1147
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
#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:161
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
static double CbOne(double a, double b)
Example Callback function.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).