A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
traced-callback-typedef-test.h
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
#ifndef TRACED_CALLBACK_TYPEDEF_TEST_H
10
#define TRACED_CALLBACK_TYPEDEF_TEST_H
11
12
#include "ns3/test.h"
13
#include "ns3/traced-callback.h"
14
15
#include <cstddef>
16
#include <iostream>
17
#include <set>
18
#include <sstream>
19
#include <string>
20
#include <tuple>
21
#include <type_traits>
22
23
/**
24
* @file
25
* @ingroup core-tests
26
*
27
* Scaffolding shared by the per-module TracedCallback typedef test suites.
28
*
29
* Each module declaring TracedCallback typedefs owns a test suite deriving
30
* from TracedCallbackTypedefTestCase, which uses the TRACED_CALLBACK_CHECK() and
31
* TRACED_CALLBACK_DUPE() macros to verify that its typedefs are invoked with the right type and
32
* number of arguments.
33
*/
34
35
namespace
ns3
36
{
37
38
/**
39
* @ingroup core-tests
40
*
41
* Number of arguments the last invoked TracedCallback passed to its sink.
42
*
43
* Since the sink function is outside the invoking class we can't use
44
* the test macros directly. Instead, we cache the argument count here,
45
* then inspect it in TracedCallbackTypedefTestCase::Checker::Cleanup().
46
*/
47
inline
std::size_t
g_nArgs
= 0;
48
49
/**
50
* @ingroup core-tests
51
*
52
* Stringify the known TracedCallback type names.
53
*
54
* @tparam T @explicit The typedef name.
55
* @param [in] N The number of arguments expected.
56
* @returns The \c TracedCallback type name.
57
*/
58
template
<
typename
T>
59
inline
std::string
60
TracedCallbackTypeName
(
int
N)
61
{
62
return
"unknown"
;
63
}
64
65
/**
66
* @ingroup core-tests
67
*
68
* Declare the stringified name of a TracedCallback typedef.
69
*
70
* Must be used at namespace scope, inside namespace ns3.
71
*/
72
#define TRACED_CALLBACK_TYPENAME(T) \
73
template <> \
74
inline std::string TracedCallbackTypeName<T>(int N) \
75
{ \
76
std::stringstream ss; \
77
ss << #T << "(" << N << ")"; \
78
return ss.str(); \
79
}
80
81
/**
82
* @ingroup core-tests
83
*
84
* Sink functions.
85
*/
86
template
<
typename
... Ts>
87
class
TracedCbSink
88
{
89
public
:
90
/**
91
* @brief Sink function, called by a TracedCallback.
92
* @tparam Ts parameters of the TracedCallback.
93
*/
94
static
void
Sink
(Ts...)
95
{
96
std::cout <<
"with "
<<
sizeof
...(Ts) <<
" args."
<< std::endl;
97
g_nArgs
=
sizeof
...(Ts);
98
}
99
};
100
101
/**
102
* @ingroup core-tests
103
*
104
* Base class for the per-module TracedCallback typedef test cases.
105
*
106
* This verifies that a TracedCallback is called with the right type
107
* and number of arguments.
108
*/
109
class
TracedCallbackTypedefTestCase
:
public
TestCase
110
{
111
public
:
112
/**
113
* Constructor.
114
*
115
* @param [in] name Name of the test case.
116
*/
117
TracedCallbackTypedefTestCase
(std::string name)
118
:
TestCase
(name)
119
{
120
}
121
122
protected
:
123
/** Typedefs which are identical to previously declared ones. */
124
std::set<std::string>
m_dupes
;
125
126
/** Callback checkers. */
127
template
<
typename
... Ts>
128
class
Checker
129
{
130
/// TracedCallback to be called.
131
TracedCallback
<Ts...>
m_cb
;
132
133
/// Arguments of the TracedCallback.
134
std::tuple<std::remove_pointer_t<std::remove_cvref_t<Ts>>...>
m_items
;
135
136
/// Number of arguments of the TracedCallback.
137
static
constexpr
std::size_t
m_nItems
=
sizeof
...(Ts);
138
139
public
:
140
/**
141
* Invoke a TracedCallback.
142
*
143
* @tparam U @explicit The TracedCallback typedef under test.
144
*/
145
template
<
typename
U>
146
void
Invoke
()
147
{
148
U
sink
=
TracedCbSink
<Ts...>::Sink;
149
Callback
<void, Ts...> cb =
MakeCallback
(
sink
);
150
151
std::cout << TracedCallbackTypeName<U>(
m_nItems
) <<
" invoked "
;
152
m_cb
.ConnectWithoutContext(cb);
153
std::apply(
m_cb
,
m_items
);
154
Cleanup
();
155
}
156
157
private
:
158
/**
159
* Cleanup the test.
160
*/
161
void
Cleanup
()
162
{
163
if
(
g_nArgs
== 0)
164
{
165
std::cout << std::endl;
166
}
167
NS_ASSERT_MSG
(
g_nArgs
==
m_nItems
,
168
"failed, g_nArgs: "
<<
g_nArgs
<<
" N: "
<<
m_nItems
);
169
g_nArgs
= 0;
170
}
171
};
172
};
173
174
/**
175
* @ingroup core-tests
176
*
177
* Check the TracedCallback by calling its Invoke function.
178
*/
179
#define TRACED_CALLBACK_CHECK(U, ...) Checker<__VA_ARGS__>().Invoke<U>()
180
181
/**
182
* @ingroup core-tests
183
*
184
* Check the TracedCallback duplicate by checking if it matches the TracedCallback
185
* it is supposed to be equal to.
186
*/
187
#define TRACED_CALLBACK_DUPE(U, T1) \
188
if (m_dupes.find(#U) == m_dupes.end()) \
189
{ \
190
NS_TEST_ASSERT_MSG_NE(0, 1, "expected to find " << #U << " in dupes."); \
191
} \
192
if (TracedCallbackTypeName<U>(0) == TracedCallbackTypeName<T1>(0)) \
193
{ \
194
std::cout << #U << " matches " << #T1 << std::endl; \
195
} \
196
else \
197
{ \
198
NS_TEST_ASSERT_MSG_EQ( \
199
TracedCallbackTypeName<U>(0), \
200
TracedCallbackTypeName<T1>(0), \
201
"the typedef " \
202
<< #U << " used to match the typedef " << #T1 \
203
<< " but no longer does. Please add a new TRACED_CALLBACK_CHECK call."); \
204
}
205
206
}
// namespace ns3
207
208
#endif
/* TRACED_CALLBACK_TYPEDEF_TEST_H */
ns3::Callback
Callback template class.
Definition
callback.h:428
ns3::TestCase::TestCase
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
ns3::TracedCallback
Forward calls to a chain of Callback.
Definition
traced-callback.h:46
ns3::TracedCallbackTypedefTestCase::Checker
Callback checkers.
Definition
traced-callback-typedef-test.h:129
ns3::TracedCallbackTypedefTestCase::Checker::Invoke
void Invoke()
Invoke a TracedCallback.
Definition
traced-callback-typedef-test.h:146
ns3::TracedCallbackTypedefTestCase::Checker::m_nItems
static constexpr std::size_t m_nItems
Number of arguments of the TracedCallback.
Definition
traced-callback-typedef-test.h:137
ns3::TracedCallbackTypedefTestCase::Checker::m_cb
TracedCallback< Ts... > m_cb
TracedCallback to be called.
Definition
traced-callback-typedef-test.h:131
ns3::TracedCallbackTypedefTestCase::Checker::m_items
std::tuple< std::remove_pointer_t< std::remove_cvref_t< Ts > >... > m_items
Arguments of the TracedCallback.
Definition
traced-callback-typedef-test.h:134
ns3::TracedCallbackTypedefTestCase::Checker::Cleanup
void Cleanup()
Cleanup the test.
Definition
traced-callback-typedef-test.h:161
ns3::TracedCallbackTypedefTestCase::m_dupes
std::set< std::string > m_dupes
Typedefs which are identical to previously declared ones.
Definition
traced-callback-typedef-test.h:124
ns3::TracedCallbackTypedefTestCase::TracedCallbackTypedefTestCase
TracedCallbackTypedefTestCase(std::string name)
Constructor.
Definition
traced-callback-typedef-test.h:117
ns3::TracedCbSink
Sink functions.
Definition
traced-callback-typedef-test.h:88
ns3::TracedCbSink::Sink
static void Sink(Ts...)
Sink function, called by a TracedCallback.
Definition
traced-callback-typedef-test.h:94
NS_ASSERT_MSG
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition
assert.h:76
ns3::MakeCallback
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:690
ns3::g_nArgs
std::size_t g_nArgs
Number of arguments the last invoked TracedCallback passed to its sink.
Definition
traced-callback-typedef-test.h:47
ns3::TracedCallbackTypeName
std::string TracedCallbackTypeName(int N)
Stringify the known TracedCallback type names.
Definition
traced-callback-typedef-test.h:60
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
sink
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition
wifi-tcp.cc:44
src
core
test
traced-callback-typedef-test.h
Generated on
for ns-3 by
1.16.1