1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/* Self tests for function_view for GDB, the GNU debugger.
Copyright (C) 2017-2023 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "gdbsupport/selftest.h"
#include "gdbsupport/function-view.h"
namespace selftests {
namespace function_view {
static int
plus_one_fn_int (int val)
{
return ++val;
}
static short
plus_one_fn_short (short val)
{
return ++val;
}
static int
call_callback_int (int val, gdb::function_view <int (int)> callback)
{
return callback (val);
}
static void
call_callback_void (int val, gdb::function_view <void (int)> callback)
{
callback (val);
}
struct plus_one_int_func_obj
{
int operator () (int val)
{
++call_count;
return ++val;
}
/* Number of times called. */
int call_count = 0;
};
static void
test_function_view ()
{
/* A simple lambda. */
auto plus_one_lambda = [] (int val) { return ++val; };
/* A function_view that references the lambda. */
gdb::function_view<int (int)> plus_one_func_view (plus_one_lambda);
/* Check calling the lambda directly. */
SELF_CHECK (plus_one_lambda (0) == 1);
SELF_CHECK (plus_one_lambda (1) == 2);
/* Check calling lambda via the view. */
SELF_CHECK (plus_one_func_view (2) == 3);
SELF_CHECK (plus_one_func_view (3) == 4);
/* Check calling a function that takes a function_view as argument,
by value. Pass a lambda, making sure a function_view is properly
constructed implicitly. */
SELF_CHECK (call_callback_int (1, [] (int val)
{
return val + 2;
}) == 3);
/* Same, passing a named/lvalue lambda. */
SELF_CHECK (call_callback_int (1, plus_one_lambda) == 2);
/* Same, passing a named/lvalue function_view (should copy). */
SELF_CHECK (call_callback_int (1, plus_one_func_view) == 2);
/* Check constructing a function view over a function-object
callable, and calling it. */
plus_one_int_func_obj func_obj;
SELF_CHECK (func_obj (0) == 1);
SELF_CHECK (call_callback_int (1, func_obj) == 2);
/* Check that the callable was referenced, not copied. */
SELF_CHECK (func_obj.call_count == 2);
/* Check constructing a function_view over a free-function callable,
and calling it. */
SELF_CHECK (call_callback_int (1, plus_one_fn_int) == 2);
/* Check calling a function with a
compatible-but-not-exactly-the-same prototype. */
SELF_CHECK (call_callback_int (1, [] (short val) -> short
{
return val + 2;
}) == 3);
/* Same, but passing a function pointer. */
SELF_CHECK (call_callback_int (1, plus_one_fn_short) == 2);
/* Like std::function, a function_view that expects a void return
can reference callables with non-void return type. The result is
simply discarded. Check a lambda, function object and a function
pointer. */
call_callback_void (1, [] (int val) -> int
{
return val + 2;
});
call_callback_void (1, func_obj);
call_callback_void (1, plus_one_fn_int);
/* Check that the main ctor doesn't hijack the copy ctor. */
auto plus_one_func_view2 (plus_one_func_view);
auto plus_one_func_view3 (plus_one_func_view2);
static_assert (std::is_same<decltype (plus_one_func_view),
decltype (plus_one_func_view2)>::value, "");
static_assert (std::is_same<decltype (plus_one_func_view),
decltype (plus_one_func_view3)>::value, "");
SELF_CHECK (plus_one_func_view3 (1) == 2);
/* Likewise, but propagate a NULL callable. If this calls the main
function_view ctor instead of the copy ctor by mistake, then
null_func_2 ends up non-NULL (because it'd instead reference
null_func_1 as just another callable). */
constexpr gdb::function_view<int (int)> null_func_view_1 = nullptr;
constexpr auto null_func_view_2 (null_func_view_1);
/* While at it, check whether the function_view is bound using
various forms, op==, op!= and op bool. */
/* op== */
static_assert (null_func_view_2 == nullptr, "");
static_assert (nullptr == null_func_view_2, "");
static_assert (null_func_view_2 == NULL, "");
static_assert (NULL == null_func_view_2, "");
/* op!= */
static_assert (!(null_func_view_2 != nullptr), "");
static_assert (!(nullptr != null_func_view_2), "");
static_assert (!(null_func_view_2 != NULL), "");
static_assert (!(NULL != null_func_view_2), "");
/* op bool */
static_assert (!null_func_view_2, "");
/* Check the nullptr_t ctor. */
constexpr gdb::function_view<int (int)> check_ctor_nullptr (nullptr);
static_assert (!check_ctor_nullptr, "");
/* Check the nullptr_t op= */
gdb::function_view<int (int)> check_op_eq_null (plus_one_fn_int);
SELF_CHECK (check_op_eq_null);
check_op_eq_null = nullptr;
SELF_CHECK (!check_op_eq_null);
}
/* A template function where the function_view type is dependent on a
template parameter. */
template<typename T>
static int
tmpl_func (T val, gdb::function_view<T (T)> callback)
{
return callback (val) + 1;
}
static int
make_fv_test_func (int val)
{
return val + 1;
}
/* A function object with const operator(). */
struct func_obj_const_op
{
int operator() (int val) const
{
return val + 1;
}
};
/* A function object with non-const operator(). */
struct func_obj_non_const_op
{
int operator() (int val)
{
return val + 1;
}
};
static void
test_make_function_view ()
{
/* Function reference. */
SELF_CHECK (3 == tmpl_func (1, gdb::make_function_view (make_fv_test_func)));
/* Function pointer. */
SELF_CHECK (3 == tmpl_func (1, gdb::make_function_view (&make_fv_test_func)));
/* Reference to const and non-const function pointers. */
typedef int (*func_ptr) (int);
func_ptr ptr = make_fv_test_func;
const func_ptr cptr = make_fv_test_func;
SELF_CHECK (3 == tmpl_func (1, gdb::make_function_view (ptr)));
SELF_CHECK (3 == tmpl_func (1, gdb::make_function_view (cptr)));
/* Lambdas. */
auto lambda = [] (int val) -> int { return val + 1; };
/* This wouldn't compile, since tmpl_func is a template and its
function_view argument's callable type is a dependent type. The
passed argument must be of the exact type of the function's
parameter. */
// SELF_CHECK (3 == tmpl_func (1, lambda));
SELF_CHECK (3 == tmpl_func (1, gdb::make_function_view (lambda)));
/* Regular function objects. */
func_obj_non_const_op fobj;
SELF_CHECK (3 == tmpl_func (1, gdb::make_function_view (fobj)));
func_obj_const_op cfobj;
SELF_CHECK (3 == tmpl_func (1, gdb::make_function_view (cfobj)));
}
static void
run_tests ()
{
test_function_view ();
test_make_function_view ();
}
} /* namespace function_view */
} /* namespace selftests */
void _initialize_function_view_selftests ();
void
_initialize_function_view_selftests ()
{
selftests::register_test ("function_view",
selftests::function_view::run_tests);
}
|