{{ message }}
Repository navigation
Expand file tree
/
Copy pathoperator_sections_test.cpp
More file actions
378 lines (374 loc) · 9.55 KB
/
Copy pathoperator_sections_test.cpp
File metadata and controls
378 lines (374 loc) · 9.55 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "catch2/catch_test_macros.hpp"
#define CPPUTILS_ENABLE_CALL_MACROS
#include "cpputils/functional/operator_sections.hpp"
using cpputils::_; // NOLINT
namespace {
struct Obj {
[[nodiscard]] int get() const { return v; }
[[nodiscard]] Obj square() const {
return Obj{v * v};
}
[[nodiscard]] int mult(int m) const {
return v * m;
}
[[nodiscard]] int operator()(int j) const {
return v + j;
}
int v{42};
};
} // namespace
// NOLINTNEXTLINE
TEST_CASE("sections test", "[test terse syntax for lambda functions]") {
SECTION("operator +") {
auto const sum = _ + _;
REQUIRE(sum(1, 2) == 3);
}
SECTION("partial operator +") {
{
auto const add_10 = _ + 10;
REQUIRE(add_10(1) == 11);
}
{
auto const add_10 = 10 + _;
REQUIRE(add_10(1) == 11);
}
}
///
SECTION("partial operator -") {
{
auto const subtract_10 = _ - 10;
REQUIRE(subtract_10(1) == -9);
}
{
auto const subtract_10 = 10 - _;
REQUIRE(subtract_10(10) == 0);
}
}
SECTION("operator -") {
auto const subtract = _ - _;
REQUIRE(subtract(1, 2) == -1);
}
SECTION("Unary minus") {
auto const negate = -_;
REQUIRE(negate(1) == -1);
}
SECTION("Unary plus") {
auto const do_plus = +_;
REQUIRE(do_plus('a') == (+'a'));
}
///
SECTION("operator *") {
auto const mul = _ * _;
REQUIRE(mul(3, 2) == 6);
}
SECTION("partial operator *") {
{
auto const mul_10 = _ * 10;
REQUIRE(mul_10(2) == 20);
}
{
auto const mul_10 = 10 * _;
REQUIRE(mul_10(2) == 20);
}
}
///
SECTION("operator /") {
auto const div = _ / _;
REQUIRE(div(6, 2) == 3);
}
SECTION("partial operator /") {
{
auto const div_10 = _ / 10;
REQUIRE(div_10(20) == 2);
}
{
auto const ten_div = 10 / _;
REQUIRE(ten_div(2) == 5);
}
}
///
SECTION("operator %") {
auto const mod = _ % _;
REQUIRE(mod(7, 2) == 1);
}
SECTION("partial operator %") {
{
auto const mod_2 = _ % 2;
REQUIRE(mod_2(3) == 1);
}
{
auto const ten_mod = 10 % _;
REQUIRE(ten_mod(3) == 1);
}
}
///
SECTION("operator ==") {
auto const eq = _ == _;
REQUIRE_FALSE(eq(7, 2));
REQUIRE(eq(2, 2));
}
SECTION("partial operator ==") {
{
auto const eq_2 = _ == 2;
REQUIRE(eq_2(2));
REQUIRE_FALSE(eq_2(1));
}
{
auto const eq_2 = 2 == _;
REQUIRE(eq_2(2));
REQUIRE_FALSE(eq_2(1));
}
}
///
SECTION("operator !=") {
auto const neq = _ != _;
REQUIRE(neq(7, 2));
REQUIRE_FALSE(neq(2, 2));
}
SECTION("partial operator !=") {
{
auto const neq_2 = _ != 2;
REQUIRE_FALSE(neq_2(2));
REQUIRE(neq_2(1));
}
{
auto const neq_2 = 2 != _;
REQUIRE_FALSE(neq_2(2));
REQUIRE(neq_2(1));
}
}
///
SECTION("operator >") {
auto const gt = _ > _;
REQUIRE(gt(7, 2));
REQUIRE_FALSE(gt(2, 2));
}
SECTION("partial operator >") {
{
auto const gt_2 = _ > 2;
REQUIRE_FALSE(gt_2(2));
REQUIRE(gt_2(3));
}
{
auto const lt_2 = 2 > _;
REQUIRE_FALSE(lt_2(2));
REQUIRE(lt_2(1));
}
}
///
SECTION("operator >=") {
auto const ge = _ >= _;
REQUIRE(ge(7, 2));
REQUIRE(ge(2, 2));
REQUIRE_FALSE(ge(1, 2));
}
SECTION("partial operator >=") {
{
auto const ge_2 = _ >= 2;
REQUIRE(ge_2(2));
REQUIRE(ge_2(3));
REQUIRE_FALSE(ge_2(1));
}
{
auto const le_2 = 2 >= _;
REQUIRE(le_2(2));
REQUIRE(le_2(1));
REQUIRE_FALSE(le_2(3));
}
}
///
SECTION("operator <") {
auto const lt = _ < _;
REQUIRE_FALSE(lt(7, 2));
REQUIRE(lt(1, 2));
}
SECTION("partial operator <") {
{
auto const lt_2 = _ < 2;
REQUIRE_FALSE(lt_2(2));
REQUIRE(lt_2(1));
}
{
auto const gt_2 = 2 < _;
REQUIRE_FALSE(gt_2(2));
REQUIRE(gt_2(3));
}
}
///
SECTION("operator <=") {
auto const le = _ <= _;
REQUIRE_FALSE(le(7, 2));
REQUIRE(le(2, 2));
REQUIRE(le(1, 2));
}
SECTION("partial operator <=") {
{
auto const le_2 = _ <= 2;
REQUIRE(le_2(2));
REQUIRE_FALSE(le_2(3));
REQUIRE(le_2(1));
}
{
auto const ge_2 = 2 <= _;
REQUIRE(ge_2(2));
REQUIRE_FALSE(ge_2(1));
REQUIRE(ge_2(3));
}
}
///
SECTION("operator !") {
auto const neg = !_;
REQUIRE(neg(false));
REQUIRE_FALSE(neg(true));
}
///
SECTION("operator &&") {
auto const and_ = _ && _;
REQUIRE(and_(true, true));
REQUIRE_FALSE(and_(true, false));
}
SECTION("partial operator &&") {
{
auto const and_true = _ && true;
REQUIRE(and_true(true));
REQUIRE_FALSE(and_true(false));
}
{
auto const and_true = true && _;
REQUIRE(and_true(true));
REQUIRE_FALSE(and_true(false));
}
}
///
SECTION("operator ||") {
auto const or_ = _ || _;
REQUIRE(or_(false, true));
REQUIRE_FALSE(or_(false, false));
}
SECTION("partial operator ||") {
{
auto const or_false = _ || false;
REQUIRE(or_false(true));
REQUIRE_FALSE(or_false(false));
}
{
auto const true_or = true || _;
REQUIRE(true_or(true));
REQUIRE(true_or(false));
}
}
///
SECTION("operator ~") {
auto const neg = ~_;
REQUIRE(neg(2U) == ~2U);
}
///
SECTION("operator &") {
auto const and_ = _ & _;
REQUIRE(and_(5U, 3U) == (5U & 3U));
}
SECTION("partial operator &") {
{
auto const and_3 = _ & 3U;
REQUIRE(and_3(2U) == (2U & 3U));
}
{
auto const three_and = 3U & _;
REQUIRE(three_and(2U) == (3U & 2U));
}
}
///
SECTION("operator |") {
auto const or_ = _ | _;
REQUIRE(or_(5U, 3U) == (5U | 3U));
}
SECTION("partial operator |") {
{
auto const or_3 = _ | 3U;
REQUIRE(or_3(2U) == (2U | 3U));
}
{
auto const three_or = 3U | _;
REQUIRE(three_or(2U) == (3U | 2U));
}
}
///
SECTION("operator ^") {
auto const xor_ = _ ^ _;
REQUIRE(xor_(5U, 3U) == (5U ^ 3U));
}
SECTION("partial operator ^") {
{
auto const xor_3 = _ ^ 3U;
REQUIRE(xor_3(2U) == (2U ^ 3U));
}
{
auto const three_xor = 3U ^ _;
REQUIRE(three_xor(2U) == (3U ^ 2U));
}
}
SECTION("fn") {
auto const getter = _.fn(&Obj::get);
REQUIRE(getter(Obj{}) == Obj{}.get());
}
SECTION("fn and wildcard") {
{
auto const greater_than_40 = _.fn(&Obj::get) > 40;
REQUIRE(greater_than_40(Obj{}));
}
{
auto const equal = _.fn(&Obj::get) == Obj{}.get();
REQUIRE(equal(Obj{}));
}
{
auto const less_than_40 = _.fn(&Obj::get) < 40;
REQUIRE_FALSE(less_than_40(Obj{}));
}
{
auto const is_greater = _.fn(&Obj::get) > _.fn(&Obj::get);
REQUIRE(is_greater(Obj{10}, Obj{1}));
}
{
auto const is_greater = _.fn(&Obj::get) > _;
REQUIRE(is_greater(Obj{10}, 1));
}
}
// SECTION("fn and wildcard chained") {
// {
// auto const square_greater_than = _.fn(&Obj::square).fn(&Obj::get) > _;
// REQUIRE_FALSE(square_greater_than(Obj{1}, 4));
// REQUIRE(square_greater_than(Obj{3}, 4));
// }
// {
// auto const square_greater_than = _.fn(&Obj::square, &Obj::get) > _;
// REQUIRE_FALSE(square_greater_than(Obj{1}, 4));
// REQUIRE(square_greater_than(Obj{3}, 4));
// }
// }
SECTION("fn and wildcard macros") {
{
auto const value_greater_than = _.fn(CALL(get)) > _;
REQUIRE_FALSE(value_greater_than(Obj{1}, 4));
REQUIRE(value_greater_than(Obj{5}, 4));
}
{
auto const x = 2;
auto const equal = _.fn(CALL_C(mult, x)) == _;
REQUIRE_FALSE(equal(Obj{1}, 3));
REQUIRE(equal(Obj{2}, 4));
}
{
auto const x = 2;
auto const equal = _.fn(CALL_R(mult, x)) == _;
REQUIRE_FALSE(equal(Obj{1}, 3));
REQUIRE(equal(Obj{2}, 4));
}
}
// SECTION("fn and wildcard callable") {
// {
// auto const sum_10_equal_to = _.args(10) == _;
// REQUIRE_FALSE(sum_10_equal_to(Obj{1}, 10));
// REQUIRE(sum_10_equal_to(Obj{1}, 11));
// }
// }
}
You can’t perform that action at this time.
