/
tests.py
202 lines (166 loc) · 4.66 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
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
import unittest
from parser import Parser, Transform, traverse
import code
import vm
import cStringIO
import runtime
import functools
def compile(source):
p = Parser(source)
visitor = Transform()
ast = p.exprs()
traverse(ast, visitor)
consts = code.generate(ast)
return consts
class run_doc(object):
def __init__(self, results=None):
self.results = results
def __call__(self, func):
results = self.results
@functools.wraps(func)
def wrapper(self, *args, **kwds):
self.run_script(func.__doc__)
if results:
self.check_results(results)
return wrapper
class Lisp(unittest.TestCase):
def check_results(self, expects):
self.outputs.seek(0)
results = [line.strip() for line in self.outputs.readlines()]
self.assertEqual(len(expects), len(results))
for result, expect in zip(results, expects):
self.assertEqual(result, expect)
def run_script(self, s, debug=False):
consts = compile(s)
m = vm.VM(consts, self.env)
if debug:
def debug_on():
m.turn_debug(True)
return [None]
def debug_off():
m.turn_debug(False)
return [None]
self.env["debug_on"] = debug_on
self.env["debug_off"] = debug_off
m.start()
def setUp(self):
outputs = cStringIO.StringIO()
def display(*args):
for arg in args:
outputs.write(str(arg))
outputs.write("\n")
return [None]
env = {}
def assertEqual(*args):
assert len(args) == 2
self.assertEqual(*args[0:2])
return [None]
def newList(*args):
return [runtime.build_list(args)]
env["display"] = display
env["assert"] = assertEqual
env["list"] = newList
self.env = env
self.outputs = outputs
@run_doc()
def test_if(self):
"""
(define (test a) (if (= 0 a) (* 2 3) (+ 2 3)))
(assert (test 0) 6)
(assert (test 2) 5)
"""
@run_doc(["(1 2 3)"])
def test_vargs(self):
"""
(define (x . a) (display a))
(x 1 2 3)
"""
@run_doc()
def test_begin(self):
"""
(define (main)
(begin
(define x 1)
x))
(assert (main) 1)
"""
@run_doc(["1", "2", "((1 . 2) 1 . 2)"])
def test_cons(self):
"""
(define x (cons 1 2))
(display (car x))
(display (cdr x))
(display (cons x x))
"""
@run_doc(["3", "7"])
def test_let(self):
"""
(define (main)
(let ((x 1) (y 2))
(begin
(display (+ x y))
(lambda () (+ x 1))))
(let ((x 3) (y 4))
(begin
(display (+ x y))
y)))
(assert (main) 4)
"""
def test_closure(self):
s = """
(define (mul x y) (* x y))
(define (muln n) (lambda (x) (mul x n)))
(define mul2 (muln 2))
(define mul3 (muln 3))
(debug_on)
(assert (mul2 2) 4)
(debug_off)
(assert (mul3 3) 9)
(assert (mul3 3.3) (* 3 3.3))
"""
self.run_script(s, debug=True)
@run_doc(["(4 1 2 3)"])
def test_uparforvarg(self):
"""
(define (add . a) (lambda (b) (cons b a)))
(define add3 (add 1 2 3))
(display (add3 4))
"""
@run_doc()
def test_binops(self):
"""
(assert 3 (+ 1 2))
(assert 2 (* 1 2))
"""
@run_doc()
def test_py_closure(self):
"""
(assert (list 1 2 3) (list 1 2 3))
"""
@run_doc()
def test_tail_call(self):
"""
(define (sum from to)
(begin
(define (iter from to acc)
(if (> from to)
acc
(iter (+ from 1) to (+ acc from))))
(iter from to 0)))
(assert (sum 1 100000) 5000050000)
"""
class LispList(unittest.TestCase):
def test_sclist2pylist(self):
from runtime import sclist2pylist, build_list
x = [1, 2, 3]
assert x == sclist2pylist(build_list(x))
x = [1, 2, [3, 4]]
assert x == sclist2pylist(build_list(x))
x = [[3, 4], [5, 6]]
assert x == sclist2pylist(build_list(x))
x = [1]
assert x == sclist2pylist(build_list(x))
x = []
assert x == sclist2pylist(build_list(x))
if __name__ == '__main__':
unittest.main()