Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
util
Traits.ut.cpp
Go to the documentation of this file.
1
// The Reactive C++ Toolbox.
2
// Copyright (C) 2013-2019 Swirly Cloud Limited
3
// Copyright (C) 2021 Reactive Markets Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "
Traits.hpp
"
18
19
#include <boost/test/unit_test.hpp>
20
21
using namespace
std
;
22
using namespace
toolbox
;
23
24
namespace
{
25
26
double
foo
(
short
,
int
,
long
)
27
{
28
return
0.0;
29
}
30
31
}
// namespace
32
33
BOOST_AUTO_TEST_SUITE
(
TraitsSuite
)
34
35
BOOST_AUTO_TEST_CASE
(
TraitsFreeFunCase
)
36
{
37
// Call foo to avoid the following Clang error:
38
// function 'foo' is not needed and will not be emitted.
39
foo
(0, 0, 0);
40
41
using
Traits
=
FunctionTraits
<
decltype
(&
foo
)>;
42
using
Tuple
= Traits::Pack<tuple>;
43
44
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
45
BOOST_CHECK
((
is_same_v<Traits::ClassType, void>
));
46
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
47
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
48
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
49
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
50
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
51
}
52
53
BOOST_AUTO_TEST_CASE
(
TraitsFunctorCase
)
54
{
55
struct
Test
{
56
double
operator()(
short
,
int
,
long
) {
return
0.0; }
57
};
58
59
using
Traits
=
FunctionTraits<Test>
;
60
using
Tuple
= Traits::Pack<tuple>;
61
62
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
63
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
64
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
65
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
66
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
67
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
68
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
69
}
70
71
BOOST_AUTO_TEST_CASE
(
TraitsConstFunctorCase
)
72
{
73
struct
Test
{
74
double
operator()(
short
,
int
,
long
)
const
{
return
0.0; }
75
};
76
77
using
Traits
=
FunctionTraits<Test>
;
78
using
Tuple
= Traits::Pack<tuple>;
79
80
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
81
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
82
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
83
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
84
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
85
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
86
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
87
}
88
89
BOOST_AUTO_TEST_CASE
(
TraitsConstNoexceptFunctorCase
)
90
{
91
struct
Test
{
92
double
operator()(
short
,
int
,
long
)
const
noexcept
{
return
0.0; }
93
};
94
95
using
Traits
=
FunctionTraits<Test>
;
96
using
Tuple
= Traits::Pack<tuple>;
97
98
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
99
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
100
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
101
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
102
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
103
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
104
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
105
}
106
107
BOOST_AUTO_TEST_CASE
(
TraitsNoexceptFunctorCase
)
108
{
109
struct
Test
{
110
double
operator()(
short
,
int
,
long
)
noexcept
{
return
0.0; }
111
};
112
113
using
Traits
=
FunctionTraits<Test>
;
114
using
Tuple
= Traits::Pack<tuple>;
115
116
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
117
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
118
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
119
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
120
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
121
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
122
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
123
}
124
125
BOOST_AUTO_TEST_CASE
(
TraitsLambdaCase
)
126
{
127
auto
fn = [](
short
,
int
,
long
) ->
double
{
return
0.0; };
128
129
using
Traits
=
FunctionTraits
<
decltype
(fn)>;
130
using
Tuple
= Traits::Pack<tuple>;
131
132
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
133
BOOST_CHECK
((
is_same_v
<Traits::ClassType,
remove_cv_t
<
decltype
(fn)>>));
134
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
135
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
136
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
137
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
138
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
139
}
140
141
BOOST_AUTO_TEST_CASE
(
TraitsConstLambdaCase
)
142
{
143
const
auto
fn = [](
short
,
int
,
long
) ->
double
{
return
0.0; };
144
145
using
Traits
=
FunctionTraits
<
decltype
(fn)>;
146
using
Tuple
= Traits::Pack<tuple>;
147
148
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
149
BOOST_CHECK
((
is_same_v
<Traits::ClassType,
remove_const_t
<
decltype
(fn)>>));
150
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
151
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
152
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
153
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
154
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
155
}
156
157
BOOST_AUTO_TEST_CASE
(
TraitsConstNoexceptLambdaCase
)
158
{
159
const
auto
fn = [](
short
,
int
,
long
)
noexcept
->
double
{
return
0.0; };
160
161
using
Traits
=
FunctionTraits
<
decltype
(fn)>;
162
using
Tuple
= Traits::Pack<tuple>;
163
164
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
165
BOOST_CHECK
((
is_same_v
<Traits::ClassType,
remove_const_t
<
decltype
(fn)>>));
166
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
167
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
168
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
169
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
170
BOOST_CHECK_EQUAL
(
tuple_size_v<Tuple>
, 3U);
171
}
172
173
BOOST_AUTO_TEST_CASE
(
TraitsMemFunCase
)
174
{
175
struct
Test
{
176
short
foo
(
int
) {
return
0; }
177
long
bar
(
double
,
double
) {
return
0; }
178
};
179
180
using
FooTraits
=
FunctionTraits
<
decltype
(&Test::foo)>;
181
using
FooTuple
= FooTraits::Pack<tuple>;
182
183
BOOST_CHECK
((
is_same_v<FooTraits::ReturnType, short>
));
184
BOOST_CHECK
((
is_same_v<FooTraits::ClassType, Test>
));
185
BOOST_CHECK_EQUAL
(FooTraits::Arity, 1);
186
BOOST_CHECK
((
is_same_v
<FooTraits::ArgType<0>,
int
>));
187
BOOST_CHECK_EQUAL
(
tuple_size_v<FooTuple>
, 1U);
188
189
using
BarTraits
=
FunctionTraits
<
decltype
(&Test::bar)>;
190
using
BarTuple
= BarTraits::Pack<tuple>;
191
192
BOOST_CHECK
((
is_same_v<BarTraits::ReturnType, long>
));
193
BOOST_CHECK
((
is_same_v<BarTraits::ClassType, Test>
));
194
BOOST_CHECK_EQUAL
(BarTraits::Arity, 2);
195
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<0>,
double
>));
196
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<1>,
double
>));
197
BOOST_CHECK_EQUAL
(
tuple_size_v<BarTuple>
, 2U);
198
}
199
200
BOOST_AUTO_TEST_CASE
(
TraitsConstMemFunCase
)
201
{
202
struct
Test
{
203
short
foo
(
int
)
const
{
return
0; }
204
long
bar
(
double
,
double
)
const
{
return
0; }
205
};
206
207
using
FooTraits
=
FunctionTraits
<
decltype
(&Test::foo)>;
208
using
FooTuple
= FooTraits::Pack<tuple>;
209
210
BOOST_CHECK
((
is_same_v<FooTraits::ReturnType, short>
));
211
BOOST_CHECK
((
is_same_v<FooTraits::ClassType, Test>
));
212
BOOST_CHECK_EQUAL
(FooTraits::Arity, 1);
213
BOOST_CHECK
((
is_same_v
<FooTraits::ArgType<0>,
int
>));
214
BOOST_CHECK_EQUAL
(
tuple_size_v<FooTuple>
, 1U);
215
216
using
BarTraits
=
FunctionTraits
<
decltype
(&Test::bar)>;
217
using
BarTuple
= BarTraits::Pack<tuple>;
218
219
BOOST_CHECK
((
is_same_v<BarTraits::ReturnType, long>
));
220
BOOST_CHECK
((
is_same_v<BarTraits::ClassType, Test>
));
221
BOOST_CHECK_EQUAL
(BarTraits::Arity, 2);
222
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<0>,
double
>));
223
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<1>,
double
>));
224
BOOST_CHECK_EQUAL
(
tuple_size_v<BarTuple>
, 2U);
225
}
226
227
BOOST_AUTO_TEST_SUITE_END
()
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(v.size(), 10U)
Traits.hpp
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TraitsFreeFunCase)
Definition
Traits.ut.cpp:35
std
STL namespace.
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox::util::Test
Test
Definition
Enum.ut.cpp:30
toolbox
Definition
Benchmark.cpp:26
toolbox::FunctionTraits
Default case for functors and lambdas.
Definition
Traits.hpp:27
BOOST_CHECK
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))
Generated by
1.9.8