-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathconcepts.cpp
More file actions
194 lines (162 loc) · 6.68 KB
/
concepts.cpp
File metadata and controls
194 lines (162 loc) · 6.68 KB
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
#include <stdx/concepts.hpp>
#include <catch2/catch_test_macros.hpp>
#include <array>
#include <type_traits>
TEST_CASE("integral", "[concepts]") {
STATIC_REQUIRE(stdx::integral<int>);
STATIC_REQUIRE(not stdx::integral<float>);
}
TEST_CASE("floating_point", "[concepts]") {
STATIC_REQUIRE(stdx::floating_point<float>);
STATIC_REQUIRE(not stdx::floating_point<int>);
}
TEST_CASE("signed_integral", "[concepts]") {
STATIC_REQUIRE(stdx::signed_integral<int>);
STATIC_REQUIRE(not stdx::signed_integral<unsigned int>);
}
TEST_CASE("unsigned_integral", "[concepts]") {
STATIC_REQUIRE(stdx::unsigned_integral<unsigned int>);
STATIC_REQUIRE(not stdx::unsigned_integral<int>);
}
TEST_CASE("same_as", "[concepts]") {
STATIC_REQUIRE(stdx::same_as<int, int>);
STATIC_REQUIRE(not stdx::same_as<float, int>);
}
TEST_CASE("same_any", "[concepts]") {
STATIC_REQUIRE(stdx::same_any<int, float, bool, int>);
STATIC_REQUIRE(not stdx::same_any<float, char, bool, int>);
}
TEST_CASE("same_none", "[concepts]") {
STATIC_REQUIRE(stdx::same_none<int, float, bool, char>);
STATIC_REQUIRE(not stdx::same_none<float, bool, char, float>);
}
TEST_CASE("same_as_unqualified", "[concepts]") {
STATIC_REQUIRE(stdx::same_as_unqualified<int, int>);
STATIC_REQUIRE(not stdx::same_as_unqualified<int, void>);
STATIC_REQUIRE(stdx::same_as_unqualified<int, int &>);
STATIC_REQUIRE(stdx::same_as_unqualified<int, int const &>);
STATIC_REQUIRE(stdx::same_as_unqualified<int, int &&>);
STATIC_REQUIRE(stdx::same_as_unqualified<int, int const &&>);
STATIC_REQUIRE(stdx::same_as_unqualified<int &, int>);
STATIC_REQUIRE(stdx::same_as_unqualified<int const &, int>);
STATIC_REQUIRE(stdx::same_as_unqualified<int &&, int>);
STATIC_REQUIRE(stdx::same_as_unqualified<int const &&, int>);
}
TEST_CASE("convertible_to", "[concepts]") {
STATIC_REQUIRE(stdx::convertible_to<char, int>);
STATIC_REQUIRE(not stdx::convertible_to<float *, int *>);
}
namespace {
struct S {};
} // namespace
TEST_CASE("equality_comparable", "[concepts]") {
STATIC_REQUIRE(stdx::equality_comparable<int>);
STATIC_REQUIRE(not stdx::equality_comparable<S>);
}
TEST_CASE("totally_ordered", "[concepts]") {
STATIC_REQUIRE(stdx::totally_ordered<int>);
STATIC_REQUIRE(not stdx::totally_ordered<S>);
}
namespace {
struct A {};
struct B : A {};
struct C : private A {};
} // namespace
TEST_CASE("derived_from", "[concepts]") {
STATIC_REQUIRE(stdx::derived_from<A, A>);
STATIC_REQUIRE(stdx::derived_from<B, A>);
STATIC_REQUIRE(not stdx::derived_from<C, A>);
STATIC_REQUIRE(not stdx::derived_from<int, int>);
}
TEST_CASE("invocable", "[concepts]") {
using invocable_no_args = auto (*)()->void;
using invocable_int = auto (*)(int)->void;
[[maybe_unused]] constexpr auto l_invocable_no_args = [] {};
[[maybe_unused]] constexpr auto l_invocable_int = [](int) {};
STATIC_REQUIRE(not stdx::invocable<int>);
STATIC_REQUIRE(stdx::invocable<invocable_no_args>);
STATIC_REQUIRE(not stdx::invocable<invocable_no_args, int>);
STATIC_REQUIRE(stdx::invocable<invocable_int, int>);
STATIC_REQUIRE(not stdx::invocable<invocable_int>);
STATIC_REQUIRE(stdx::invocable<decltype(l_invocable_no_args)>);
STATIC_REQUIRE(not stdx::invocable<decltype(l_invocable_no_args), int>);
STATIC_REQUIRE(stdx::invocable<decltype(l_invocable_int), int>);
STATIC_REQUIRE(not stdx::invocable<decltype(l_invocable_int)>);
}
TEST_CASE("predicate (negative cases)", "[concepts]") {
using not_predicate_no_args = auto (*)()->void;
using not_predicate_int = auto (*)(int)->void;
[[maybe_unused]] constexpr auto l_not_predicate_no_args = [] {};
[[maybe_unused]] constexpr auto l_not_predicate_int = [](int) {};
STATIC_REQUIRE(not stdx::predicate<not_predicate_no_args>);
STATIC_REQUIRE(not stdx::predicate<not_predicate_int, int>);
STATIC_REQUIRE(not stdx::predicate<decltype(l_not_predicate_no_args)>);
STATIC_REQUIRE(not stdx::predicate<decltype(l_not_predicate_int), int>);
}
TEST_CASE("predicate (positive cases)", "[concepts]") {
using predicate_no_args = auto (*)()->bool;
using predicate_int = auto (*)(int)->bool;
[[maybe_unused]] constexpr auto l_predicate_no_args = [] { return true; };
[[maybe_unused]] constexpr auto l_predicate_int = [](int) { return true; };
[[maybe_unused]] constexpr auto convert_predicate_no_args = [] {
return std::true_type{};
};
[[maybe_unused]] constexpr auto convert_predicate_int = [](int) {
return std::true_type{};
};
STATIC_REQUIRE(stdx::predicate<predicate_no_args>);
STATIC_REQUIRE(stdx::predicate<predicate_int, int>);
STATIC_REQUIRE(stdx::predicate<decltype(l_predicate_no_args)>);
STATIC_REQUIRE(stdx::predicate<decltype(l_predicate_int), int>);
STATIC_REQUIRE(stdx::predicate<decltype(convert_predicate_no_args)>);
STATIC_REQUIRE(stdx::predicate<decltype(convert_predicate_int), int>);
}
namespace {
[[maybe_unused]] auto func_no_args() {}
[[maybe_unused]] auto func_one_arg(int) {}
struct funcobj {
auto operator()() {}
};
struct generic_funcobj {
template <typename> auto operator()() {}
};
} // namespace
TEST_CASE("callable", "[concepts]") {
[[maybe_unused]] constexpr auto l_callable_no_args = [] {};
[[maybe_unused]] constexpr auto l_callable_int = [](int) {};
[[maybe_unused]] constexpr auto l_callable_generic = [](auto) {};
STATIC_REQUIRE(not stdx::callable<int>);
STATIC_REQUIRE(stdx::callable<decltype(func_no_args)>);
STATIC_REQUIRE(stdx::callable<decltype(func_one_arg)>);
STATIC_REQUIRE(stdx::callable<funcobj>);
STATIC_REQUIRE(stdx::callable<generic_funcobj>);
STATIC_REQUIRE(stdx::callable<decltype(l_callable_no_args)>);
STATIC_REQUIRE(stdx::callable<decltype(l_callable_int)>);
STATIC_REQUIRE(stdx::callable<decltype(l_callable_generic)>);
}
TEST_CASE("models_trait", "[concepts]") {
STATIC_REQUIRE(stdx::has_trait<int *, std::is_pointer>);
STATIC_REQUIRE(not stdx::has_trait<int, std::is_pointer>);
}
namespace {
struct non_structural {
~non_structural() {} // nontrivial destructor
};
} // namespace
TEST_CASE("structural", "[concepts]") {
STATIC_REQUIRE(stdx::structural<int>);
STATIC_REQUIRE(not stdx::structural<non_structural>);
}
TEST_CASE("complete", "[concepts]") {
struct incomplete;
STATIC_REQUIRE(stdx::complete<int>);
STATIC_REQUIRE(not stdx::complete<incomplete>);
}
namespace {
template <typename> struct unary_t {};
template <typename...> struct variadic_t {};
} // namespace
TEST_CASE("same_template_as", "[concepts]") {
STATIC_REQUIRE(stdx::same_template_as<unary_t<int>, unary_t<void>>);
STATIC_REQUIRE(not stdx::same_template_as<unary_t<int>, variadic_t<void>>);
}