-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval_test.go
225 lines (203 loc) · 4.84 KB
/
eval_test.go
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
package mita
import (
"strings"
"testing"
)
func TestIsLawaKucha(t *testing.T) {
for _, tc := range []struct {
gold string
expect bool
}{
{"lakuwa", false},
{"lalawa", true},
{"lakukukuwa", false},
{"lalalalawa", true},
{"lalakulawa", true},
{"kulakucha", true},
{"lalwa", false},
} {
if got := isLaKucha(tc.gold); tc.expect != got {
t.Errorf("%s expect %v got %v", tc.gold, tc.expect, got)
}
}
}
var consTests = []struct {
a, b string
c string
}{
{"a", "b", "(a . b)"},
{"(a . b)", "c", "((a . b) . c)"},
{"a", "(b . c)", "(a . (b . c))"},
}
func TestUpa(t *testing.T) {
for _, test := range consTests {
a := NewParser(strings.NewReader(test.a)).SExpr()
b := NewParser(strings.NewReader(test.b)).SExpr()
c := Upa(a, b)
str := c.SExprString()
if str != test.c {
t.Errorf("upa(%s, %s) = %s, expected %s", test.a, test.b, str, test.c)
}
}
}
func strEval(str string, t *testing.T) string {
p := NewParser(strings.NewReader(str))
list := p.List()
return NewContext(0).Eval(list).String()
}
var upaEvalTests = []struct {
in string
out string
}{
// A nice little set from
// https://medium.com/@aleksandrasays/my-other-car-is-a-cdr-3058e6743c15
{"(upa 1 2)", "(1 . 2)"},
{"(upa 'a (upa 'b (upa 'c '())))", "(a b c)"},
{"(list 'a 'b 'c)", "(a b c)"}, // Original has c, not 'c, can't be right.
{"(upa 1 '(2 3 4))", "(1 2 3 4)"},
{"(upa '(a b c) ())", "((a b c))"},
{"(upa '(a b c) '(d))", "((a b c) d)"},
{"unu", "1"},
{"du", "2"},
{"unudu", "3"},
{"dudu", "4"},
{"mani", "5"},
}
func TestUpaEval(t *testing.T) {
for _, test := range upaEvalTests {
if got := strEval(test.in, t); got != test.out {
t.Errorf("%s = %s, expected %s", test.in, got, test.out)
}
}
}
var stringTests = []struct {
in string
out string
}{
{`"ohla odomu!"`, `"ohla odomu!"`},
}
func TestStrings(t *testing.T) {
for _, test := range stringTests {
if got := strEval(test.in, t); got != test.out {
t.Errorf("%s = %s, expected %s", test.in, got, test.out)
}
}
}
var condEvalTests = []struct {
in string
out string
}{
{"(celi 3 2)", "5"},
{"(movo 3 2)", "1"},
{"(celida 10 2)", "20"},
{"(movoda 6 3)", "2"},
{"(aba 2 3)", "da"},
{"(dala ((aba 2 3) 'UNTA) (da 'ABA))", "UNTA"},
{"(aba 3 2)", "nye"},
{"(dala ((aba 3 2) 'UNTA) (da 'ABA))", "ABA"},
{"(unta 3 2)", "da"},
{"(dala ((unta 3 2) 'UNTA) (da 'ABA))", "UNTA"},
{"(unta 2 3)", "nye"},
{"(dala ((unta 2 3) 'UNTA) (da 'ABA))", "ABA"},
{"(dala ((shato 6 3) 'DA) (da 'NYE))", "NYE"},
{"(dala ((shato 3 3) 'DA) (da 'NYE))", "DA"},
{"(dala ((nyeshato 6 3) 'DA) (da 'NYE))", "DA"},
{"(dala ((nyeshato 3 3) 'DA) (da 'NYE))", "NYE"},
}
func TestCondEval(t *testing.T) {
for _, test := range condEvalTests {
if got := strEval(test.in, t); got != test.out {
t.Errorf("%s = %s, expected %s", test.in, got, test.out)
}
}
}
func TestApply(t *testing.T) {
l := "(mita (x y) (upa (lawa x) y))"
lambda := NewParser(strings.NewReader(l)).List()
a := "((a b) (c d))"
args := NewParser(strings.NewReader(a)).List()
c := NewContext(0)
expr := c.apply(l, lambda, args)
const want = "(a c d)"
if expr.String() != want {
t.Fatal(expr)
}
}
var examples = []struct {
name string
fn string
in string
out string
}{
{
"(yafib)",
`(muhe(
(yafib (mita (si)
(dala ((shato si 0) 0)
(da (dala ((aba si du) unu)
(da (celi (yafib(movo si du)) (yafib(movo si unu))))
))
)
))
))`,
"(yafib 10)",
"55",
},
{
"(testlalalakukucha)",
`(muhe(
(testlalalakukucha (mita (si) (lalalakukucha si)))
))`,
"(testlalalakukucha '((1 2) (3 4) ((5 6)) (7 8)))",
"5",
},
//(da (celi (yafib (movo si unu)) (yafib (movo si du)))))
}
func TestExample(t *testing.T) {
for _, test := range examples {
c := NewContext(0)
p := NewParser(strings.NewReader(test.fn))
l := p.List()
t.Log(l)
if got := c.Eval(l).String(); got != test.name {
t.Errorf("%s = %s, expected %s", test.fn, got, test.name)
}
p = NewParser(strings.NewReader(test.in))
l = p.List()
t.Log(l)
if got := c.Eval(l).String(); got != test.out {
t.Errorf("%s = %s, expected %s", test.in, got, test.out)
}
}
}
func TestStackTrace(t *testing.T) {
const prog = `
(muhe(
(error (mita (x)
(dala ((shato x 0) (movoda 0 0))
(da (error (movo x 1)))
)
))
))`
const crash = `(error 5)`
c := NewContext(0)
p := NewParser(strings.NewReader(prog))
if got := c.Eval(p.List()).String(); got != "(error)" {
t.Fatal("did not declare error")
}
p = NewParser(strings.NewReader(crash))
defer func() {
e := recover()
_, ok := e.(Error)
if !ok {
t.Fatal("no error")
}
const expect = "stack: (error 0) (error 1) (error 2) (error 3) (error 4) (error 5)"
stack := c.StackTrace()
if strings.Join(strings.Fields(stack), " ") != expect {
t.Fatal(stack)
}
}()
c.Eval(p.List())
t.Fatal("did not crash")
}