-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.html
242 lines (214 loc) · 6.48 KB
/
tests.html
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
<html>
<body>
<head>
<title>Test Suite For lmsm.js</title>
<script src="lmsm.js"></script>
</head>
<script>
//=================================================
// Tests Helpers
//=================================================
function assertEqual(value, expected, msg) {
if (value !== expected) {
throw msg || "Expected " + value + " to equal " + expected;
}
}
function assertArraysEqual(value, expected, msg) {
if (value.length !== expected.length) {
throw msg || "Expected " + value + " to equal " + expected;
}
for (let i = 0; i < value.length; i++) {
if (value[i] !== expected[i]) {
throw msg || "Expected " + value + " to equal " + expected;
}
}
}
</script>
<script>
//=================================================
// Tokenizer Tests
//=================================================
function testTokenizerTokenizesEmptyString(){
let tok = new LMSMTokenizer("");
assertEqual(tok.tokenize().length, 0);
}
function testTokenizerTokenizesWhiteSpaceOnly(){
let tok = new LMSMTokenizer(" ");
assertEqual(tok.tokenize().length, 0);
}
function testTokenizerIgnoresComments(){
let tok = new LMSMTokenizer("# a comment");
assertEqual(tok.tokenize().length, 0);
}
function testTokenizerTokenizesBasicToken(){
let tok = new LMSMTokenizer("foo");
let tokens = tok.tokenize();
assertEqual(tokens.length, 1);
assertEqual(tokens[0].value, "foo");
}
function testTokenizerTokenizesTwoBasicToken(){
let tok = new LMSMTokenizer("foo bar");
let tokens = tok.tokenize();
assertEqual(tokens.length, 2);
assertEqual(tokens[0].value, "foo");
assertEqual(tokens[1].value, "bar");
}
function testTokenizerGetsOffsetsRight(){
let tok = new LMSMTokenizer("foo bar");
let tokens = tok.tokenize();
assertEqual(tokens.length, 2);
assertEqual(tokens[0].offset, 0);
assertEqual(tokens[1].offset, 4);
}
function testTokenizerGetsLinesRight(){
let tok = new LMSMTokenizer("foo\n bar");
let tokens = tok.tokenize();
assertEqual(tokens.length, 2);
assertEqual(tokens[0].line, 1);
assertEqual(tokens[1].line, 2);
}
function testTokenizerGetsLineOffsetRight(){
let tok = new LMSMTokenizer("foo\n bar");
let tokens = tok.tokenize();
assertEqual(tokens.length, 2);
assertEqual(tokens[0].lineOffset, 0);
assertEqual(tokens[1].lineOffset, 1);
}
</script>
<script>
//=================================================
// End-To-End Tests
//=================================================
function testEndToEndBasicAssemblyProgramRuns(){
let lmsm = new LittleManStackMachine();
lmsm.assembleAndRun("LDI 1 OUT")
assertEqual(lmsm.output[0], 1);
}
function testEndToEndBasicFirthProgramRuns(){
let lmsm = new LittleManStackMachine();
lmsm.compileAndRun("1 .")
assertEqual(lmsm.output[0], 1);
}
function testEndToEndZeroConditionalWorks(){
let lmsm = new LittleManStackMachine();
lmsm.compileAndRun(`
0
zero?
1 . pop
else
2 . pop
end
1
zero?
1 . pop
else
2 . pop
end
`)
assertArraysEqual(lmsm.output, [1, 2]);
}
function testEndToEndPositiveConditionalWorks(){
let lmsm = new LittleManStackMachine();
lmsm.compileAndRun(`
-1
positive?
1 . pop
else
2 . pop
end
0
positive?
1 . pop
else
2 . pop
end
1
positive?
1 . pop
else
2 . pop
end
`)
assertArraysEqual(lmsm.output, [2, 1, 1]);
}
function testEndToEndFirthLoopRuns(){
let lmsm = new LittleManStackMachine();
lmsm.compileAndRun(`
5
do
.
1 -
dup zero?
stop
end
loop
`)
assertArraysEqual(lmsm.output, [5, 4, 3, 2, 1]);
}
function testEndToEndVariablesWork(){
let lmsm = new LittleManStackMachine();
lmsm.compileAndRun(`
var x
5 x!
x .
`)
assertArraysEqual(lmsm.output, [5]);
}
function testEndToEndVariablesWork(){
let lmsm = new LittleManStackMachine();
lmsm.compileAndRun(`
var x
5 x!
x .
`)
assertArraysEqual(lmsm.output, [5]);
}
function testEndToEndFibFirthProgramRuns(){
let lmsm = new LittleManStackMachine();
lmsm.compileAndRun(`8
fib()
.
##############################################################################
## fib()
## expects a number n on the stack, leaves the nth fib on the stack
##############################################################################
def fib()
dup
zero?
return
end
dup 1 -
zero?
return
end
dup 2 -
fib()
swap 1 -
fib()
+
end`)
assertEqual(lmsm.output[0], 21);
}
</script>
<h1>Test Results:</h1>
<ul id="test-results">
</ul>
<script>
//=================================================
// Tests Harness
//=================================================
for (const symbol in window) {
let testResults = document.getElementById("test-results");
if (symbol.startsWith("test")) {
try {
window[symbol]();
testResults.innerHTML += `<li style='color: darkgreen'>PASSED: ${symbol}()</li>`
} catch (e) {
console.log(e)
testResults.innerHTML += `<li style='color: red'>FAILED: ${symbol}() - ${e}</li>`
}
}
}
</script>
</body>
</html>