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
43
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
44
BOOST_CHECK
((
is_same_v<Traits::ClassType, void>
));
45
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
46
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
47
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
48
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
49
}
50
51
BOOST_AUTO_TEST_CASE
(
TraitsFunctorCase
)
52
{
53
struct
Test
{
54
double
operator()(
short
,
int
,
long
) {
return
0.0; }
55
};
56
57
using
Traits
=
FunctionTraits<Test>
;
58
59
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
60
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
61
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
62
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
63
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
64
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
65
}
66
67
BOOST_AUTO_TEST_CASE
(
TraitsConstFunctorCase
)
68
{
69
struct
Test
{
70
double
operator()(
short
,
int
,
long
)
const
{
return
0.0; }
71
};
72
73
using
Traits
=
FunctionTraits<Test>
;
74
75
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
76
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
77
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
78
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
79
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
80
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
81
}
82
83
BOOST_AUTO_TEST_CASE
(
TraitsConstNoexceptFunctorCase
)
84
{
85
struct
Test
{
86
double
operator()(
short
,
int
,
long
)
const
noexcept
{
return
0.0; }
87
};
88
89
using
Traits
=
FunctionTraits<Test>
;
90
91
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
92
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
93
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
94
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
95
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
96
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
97
}
98
99
BOOST_AUTO_TEST_CASE
(
TraitsNoexceptFunctorCase
)
100
{
101
struct
Test
{
102
double
operator()(
short
,
int
,
long
)
noexcept
{
return
0.0; }
103
};
104
105
using
Traits
=
FunctionTraits<Test>
;
106
107
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
108
BOOST_CHECK
((
is_same_v<Traits::ClassType, Test>
));
109
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
110
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
111
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
112
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
113
}
114
115
BOOST_AUTO_TEST_CASE
(
TraitsLambdaCase
)
116
{
117
auto
fn = [](
short
,
int
,
long
) ->
double
{
return
0.0; };
118
119
using
Traits
=
FunctionTraits
<
decltype
(fn)>;
120
121
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
122
BOOST_CHECK
((
is_same_v
<Traits::ClassType,
remove_cv_t
<
decltype
(fn)>>));
123
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
124
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
125
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
126
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
127
}
128
129
BOOST_AUTO_TEST_CASE
(
TraitsConstLambdaCase
)
130
{
131
const
auto
fn = [](
short
,
int
,
long
) ->
double
{
return
0.0; };
132
133
using
Traits
=
FunctionTraits
<
decltype
(fn)>;
134
135
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
136
BOOST_CHECK
((
is_same_v
<Traits::ClassType,
remove_const_t
<
decltype
(fn)>>));
137
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
138
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
139
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
140
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
141
}
142
143
BOOST_AUTO_TEST_CASE
(
TraitsConstNoexceptLambdaCase
)
144
{
145
const
auto
fn = [](
short
,
int
,
long
)
noexcept
->
double
{
return
0.0; };
146
147
using
Traits
=
FunctionTraits
<
decltype
(fn)>;
148
149
BOOST_CHECK
((
is_same_v<Traits::ReturnType, double>
));
150
BOOST_CHECK
((
is_same_v
<Traits::ClassType,
remove_const_t
<
decltype
(fn)>>));
151
BOOST_CHECK_EQUAL
(Traits::Arity, 3);
152
BOOST_CHECK
((
is_same_v
<Traits::ArgType<0>,
short
>));
153
BOOST_CHECK
((
is_same_v
<Traits::ArgType<1>,
int
>));
154
BOOST_CHECK
((
is_same_v
<Traits::ArgType<2>,
long
>));
155
}
156
157
BOOST_AUTO_TEST_CASE
(
TraitsMemFunCase
)
158
{
159
struct
Test
{
160
short
foo
(
int
) {
return
0; }
161
long
bar
(
double
,
double
) {
return
0; }
162
};
163
164
using
FooTraits
=
FunctionTraits
<
decltype
(&Test::foo)>;
165
166
BOOST_CHECK
((
is_same_v<FooTraits::ReturnType, short>
));
167
BOOST_CHECK
((
is_same_v<FooTraits::ClassType, Test>
));
168
BOOST_CHECK_EQUAL
(FooTraits::Arity, 1);
169
BOOST_CHECK
((
is_same_v
<FooTraits::ArgType<0>,
int
>));
170
171
using
BarTraits
=
FunctionTraits
<
decltype
(&Test::bar)>;
172
173
BOOST_CHECK
((
is_same_v<BarTraits::ReturnType, long>
));
174
BOOST_CHECK
((
is_same_v<BarTraits::ClassType, Test>
));
175
BOOST_CHECK_EQUAL
(BarTraits::Arity, 2);
176
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<0>,
double
>));
177
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<1>,
double
>));
178
}
179
180
BOOST_AUTO_TEST_CASE
(
TraitsConstMemFunCase
)
181
{
182
struct
Test
{
183
short
foo
(
int
)
const
{
return
0; }
184
long
bar
(
double
,
double
)
const
{
return
0; }
185
};
186
187
using
FooTraits
=
FunctionTraits
<
decltype
(&Test::foo)>;
188
189
BOOST_CHECK
((
is_same_v<FooTraits::ReturnType, short>
));
190
BOOST_CHECK
((
is_same_v<FooTraits::ClassType, Test>
));
191
BOOST_CHECK_EQUAL
(FooTraits::Arity, 1);
192
BOOST_CHECK
((
is_same_v
<FooTraits::ArgType<0>,
int
>));
193
194
using
BarTraits
=
FunctionTraits
<
decltype
(&Test::bar)>;
195
196
BOOST_CHECK
((
is_same_v<BarTraits::ReturnType, long>
));
197
BOOST_CHECK
((
is_same_v<BarTraits::ClassType, Test>
));
198
BOOST_CHECK_EQUAL
(BarTraits::Arity, 2);
199
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<0>,
double
>));
200
BOOST_CHECK
((
is_same_v
<BarTraits::ArgType<1>,
double
>));
201
}
202
203
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:97
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