Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
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
21using namespace std;
22using namespace toolbox;
23
24namespace {
25
26double foo(short, int, long)
27{
28 return 0.0;
29}
30
31} // namespace
32
34
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
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>));
51}
52
54{
55 struct Test {
56 double operator()(short, int, long) { return 0.0; }
57 };
58
60 using Tuple = Traits::Pack<tuple>;
61
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>));
69}
70
72{
73 struct Test {
74 double operator()(short, int, long) const { return 0.0; }
75 };
76
78 using Tuple = Traits::Pack<tuple>;
79
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>));
87}
88
90{
91 struct Test {
92 double operator()(short, int, long) const noexcept { return 0.0; }
93 };
94
96 using Tuple = Traits::Pack<tuple>;
97
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>));
105}
106
108{
109 struct Test {
110 double operator()(short, int, long) noexcept { return 0.0; }
111 };
112
114 using Tuple = Traits::Pack<tuple>;
115
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>));
123}
124
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
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>));
139}
140
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
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>));
155}
156
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
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>));
171}
172
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
185 BOOST_CHECK_EQUAL(FooTraits::Arity, 1);
186 BOOST_CHECK((is_same_v<FooTraits::ArgType<0>, int>));
188
189 using BarTraits = FunctionTraits<decltype(&Test::bar)>;
190 using BarTuple = BarTraits::Pack<tuple>;
191
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>));
198}
199
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
212 BOOST_CHECK_EQUAL(FooTraits::Arity, 1);
213 BOOST_CHECK((is_same_v<FooTraits::ArgType<0>, int>));
215
216 using BarTraits = FunctionTraits<decltype(&Test::bar)>;
217 using BarTuple = BarTraits::Pack<tuple>;
218
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>));
225}
226
BOOST_CHECK_EQUAL(v.size(), 10U)
BOOST_AUTO_TEST_CASE(TraitsFreeFunCase)
Definition Traits.ut.cpp:35
STL namespace.
constexpr auto bind() noexcept
Definition Slot.hpp:92
Default case for functors and lambdas.
Definition Traits.hpp:27
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))