-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_test.go
More file actions
324 lines (294 loc) · 9.27 KB
/
Copy patheval_test.go
File metadata and controls
324 lines (294 loc) · 9.27 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
314
315
316
317
318
319
320
321
322
323
324
package glerp_test
import (
"testing"
"github.com/matryer/is"
"go.e64ec.com/glerp"
)
func TestEval(t *testing.T) {
tests := []struct {
name string
src string
want string
}{
// literals
{"number int", "42", "42"},
{"number float", "3.14", "3.14"},
{"string", `"hello"`, `"hello"`},
{"bool true", "#t", "#t"},
{"bool false", "#f", "#f"},
{"empty list", "'()", "()"},
// arithmetic
{"add", "(+ 1 2)", "3"},
{"add variadic", "(+ 1 2 3 4)", "10"},
{"add zero args", "(+)", "0"},
{"sub", "(- 10 3)", "7"},
{"sub unary", "(- 5)", "-5"},
{"mul", "(* 4 5)", "20"},
{"mul variadic", "(* 2 3 4)", "24"},
{"div", "(/ 10 2)", "5"},
{"nested arith", "(+ (* 2 3) (- 10 4))", "12"},
// comparisons
{"less true", "(< 1 2)", "#t"},
{"less false", "(< 2 1)", "#f"},
{"greater true", "(> 2 1)", "#t"},
{"greater false", "(> 1 2)", "#f"},
{"num eq true", "(= 3 3)", "#t"},
{"num eq false", "(= 3 4)", "#f"},
{"less eq", "(<= 3 3)", "#t"},
{"greater eq", "(>= 4 3)", "#t"},
// logic
{"not false", "(not #f)", "#t"},
{"not true", "(not #t)", "#f"},
{"not truthy", "(not 42)", "#f"},
// define & lookup
{"define var", "(define x 10) x", "10"},
{"define overwrites", "(define x 1) (define x 2) x", "2"},
// lambda
{"lambda call", "((lambda (x) (* x x)) 5)", "25"},
{"lambda zero args", "((lambda () 42))", "42"},
{"lambda zero args multi-body", "((lambda () (define x 7) (* x x)))", "49"},
{"lambda zero args closure", "(define n 10) ((lambda () n))", "10"},
{"lambda zero args returns lambda", `
(define (make-counter)
(define n 0)
(lambda ()
(set! n (+ n 1))
n))
(define inc (make-counter))
(inc) (inc) (inc)
`, "3"},
{
"lambda closure",
"(define make-adder (lambda (n) (lambda (x) (+ n x)))) ((make-adder 3) 7)",
"10",
},
// define function shorthand
{"define fn", "(define (square x) (* x x)) (square 7)", "49"},
{"define fn multi-body", "(define (abs x) (if (< x 0) (- x) x)) (abs -5)", "5"},
// if
{"if true branch", "(if #t 1 2)", "1"},
{"if false branch", "(if #f 1 2)", "2"},
{"if truthy", "(if 42 \"yes\" \"no\")", `"yes"`},
{"if no else true", "(if #t 99)", "99"},
{"if no else false", "(if #f 99)", ""},
// let
{"let basic", "(let ((x 3) (y 4)) (+ x y))", "7"},
{"let shadow", "(define x 10) (let ((x 99)) x)", "99"},
{"let parallel", "(define x 1) (let ((x 2) (y x)) y)", "1"},
// let*
{"let* sequential", "(let* ((x 3) (y (* x 2))) y)", "6"},
// set!
{"set!", "(define x 1) (set! x 99) x", "99"},
// quote
{"quote shorthand", "'(1 2 3)", "(1 2 3)"},
{"quote long form", "(quote (a b c))", "(a b c)"},
{"quote atom", "'hello", "hello"},
// string interpolation
{"interp plain", `$"hello"`, `"hello"`},
{"interp var", `(define name "world") $"Hello {name}!"`, `"Hello world!"`},
{"interp expr", `$"1+2={( + 1 2)}"`, `"1+2=3"`},
{"interp number", `(define n 7) $"n={n}"`, `"n=7"`},
{"interp multi", `(define a "x") (define b "y") $"{a}+{b}"`, `"x+y"`},
// quasiquote / unquote / unquote-splicing
{"quasiquote plain", "`(1 2 3)", "(1 2 3)"},
{"quasiquote atom", "`hello", "hello"},
{"unquote", "(define x 42) `(a ,x c)", "(a 42 c)"},
{"unquote expr", "`(a ,(+ 1 2) c)", "(a 3 c)"},
{"unquote-splicing", "(define xs '(2 3)) `(1 ,@xs 4)", "(1 2 3 4)"},
{"unquote-splicing empty", "`(1 ,@'() 2)", "(1 2)"},
{"quasiquote nested lists", "`((a ,(+ 1 1)) (b ,(+ 2 2)))", "((a 2) (b 4))"},
{"quasiquote hash table", "(define x 42) `{a ,x}", "{a 42}"},
{"quasiquote hash nested", "`{a ,(+ 1 2) b ,(+ 3 4)}", "{a 3 b 7}"},
// do
{"do basic counter", `
(do [(i 0 (+ i 1))]
[(= i 5) i])
`, "5"},
{"do sum", `
(do [(i 1 (+ i 1))
(sum 0 (+ sum i))]
[(> i 10) sum])
`, "55"},
{"do no result expr returns void", `
(do [(i 0 (+ i 1))]
[(= i 3)])
`, ""},
{"do no step keeps value", `
(do [(x 42)
(i 0 (+ i 1))]
[(= i 3) x])
`, "42"},
{"do parallel step update", `
(do [(a 1 b)
(b 2 a)]
[(= a 2) (list a b)])
`, "(2 1)"},
{"do no vars", `
(define x 0)
(do []
[(= x 3) x]
(set! x (+ x 1)))
`, "3"},
{"do body side effects", `
(import :scheme/list)
(define result '())
(do [(i 0 (+ i 1))]
[(= i 3) (reverse result)]
(set! result (cons i result)))
`, "(0 1 2)"},
// values / define-values
{"values single is identity", "(values 42)", "42"},
{"values multiple", "(values 1 2 3)", "(values 1 2 3)"},
{"define-values basic", "(define-values (a b c) (values 1 2 3)) (+ a b c)", "6"},
{"define-values single", "(define-values (x) (values 99)) x", "99"},
{"define-values single non-values", "(define-values (x) 42) x", "42"},
{"define-values from lambda", `
(define (minmax a b)
(if (< a b) (values a b) (values b a)))
(define-values (lo hi) (minmax 7 3))
(list lo hi)
`, "(3 7)"},
// case
{"case match first", `(case 1 ((1) "one") ((2) "two"))`, `"one"`},
{"case match second", `(case 2 ((1) "one") ((2) "two"))`, `"two"`},
{"case multi-datum", `(case 3 ((1 2) "low") ((3 4) "high"))`, `"high"`},
{"case else", `(case 99 ((1) "one") (else "other"))`, `"other"`},
{"case no match", `(case 5 ((1) "one") ((2) "two"))`, ``},
{"case key is expression", `(case (+ 1 1) ((1) "one") ((2) "two"))`, `"two"`},
{"case symbol datum", `(case 'b ((a) 1) ((b) 2) ((c) 3))`, `2`},
{"case bool datum", `(case #t ((#f) "no") ((#t) "yes"))`, `"yes"`},
{"case multi-expr body", `(define x 0) (case 1 ((1) (set! x 10) x))`, `10`},
// square bracket list syntax
{"bracket list literal", "'[1 2 3]", "(1 2 3)"},
{"bracket let bindings", "(let [(x 3) (y 4)] (+ x y))", "7"},
{"bracket let* bindings", "(let* [(x 3) (y (* x 2))] y)", "6"},
{"bracket nested in parens", "(+ 1 [+ 2 3])", "6"},
{"parens nested in brackets", "[+ 1 (+ 2 3)]", "6"},
{
"bracket let quoted value",
"(let [(foo 'foo-value) (bar \"bar value\")] foo)",
"foo-value",
},
// recursion
{"factorial", "(define (fact n) (if (= n 0) 1 (* n (fact (- n 1))))) (fact 5)", "120"},
{
"fibonacci",
"(define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (fib 10)",
"55",
},
// tail call optimization
{"tco tail-recursive loop", `
(define (loop n)
(if (= n 0) "done"
(loop (- n 1))))
(loop 1000000)
`, `"done"`},
{"tco accumulator factorial", `
(define (fact-acc n acc)
(if (= n 0) acc
(fact-acc (- n 1) (* n acc))))
(fact-acc 20 1)
`, "2432902008176640000"},
{"tco mutual recursion", `
(define (even? n)
(if (= n 0) #t (odd? (- n 1))))
(define (odd? n)
(if (= n 0) #f (even? (- n 1))))
(even? 100000)
`, "#t"},
{"tco via and tail position", `
(define (all-positive? lst)
(if (empty? lst) #t
(and (> (car lst) 0) (all-positive? (cdr lst)))))
(all-positive? '(1 2 3 4 5))
`, "#t"},
{"tco via or tail position", `
(define (any-zero? lst)
(if (empty? lst) #f
(or (= (car lst) 0) (any-zero? (cdr lst)))))
(any-zero? '(1 2 3 0 5))
`, "#t"},
{"tco via cond", `
(define (classify n)
(cond
((< n 0) "negative")
((= n 0) "zero")
(else (classify (- n 1)))))
(classify 100000)
`, `"zero"`},
{"tco via let body", `
(define (count-down n)
(let ((x n))
(if (= x 0) "done"
(count-down (- x 1)))))
(count-down 100000)
`, `"done"`},
// higher-order
{"map-like", `
(define (my-map f lst)
(if (empty? lst)
'()
(cons (f (car lst)) (my-map f (cdr lst)))))
(my-map (lambda (x) (* x x)) '(1 2 3 4 5))
`, "(1 4 9 16 25)"},
// begin
{"begin returns last", "(begin 1 2 3)", "3"},
{"begin side effects", "(define x 0) (begin (set! x 1) (set! x 2)) x", "2"},
{"begin empty", "(begin)", ""},
// cond
{"cond first match", "(cond ((= 1 1) \"one\") ((= 1 2) \"two\"))", `"one"`},
{"cond second match", "(cond ((= 1 2) \"one\") ((= 1 1) \"two\"))", `"two"`},
{"cond else", "(cond ((= 1 2) \"no\") (else \"yes\"))", `"yes"`},
{"cond no match", "(cond (#f 1))", ""},
// and / or
{"and all true", "(and 1 2 3)", "3"},
{"and short circuit", "(and 1 #f 3)", "#f"},
{"and empty", "(and)", "#t"},
{"or first true", "(or #f 2 3)", "2"},
{"or all false", "(or #f #f)", "#f"},
{"or empty", "(or)", "#f"},
// export form in user code
{"export declares exports", `
(define (double x) (* x 2))
(export double)
(double 5)
`, "10"},
{"export #t exports all", `
(define (double x) (* x 2))
(export #t)
(double 5)
`, "10"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
is := is.New(t)
env := glerp.NewEnvironment(glerp.DefaultConfig())
results, err := glerp.Eval(tt.src, env)
is.NoErr(err)
is.True(len(results) > 0)
is.Equal(results[len(results)-1].String(), tt.want)
})
}
}
func TestEvalErrors(t *testing.T) {
tests := []struct {
name string
src string
}{
{"unbound var", "x"},
{"wrong arg count", "((lambda (x) x) 1 2)"},
{"car empty list", "(car '())"},
{"cdr empty list", "(cdr '())"},
{"div by zero", "(/ 1 0)"},
{"not a procedure", "(1 2 3)"},
{"set! unbound", "(set! undefined-var 42)"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
is := is.New(t)
env := glerp.NewEnvironment(glerp.DefaultConfig())
_, err := glerp.Eval(tt.src, env)
is.True(err != nil)
})
}
}