-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·313 lines (274 loc) · 9.8 KB
/
Copy pathtest.js
File metadata and controls
executable file
·313 lines (274 loc) · 9.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env qjs
/*
* test_jorth.js
*
* Runs a battery of test cases (extracted from the "Easy Forth" guide)
* against the jorth binary, invoked as:
*
* ./dist/jorth "<forth code>"
*
* Usage:
* qjs -m test_jorth.js // shows every test (default)
* qjs -m test_jorth.js -q // quiet: only show failures + info-only tests
* JORTH=/path/to/jorth qjs -m test_jorth.js
*
* Notes on the port from Python:
* - QuickJS has no subprocess/pipe-with-timeout API as convenient as
* Python's, so this uses os.exec(..., {block:false}) to fork the
* child with stdout/stderr redirected to a temp file, then polls
* os.waitpid(pid, os.WNOHANG) until it exits or the timeout elapses
* (at which point it's SIGKILLed). This sidesteps the fact that
* reading a pipe directly would block indefinitely if the child hangs.
* - "binary not found" detection is best-effort: os.exec() forks
* immediately, so a failed execve() happens in the child. We treat
* an exception from os.exec() itself as the not-found case; if your
* qjs build instead lets the child exit nonzero silently, that will
* just show up as a failed/odd test result instead of the explicit
* "[jorth binary not found ...]" message.
*/
import * as std from "std";
import * as os from "os";
const JORTH = std.getenv("JORTH") || "./dist/jorth";
const TIMEOUT_SECS = 5;
const QUIET =
scriptArgs.indexOf("-q") >= 0 || scriptArgs.indexOf("--quiet") >= 0;
const PASS_MARK = "✅";
const FAIL_MARK = "❌";
const INFO_MARK = "ℹ️";
// expected === null => informational/skip test, just show output
const TESTS = [
// -- basic arithmetic -----------------------------------------------
{ name: "basic-add", code: "1 2 3 + + .", expected: "6" },
{ name: "reverse-polish-mult", code: "5 2 + 10 * .", expected: "70" },
// -- defining words ---------------------------------------------------
{ name: "define-foo", code: ": foo 100 + ; 1000 foo .", expected: "1100" },
{
name: "define-foo-chained",
code: ": foo 100 + ; 1000 foo foo foo .",
expected: "1300",
},
// -- stack manipulation ------------------------------------------------
{ name: "dup", code: "1 2 3 dup . . . .", expected: "3 3 2 1" },
{ name: "drop", code: "1 2 3 drop . .", expected: "2 1" },
{ name: "swap", code: "1 2 3 4 swap . . . .", expected: "3 4 2 1" },
{ name: "over", code: "1 2 3 over . . . .", expected: "2 3 2 1" },
{ name: "rot", code: "1 2 3 rot . . .", expected: "1 3 2" },
// -- output ------------------------------------------------------------
{ name: "dot-sequence", code: "1 . 2 . 3 . 4 5 6 . . .", expected: "1 2 3 6 5 4" },
{ name: "emit-wow", code: "33 119 111 87 emit emit emit emit", expected: "Wow!" },
{
name: "cr-numbers",
code: "cr 100 . cr 200 . cr 300 .",
expected: "\n100 \n200 \n300",
},
{
name: "dot-quote-hello",
code: ': say-hello ." Hello there!" ; say-hello',
expected: "Hello there!",
},
{
name: "print-stack-top",
code:
': print-stack-top cr dup ." The top of the stack is " . ' +
'cr ." which looks like \'" dup emit ." \' in ascii" ; ' +
"48 print-stack-top",
expected: "The top of the stack is 48 \nwhich looks like '0' in ascii "
},
// -- booleans / comparisons ---------------------------------------------
{ name: "equals", code: "3 4 = . 5 5 = .", expected: "0 -1" },
{ name: "less-greater", code: "3 4 < . 3 4 > .", expected: "-1 0" },
{ name: "and", code: "3 4 < 20 30 < and .", expected: "-1" },
{ name: "or", code: "3 4 < 20 30 > or .", expected: "-1" },
{ name: "invert", code: "3 4 < invert .", expected: "0" },
// -- conditionals --------------------------------------------------------
{
name: "if-then-buzz",
code: ': buzz? 5 mod 0 = if ." Buzz" then ; 3 buzz? 4 buzz? 5 buzz?',
expected: "Buzz",
},
{
name: "if-else-then",
code:
': is-it-zero? 0 = if ." Yes!" else ." No!" then ; ' +
"0 is-it-zero? 1 is-it-zero? 2 is-it-zero?",
expected: "Yes!No!No!",
},
// -- do loop ---------------------------------------------------------------
{
name: "do-loop",
code: ": loop-test 10 0 do i . loop ; loop-test",
expected: "0 1 2 3 4 5 6 7 8 9",
},
// -- fizzbuzz (composite) ----------------------------------------------------
{
name: "fizzbuzz",
code:
': fizz? 3 mod 0 = dup if ." Fizz" then ; ' +
': buzz? 5 mod 0 = dup if ." Buzz" then ; ' +
": fizz-buzz? dup fizz? swap buzz? or invert ; " +
": do-fizz-buzz 25 1 do cr i fizz-buzz? if i . then loop ; " +
"do-fizz-buzz",
expected: "\n1 \n2 \nFizz\n4 \nBuzz\nFizz\n7 \n8 \nFizz\nBuzz\n11 \nFizz\n13 \n14 \nFizzBuzz\n16 \n17 \nFizz\n19 \nBuzz\nFizz\n22 \n23 \nFizz"
},
// -- variables and constants ---------------------------------------------
{
name: "variable-store-fetch",
code: "variable balance 123 balance ! balance @ .",
expected: "123",
},
{
name: "variable-question-mark",
code: "variable balance 123 balance ! balance ? 50 balance +! balance ?",
expected: "123 173",
},
{ name: "constant", code: "42 constant answer 2 answer * .", expected: "84" },
// -- arrays (allot) --------------------------------------------------------
{
name: "array-number-word",
code:
"variable numbers 3 cells allot " +
": number ( offset -- addr ) cells numbers + ; " +
"10 0 number ! 20 1 number ! 30 2 number ! 40 3 number ! " +
"2 number ?",
expected: "30",
},
// -- error handling ----------------------------------------------------------
{
name: "stack-underflow",
code: "1 2 3 + + + .",
expected: "Error: Stack underflow",
},
];
function stripTrailingOk(s) {
// jorth appends a trailing ' ok' (REPL-echo convention) after
// successful single-shot runs, e.g. '6 ok'. Strip exactly one
// trailing 'ok' token (plus surrounding whitespace).
s = s.trim();
if (s.endsWith("ok")) {
s = s.slice(0, s.length - "ok".length).trimEnd();
}
return s;
}
function execCapture(args, timeoutSec) {
const tmpPath = `/tmp/jorth_test_${Date.now()}_${Math.floor(
Math.random() * 1e9
)}.out`;
let outFd;
try {
outFd = os.open(tmpPath, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o644);
} catch (e) {
return {
raw: "[failed to create temp output file]",
timedOut: false,
notFound: false,
};
}
let pid;
try {
pid = os.exec(args, {
block: false,
stdout: outFd,
stderr: outFd,
usePath: true,
});
} catch (e) {
os.close(outFd);
os.remove(tmpPath);
return {
raw: `[jorth binary not found at ${args[0]}]`,
timedOut: false,
notFound: true,
};
}
os.close(outFd);
const deadlineMs = Date.now() + timeoutSec * 1000;
let exited = false;
while (Date.now() < deadlineMs) {
const res = os.waitpid(pid, os.WNOHANG);
if (res && res[0] === pid) {
exited = true;
break;
}
os.sleep(25);
}
let timedOut = false;
if (!exited) {
timedOut = true;
try {
os.kill(pid, 9); // SIGKILL
} catch (e) {
/* ignore */
}
os.waitpid(pid, 0);
}
let raw = "";
try {
const f = std.open(tmpPath, "r");
raw = f.readAsString();
f.close();
} catch (e) {
raw = "";
}
os.remove(tmpPath);
if (timedOut) {
return { raw: "[TIMEOUT]", timedOut: true, notFound: false };
}
return { raw, timedOut: false, notFound: false };
}
function runCase(tc) {
const { raw: rawResult } = execCapture([JORTH, "-e", tc.code], TIMEOUT_SECS);
const raw = rawResult.trim();
const normalized = stripTrailingOk(raw);
if (tc.expected === null || tc.expected === undefined) {
return { passed: true, raw, normalized };
}
const passed = normalized === tc.expected.trim();
return { passed, raw, normalized };
}
function repr(s) {
// Rough stand-in for Python's !r formatting on strings.
return JSON.stringify(s);
}
function main() {
print(`🧪 Testing jorth at: ${JORTH}`);
print("=".repeat(60));
let passedCount = 0;
let failedCount = 0;
let skippedCount = 0;
for (const tc of TESTS) {
const { passed, raw, normalized } = runCase(tc);
const isInfoOnly = tc.expected === null || tc.expected === undefined;
if (isInfoOnly) {
skippedCount++;
} else if (passed) {
passedCount++;
} else {
failedCount++;
}
if (QUIET && passed && !isInfoOnly) {
continue;
}
print(`\n--- ${tc.name} ---`);
print(` code: ${tc.code}`);
if (isInfoOnly) {
print(` output: ${repr(raw)}`);
print(` ${INFO_MARK} INFO ONLY (no fixed expected value)`);
continue;
}
print(` expected: ${repr(tc.expected)}`);
print(` actual: ${repr(raw)}`);
if (raw !== normalized) {
print(` actual (ok stripped): ${repr(normalized)}`);
}
print(passed ? ` ${PASS_MARK} PASS` : ` ${FAIL_MARK} FAIL`);
}
print("\n" + "=".repeat(60));
print(
`📊 Results: ${PASS_MARK} ${passedCount} passed, ` +
`${FAIL_MARK} ${failedCount} failed, ` +
`${INFO_MARK} ${skippedCount} info-only, ` +
`${TESTS.length} total`
);
std.exit(failedCount > 0 ? 1 : 0);
}
main();