Skip to content
Newer
Older
100644 300 lines (201 sloc) 6.98 KB
724529d added beginner test suite
Danny Yoo authored Jul 12, 2011
1 #lang racket/base
2
3 (require racket/sandbox
4 racket/runtime-path
5 racket/list
6 rackunit
9700dea removing unneeded require
Danny Yoo authored Jul 12, 2011
7 syntax/strip-context)
8
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
9
10 (define-runtime-path language.rkt "language.rkt")
277cd23 got the test suite to use the same instantiations of the modules, so …
Danny Yoo authored Jul 12, 2011
11 (define-runtime-path runtime.rkt "runtime.rkt")
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
12
13 (define my-eval #f)
14 (define output-port #f)
15 (define error-port #f)
16
277cd23 got the test suite to use the same instantiations of the modules, so …
Danny Yoo authored Jul 13, 2011
17 (define my-namespace-specs
18 (let ([specs (sandbox-namespace-specs)])
19 `(,(car specs)
20 ,@(cdr specs)
21 racket/mpair
22 (file ,(path->string runtime.rkt)))))
23
24
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
25 (define (init-my-eval!)
ab2f654 some more
Danny Yoo authored Jul 16, 2011
26 (parameterize ([sandbox-output 'string]
27 [sandbox-namespace-specs my-namespace-specs])
cdc0dca added language helpers to help me generate the right code for datum a…
Danny Yoo authored Jul 12, 2011
28 (set! my-eval (make-evaluator `(file ,(path->string language.rkt))))))
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
29
277cd23 got the test suite to use the same instantiations of the modules, so …
Danny Yoo authored Jul 13, 2011
30
31 (define my-rack-eval
32 (parameterize ([sandbox-namespace-specs my-namespace-specs])
33 (make-evaluator 'racket/base
34 #:requires `((file ,(path->string runtime.rkt))
35 racket/mpair))))
36
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
37 (init-my-eval!)
38
39
40 (define (run-tests tests)
41 (let loop ([last-result #f]
42 [tests (syntax->list tests)])
43 (cond
44 [(empty? tests)
45 (void)]
46 [(eq? (syntax->datum (first tests)) '---)
47 (init-my-eval!)
48 (loop #f (rest tests))]
49 [(eq? (syntax->datum (first tests)) '==>)
50 (let ([src (first tests)]
277cd23 got the test suite to use the same instantiations of the modules, so …
Danny Yoo authored Jul 13, 2011
51 [expected (my-rack-eval (syntax->datum (second tests)))])
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
52 (with-check-info* (list (make-check-location (list (syntax-source src)
53 (syntax-line src)
54 (syntax-column src)
55 (syntax-position src)
56 (syntax-span src))))
57 (lambda ()
58 (check-equal? last-result expected))))
59 (loop #f (rest (rest tests)))]
ab2f654 some more
Danny Yoo authored Jul 16, 2011
60 [(eq? (syntax->datum (first tests)) '==>/stdout)
61 (let ([src (first tests)]
62 [expected (my-rack-eval (syntax->datum (second tests)))])
63 (with-check-info* (list (make-check-location (list (syntax-source src)
64 (syntax-line src)
65 (syntax-column src)
66 (syntax-position src)
67 (syntax-span src))))
68 (lambda ()
69 (check-equal? (get-output my-eval) expected))))
70 (loop #f (rest (rest tests)))]
71
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
72 [else
73 (let ([next-result (my-eval (first tests))])
74 (loop next-result (rest tests)))])))
75
76
77
78 (define program-tests
79 (strip-context
cdc0dca added language helpers to help me generate the right code for datum a…
Danny Yoo authored Jul 13, 2011
80 #'(42 ==> 42
81
82
83 (= foo 13)
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
84 foo ==> 13
277cd23 got the test suite to use the same instantiations of the modules, so …
Danny Yoo authored Jul 13, 2011
85 'foo ==> 'foo
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
86
87 ---
88
89 (+ (+ 1 2)
90 (+ 3 (+ 4 5))) ==> 15
91
92 ---
93
ab2f654 some more
Danny Yoo authored Jul 16, 2011
94 (cons 'f '(a b)) ==> (list->arc-list '(f a b))
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
95 (= f '(a b))
ab2f654 some more
Danny Yoo authored Jul 16, 2011
96 f ==> (list->arc-list '(a b))
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
97
98 ---
99
100
101 (= z (cons (cons 3 4) 5))
102 (= (car (car z)) 'three)
32576ae using separate data structure for cons pairs. car and cdr themselves …
Danny Yoo authored Aug 1, 2011
103 z ==> (arc-cons (arc-cons 'three 4) 5)
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
104
105 (= (cdr z) 'five)
32576ae using separate data structure for cons pairs. car and cdr themselves …
Danny Yoo authored Aug 1, 2011
106 z ==> (arc-cons (arc-cons 'three 4) 'five)
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
107
108 ---
109
110 (def average (x y)
111 (/ (+ x y) 2))
112
113 (average 2 4) ==> 3
114
115 ((fn (x y) (/ (+ x y) 2)) 2 4) ==> 3
116
117
118
119 (= a (fn (x y) (/ (+ x y) 2)))
120
121 (a 17 0) ==> 17/2
122
0e83262 renaming make-str to str, for modern Racket purposes
Danny Yoo authored Jul 12, 2011
123 "hello" ==> (str "hello")
277cd23 got the test suite to use the same instantiations of the modules, so …
Danny Yoo authored Jul 13, 2011
124 ("hello" 3) ==> #\l
125 ("hello" 4) ==> #\o
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
126 ---
127
128
129 (= message "Hello world")
130 (= (message 2) #\x)
0e83262 renaming make-str to str, for modern Racket purposes
Danny Yoo authored Jul 13, 2011
131 message ==> (str "Hexlo world")
cdc0dca added language helpers to help me generate the right code for datum a…
Danny Yoo authored Jul 13, 2011
132
ab2f654 some more
Danny Yoo authored Jul 16, 2011
133 ---
134
135 (let x 1
136 (+ x (* x 2))) ==> 3
137
138
139 (with (x 3 y 4)
140 (sqrt (+ (expt x 2)
141 (expt y 2)))) ==> 5
142
143 ---
144
145 (prn "hello") ==>/stdout '"hello\n"
146
147 ---
148
149 (def average (x y)
150 (prn "my arguments were: " x " " y)
151 (/ (+ x y) 2))
152
153 (average 100 200)
154 ==> 150
155 ==>/stdout '"my arguments were: 100 200\n"
156
157
158 ---
159
160 (if (odd 1) 'a 'b) ==> 'a
161 (if (odd 2) 'a 'b) ==> 'b
162
163
164 ---
165
166 (odd 1) ==> t
167 (odd 2) ==> nil
168
169 ---
170
171 (if (odd 2) 'a) ==> nil
172
173 ---
174
175 (do (prn "hello")
176 (+ 2 3))
177 ==> 5
178 ==>/stdout '"hello\n"
179
180 ---
181
182 (and nil (pr "you'll never see this"))
183 ==> nil
184 ==>/stdout '""
a126b74 adding case
Danny Yoo authored Jul 28, 2011
185
186
ab2f654 some more
Danny Yoo authored Jul 16, 2011
187 ---
188
189 (def mylen (xs)
190 (if (no xs)
191 0
192 (+ 1 (mylen (cdr xs)))))
193
194 (mylen nil)
195 ==> 0
196
197 (mylen '(a b))
198 ==> 2
199
200 ---
a126b74 adding case
Danny Yoo authored Jul 28, 2011
201
202 (when (odd 1) 'odd) ==> 'odd
203 (when (odd 0) 'odd) ==> nil
204
205 ---
ab2f654 some more
Danny Yoo authored Jul 16, 2011
206
207 ;; Re-definition is allowed.
208 (def f (x) (* x x))
209 (f 3) ==> 9
210 (def f (x) (* x x x))
211 (f 3) ==> 27
212
213 ---
214
0226eca more play
Danny Yoo authored Jul 16, 2011
215 (is 'a 'a) ==> t
216 (is 'a 'b) ==> nil
217 (is "foo" "foo") ==> t
218 (iso "foo" "bar") ==> nil
219 (let x (list 'a) (is x x)) ==> t
220 (is (list 'a) (list 'a)) ==> nil
221
222 ---
223
224 (iso 'a 'a) ==> t
225 (iso 'a 'b) ==> nil
226 (iso "foo" "foo") ==> t
227 (iso "foo" "bar") ==> nil
228 (let x (list 'a) (iso x x)) ==> t
229 (iso (list 'a) (list 'a)) ==> t
230 (iso (list 'a) (list 'b)) ==> nil
231
a126b74 adding case
Danny Yoo authored Jul 28, 2011
232
233 ---
234 ;; Case statements
235
236 (def translate (sym)
237 (case sym
238 apple 'mela
239 onion 'cipolla
240 'che?))
241
242 (translate 'apple) ==> 'mela
243 (translate 'syzygy) ==> 'che?
244
0226eca more play
Danny Yoo authored Jul 16, 2011
245
246 ---
a126b74 adding case
Danny Yoo authored Jul 28, 2011
247
0226eca more play
Danny Yoo authored Jul 16, 2011
248 (def foo (x y . z)
249 (list x y z))
250
cb49367 old work
Danny Yoo authored Mar 25, 2012
251 (foo (+ 1 2) (+ 3 4) (+ 5 6) (+ 7 8)) ==> (arc-cons 3
252 (arc-cons 7
253 (arc-cons
254 (arc-cons 11 (arc-cons 15 nil))
255 nil)))
256
0226eca more play
Danny Yoo authored Jul 16, 2011
257
258 ---
259
9d7ac58 very hacky versions of the noation for function composition and negation
Danny Yoo authored Jul 27, 2011
260 ;; Check composition
261 (def f (x)
262 (* x x))
263
264 (def g (x)
265 (+ x x))
266
267 (f:g 3) ==> '36
60cb8b9 fixing some small bugs, added more tests for implicit lambda
Danny Yoo authored Jul 27, 2011
268
32576ae using separate data structure for cons pairs. car and cdr themselves …
Danny Yoo authored Aug 1, 2011
269 (map odd:car '((1 2) (4 5) (7 9))) ==> (arc-cons t (arc-cons nil (arc-cons t nil)))
60cb8b9 fixing some small bugs, added more tests for implicit lambda
Danny Yoo authored Jul 27, 2011
270
9d7ac58 very hacky versions of the noation for function composition and negation
Danny Yoo authored Jul 27, 2011
271 ---
272
273 (def f (x)
60cb8b9 fixing some small bugs, added more tests for implicit lambda
Danny Yoo authored Jul 27, 2011
274 (is x 0))
9d7ac58 very hacky versions of the noation for function composition and negation
Danny Yoo authored Jul 27, 2011
275
276 (f 0) ==> t
277 (f 1) ==> nil
278 (~f 0) ==> nil
279 (~f 1) ==> t
ab2f654 some more
Danny Yoo authored Jul 16, 2011
280
32576ae using separate data structure for cons pairs. car and cdr themselves …
Danny Yoo authored Aug 1, 2011
281 (map ~odd '(1 2 3 4 5)) ==> (arc-cons nil (arc-cons t (arc-cons nil (arc-cons t (arc-cons nil nil)))))
60cb8b9 fixing some small bugs, added more tests for implicit lambda
Danny Yoo authored Jul 27, 2011
282
283 ---
284
97bf43a correcting the behavior of implicit lambda
Danny Yoo authored Mar 25, 2012
285 ;; Implicit lambdas, by using the bracket notation.
286 ([+ _ 10] 3) ==> 13
60cb8b9 fixing some small bugs, added more tests for implicit lambda
Danny Yoo authored Jul 27, 2011
287
32576ae using separate data structure for cons pairs. car and cdr themselves …
Danny Yoo authored Aug 1, 2011
288 (map [+ _ 10] '(1 2 3)) ==> (arc-cons 11 (arc-cons 12 (arc-cons 13 nil)))
60cb8b9 fixing some small bugs, added more tests for implicit lambda
Danny Yoo authored Jul 27, 2011
289
290
97bf43a correcting the behavior of implicit lambda
Danny Yoo authored Mar 26, 2012
291 ---
292
293 (= my-app-zero [_ 0])
294 (my-app-zero (fn (x) x)) ==> 0
295
296
724529d added beginner test suite
Danny Yoo authored Jul 13, 2011
297 )))
298
299
300 (run-tests program-tests)
Something went wrong with that request. Please try again.