A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
30using namespace ns3;
31
32/**
33 * \file
34 * \ingroup system-tests-traced
35 *
36 * TracedValueCallback tests to verify that they work with different types
37 * of classes - it tests bool, double, various types of integers types,
38 * Time, and SequenceNumber32.
39 */
40
41namespace
42{
43
44/**
45 * \ingroup system-tests-traced
46 *
47 * Result of callback test.
48 *
49 * Since the sink function is outside the invoking class,
50 * which in this case is TracedValueCallbackTestCase, we can't use
51 * the test macros directly. Instead, we cache the result
52 * in the \c g_Result global value, then inspect it
53 * in the TracedValueCallbackTestCase::CheckType method.
54 */
55std::string g_Result = "";
56
57/**
58 * \ingroup system-tests-traced
59 *
60 * Template for TracedValue sink functions.
61 *
62 * This generates a sink function for any underlying type.
63 *
64 * \tparam T \explicit The type of the value being traced.
65 * Since the point of this template is to create a
66 * sink function, the template type must be given explicitly.
67 * \param [in] oldValue The original value
68 * \param [in] newValue The new value
69 */
70template <typename T>
71void
72TracedValueCbSink(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
88/**
89 * \ingroup system-tests-traced
90 *
91 * TracedValueCbSink specialization for Time.
92 * \param oldValue The old value
93 * \param newValue The new value
94 */
95template <>
96void
97TracedValueCbSink<Time>(Time oldValue, Time newValue)
98{
99 TracedValueCbSink<int64_t>(oldValue.GetInteger(), newValue.GetInteger());
100}
101
102/**
103 * \ingroup system-tests-traced
104 *
105 * TracedValueCbSink specialization for SequenceNumber32.
106 * \param oldValue The old value
107 * \param newValue The new value
108 */
109template <>
110void
112{
113 TracedValueCbSink<int64_t>(oldValue.GetValue(), newValue.GetValue());
114}
115
116} // unnamed namespace
117
118/**
119 * \ingroup system-tests-traced
120 *
121 * \brief TracedValueCallback Test Case
122 */
124{
125 public:
127
129 {
130 }
131
132 private:
133 /**
134 * A class to check that the callback function typedef will
135 * actually connect to the TracedValue.
136 */
137 template <typename T>
138 class CheckTvCb : public Object
139 {
140 /// Traced value.
142
143 public:
144 /** Constructor. */
146 : m_value(0)
147 {
148 }
149
150 /**
151 * \brief Register this type.
152 * \return The object TypeId.
153 */
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
166 /**
167 * Check the sink function against the actual TracedValue invocation.
168 *
169 * We connect the TracedValue to the sink. If the types
170 * aren't compatible, the connection will fail.
171 *
172 * Just to make sure, we increment the TracedValue,
173 * which calls the sink.
174 *
175 * \param cb Callback.
176 */
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 ")
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
204 /**
205 * Check the TracedValue typedef against TracedValueCbSink<T>.
206 *
207 * We instantiate a sink function of type \c U, initialized to
208 * TracedValueCbSink<T>. If this compiles, we've proved the
209 * sink function and the typedef agree.
210 *
211 * \tparam T \explicit The base type.
212 * \tparam U \explicit The TracedValueCallback sink typedef type.
213 */
214 template <typename T, typename U>
216 {
217 U sink = TracedValueCbSink<T>;
218 CreateObject<CheckTvCb<T>>()->Invoke(sink);
219
221 g_Result = "";
222
223 } // CheckType<>()
224
225 void DoRun() override;
226};
227
229 : TestCase("Check basic TracedValue callback operation")
230{
231}
232
233void
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
250/**
251 * \ingroup system-tests-traced
252 *
253 * \brief TracedValueCallback TestSuite
254 */
256{
257 public:
259};
260
262 : TestSuite("traced-value-callback", Type::UNIT)
263{
264 AddTestCase(new TracedValueCallbackTestCase, TestCase::Duration::QUICK);
265}
266
267/// Static variable for test initialization
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.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:322
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:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
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::TraceSourceInformation GetTraceSource(std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:1131
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
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:145
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:706
std::string callback
Callback function signature type.
Definition: type-id.h:110
static TracedValueCallbackTestSuite tracedValueCallbackTestSuite
Static variable for test initialization.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55