-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathWhammTest.v3
255 lines (225 loc) · 7.75 KB
/
WhammTest.v3
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
// Copyright 2024 Wizard authors. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
def T = UnitTests.registerT("whamm:", _, WhammTester.new, _);
def X_ = void(
T("parse1", test_parse1),
T("parse2", test_parse2),
T("call1", test_call1),
T("call2", test_call2),
T("call3", test_call3),
T("imm0", test_imm0),
T("imm1", test_imm1),
T("local0", test_local0),
T("local1", test_local1),
T("arg0", test_arg0),
T("arg1", test_arg1),
T("fail0", test_fail0),
T("fail1", test_fail1),
T("fail2", test_fail2),
T("fail3", test_fail3),
T("opcode0", test_opcode0),
T("opcode1", test_opcode1),
T("opcodes_all", test_opcodes_all),
T("opcode_arg0", test_opcode_arg0),
()
);
class WhammTester(t: Tester) {
def assert_opcode_pattern(expected: (Opcode, Array<WhammParam>), str: string) {
var r = TextReader.new("<input>", str);
var got = Whamm.parseOpcodePattern(r);
if (!r.ok) return report_parse_fail(r);
if (expected.0 != got.0) return t.fail2("expected opcode %s, got %s", expected.0.name, got.0.name);
check_params(expected.1, got.1);
}
def assert_params(expected: Array<WhammParam>, str: string) {
var r = TextReader.new("<input>", str);
var got = Whamm.parseParams(r);
if (!r.ok) return report_parse_fail(r);
check_params(expected, got);
}
def report_parse_fail(r: TextReader) {
Trace.OUT.puts("failed to parse:").ln();
r.renderCurrentLineWithCaret(Trace.OUT, r.error_column);
Trace.OUT.ln();
t.fail1("failed to parse: %s", r.error_msg);
}
def check_params(expected: Array<WhammParam>, got: Array<WhammParam>) {
if (got.length != expected.length) return t.fail2("expected %d params, got %d", expected.length, got.length);
for (i < expected.length) {
if (!equal(expected[i], got[i])) {
var buf = StringBuilder.new();
buf.puts("expected [");
for (i < expected.length) {
if (i > 0) buf.csp();
expected[i].render(buf);
}
buf.puts("], got [");
for (i < got.length) {
if (i > 0) buf.csp();
got[i].render(buf);
}
buf.puts("]");
return t.fail(buf.toString());
}
}
}
def assert_params_fail(expected_msg: string, str: string) {
var r = TextReader.new("<input>", str);
var got = Whamm.parseParams(r);
if (r.ok) return t.fail1("expected parse failure: \"%s\"", str);
if (!Strings.startsWith(r.error_msg, expected_msg)) t.fail2("expected parse failure {%s}, got {%s}", expected_msg, r.error_msg);
}
def equal(a: WhammParam, b: WhammParam) -> bool {
if (a == b) return true;
match (a) {
Imm(orig, i) => match (b) {
Imm(orig, j) => return i == j;
_ => ;
}
Arg(orig, i) => match (b) {
Arg(orig, j) => return i == j;
_ => ;
}
Local(orig, i) => match (b) {
Local(orig, j) => return i == j;
_ => ;
}
Call(target, params) => match (b) {
Call(t2, p2) => {
return Strings.equal(target.image, t2.image) && Arrays.allTrue(params, p2, equal);
}
_ => ;
}
_ => ;
}
return false;
}
}
def PC = WhammParam.Pc;
def FUNC = WhammParam.Func;
def FRAME = WhammParam.FrameAccessor;
def CALL(str: string, params: Array<WhammParam>) -> WhammParam.Call {
return WhammParam.Call(Token.new("<file>", str, 0, 0), params);
}
def IMM = WhammParam.Imm(null, _);
def LOCAL = WhammParam.Local(null, _);
def ARG = WhammParam.Arg(null, _);
def test_parse1(t: WhammTester) {
t.assert_params([PC], "(pc)");
t.assert_params([FUNC], "(fid)");
t.assert_params([FRAME], "(frame)");
}
def test_parse2(t: WhammTester) {
t.assert_params([PC, PC], "(pc,pc)");
t.assert_params([FUNC, PC], "(fid,pc)");
t.assert_params([FRAME, PC], "(frame,pc)");
t.assert_params([PC, FUNC], "(pc, fid)");
t.assert_params([FUNC, FUNC], "(fid, fid)");
t.assert_params([FRAME, FUNC], "(frame, fid)");
t.assert_params([PC, FRAME], "(pc, frame)");
t.assert_params([FUNC, FRAME], "(fid, frame)");
t.assert_params([FRAME, FRAME], "(frame, frame)");
}
def test_call1(t: WhammTester) {
t.assert_params([CALL("foobar", [PC])], "(foobar(pc))");
t.assert_params([CALL("bar", [FUNC])], "(bar(fid))");
t.assert_params([CALL("$BAZ", [FRAME])], "($BAZ(frame))");
t.assert_params([CALL("$BAZ", [FRAME])], "($BAZ(frame))");
t.assert_params([CALL("11_29", [])], "(11_29())");
}
def test_call2(t: WhammTester) {
t.assert_params([CALL("foo", [FUNC]), CALL("bar", [])], "(foo(fid), bar())");
}
def test_call3(t: WhammTester) {
t.assert_params([CALL("foo", [ARG(0)]), LOCAL(0), IMM(0)], "(foo(arg0), local0, imm0))");
t.assert_params([CALL("$BAZ", [LOCAL(1), LOCAL(2), ARG(0), ARG(1)])], "($BAZ(local1, local2, arg0, arg1))");
}
def test_imm0(t: WhammTester) {
t.assert_params([IMM(0)], "(imm0)");
}
def test_imm1(t: WhammTester) {
t.assert_params([IMM(1)], "(imm1)");
t.assert_params([IMM(7)], "(imm7)");
t.assert_params([IMM(22)], "(imm22)");
}
def test_local0(t: WhammTester) {
t.assert_params([LOCAL(0)], "(local0)");
}
def test_local1(t: WhammTester) {
t.assert_params([LOCAL(1)], "(local1)");
t.assert_params([LOCAL(6)], "(local6)");
t.assert_params([LOCAL(99)], "(local99)");
t.assert_params([LOCAL(1273)], "(local1273)");
}
def test_arg0(t: WhammTester) {
t.assert_params([ARG(0)], "(arg0)");
}
def test_arg1(t: WhammTester) {
t.assert_params([ARG(1)], "(arg1)");
t.assert_params([ARG(3)], "(arg3)");
t.assert_params([ARG(888)], "(arg888)");
t.assert_params([ARG(999)], "(arg999)");
}
def test_fail0(t: WhammTester) {
t.assert_params_fail("expected identifier", "(");
t.assert_params_fail("\"(\" expected", "arg");
}
def test_fail1(t: WhammTester) {
t.assert_params_fail("unresolved identifier", "(unknown)");
t.assert_params_fail("unresolved identifier", "(foo)");
t.assert_params_fail("unresolved identifier", "(pc, bar)");
t.assert_params_fail("unresolved identifier", "(pc, $NEN)");
}
def test_fail2(t: WhammTester) {
t.assert_params_fail("expected positive integer", "(arg)");
t.assert_params_fail("expected positive integer", "(imm)");
t.assert_params_fail("expected positive integer", "(local)");
t.assert_params_fail("unresolved", "(pc0)");
t.assert_params_fail("unresolved", "(fid1)");
t.assert_params_fail("unresolved", "(frame2)");
}
def test_fail3(t: WhammTester) {
t.assert_params_fail("", "((");
t.assert_params_fail("", ")(");
t.assert_params_fail("", "(()");
t.assert_params_fail("", "(())");
t.assert_params_fail("", "[]");
t.assert_params_fail("", "(pc");
t.assert_params_fail("", "(pc, fid");
t.assert_params_fail("", "(pc, fid]");
t.assert_params_fail("", "(pc + fid)");
t.assert_params_fail("", "(pc - fid]");
t.assert_params_fail("", "(pc fid]");
t.assert_params_fail("", "(pc(fid)]");
}
def test_opcode0(t: WhammTester) {
var none = Whamm.NO_PARAMS;
t.assert_opcode_pattern((Opcode.NOP, none), "wasm:opcode:nop");
t.assert_opcode_pattern((Opcode.UNREACHABLE, none), "wasm:opcode:unreachable");
t.assert_opcode_pattern((Opcode.END, none), "wasm:opcode:end");
}
def test_opcode1(t: WhammTester) {
var none = Whamm.NO_PARAMS;
t.assert_opcode_pattern((Opcode.NOP, none), "wasm:opcode:nop()");
t.assert_opcode_pattern((Opcode.IF, none), "wasm:opcode:if()");
t.assert_opcode_pattern((Opcode.CALL, none), "wasm:opcode:call()");
}
def test_opcodes_all(t: WhammTester) {
var none = Whamm.NO_PARAMS;
var buf = StringBuilder.new();
for (op in Opcode) {
if (op == Opcode.INVALID) continue;
if (op == Opcode.SELECT_T) continue;
buf.reset();
buf.puts("wasm:opcode:");
buf.puts(op.mnemonic);
t.assert_opcode_pattern((op, none), buf.toString());
}
}
def test_opcode_arg0(t: WhammTester) {
t.assert_opcode_pattern((Opcode.NOP, [FUNC]), "wasm:opcode:nop(fid)");
t.assert_opcode_pattern((Opcode.BR, [PC]), "wasm:opcode:br(pc)");
t.assert_opcode_pattern((Opcode.ELSE, [FRAME]), "wasm:opcode:else(frame)");
t.assert_opcode_pattern((Opcode.END, [LOCAL(0), LOCAL(1)]), "wasm:opcode:end(local0, local1)");
t.assert_opcode_pattern((Opcode.RETURN, [ARG(0)]), "wasm:opcode:return(arg0)");
}