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
|
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <boost/hana/assert.hpp>
#include <boost/hana/functional/placeholder.hpp>
#include <utility>
namespace hana = boost::hana;
using hana::_;
struct extra_t { virtual ~extra_t() { } };
extra_t extra{};
constexpr struct { } invalid{};
template <typename ...> using bool_t = bool;
constexpr bool valid_call(...) { return false; }
template <typename F, typename ...Args>
constexpr auto valid_call(F&& f, Args&& ...args)
-> bool_t<decltype(std::forward<F>(f)(std::forward<Args>(args)...))>
{ return true; }
#define BOOST_HANA_TEST_BINARY_OP(op, x, y) \
static_assert((_ op _)(x, y) == (x op y), ""); \
BOOST_HANA_RUNTIME_CHECK((_ op _)(x, y, extra) == (x op y)); \
BOOST_HANA_RUNTIME_CHECK((_ op _)(x, y, extra, extra) == (x op y)); \
static_assert(!valid_call(_ op _), ""); \
static_assert(!valid_call(_ op _, invalid), ""); \
static_assert(!valid_call(_ op _, invalid, invalid), ""); \
\
static_assert((_ op y)(x) == (x op y), ""); \
BOOST_HANA_RUNTIME_CHECK((_ op y)(x, extra) == (x op y)); \
BOOST_HANA_RUNTIME_CHECK((_ op y)(x, extra, extra) == (x op y)); \
static_assert(!valid_call(_ op y), ""); \
static_assert(!valid_call(_ op y, invalid), ""); \
\
static_assert((x op _)(y) == (x op y), ""); \
BOOST_HANA_RUNTIME_CHECK((x op _)(y, extra) == (x op y)); \
BOOST_HANA_RUNTIME_CHECK((x op _)(y, extra, extra) == (x op y)); \
static_assert(!valid_call(x op _), ""); \
static_assert(!valid_call(x op _, invalid), ""); \
static_assert(!valid_call(x op _, invalid), ""); \
/**/
#define BOOST_HANA_TEST_UNARY_OP(op, x) \
static_assert((op _)(x) == (op x), ""); \
BOOST_HANA_RUNTIME_CHECK((op _)(x, extra) == (op x)); \
BOOST_HANA_RUNTIME_CHECK((op _)(x, extra, extra) == (op x)); \
static_assert(!valid_call(op _), ""); \
static_assert(!valid_call(op _, invalid), ""); \
/**/
struct incr_t {
template <typename X>
constexpr auto operator()(X x) const -> decltype(x + 1)
{ return x + 1; }
};
constexpr incr_t incr{};
int main() {
// Arithmetic
BOOST_HANA_TEST_UNARY_OP(+, 1)
BOOST_HANA_TEST_UNARY_OP(-, 1)
BOOST_HANA_TEST_BINARY_OP(+, 6, 3)
BOOST_HANA_TEST_BINARY_OP(-, 6, 3)
BOOST_HANA_TEST_BINARY_OP(*, 6, 3)
BOOST_HANA_TEST_BINARY_OP(/, 6, 3)
BOOST_HANA_TEST_BINARY_OP(%, 6, 3)
// Bitwise
BOOST_HANA_TEST_UNARY_OP(~, 5)
BOOST_HANA_TEST_BINARY_OP(&, 6, 3)
BOOST_HANA_TEST_BINARY_OP(|, 6, 3)
BOOST_HANA_TEST_BINARY_OP(^, 6, 3)
BOOST_HANA_TEST_BINARY_OP(<<, 6, 3)
BOOST_HANA_TEST_BINARY_OP(>>, 6, 3)
// Comparison
BOOST_HANA_TEST_BINARY_OP(==, 6, 3)
BOOST_HANA_TEST_BINARY_OP(!=, 6, 3)
BOOST_HANA_TEST_BINARY_OP(<, 6, 3)
BOOST_HANA_TEST_BINARY_OP(<=, 6, 3)
BOOST_HANA_TEST_BINARY_OP(>, 6, 3)
BOOST_HANA_TEST_BINARY_OP(>=, 6, 3)
// Logical
BOOST_HANA_TEST_BINARY_OP(||, true, false)
BOOST_HANA_TEST_BINARY_OP(&&, true, true)
BOOST_HANA_TEST_UNARY_OP(!, true)
// Member access
constexpr int i = 4;
constexpr int array[] = {0, 1, 2};
BOOST_HANA_TEST_UNARY_OP(*, &i)
static_assert(_[0](array) == array[0], "");
BOOST_HANA_RUNTIME_CHECK(_[0](array, extra) == array[0]);
BOOST_HANA_RUNTIME_CHECK(_[0](array, extra, extra) == array[0]);
static_assert(_[1](array) == array[1], "");
static_assert(_[1](array) == array[1], "");
static_assert(_[2](array) == array[2], "");
static_assert(!valid_call(_[invalid]), "");
static_assert(!valid_call(_[invalid], array), "");
static_assert(!valid_call(_[invalid], invalid), "");
static_assert(!valid_call(_[0], invalid), "");
// Call operator
static_assert(_(1)(incr) == incr(1), "");
BOOST_HANA_RUNTIME_CHECK(_(1)(incr, extra) == incr(1));
BOOST_HANA_RUNTIME_CHECK(_(1)(incr, extra, extra) == incr(1));
static_assert(_(2)(incr) == incr(2), "");
static_assert(_(3)(incr) == incr(3), "");
static_assert(!valid_call(_(invalid)), "");
static_assert(!valid_call(_(invalid), incr), "");
static_assert(!valid_call(_(invalid), invalid), "");
static_assert(!valid_call(_(1), invalid), "");
}
|