-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
176 lines (138 loc) · 4.17 KB
/
tests.py
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
import pytest
from lisp import tokenize, parse, Lisp
META_LISP = open('meta.lisp').read()
def meta_eval(meta_code, env=None):
"""Evaluate an expression using MetaLISP - LISP in LISP."""
source = META_LISP + meta_code
return Lisp(env).eval(source)
def e(source):
"""Shortcut for evaluating expressions in tests."""
return Lisp().eval(source)
def p(source):
"""Shortcut for parsing a single expression in tests."""
expressions = parse(source)
assert len(expressions) == 1
return expressions[0]
def test_tokenizer():
source = '((some atoms) (could be (here)))'
tokens = ['(', '(', 'some', 'atoms', ')', '(', 'could', 'be', '(', 'here', ')', ')', ')']
assert tokenize(source) == tokens
def test_parser():
assert p("'a") == ['quote', 'a']
assert p('b') == 'b'
assert p('()') == []
assert p('(c)') == ['c']
assert p('(c d)') == ['c', 'd']
assert p('((e) f)') == [['e'], 'f']
assert p('(g (h))') == ['g', ['h']]
assert p('(g (h) ())') == ['g', ['h'], []]
assert parse('(g) (h)') == [['g'], ['h']]
assert parse("(label a 'b) (wrap a)") == [['label', 'a', ['quote', 'b']], ['wrap', 'a']]
with pytest.raises(AssertionError):
parse('(g(')
# These tests work in both "LISP in Python" implementation and "LISP in "LISP in Python"" :)
base_tests = [
"(quote a) --> a",
"'a --> a",
"''a --> (quote a)",
"''() --> (quote ())",
"(atom (quote a)) --> t",
"(atom (quote (a b c))) --> ()",
"(atom (quote ())) --> t",
"(atom (atom (quote a))) --> t",
"(atom (quote (atom (quote a)))) --> ()",
"(eq 'a 'a) --> t",
"(eq 'a 'b) --> ()",
"(eq '() '()) --> t",
"(eq '(a) '(a)) --> ()",
"(car '(a b c)) --> a",
"(cdr '(a b c)) --> (b c)",
"(cons 'a '(b c)) --> (a b c)",
"(cons 'a (cons 'b (cons 'c '()))) --> (a b c)",
"""
(cond ((eq 'a 'b) 'first)
((atom 'a) 'second))
--> second
""",
"""
((lambda (x) x) 'z)
--> z
""",
"""
((lambda (x) (cons x '(b c))) 'z)
--> (z b c)
""",
]
# Next tests are multi-line and more tricky (with arithmetic inside!)
# so they work only in the first level of Matrix :)
hard_tests = [
"""
(label l (a b c))
(cdr l)
--> (b c)
""",
"""
(label l (a b c))
(label REST cdr)
(REST l)
--> (b c)
""",
"""
(label wrap (lambda (x) (cons x '())))
(wrap 'wrap_me)
--> (wrap_me)
""",
"""
(defun separate (lst)
(cond
((eq (cdr lst) '()) lst)
('t (cons (car lst) (cons '| (separate (cdr lst)))))))
(separate '(a b c))
--> (a | b | c)
""",
"(add 1 2 3 4 5) --> 15",
"(sub 10 3) --> 7",
"(lt 3 5) --> t",
"(lt 7 5) --> ()",
"""
(label + add)
(label - sub)
(label < lt)
(defun fib (n)
(cond ((< n 2) n)
('t (+ (fib (- n 1))
(fib (- n 2))))))
(fib 10)
--> 55
""",
]
# This couple of tests was made to check the special form of (label f (lambda ...))
# expression which was implemented in the canonical McCarthy's LISP in LISP.
# It allows us to create recursive functions.
meta_tests = [
"""
((label wrap (lambda (x) (cons x '()))) 'wrap_me)
--> (wrap_me)
""",
"""
((label separate (lambda (lst)
(cond
((eq (cdr lst) '()) lst)
('t (cons (car lst) (cons '| (separate (cdr lst))))))))
'(a b c))
--> (a | b | c)
"""
]
def make_params(*test_packs):
return [pair.split("-->") for pair in sum(test_packs, [])]
@pytest.mark.parametrize("code,result", make_params(base_tests, hard_tests))
def test_eval(code, result):
assert e(code) == p(result)
@pytest.mark.parametrize("code,result", make_params(base_tests, meta_tests))
def test_meta_eval(code, result):
meta_code = "(eval. '{} '())".format(code)
assert meta_eval(meta_code) == p(result)
def test_meta_env_lookup():
meta_code = "(eval. 'x '((x a) (y b)))"
result = meta_eval(meta_code)
assert result == p('a')