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 <uint8_t> (void) { return "Uint8_t" ; }
43 template <> inline std::string TypeName <uint16_t> (void) { return "Uint16_t"; }
44 template <> inline std::string TypeName <uint32_t> (void) { return "Uint32_t"; }
45 template <> inline std::string TypeName <double> (void) { return "Double" ; }
46 template <> inline std::string TypeName <Time> (void) { return "Time" ; }
47 template <> inline std::string TypeName <SequenceNumber32> (void) { return "SequenceNumber32" ; }
60 std::string g_Result = "";
61 
62 
74 template <typename T>
75 void TracedValueCbSink (T oldValue, T newValue)
76 {
77  std::cout << ": "
78  << (int64_t)oldValue << " -> "
79  << (int64_t)newValue
80  << std::endl;
81  if (oldValue != 0)
82  g_Result = "oldValue should be 0";
83  else if (newValue != 1)
84  g_Result = "newValue should be 1";
85 
86 } // TracedValueCbSink<>()
87 
91 template <>
92 void TracedValueCbSink<Time> (Time oldValue, Time newValue)
93 {
94  TracedValueCbSink <int64_t> (oldValue.GetInteger (),
95  newValue.GetInteger ());
96 }
100 template <>
102  SequenceNumber32 newValue)
103 {
104  TracedValueCbSink <int64_t> (oldValue.GetValue (), newValue.GetValue ());
105 }
106 
107 
108 } // unnamed namespace
109 
110 
112 {
113 public:
116 
117 private:
118 
123  template <typename T>
124  class CheckTvCb : public Object
125  {
127 
128  public:
130  CheckTvCb (void) : m_value (0) { }
131 
133  static TypeId GetTypeId (void)
134  {
135  static TypeId tid =
136  TypeId ( ("CheckTvCb<" + TypeName<T>() + ">").c_str ())
137  .SetParent <Object> ()
138  .AddTraceSource ("value",
139  "A value being traced.",
141  ("ns3::TracedValueCallback::" + TypeName<T>()).c_str () )
142  ;
143  return tid;
144  } // GetTypeId ()
145 
155  template <typename U>
156  void Invoke (U cb)
157  {
158  bool ok = TraceConnectWithoutContext ("value", MakeCallback (cb));
159  std::cout << GetTypeId () << ": "
160  << (ok ? "connected " : "failed to connect ")
161  << GetTypeId ().GetTraceSource (0).callback
162  ;
163  // The endl is in the sink function.
164 
165  if (ok)
166  // Odd form here is to accommodate the uneven operator support
167  // of Time and SequenceNumber32.
168  m_value = m_value + (T) 1;
169  else
170  {
171  // finish the line started above
172  std::cout << std::endl;
173 
174  // and log the error
175  g_Result = "failed to connect callback";
176  }
177 
178  } // Invoke()
179 
180  }; // class CheckTvCb<T>
181 
182 
193  template <typename T, typename U>
194  void CheckType (void)
195  {
196  U sink = TracedValueCbSink<T>;
197  CreateObject<CheckTvCb<T> > ()->Invoke (sink);
198 
200  g_Result = "";
201 
202  } // CheckType<>()
203 
204  virtual void DoRun (void);
205 
206 };
207 
209  : TestCase ("Check basic TracedValue callback operation")
210 {
211 }
212 
213 void
215 {
216  CheckType< bool, TracedValueCallback::Bool > ();
217  CheckType< int8_t, TracedValueCallback::Int8 > ();
218  CheckType< int16_t, TracedValueCallback::Int16 > ();
219  CheckType< int32_t, TracedValueCallback::Int32 > ();
220  CheckType< uint8_t, TracedValueCallback::Uint8 > ();
221  CheckType< uint16_t, TracedValueCallback::Uint16 > ();
222  CheckType< uint32_t, TracedValueCallback::Uint32 > ();
223  CheckType< double, TracedValueCallback::Double > ();
224  CheckType< Time, TracedValueCallback::Time > ();
225  CheckType< SequenceNumber32, TracedValueCallback::SequenceNumber32 > ();
226 }
227 
229 {
230 public:
232 };
233 
235  : TestSuite ("traced-value-callback", UNIT)
236 {
237  AddTestCase (new TracedValueCallbackTestCase, TestCase::QUICK);
238 }
239 
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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:1342
encapsulates test code
Definition: test.h:1155
Trace classes with value semantics.
Definition: traced-value.h:111
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:168
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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:915
void CheckType(void)
Check the TracedValue typedef against TracedValueCbSink<T>.