-
Notifications
You must be signed in to change notification settings - Fork 2
/
to_f_tests.cpp
80 lines (75 loc) · 1.52 KB
/
to_f_tests.cpp
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
#include "to_f.h"
#include <cassert>
#include <iostream>
#include <memory>
void tests() {
#undef NDEBUG
// basic test
{
int(*f)(char x, char y) = to_f([&](char x, char y) { return x + y; });
}
// no args
{
bool called = false;
to_f([&]() { called = true; })();
assert(called);
}
// r-value args
{
bool called = false;
to_f([&](int x, std::string y) {
called = true;
assert(x == 10);
assert(y == "test");
})(10, "test");
assert(called);
}
// l-value args
{
bool called = false;
int x = 20;
to_f([&](int &x, std::string y) {
called = true;
assert(x == 20);
assert(y == "test");
x = 100;
})(x, "test");
assert(called);
assert(x == 100);
}
// l-value const args
{
bool called = false;
to_f([&](const int &x, std::string y) {
called = true;
assert(x == 20);
assert(y == "test");
})(20, "test");
assert(called);
}
// C-style out param
{
bool result = false;
to_f([&](bool *result) { *result = true; })(&result);
assert(result);
}
// non-copyable lambda
{
assert(to_f([x = std::make_unique<int>(10)](int y) { return *x + y; })(
100) == 110);
}
// mutable lambda
{
int(*f)(int) = to_f([x = std::make_unique<int>(10)](int y) mutable {
auto z = *x;
x.reset();
return z + y;
});
assert(f(100) == 110);
}
std::cout << "all passed\n";
}
int main() {
std::cout << "- tests -\n";
tests();
}