-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.lua
137 lines (100 loc) · 2.98 KB
/
tests.lua
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
class = require("libs.middleclass")
local LambdaVM = require("lambdavm")
local function run(s)
local lvm = LambdaVM()
lvm.loopback = true
return tostring(lvm:runString(s))
end
local function parse(s, astree)
local lexer = require("lexer")(require("source")(s))
local tokens, error = lexer:tokenize()
if error then
return error
end
local parser = require("parser")(tokens)
local result = parser:parse()
if result.error then
return result.error
end
local ast = result.node
return astree and ast or tostring(ast)
end
local current_test
local fail_state = false
local function assert_func(func, s_in, s_out)
local result = func(s_in)
local success = s_out == result
local symb = success and "\27[32mv\27[0m" or "\27[31mx\27[0m"
print("[" .. symb .. "] | Result: " .. result .. (success and "" or (" | Must be: " .. s_out)))
end
local function test_print(name)
if current_test then
print("--------------------------")
print(fail_state and "FAILURE" or "SUCCESS")
print("================================")
end
if not name then return end
fail_state = false
current_test = name
print(name)
print("--------------------------")
end
print("================================")
test_print("TEST_PARSE_0")
do
--[[
local tree = parse("^x.xyz", true)
tree:treePrint()
]]
assert_func(parse, "(xyz)", "(xy)z")
assert_func(parse, "xyz", "(xy)z")
assert_func(parse, "^x.xyz", "^x.((xy)z)")
assert_func(parse, "^x.xy z ^p.q u", "^x.(((xy)z)(^p.(qu)))")
assert_func(parse, "^xyz.zyx", "^x.(^y.(^z.((zy)x)))")
assert_func(parse, "((^xy.x)(^xy.x))^xy.x", "((^x.(^y.x))(^x.(^y.x)))(^x.(^y.x))")
assert_func(parse, "(^xy.yx) qp", "((^x.(^y.(yx)))q)p")
assert_func(parse, "^x.xz^y.yxz", "^x.((xz)(^y.((yx)z)))")
end
test_print("TEST_SUBS_1")
do
assert_func(run, "(^x.x) y", "y")
end
test_print("TEST_SUBS_2")
do
assert_func(run, "(^x.y) z", "y")
end
test_print("TEST_SUBS_3")
do
assert_func(run, "(^x.xy) z", "zy")
assert_func(run, "(^xy.yx) qp", "pq")
assert_func(run, "(^xy.yxz) qp", "(pq)z")
end
test_print("TEST_SUBS_4")
do
assert_func(run, "(^x.^x.x) z", "^x.x")
assert_func(run, "(^x.^x.y) z", "^x.y")
assert_func(run, "(^x.^x.z) z", "^x.z")
end
test_print("TEST_SUBS_5")
do
assert_func(run, "(^x.^y.y) z", "^y.y")
assert_func(run, "(^x.^y.z) z", "^y.z")
end
test_print("TEST_SUBS_6")
do
assert_func(run, "(^x.^y.x) z", "^y.z")
assert_func(run, "(^x.^y.^f.xx(xx)x) z", "^y.(^f.(((zz)(zz))z))")
end
test_print("TEST_SUBS_7")
do
assert_func(run, "(^x.^y.x) y", "^y0.y")
assert_func(run, "(^x.x^y.yx) (vw)", "(vw)(^y.(y(vw)))")
assert_func(run, "(^yx.yzx) x", "^x0.((xz)x0)")
end
test_print("TEST_EXTRA")
do
print("2 * (if FALSE then 1 else 0) = 0")
print("MULT 2 (FALSE 1 0) = 0")
assert_func(run, "(^mnf.m(nf)) (^fx.f(fx)) ((^xy.y) (^fx.fx) (^fx.x))", "^f.(^x.x)")
end
test_print()