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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9#include "ns3/nstime.h"
10#include "ns3/object.h"
11#include "ns3/sequence-number.h"
12#include "ns3/test.h"
13#include "ns3/traced-value.h"
14#include "ns3/type-id.h"
15#include "ns3/type-name.h"
16
17#include <type_traits>
18
19using namespace ns3;
20
21/**
22 * @file
23 * @ingroup system-tests-traced
24 *
25 * TracedValueCallback tests to verify that they work with different types
26 * of classes - it tests bool, double, various types of integers types,
27 * Time, and SequenceNumber32.
28 */
29
30namespace
31{
32
33/**
34 * @ingroup system-tests-traced
35 *
36 * Result of callback test.
37 *
38 * Since the sink function is outside the invoking class,
39 * which in this case is TracedValueCallbackTestCase, we can't use
40 * the test macros directly. Instead, we cache the result
41 * in the \c g_Result global value, then inspect it
42 * in the TracedValueCallbackTestCase::CheckType method.
43 */
44std::string g_Result = "";
45
46/**
47 * @ingroup system-tests-traced
48 *
49 * Template for TracedValue sink functions.
50 *
51 * This generates a sink function for any underlying type.
52 *
53 * @tparam T \explicit The type of the value being traced.
54 * Since the point of this template is to create a
55 * sink function, the template type must be given explicitly.
56 * @param [in] oldValue The original value
57 * @param [in] newValue The new value
58 */
59template <typename T>
60void
61TracedValueCbSink(T oldValue, T newValue)
62{
63 std::cout << ": " << static_cast<int64_t>(oldValue) << " -> " << static_cast<int64_t>(newValue)
64 << std::endl;
65
66 if (oldValue != 0)
67 {
68 g_Result = "oldValue should be 0";
69 }
70
71 if (newValue != 1)
72 {
73 g_Result += std::string(g_Result.empty() ? "" : " | ") + "newValue should be 1";
74 }
75} // TracedValueCbSink<>()
76
77/**
78 * @ingroup system-tests-traced
79 *
80 * TracedValueCbSink specialization for Time.
81 * @param oldValue The old value
82 * @param newValue The new value
83 */
84template <>
85void
86TracedValueCbSink<Time>(Time oldValue, Time newValue)
87{
88 TracedValueCbSink<int64_t>(oldValue.GetInteger(), newValue.GetInteger());
89}
90
91/**
92 * @ingroup system-tests-traced
93 *
94 * TracedValueCbSink specialization for SequenceNumber32.
95 * @param oldValue The old value
96 * @param newValue The new value
97 */
98template <>
104
105} // unnamed namespace
106
107/**
108 * @ingroup system-tests-traced
109 *
110 * @brief TracedValueCallback Test Case
111 */
113{
114 public:
116
118 {
119 }
120
121 private:
122 /**
123 * A class to check that the callback function typedef will
124 * actually connect to the TracedValue.
125 */
126 template <typename T>
127 class CheckTvCb : public Object
128 {
129 /// Traced value.
131
132 public:
133 /** Constructor. */
135 : m_value(0)
136 {
137 }
138
139 /**
140 * @brief Register this type.
141 * @return The object TypeId.
142 */
144 {
145 static TypeId tid =
146 TypeId("CheckTvCb<" + TypeNameGet<T>() + ">")
147 .SetParent<Object>()
148 .AddTraceSource("value",
149 "A value being traced.",
151 ("ns3::TracedValueCallback::" + TypeNameGet<T>()));
152 return tid;
153 } // GetTypeId ()
154
155 /**
156 * Check the sink function against the actual TracedValue invocation.
157 *
158 * We connect the TracedValue to the sink. If the types
159 * aren't compatible, the connection will fail.
160 *
161 * Just to make sure, we increment the TracedValue,
162 * which calls the sink.
163 *
164 * @param cb Callback.
165 */
166 template <typename U>
167 void Invoke(U cb)
168 {
169 bool ok = TraceConnectWithoutContext("value", MakeCallback(cb));
170 std::cout << GetTypeId() << ": " << (ok ? "connected " : "failed to connect ")
172 // The endl is in the sink function.
173
174 if (!ok)
175 {
176 // finish the line started above
177 std::cout << std::endl;
178
179 // and log the error
180 g_Result = "failed to connect callback";
181
182 return;
183 }
184
185 // Odd form here is to accommodate the uneven operator support
186 // of Time and SequenceNumber32.
187 m_value = m_value + static_cast<T>(1);
188
189 } // Invoke()
190
191 // end of class CheckTvCb<T>
192 };
193
194 /**
195 * Check the TracedValue typedef against TracedValueCbSink<T>.
196 *
197 * We instantiate a sink function of type \c U, initialized to
198 * TracedValueCbSink<T>. If this compiles, we've proved the
199 * sink function and the typedef agree.
200 *
201 * @tparam T \explicit The base type.
202 * @tparam U \explicit The TracedValueCallback sink typedef type.
203 */
204 template <typename T, typename U>
206 {
209
211 g_Result = "";
212
213 } // CheckType<>()
214
215 void DoRun() override;
216};
217
219 : TestCase("Check basic TracedValue callback operation")
220{
221}
222
223void
239
240/**
241 * @ingroup system-tests-traced
242 *
243 * @brief TracedValueCallback TestSuite
244 */
246{
247 public:
249};
250
256
257/// Static variable for test initialization
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.
Object()
Constructor.
Definition object.cc:96
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
@ QUICK
Fast test.
Definition test.h:1055
TestCase(const TestCase &)=delete
Type
Type of test.
Definition test.h:1274
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:490
static constexpr auto UNIT
Definition test.h:1291
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:444
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:49
TypeId::TraceSourceInformation GetTraceSource(std::size_t i) const
Get the trace source by index.
Definition type-id.cc:1200
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
std::string TypeNameGet()
Type name strings for AttributeValue types.
Definition type-name.h:36
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
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:134
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:684
std::string callback
Callback function signature type.
Definition type-id.h:115
static TracedValueCallbackTestSuite tracedValueCallbackTestSuite
Static variable for test initialization.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44