A Discrete-Event Network Simulator
API
traced-value-callback-typedef-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) 2015 Lawrence Livermore National Laboratory
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  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/core-module.h"
23 #include "ns3/network-module.h" // SequenceNumber32
24 
25 using namespace ns3;
26 
36 namespace {
37 
48 template <typename T> inline
49 std::string TypeName (void) { return "unknown"; }
51 template <> inline std::string TypeName <bool> (void) { return "Bool" ; }
52 template <> inline std::string TypeName <int8_t> (void) { return "Int8_t" ; }
53 template <> inline std::string TypeName <int16_t> (void) { return "Int16_t" ; }
54 template <> inline std::string TypeName <int32_t> (void) { return "Int32_t" ; }
55 template <> inline std::string TypeName <int64_t> (void) { return "Int64_t" ; }
56 template <> inline std::string TypeName <uint8_t> (void) { return "Uint8_t" ; }
57 template <> inline std::string TypeName <uint16_t> (void) { return "Uint16_t"; }
58 template <> inline std::string TypeName <uint32_t> (void) { return "Uint32_t"; }
59 template <> inline std::string TypeName <uint64_t> (void) { return "Uint64_t"; }
60 template <> inline std::string TypeName <double> (void) { return "Double" ; }
61 template <> inline std::string TypeName <Time> (void) { return "Time" ; }
62 template <> inline std::string TypeName <SequenceNumber32> (void) { return "SequenceNumber32" ; }
77 std::string g_Result = "";
78 
79 
93 template <typename T>
94 void TracedValueCbSink (T oldValue, T newValue)
95 {
96  std::cout << ": "
97  << (int64_t)oldValue << " -> "
98  << (int64_t)newValue
99  << std::endl;
100  if (oldValue != 0)
101  g_Result = "oldValue should be 0";
102  else if (newValue != 1)
103  g_Result = "newValue should be 1";
104 
105 } // TracedValueCbSink<>()
106 
114 template <>
115 void TracedValueCbSink<Time> (Time oldValue, Time newValue)
116 {
117  TracedValueCbSink <int64_t> (oldValue.GetInteger (),
118  newValue.GetInteger ());
119 }
127 template <>
129  SequenceNumber32 newValue)
130 {
131  TracedValueCbSink <int64_t> (oldValue.GetValue (), newValue.GetValue ());
132 }
133 
134 
135 } // unnamed namespace
136 
137 
144 {
145 public:
148 
149 private:
150 
155  template <typename T>
156  class CheckTvCb : public Object
157  {
160 
161  public:
163  CheckTvCb (void) : m_value (0) { }
164 
169  static TypeId GetTypeId (void)
170  {
171  static TypeId tid =
172  TypeId ("CheckTvCb<" + TypeName<T>() + ">")
173  .SetParent <Object> ()
174  .AddTraceSource ("value",
175  "A value being traced.",
177  ("ns3::TracedValueCallback::" + TypeName<T>()) )
178  ;
179  return tid;
180  } // GetTypeId ()
181 
193  template <typename U>
194  void Invoke (U cb)
195  {
196  bool ok = TraceConnectWithoutContext ("value", MakeCallback (cb));
197  std::cout << GetTypeId () << ": "
198  << (ok ? "connected " : "failed to connect ")
199  << GetTypeId ().GetTraceSource (0).callback
200  ;
201  // The endl is in the sink function.
202 
203  if (ok)
204  // Odd form here is to accommodate the uneven operator support
205  // of Time and SequenceNumber32.
206  m_value = m_value + (T) 1;
207  else
208  {
209  // finish the line started above
210  std::cout << std::endl;
211 
212  // and log the error
213  g_Result = "failed to connect callback";
214  }
215 
216  } // Invoke()
217 
218  }; // class CheckTvCb<T>
219 
220 
231  template <typename T, typename U>
232  void CheckType (void)
233  {
234  U sink = TracedValueCbSink<T>;
235  CreateObject<CheckTvCb<T> > ()->Invoke (sink);
236 
238  g_Result = "";
239 
240  } // CheckType<>()
241 
242  virtual void DoRun (void);
243 
244 };
245 
247  : TestCase ("Check basic TracedValue callback operation")
248 {
249 }
250 
251 void
253 {
254  CheckType< bool, TracedValueCallback::Bool > ();
255  CheckType< int8_t, TracedValueCallback::Int8 > ();
256  CheckType< int16_t, TracedValueCallback::Int16 > ();
257  CheckType< int32_t, TracedValueCallback::Int32 > ();
258  CheckType< int64_t, TracedValueCallback::Int64 > ();
259  CheckType< uint8_t, TracedValueCallback::Uint8 > ();
260  CheckType< uint16_t, TracedValueCallback::Uint16 > ();
261  CheckType< uint32_t, TracedValueCallback::Uint32 > ();
262  CheckType< uint64_t, TracedValueCallback::Uint64 > ();
263  CheckType< double, TracedValueCallback::Double > ();
264  CheckType< Time, TracedValueCallback::Time > ();
265  CheckType< SequenceNumber32, TracedValueCallback::SequenceNumber32 > ();
266 }
267 
274 {
275 public:
277 };
278 
280  : TestSuite ("traced-value-callback", UNIT)
281 {
282  AddTestCase (new TracedValueCallbackTestCase, TestCase::QUICK);
283 }
284 
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void CheckType(void)
Check the TracedValue typedef against TracedValueCbSink<T>.
A base class which provides memory management and object aggregation.
Definition: object.h:88
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
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
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:423
Trace classes with value semantics.
Definition: traced-value.h:117
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
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:141
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
std::string TypeName< SequenceNumber32 >(void)
Generic template for unknown classes.
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 TracedValueCallbackTestSuite tracedValueCallbackTestSuite
Static variable for test initialization.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56