-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional_test.cpp
71 lines (52 loc) · 1.23 KB
/
functional_test.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
#include <gtest/gtest.h>
#include <sps/memory>
#include <sps/functional.hpp>
int f(int a, int b, int c, int d) {
return a + b + c - d;
}
int g(int a) {
return 1 + a;
}
int h() {
return 1;
}
int i(int a, int b, int c, int d) {
return 1 + 2*a + b + c + d;
}
TEST(functional_test, prebinder) {
using sps::trailing_bind;
using sps::prebind;
auto a1 = trailing_bind(f, 1);
int result = a1(3, 8, 13);
ASSERT_EQ(result, -1);
// completely bound
auto a = prebind(g, 1);
ASSERT_EQ(a(),2);
// nested bind by reference
auto b = prebind(g, a);
ASSERT_EQ(b(),3);
std::get<1>(a.closure) = 2;
// bind to prebinder
auto c = prebind(b);
ASSERT_EQ(c(),4);
// nested bind of temp to temp
auto d = prebind(prebind(g), prebind(h));
ASSERT_EQ(d(), 2);
// and the one you wanted orginally
auto e = prebind(i, 2, 1, 1);
ASSERT_EQ(e(1), 8);
}
// Stack-allocated
void test(void* ptr) {
size_t a[40]{};
a[0]= sizeof(a);
std::cout << (reinterpret_cast<char const *>(a) - reinterpret_cast<char *>(ptr)) << " " << a[0] << "\n";
}
TEST(abe, test) {
sps::static_function<void(void*)> h(test);
h(&h);
}
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}