-
Notifications
You must be signed in to change notification settings - Fork 8
/
tests.cpp
243 lines (206 loc) · 5.35 KB
/
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
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
// function hooks.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include "fcf.h"
#include <iostream>
bool calledUpdate = false;
__declspec(noinline) void update()
{
calledUpdate = true;
}
auto oldUpdate = fcf::function(update);
bool calledNewUpdate = false;
__declspec(noinline) void newUpdate()
{
calledNewUpdate = true;
oldUpdate();
}
void test1()
{
// see if the function does what I expect
FCF_ASSERT(!calledUpdate);
update();
FCF_ASSERT(calledUpdate);
calledUpdate = false;
// test simple assignment
FCF_ASSIGN(&update, &newUpdate);
FCF_ASSERT(!calledUpdate && !calledNewUpdate);
update();
FCF_ASSERT(calledUpdate && calledNewUpdate);
calledUpdate = calledNewUpdate = false;
// test lambda assignment
bool calledLambdaUpdate = false;
std::function<void ()> lambdaUpdate = [&calledLambdaUpdate]()
{
calledLambdaUpdate = true;
};
FCF_ASSIGN(&update, lambdaUpdate);
FCF_ASSERT(!calledUpdate && !calledNewUpdate && !calledLambdaUpdate);
update();
FCF_ASSERT(!calledUpdate && !calledNewUpdate && calledLambdaUpdate);
calledLambdaUpdate = false;
// test assigning back
FCF_ASSIGN(&update, oldUpdate);
FCF_ASSERT(!calledUpdate && !calledNewUpdate && !calledLambdaUpdate);
update();
FCF_ASSERT(calledUpdate && !calledNewUpdate && !calledLambdaUpdate);
}
__declspec(noinline) int addOne(int a)
{
return a + 1;
}
void test2()
{
auto oldAddOne = fcf::function(&addOne);
// test lambda assignment
FCF_ASSERT(addOne(1) == 2);
FCF_ASSIGN(&addOne, [](int a) -> int
{
return a + 2;
});
FCF_ASSERT(addOne(1) == 3);
FCF_ASSIGN(&addOne, [&oldAddOne](int a) -> int
{
return oldAddOne(a) + 3;
});
FCF_ASSERT(addOne(1) == 5);
}
struct Bar
{
Bar(int i) : i(i) {}
__declspec(noinline) int foo()
{
return i;
}
__declspec(noinline) virtual int blub(int j)
{
return i + j;
}
int i;
};
struct ReroutedBar
{
int foo()
{
Bar * barthis = reinterpret_cast<Bar *>(this);
return barthis->i - 1;
}
int blubblub(int j)
{
Bar * barthis = reinterpret_cast<Bar *>(this);
return barthis->i - j;
}
};
void test3()
{
// check that fcf::function works for member functions
auto oldFoo = fcf::function(&Bar::foo);
Bar a(10);
FCF_ASSERT(oldFoo(a) == 10);
// check that rerouting works for member functions
FCF_ASSERT(a.foo() == 10);
FCF_ASSIGN(&Bar::foo, &ReroutedBar::foo);
FCF_ASSERT(a.foo() == 9);
// check that rerouting to lambda works for member functions
FCF_ASSIGN(&Bar::foo, [](Bar & bar) -> int
{
return bar.i * 2;
});
FCF_ASSERT(a.foo() == 20);
// check that setting it back works
FCF_ASSIGN(&Bar::foo, oldFoo);
FCF_ASSERT(a.foo() == 10);
}
struct Derived : public Bar
{
Derived() : Bar(1000) {}
__declspec(noinline) virtual int blub(int j) override
{
return i + 2 * j;
}
__declspec(noinline) int blubblub(int j)
{
return i + 3 * j;
}
};
void test4()
{
// check that capturing works
Derived d;
auto oldBlub = fcf::virtual_function(d, &Derived::blub);
FCF_ASSERT(oldBlub(d, 10) == 1020);
// check that rerouting works
FCF_VIRTUAL_ASSIGN(d, &Bar::blub, &Derived::blubblub);
FCF_ASSERT(oldBlub(d, 10) == 1020);
FCF_ASSERT(d.blub(10) == 1030);
Bar & b = d; static_cast<void>(b);
FCF_ASSERT(b.blub(10) == 1030);
// check that rerouting works for unconnected type
FCF_VIRTUAL_ASSIGN(d, &Bar::blub, &ReroutedBar::blubblub);
FCF_ASSERT(d.blub(10) == 990);
// check that rerouting works for lambda functions
FCF_VIRTUAL_ASSIGN(d, &Derived::blub, [](Derived & d, int j) -> int
{
return d.i / j;
});
FCF_ASSERT(d.blub(10) == 100);
// check that setting back works
FCF_VIRTUAL_ASSIGN(d, &Derived::blub, oldBlub);
FCF_ASSERT(d.blub(10) == 1020);
}
struct Foo
{
Foo(int i) : i(i) {}
int plusOne() const { return i + 1; }
int plusTwo() const { return i + 2; }
int plusThree() const { return i + 3; }
int i;
};
void test5()
{
Foo foo(4);
FCF_ASSERT(foo.plusOne() == 5);
FCF_ASSIGN(&Foo::plusOne, &Foo::plusTwo);
FCF_ASSERT(foo.plusOne() == 6);
// check that rerouting back to a rerouted function works
auto oldPlusOne = fcf::function(&Foo::plusOne);
FCF_ASSIGN(&Foo::plusOne, &Foo::plusThree);
FCF_ASSERT(foo.plusOne() == 7);
FCF_ASSERT(oldPlusOne(foo) == 6);
FCF_ASSIGN(&Foo::plusOne, oldPlusOne);
FCF_ASSERT(foo.plusOne() == 6);
}
int test6_overridable()
{
return 5;
}
void test6()
{
FCF_ASSERT(test6_overridable() == 5);
auto first_state = fcf::function(&test6_overridable);
FCF_ASSIGN(&test6_overridable, [] { return 10; });
FCF_ASSERT(test6_overridable() == 10);
auto second_state = fcf::function(&test6_overridable);
FCF_ASSIGN(&test6_overridable, [] { return 15; });
FCF_ASSERT(test6_overridable() == 15);
auto third_state = fcf::function(&test6_overridable);
FCF_ASSIGN(&test6_overridable, second_state);
FCF_ASSERT(test6_overridable() == 10);
FCF_ASSIGN(&test6_overridable, first_state);
FCF_ASSERT(test6_overridable() == 5);
FCF_ASSERT(second_state() == 10);
FCF_ASSERT(third_state() == 15);
FCF_ASSIGN(&test6_overridable, third_state);
FCF_ASSERT(test6_overridable() == 15);
FCF_ASSERT(first_state() == 5);
}
void main()
{
test1();
test2();
test3();
test4();
test5();
test6();
std::cout << "\nPress Return to quit." << std::endl;
std::cin.get();
}