A Discrete-Event Network Simulator
API
traced-value-callback-typedef-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Lawrence Livermore National Laboratory
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  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18  */
19 
20 #include "ns3/nstime.h"
21 #include "ns3/object.h"
22 #include "ns3/sequence-number.h"
23 #include "ns3/test.h"
24 #include "ns3/traced-value.h"
25 #include "ns3/type-id.h"
26 #include "ns3/type-name.h"
27 
28 #include <type_traits>
29 
30 using namespace ns3;
31 
41 namespace
42 {
43 
55 std::string g_Result = "";
56 
70 template <typename T>
71 void
72 TracedValueCbSink(T oldValue, T newValue)
73 {
74  std::cout << ": " << static_cast<int64_t>(oldValue) << " -> " << static_cast<int64_t>(newValue)
75  << std::endl;
76 
77  if (oldValue != 0)
78  {
79  g_Result = "oldValue should be 0";
80  }
81 
82  if (newValue != 1)
83  {
84  g_Result += std::string(g_Result.empty() ? "" : " | ") + "newValue should be 1";
85  }
86 } // TracedValueCbSink<>()
87 
95 template <>
96 void
97 TracedValueCbSink<Time>(Time oldValue, Time newValue)
98 {
99  TracedValueCbSink<int64_t>(oldValue.GetInteger(), newValue.GetInteger());
100 }
101 
109 template <>
110 void
112 {
113  TracedValueCbSink<int64_t>(oldValue.GetValue(), newValue.GetValue());
114 }
115 
116 } // unnamed namespace
117 
124 {
125  public:
127 
129  {
130  }
131 
132  private:
137  template <typename T>
138  class CheckTvCb : public Object
139  {
142 
143  public:
146  : m_value(0)
147  {
148  }
149 
154  static TypeId GetTypeId()
155  {
156  static TypeId tid =
157  TypeId("CheckTvCb<" + TypeNameGet<T>() + ">")
158  .SetParent<Object>()
159  .AddTraceSource("value",
160  "A value being traced.",
162  ("ns3::TracedValueCallback::" + TypeNameGet<T>()));
163  return tid;
164  } // GetTypeId ()
165 
177  template <typename U>
178  void Invoke(U cb)
179  {
180  bool ok = TraceConnectWithoutContext("value", MakeCallback(cb));
181  std::cout << GetTypeId() << ": " << (ok ? "connected " : "failed to connect ")
182  << GetTypeId().GetTraceSource(0).callback;
183  // The endl is in the sink function.
184 
185  if (!ok)
186  {
187  // finish the line started above
188  std::cout << std::endl;
189 
190  // and log the error
191  g_Result = "failed to connect callback";
192 
193  return;
194  }
195 
196  // Odd form here is to accommodate the uneven operator support
197  // of Time and SequenceNumber32.
198  m_value = m_value + static_cast<T>(1);
199 
200  } // Invoke()
201 
202  }; // class CheckTvCb<T>
203 
214  template <typename T, typename U>
215  void CheckType()
216  {
217  U sink = TracedValueCbSink<T>;
218  CreateObject<CheckTvCb<T>>()->Invoke(sink);
219 
220  NS_TEST_ASSERT_MSG_EQ(g_Result.empty(), true, g_Result);
221  g_Result = "";
222 
223  } // CheckType<>()
224 
225  void DoRun() override;
226 };
227 
229  : TestCase("Check basic TracedValue callback operation")
230 {
231 }
232 
233 void
235 {
236  CheckType<bool, TracedValueCallback::Bool>();
237  CheckType<int8_t, TracedValueCallback::Int8>();
238  CheckType<int16_t, TracedValueCallback::Int16>();
239  CheckType<int32_t, TracedValueCallback::Int32>();
240  CheckType<int64_t, TracedValueCallback::Int64>();
241  CheckType<uint8_t, TracedValueCallback::Uint8>();
242  CheckType<uint16_t, TracedValueCallback::Uint16>();
243  CheckType<uint32_t, TracedValueCallback::Uint32>();
244  CheckType<uint64_t, TracedValueCallback::Uint64>();
245  CheckType<double, TracedValueCallback::Double>();
246  CheckType<Time, TracedValueCallback::Time>();
247  CheckType<SequenceNumber32, TracedValueCallback::SequenceNumber32>();
248 }
249 
256 {
257  public:
259 };
260 
262  : TestSuite("traced-value-callback", UNIT)
263 {
264  AddTestCase(new TracedValueCallbackTestCase, TestCase::QUICK);
265 }
266 
A class to check that the callback function typedef will actually connect to the TracedValue.
void Invoke(U cb)
Check the sink function against the actual TracedValue invocation.
void CheckType()
Check the TracedValue typedef against TracedValueCbSink<T>.
void DoRun() override
Implementation to actually run this TestCase.
A base class which provides memory management and object aggregation.
Definition: object.h:89
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:455
Trace classes with value semantics.
Definition: traced-value.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
void TracedValueCbSink< SequenceNumber32 >(SequenceNumber32 oldValue, SequenceNumber32 newValue)
TracedValueCbSink specialization for SequenceNumber32.
void TracedValueCbSink< Time >(Time oldValue, Time newValue)
TracedValueCbSink specialization for Time.
void TracedValueCbSink(T oldValue, T newValue)
Template for TracedValue sink functions.
#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:144
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
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 TracedValueCallbackTestSuite tracedValueCallbackTestSuite
Static variable for test initialization.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55