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 
27 namespace {
28 
35 template <typename T> inline
36 std::string TypeName (void) { return "unknown"; }
37 
38 template <> inline std::string TypeName <bool> (void) { return "Bool" ; }
39 template <> inline std::string TypeName <int8_t> (void) { return "Int8_t" ; }
40 template <> inline std::string TypeName <int16_t> (void) { return "Int16_t" ; }
41 template <> inline std::string TypeName <int32_t> (void) { return "Int32_t" ; }
42 template <> inline std::string TypeName <int64_t> (void) { return "Int64_t" ; }
43 template <> inline std::string TypeName <uint8_t> (void) { return "Uint8_t" ; }
44 template <> inline std::string TypeName <uint16_t> (void) { return "Uint16_t"; }
45 template <> inline std::string TypeName <uint32_t> (void) { return "Uint32_t"; }
46 template <> inline std::string TypeName <uint64_t> (void) { return "Uint64_t"; }
47 template <> inline std::string TypeName <double> (void) { return "Double" ; }
48 template <> inline std::string TypeName <Time> (void) { return "Time" ; }
49 template <> inline std::string TypeName <SequenceNumber32> (void) { return "SequenceNumber32" ; }
62 std::string g_Result = "";
63 
64 
76 template <typename T>
77 void TracedValueCbSink (T oldValue, T newValue)
78 {
79  std::cout << ": "
80  << (int64_t)oldValue << " -> "
81  << (int64_t)newValue
82  << std::endl;
83  if (oldValue != 0)
84  g_Result = "oldValue should be 0";
85  else if (newValue != 1)
86  g_Result = "newValue should be 1";
87 
88 } // TracedValueCbSink<>()
89 
93 template <>
94 void TracedValueCbSink<Time> (Time oldValue, Time newValue)
95 {
96  TracedValueCbSink <int64_t> (oldValue.GetInteger (),
97  newValue.GetInteger ());
98 }
102 template <>
104  SequenceNumber32 newValue)
105 {
106  TracedValueCbSink <int64_t> (oldValue.GetValue (), newValue.GetValue ());
107 }
108 
109 
110 } // unnamed namespace
111 
112 
114 {
115 public:
118 
119 private:
120 
125  template <typename T>
126  class CheckTvCb : public Object
127  {
129 
130  public:
132  CheckTvCb (void) : m_value (0) { }
133 
135  static TypeId GetTypeId (void)
136  {
137  static TypeId tid =
138  TypeId ( ("CheckTvCb<" + TypeName<T>() + ">").c_str ())
139  .SetParent <Object> ()
140  .AddTraceSource ("value",
141  "A value being traced.",
143  ("ns3::TracedValueCallback::" + TypeName<T>()).c_str () )
144  ;
145  return tid;
146  } // GetTypeId ()
147 
157  template <typename U>
158  void Invoke (U cb)
159  {
160  bool ok = TraceConnectWithoutContext ("value", MakeCallback (cb));
161  std::cout << GetTypeId () << ": "
162  << (ok ? "connected " : "failed to connect ")
163  << GetTypeId ().GetTraceSource (0).callback
164  ;
165  // The endl is in the sink function.
166 
167  if (ok)
168  // Odd form here is to accommodate the uneven operator support
169  // of Time and SequenceNumber32.
170  m_value = m_value + (T) 1;
171  else
172  {
173  // finish the line started above
174  std::cout << std::endl;
175 
176  // and log the error
177  g_Result = "failed to connect callback";
178  }
179 
180  } // Invoke()
181 
182  }; // class CheckTvCb<T>
183 
184 
195  template <typename T, typename U>
196  void CheckType (void)
197  {
198  U sink = TracedValueCbSink<T>;
199  CreateObject<CheckTvCb<T> > ()->Invoke (sink);
200 
202  g_Result = "";
203 
204  } // CheckType<>()
205 
206  virtual void DoRun (void);
207 
208 };
209 
211  : TestCase ("Check basic TracedValue callback operation")
212 {
213 }
214 
215 void
217 {
218  CheckType< bool, TracedValueCallback::Bool > ();
219  CheckType< int8_t, TracedValueCallback::Int8 > ();
220  CheckType< int16_t, TracedValueCallback::Int16 > ();
221  CheckType< int32_t, TracedValueCallback::Int32 > ();
222  CheckType< int64_t, TracedValueCallback::Int64 > ();
223  CheckType< uint8_t, TracedValueCallback::Uint8 > ();
224  CheckType< uint16_t, TracedValueCallback::Uint16 > ();
225  CheckType< uint32_t, TracedValueCallback::Uint32 > ();
226  CheckType< uint64_t, TracedValueCallback::Uint64 > ();
227  CheckType< double, TracedValueCallback::Double > ();
228  CheckType< Time, TracedValueCallback::Time > ();
229  CheckType< SequenceNumber32, TracedValueCallback::SequenceNumber32 > ();
230 }
231 
233 {
234 public:
236 };
237 
239  : TestSuite ("traced-value-callback", UNIT)
240 {
241  AddTestCase (new TracedValueCallbackTestCase, TestCase::QUICK);
242 }
243 
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
void TracedValueCbSink< Time >(Time oldValue, Time newValue)
TracedValueCbSink specialization for Time.
void TracedValueCbSink< SequenceNumber32 >(SequenceNumber32 oldValue, SequenceNumber32 newValue)
TracedValueCbSink specialization for SequenceNumber32.
A suite of tests to run.
Definition: test.h:1343
encapsulates test code
Definition: test.h:1153
Trace classes with value semantics.
Definition: traced-value.h:113
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
static TracedValueCallbackTestSuite tracedValueCallbackTestSuite
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
virtual void DoRun(void)
Implementation to actually run this TestCase.
A class to check that the callback function typedef will actually connect to the TracedValue.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Invoke(U cb)
Check the sink function against the actual TracedValue invocation.
void TracedValueCbSink(T oldValue, T newValue)
Template for TracedValue sink functions.
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
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 CheckType(void)
Check the TracedValue typedef against TracedValueCbSink<T>.