-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
289 lines (257 loc) · 8.62 KB
/
test.lua
File metadata and controls
289 lines (257 loc) · 8.62 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
-------------------------------------------------------------------------
---- Test functions and storage.
local test = {section={name="", num=0}, subsection={name="", num=0}, test={num=0}}
setmetatable(test, test)
function test:verse(t, short)
if short then
return string.format("[%d.%d.%d]", self.section.num, self.subsection.num, self.test.num)
else
return string.format("[%d.%d.%d - %s]", self.section.num, self.subsection.num, self.test.num, tostring(t))
end
end
function test:__call(name, case, expected)
self.test.num = self.test.num+1
local t, ret = loadstring(case, self:verse(name, true))
if t then
t, ret = pcall(t)
else
print(string.format("%s failed. Uncompilable. \n\t%s\n\t%s",
self:verse(name), tostring(case), tostring(err)))
return false
end
if not t then --runtime error
print(string.format("%s failed. Error. \n\t%s\n\t%s",
self:verse(name), tostring(case), tostring(ret)))
return false
end
if ret == expected then
print(string.format("%s passed.", self:verse(name)))
return true
else
print(string.format("%s Failed. expected '%s', got '%s' instead.",
self:verse(name), tostring(expected), tostring(ret)))
return false
end
return false
end
function test:newsection(name)
self.section = {name=tostring(name), num=self.section.num+1}
self.subsection = {name="", num=0}
self.test.num=0
print(string.format("[%d] - %s", self.section.num, tostring(name)))
end
function test:newsubsection(name)
self.subsection = {name=tostring(name), num=self.subsection.num+1}
self.test.num=0
print(string.format("[%d.%d] - %s", self.section.num, self.subsection.num, tostring(name)))
end
-------------------------------------------------------------------------
---- Actual tests
test:newsection("Test harness")
test:newsubsection("Core - must succeed")
test("true == true",
"return true", true)
test("false == false",
"return false", false)
test("42 == 42",
"return 42", 42)
test("type(42) == 'number'",
"return type(42)", 'number')
test("not 0",
"return not 0", false)
test:newsubsection("Core - must fail.")
test("true == false",
"return true", false)
test("false == nil",
"return false", nil)
test("nil == false",
"return nil", false)
-- actually, this is just here so it doesn't catch anyone by surprise.
test("not not 0",
"return not not 0", false)
-------------------------------------------------------------------------
test:newsection("Bitfield")
bitfield = require 'bitfield'
test:newsubsection("Core")
test("Creation, indexing, and formatting",
"local b = bitfield:new(); return tostring(b)", '0000000000000000b')
test("Initialisation",
"local b = bitfield:new(42); return b.value", 42)
test("Indexing and setting",
"local b = bitfield:new(0); b[2]=1; b[1]=1 return b.value", 3)
test("widths",
"local b = bitfield:new(4, 3); return tostring(b)", '100b')
test:newsubsection("NOT")
test("NOT 0 (simple)",
"local b = bitfield:new(0, 1); b:NOT(); return tostring(b)", '1b')
test("NOT 1 (simple)",
"local b = bitfield:new(1, 1); b:NOT(); return tostring(b)", '0b')
test("NOT 100b",
"local b = bitfield:new(4, 3); b:NOT(); return tostring(b)", '011b')
test:newsubsection("AND")
test("0 AND 0 (simple)",
"local b = bitfield:new(0, 1); b:AND(0); return tostring(b)", '0b')
test("0 AND 1 (simple)",
"local b = bitfield:new(0, 1); b:AND(1); return tostring(b)", '0b')
test("1 AND 0 (simple)",
"local b = bitfield:new(1, 1); b:AND(0); return tostring(b)", '0b')
test("1 AND 1 (simple)",
"local b = bitfield:new(1, 1); b:AND(1); return tostring(b)", '1b')
test:newsubsection("OR")
test("0 OR 0 (simple)",
"local b = bitfield:new(0, 1); b:OR(0); return tostring(b)", '0b')
test("0 OR 1 (simple)",
"local b = bitfield:new(0, 1); b:OR(1); return tostring(b)", '1b')
test("1 OR 0 (simple)",
"local b = bitfield:new(1, 1); b:OR(0); return tostring(b)", '1b')
test("1 OR 1 (simple)",
"local b = bitfield:new(1, 1); b:OR(1); return tostring(b)", '1b')
test:newsubsection("XOR")
test("0 XOR 0 (simple)",
"local b = bitfield:new(0, 1); b:XOR(0); return tostring(b)", '0b')
test("0 XOR 1 (simple)",
"local b = bitfield:new(0, 1); b:XOR(1); return tostring(b)", '1b')
test("1 XOR 0 (simple)",
"local b = bitfield:new(1, 1); b:XOR(0); return tostring(b)", '1b')
test("1 XOR 1 (simple)",
"local b = bitfield:new(1, 1); b:XOR(1); return tostring(b)", '0b')
test:newsubsection("NAND")
test("0 NAND 0 (simple)",
"local b = bitfield:new(0, 1); b:NAND(0); return tostring(b)", '1b')
test("0 NAND 1 (simple)",
"local b = bitfield:new(0, 1); b:NAND(1); return tostring(b)", '1b')
test("1 NAND 0 (simple)",
"local b = bitfield:new(1, 1); b:NAND(0); return tostring(b)", '1b')
test("1 NAND 1 (simple)",
"local b = bitfield:new(1, 1); b:NAND(1); return tostring(b)", '0b')
test:newsubsection("NOR")
test("0 NOR 0 (simple)",
"local b = bitfield:new(0, 1); b:NOR(0); return tostring(b)", '1b')
test("0 NOR 1 (simple)",
"local b = bitfield:new(0, 1); b:NOR(1); return tostring(b)", '0b')
test("1 NOR 0 (simple)",
"local b = bitfield:new(1, 1); b:NOR(0); return tostring(b)", '0b')
test("1 NOR 1 (simple)",
"local b = bitfield:new(1, 1); b:NOR(1); return tostring(b)", '0b')
test:newsubsection("XNOR")
test("0 XNOR 0 (simple)",
"local b = bitfield:new(0, 1); b:XNOR(0); return tostring(b)", '1b')
test("0 XNOR 1 (simple)",
"local b = bitfield:new(0, 1); b:XNOR(1); return tostring(b)", '0b')
test("1 XNOR 0 (simple)",
"local b = bitfield:new(1, 1); b:XNOR(0); return tostring(b)", '0b')
test("1 XNOR 1 (simple)",
"local b = bitfield:new(1, 1); b:XNOR(1); return tostring(b)", '1b')
test:newsubsection("shifts")
test("single shift left (simple)",
"local b = bitfield:new(1); b:shift(1); return b.value", 2)
test("multi shift left (simple)",
"local b = bitfield:new(1); b:shift(4); return b.value", 16)
test("single shift right (simple)",
"local b = bitfield:new(2); b:shift(-1); return b.value", 1)
test("multi shift left (simple)",
"local b = bitfield:new(16); b:shift(-4); return b.value", 1)
test:newsubsection("rolls")
test("single roll left (simple)",
"local b = bitfield:new(1, 3); b:roll(1); return b.value", 2)
test("multi roll left (simple)",
"local b = bitfield:new(1, 3); b:roll(4); return b.value", 2)
test("single roll right (simple)",
"local b = bitfield:new(1, 3); b:roll(-1); return b.value", 4)
test("multi roll right (simple)",
"local b = bitfield:new(2, 3); b:roll(-4); return b.value", 1)
-------------------------------------------------------------------------
test:newsection("FUASSM")
require 'asm'
test:newsubsection("Overview")
test("FUASSM parsing",
"return 'junk'", "useful")
test("FUASSM encoding",
"return 'meh'", "good")
test:newsubsection("Build examples")
test("load 'fuassm-test.asm'",
"local chk = asm.parse(asm.load('examples/fuassm-test.asm')) return #chk",
5)
test("load 'count10.asm'",
"local chk = asm.parse(asm.load('examples/count10.asm')) return not not chk", true)
test("load 'jump.asm'",
"local chk = asm.parse(asm.load('examples/jump.asm')) return not not chk", true)
test("load 'show.asm'",
"local chk = asm.parse(asm.load('examples/show.asm')) return not not chk", true)
test("load 'test.asm'",
"local chk = asm.parse(asm.load('examples/test.asm')) return not not chk", true)
test("load 'mnz-test.asm'",
"local chk = asm.parse(asm.load('examples/mnz-test.asm')) return not not chk", true)
test:newsection("Machine")
require 'machine'
test:newsubsection("Opcodes")
test("MOV - A clearing house forms",
[=[
local m = machine:new(1)
local testp, err = asm.parse[[
MOV $42, A
MOV A, &$42
MOV &$42,A
MOV A, &$42
HLT
]]
assert(testp, err)
m:load(testp)
m:cycle(10)
return m.memory[0x42] == 0x42
]=], true)
test("MOV - B clearing house forms",
[=[
local m = machine:new(1)
local testp, err = asm.parse[[
MOV $42, B
MOV B, &$42
MOV &$42,B
MOV B, &$42
HLT
]]
assert(testp, err)
m:load(testp)
m:cycle(10)
return m.memory[0x42] == 0x42
]=], true)
test("MNZ, conditional set",
[=[
local m = machine:new(1)
local testp, err = asm.parse[[
MOV $01,A
MOV $01,B
EQL A, B, RET
MNZ RET, A, $2a
MOV $00,B
EQL A, B, RET
MNZ RET, A, $42
HLT
]]
assert(testp, err)
m:load(testp)
m:cycle(10)
return (m.memory[0x2a] == 1) and m.memory[0x42] ~= 1
]=], true)
test("INC and DEC, increment and decrement",
[=[
local m = machine:new(1)
local testp, err = asm.parse[[
MOV $00,A
MOV A, ACC
INC ACC
INC ACC
INC ACC
DEC ACC
DEC ACC
MOV ACC, A
MOV A, &42
HLT
]]
assert(testp, err)
m:load(testp)
m:cycle(10)
return m.memory[42] == 1
]=], true)
-------------------------------------------------------------------------
---- End tests