-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcall_by_need.cpp
More file actions
216 lines (190 loc) · 8.42 KB
/
call_by_need.cpp
File metadata and controls
216 lines (190 loc) · 8.42 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "detail/tuple_types.hpp"
#include <stdx/call_by_need.hpp>
#include <stdx/tuple.hpp>
#include <catch2/catch_test_macros.hpp>
namespace {
template <auto> struct arg_t {};
template <auto V> constexpr auto arg = arg_t<V>{};
template <auto V1, auto V2>
constexpr auto operator==(arg_t<V1>, arg_t<V2>) -> bool {
if constexpr (std::is_same_v<stdx::remove_cvref_t<decltype(V1)>,
stdx::remove_cvref_t<decltype(V2)>>) {
return V1 == V2;
}
return false;
}
} // namespace
TEST_CASE("single function, exact args", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](arg_t<0>) { return 17; }}, stdx::tuple{arg<0>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
STATIC_REQUIRE(get<0>(r) == 17);
}
TEST_CASE("single function, drop void", "[call_by_need]") {
auto called = 0;
auto const r = stdx::call_by_need(stdx::tuple{[&](arg_t<0>) { ++called; }},
stdx::tuple{arg<0>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<> const>);
CHECK(called == 1);
}
TEST_CASE("single function, extra args (beginning)", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(stdx::tuple{[](arg_t<1>) {}},
stdx::tuple{arg<0>, arg<1>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<arg_t<0>> const>);
}
TEST_CASE("single function, extra args (end)", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(stdx::tuple{[](arg_t<0>) {}},
stdx::tuple{arg<0>, arg<1>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<arg_t<1>> const>);
}
TEST_CASE("multi function, exact args", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](arg_t<0>) { return 17; }, [](arg_t<1>) { return 18; }},
stdx::tuple{arg<0>, arg<1>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int, int> const>);
STATIC_REQUIRE(get<0>(r) == 17);
STATIC_REQUIRE(get<1>(r) == 18);
}
TEST_CASE("multi function, drop void", "[call_by_need]") {
auto called = 0;
auto const r = stdx::call_by_need(
stdx::tuple{[](arg_t<0>) { return 17; }, [&](arg_t<1>) { ++called; }},
stdx::tuple{arg<0>, arg<1>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
CHECK(get<0>(r) == 17);
CHECK(called == 1);
}
TEST_CASE("multi function, extra args (beginning)", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](arg_t<1>) { return 17; }, [](arg_t<2>) { return 18; }},
stdx::tuple{arg<0>, arg<1>, arg<2>});
STATIC_REQUIRE(
std::is_same_v<decltype(r), stdx::tuple<int, int, arg_t<0>> const>);
STATIC_REQUIRE(get<0>(r) == 17);
STATIC_REQUIRE(get<1>(r) == 18);
}
TEST_CASE("multi function, extra args (middle)", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](arg_t<0>) { return 17; }, [](arg_t<2>) { return 18; }},
stdx::tuple{arg<0>, arg<1>, arg<2>});
STATIC_REQUIRE(
std::is_same_v<decltype(r), stdx::tuple<int, int, arg_t<1>> const>);
STATIC_REQUIRE(get<0>(r) == 17);
STATIC_REQUIRE(get<1>(r) == 18);
}
TEST_CASE("multi function, extra args (end)", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](arg_t<0>) { return 17; }, [](arg_t<1>) { return 18; }},
stdx::tuple{arg<0>, arg<1>, arg<2>});
STATIC_REQUIRE(
std::is_same_v<decltype(r), stdx::tuple<int, int, arg_t<2>> const>);
STATIC_REQUIRE(get<0>(r) == 17);
STATIC_REQUIRE(get<1>(r) == 18);
}
TEST_CASE("multi function, same args", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](arg_t<0>) { return 17; }, [](arg_t<0>) { return 18; }},
stdx::tuple{arg<0>, arg<1>});
STATIC_REQUIRE(
std::is_same_v<decltype(r), stdx::tuple<int, int, arg_t<1>> const>);
STATIC_REQUIRE(get<0>(r) == 17);
STATIC_REQUIRE(get<1>(r) == 18);
}
TEST_CASE("multi function, overlapping args", "[call_by_need]") {
constexpr auto r =
stdx::call_by_need(stdx::tuple{[](arg_t<0>, arg_t<1>) { return 17; },
[](arg_t<1>, arg_t<2>) { return 18; }},
stdx::tuple{arg<0>, arg<1>, arg<2>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int, int> const>);
STATIC_REQUIRE(get<0>(r) == 17);
STATIC_REQUIRE(get<1>(r) == 18);
}
TEST_CASE("move-only arg", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](move_only) { return 17; }}, stdx::tuple{move_only{17}});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
STATIC_REQUIRE(get<0>(r) == 17);
}
TEST_CASE("move-only arg (by reference)", "[call_by_need]") {
auto t = stdx::tuple{move_only{1}};
auto const r =
stdx::call_by_need(stdx::tuple{[](move_only &) { return 17; }}, t);
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
CHECK(get<0>(r) == 17);
}
TEST_CASE("move-only return", "[call_by_need]") {
constexpr auto r =
stdx::call_by_need(stdx::tuple{[](arg_t<0>) { return move_only{17}; }},
stdx::tuple{arg<0>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<move_only> const>);
STATIC_REQUIRE(get<0>(r).value == 17);
}
TEST_CASE("move-only function", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[m = move_only{17}](int) mutable { return std::move(m); }},
stdx::tuple{17});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<move_only> const>);
STATIC_REQUIRE(get<0>(r) == move_only{17});
}
TEST_CASE("converted arguments", "[call_by_need]") {
auto const r = stdx::call_by_need(stdx::tuple{[](char c) {
CHECK(c == 'a');
return 17;
},
[](int i) {
CHECK(i == 'a');
return 18;
}},
stdx::tuple{'a', 42});
STATIC_REQUIRE(
std::is_same_v<decltype(r), stdx::tuple<int, int, int> const>);
CHECK(get<0>(r) == 17);
CHECK(get<1>(r) == 18);
CHECK(get<2>(r) == 42);
}
TEST_CASE("nullary function, no args", "[call_by_need]") {
auto called = 0;
auto const r = stdx::call_by_need(stdx::tuple{[&] {
++called;
return 17;
}},
stdx::tuple{});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
CHECK(called == 1);
}
TEST_CASE("nullary function, passthrough arg", "[call_by_need]") {
auto called = 0;
auto const r = stdx::call_by_need(stdx::tuple{[&] {
++called;
return 17;
}},
stdx::tuple{arg<0>});
STATIC_REQUIRE(
std::is_same_v<decltype(r), stdx::tuple<int, arg_t<0>> const>);
CHECK(called == 1);
}
TEST_CASE("default arguments unfilled", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](int i = 17) { return i; }}, stdx::tuple{});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
STATIC_REQUIRE(get<0>(r) == 17);
}
TEST_CASE("default arguments filled", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(
stdx::tuple{[](int i = 17) { return i; }}, stdx::tuple{18});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
STATIC_REQUIRE(get<0>(r) == 18);
}
TEST_CASE("no functions given", "[call_by_need]") {
constexpr auto r = stdx::call_by_need(stdx::tuple{}, stdx::tuple{17});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int> const>);
STATIC_REQUIRE(get<0>(r) == 17);
}
TEST_CASE("function returning reference", "[call_by_need]") {
int value{17};
auto const r = stdx::call_by_need(
stdx::tuple{[&](arg_t<0>) -> int & { return value; }},
stdx::tuple{arg<0>});
STATIC_REQUIRE(std::is_same_v<decltype(r), stdx::tuple<int &> const>);
CHECK(get<0>(r) == 17);
}