-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.cpp
233 lines (203 loc) · 5.63 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
/* Test suite.
* tests.cpp
*
* Copyright (c) 2011, Marek Materzok <tilk@tilk.eu>
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The software is provided 'as is', without any warranty.
*/
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include "shift0.hpp"
#define SETREGS "movl %[ebxin], %%ebx\
\n\tmovl %[esiin], %%esi\
\n\tmovl %[ediin], %%edi"
#define GETREGS "movl %%ebx, %[ebxout]\
\n\tmovl %%esi, %[esiout]\
\n\tmovl %%edi, %[ediout]"
#define INREGS(ebx,esi,edi) [ebxin] "m" (ebx), [esiin] "m" (esi), [ediin] "m" (edi)
#define OUTREGS(ebx,esi,edi) [ebxout] "=m" (ebx), [esiout] "=m" (esi), [ediout] "=m" (edi)
#define CLEANEDREGS "ebx", "esi", "edi"
#define CALLERSAVEDREGS "eax", "ecx", "edx"
#define CALLERSAVEDREGS_NOEAX "ecx", "edx"
#define NUMREGS 3
#define BEGIN_TEST_REGISTERS \
int inregs[NUMREGS], outregs[NUMREGS]; \
for (int i = 0; i < NUMREGS; i++) inregs[i] = rand();
#define END_TEST_REGISTERS \
for (int i = 0; i < NUMREGS; i++) \
if (outregs[i] != inregs[i]) abort();
#define TEST_REGISTERS(instr) { \
int inregs[NUMREGS], outregs[NUMREGS]; \
for (int i = 0; i < NUMREGS; i++) inregs[i] = rand(); \
instr; \
for (int i = 0; i < NUMREGS; i++) \
if (outregs[i] != inregs[i]) abort(); \
}
inline void* tested_reset_arg(void* (*f)(void*), void* arg)
{
void* ret;
BEGIN_TEST_REGISTERS
asm volatile(SETREGS : : INREGS(inregs[0], inregs[1], inregs[2]) : );
ret = reset_arg(f, arg);
asm volatile(GETREGS : OUTREGS(outregs[0], outregs[1], outregs[2]) : : CLEANEDREGS);
END_TEST_REGISTERS
return ret;
}
inline void* tested_shift0(void* (*f)(struct cont*))
{
void* ret;
BEGIN_TEST_REGISTERS
asm volatile(SETREGS : : INREGS(inregs[0], inregs[1], inregs[2]) : );
ret = shift0(f);
asm volatile(GETREGS : OUTREGS(outregs[0], outregs[1], outregs[2]) : : CLEANEDREGS);
END_TEST_REGISTERS
return ret;
}
inline void* tested_runcont(struct cont* cont, void* p)
{
void* ret;
BEGIN_TEST_REGISTERS
asm volatile(SETREGS : : INREGS(inregs[0], inregs[1], inregs[2]) : CLEANEDREGS);
ret = runcont(cont, p);
asm volatile(GETREGS : OUTREGS(outregs[0], outregs[1], outregs[2]) : : );
END_TEST_REGISTERS
return ret;
}
inline void* tested_reset_arg_catch(void* (*f)(void*), void* arg)
{
void* ret;
BEGIN_TEST_REGISTERS
asm volatile(SETREGS : : INREGS(inregs[0], inregs[1], inregs[2]) : );
try {
ret = reset_arg(f, arg);
abort();
} catch (int32_t v) {
asm volatile(GETREGS : OUTREGS(outregs[0], outregs[1], outregs[2]) : : CLEANEDREGS);
END_TEST_REGISTERS
return (void*)v;
}
}
int counter;
int32_t value1, value2;
#define RUN_TEST(c, t) { \
value1 = rand(); value2 = rand(); \
counter = 0; \
t; \
assert(counter==c); \
}
void throw_scramble(int32_t v)
{
BEGIN_TEST_REGISTERS
asm volatile(SETREGS : : INREGS(inregs[0], inregs[1], inregs[2]) : CLEANEDREGS);
throw v;
END_TEST_REGISTERS
}
void* test1(void* a)
{
assert((int32_t)a == value1);
counter++;
return (void*)value1;
}
void* test2x(struct cont *p)
{
counter++;
freecont(p);
return (void*)value1;
}
void* test2(void* a)
{
assert((int32_t)a == value1);
counter++;
tested_shift0(test2x);
counter++;
return 0;
}
void* test3x(struct cont *p)
{
counter++;
assert((int32_t)tested_runcont(p, (void*)3) == 3+value2);
assert((int32_t)tested_runcont(p, (void*)4) == 4+value2);
assert((int32_t)tested_runcont(p, (void*)5) == 5+value2);
counter++;
freecont(p);
return (void*)value1;
}
void* test3(void* a)
{
assert((int32_t)a == value1);
counter++;
void* v = tested_shift0(test3x);
counter++;
return (void*)((int32_t)v+value2);
}
void* test4(void* a)
{
assert((int32_t)a == value1);
counter++;
throw_scramble(value1);
}
void* test5x(struct cont *c)
{
counter++;
freecont(c);
throw_scramble(value1);
}
void* test5(void* a)
{
assert((int32_t)a == value1);
counter++;
tested_shift0(test5x);
}
void* test6x(struct cont *c)
{
counter++;
try {
tested_runcont(c, (void*)0);
tested_runcont(c, (void*)1);
abort();
} catch (int32_t v) {
assert(v == value1);
}
tested_runcont(c, (void*)0);
tested_runcont(c, (void*)1);
}
void* test6(void* a)
{
assert((int32_t)a == value1);
counter++;
void* ret = tested_shift0(test6x);
if (ret) {
counter++;
throw_scramble(value1);
}
}
struct fobj1 {
char operator()(s0::cont<int, char> *c) {
return 'b' + (*c)(1) - 'a';
}
};
struct fobj {
char operator()() {
fobj1 xobj;
int x = s0::shift0<int, char>(xobj);
return 'a' + x;
};
};
fobj tfobj;
int main()
{
RUN_TEST(1, assert((int32_t)tested_reset_arg(test1, (void*)value1) == value1));
RUN_TEST(2, assert((int32_t)tested_reset_arg(test2, (void*)value1) == value1));
RUN_TEST(6, assert((int32_t)tested_reset_arg(test3, (void*)value1) == value1));
RUN_TEST(1, assert((int32_t)tested_reset_arg_catch(test4, (void*)value1) == value1));
RUN_TEST(2, assert((int32_t)tested_reset_arg_catch(test5, (void*)value1) == value1));
RUN_TEST(4, assert((int32_t)tested_reset_arg_catch(test6, (void*)value1) == value1));
char c = s0::reset<char,fobj>(tfobj); assert(c == 'c');
}