Finish step8
[jackhill/mal.git] / impls / tests / step4_if_fn_do.mal
1 ;; -----------------------------------------------------
2
3
4 ;; Testing list functions
5 (list)
6 ;=>()
7 (list? (list))
8 ;=>true
9 (empty? (list))
10 ;=>true
11 (empty? (list 1))
12 ;=>false
13 (list 1 2 3)
14 ;=>(1 2 3)
15 (count (list 1 2 3))
16 ;=>3
17 (count (list))
18 ;=>0
19 (count nil)
20 ;=>0
21 (if (> (count (list 1 2 3)) 3) 89 78)
22 ;=>78
23 (if (>= (count (list 1 2 3)) 3) 89 78)
24 ;=>89
25
26
27 ;; Testing if form
28 (if true 7 8)
29 ;=>7
30 (if false 7 8)
31 ;=>8
32 (if false 7 false)
33 ;=>false
34 (if true (+ 1 7) (+ 1 8))
35 ;=>8
36 (if false (+ 1 7) (+ 1 8))
37 ;=>9
38 (if nil 7 8)
39 ;=>8
40 (if 0 7 8)
41 ;=>7
42 (if (list) 7 8)
43 ;=>7
44 (if (list 1 2 3) 7 8)
45 ;=>7
46 (= (list) nil)
47 ;=>false
48
49
50 ;; Testing 1-way if form
51 (if false (+ 1 7))
52 ;=>nil
53 (if nil 8)
54 ;=>nil
55 (if nil 8 7)
56 ;=>7
57 (if true (+ 1 7))
58 ;=>8
59
60
61 ;; Testing basic conditionals
62 (= 2 1)
63 ;=>false
64 (= 1 1)
65 ;=>true
66 (= 1 2)
67 ;=>false
68 (= 1 (+ 1 1))
69 ;=>false
70 (= 2 (+ 1 1))
71 ;=>true
72 (= nil 1)
73 ;=>false
74 (= nil nil)
75 ;=>true
76
77 (> 2 1)
78 ;=>true
79 (> 1 1)
80 ;=>false
81 (> 1 2)
82 ;=>false
83
84 (>= 2 1)
85 ;=>true
86 (>= 1 1)
87 ;=>true
88 (>= 1 2)
89 ;=>false
90
91 (< 2 1)
92 ;=>false
93 (< 1 1)
94 ;=>false
95 (< 1 2)
96 ;=>true
97
98 (<= 2 1)
99 ;=>false
100 (<= 1 1)
101 ;=>true
102 (<= 1 2)
103 ;=>true
104
105
106 ;; Testing equality
107 (= 1 1)
108 ;=>true
109 (= 0 0)
110 ;=>true
111 (= 1 0)
112 ;=>false
113 (= true true)
114 ;=>true
115 (= false false)
116 ;=>true
117 (= nil nil)
118 ;=>true
119
120 (= (list) (list))
121 ;=>true
122 (= (list 1 2) (list 1 2))
123 ;=>true
124 (= (list 1) (list))
125 ;=>false
126 (= (list) (list 1))
127 ;=>false
128 (= 0 (list))
129 ;=>false
130 (= (list) 0)
131 ;=>false
132 (= (list nil) (list))
133 ;=>false
134
135
136 ;; Testing builtin and user defined functions
137 (+ 1 2)
138 ;=>3
139 ( (fn* (a b) (+ b a)) 3 4)
140 ;=>7
141 ( (fn* () 4) )
142 ;=>4
143
144 ( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)
145 ;=>8
146
147
148 ;; Testing closures
149 ( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)
150 ;=>12
151
152 (def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))
153 (def! plus5 (gen-plus5))
154 (plus5 7)
155 ;=>12
156
157 (def! gen-plusX (fn* (x) (fn* (b) (+ x b))))
158 (def! plus7 (gen-plusX 7))
159 (plus7 8)
160 ;=>15
161
162 ;; Testing do form
163 (do (prn 101))
164 ;/101
165 ;=>nil
166 (do (prn 102) 7)
167 ;/102
168 ;=>7
169 (do (prn 101) (prn 102) (+ 1 2))
170 ;/101
171 ;/102
172 ;=>3
173
174 (do (def! a 6) 7 (+ a 8))
175 ;=>14
176 a
177 ;=>6
178
179 ;; Testing special form case-sensitivity
180 (def! DO (fn* (a) 7))
181 (DO 3)
182 ;=>7
183
184 ;; Testing recursive sumdown function
185 (def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown (- N 1))) 0)))
186 (sumdown 1)
187 ;=>1
188 (sumdown 2)
189 ;=>3
190 (sumdown 6)
191 ;=>21
192
193
194 ;; Testing recursive fibonacci function
195 (def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))
196 (fib 1)
197 ;=>1
198 (fib 2)
199 ;=>2
200 (fib 4)
201 ;=>5
202
203
204 ;; Testing recursive function in environment.
205 (let* (cst (fn* (n) (if (= n 0) nil (cst (- n 1))))) (cst 1))
206 ;=>nil
207 (let* (f (fn* (n) (if (= n 0) 0 (g (- n 1)))) g (fn* (n) (f n))) (f 2))
208 ;=>0
209
210
211 ;>>> deferrable=True
212 ;;
213 ;; -------- Deferrable Functionality --------
214
215 ;; Testing if on strings
216
217 (if "" 7 8)
218 ;=>7
219
220 ;; Testing string equality
221
222 (= "" "")
223 ;=>true
224 (= "abc" "abc")
225 ;=>true
226 (= "abc" "")
227 ;=>false
228 (= "" "abc")
229 ;=>false
230 (= "abc" "def")
231 ;=>false
232 (= "abc" "ABC")
233 ;=>false
234 (= (list) "")
235 ;=>false
236 (= "" (list))
237 ;=>false
238
239 ;; Testing variable length arguments
240
241 ( (fn* (& more) (count more)) 1 2 3)
242 ;=>3
243 ( (fn* (& more) (list? more)) 1 2 3)
244 ;=>true
245 ( (fn* (& more) (count more)) 1)
246 ;=>1
247 ( (fn* (& more) (count more)) )
248 ;=>0
249 ( (fn* (& more) (list? more)) )
250 ;=>true
251 ( (fn* (a & more) (count more)) 1 2 3)
252 ;=>2
253 ( (fn* (a & more) (count more)) 1)
254 ;=>0
255 ( (fn* (a & more) (list? more)) 1)
256 ;=>true
257
258
259 ;; Testing language defined not function
260 (not false)
261 ;=>true
262 (not nil)
263 ;=>true
264 (not true)
265 ;=>false
266 (not "a")
267 ;=>false
268 (not 0)
269 ;=>false
270
271
272 ;; -----------------------------------------------------
273
274 ;; Testing string quoting
275
276 ""
277 ;=>""
278
279 "abc"
280 ;=>"abc"
281
282 "abc def"
283 ;=>"abc def"
284
285 "\""
286 ;=>"\""
287
288 "abc\ndef\nghi"
289 ;=>"abc\ndef\nghi"
290
291 "abc\\def\\ghi"
292 ;=>"abc\\def\\ghi"
293
294 "\\n"
295 ;=>"\\n"
296
297 ;; Testing pr-str
298
299 (pr-str)
300 ;=>""
301
302 (pr-str "")
303 ;=>"\"\""
304
305 (pr-str "abc")
306 ;=>"\"abc\""
307
308 (pr-str "abc def" "ghi jkl")
309 ;=>"\"abc def\" \"ghi jkl\""
310
311 (pr-str "\"")
312 ;=>"\"\\\"\""
313
314 (pr-str (list 1 2 "abc" "\"") "def")
315 ;=>"(1 2 \"abc\" \"\\\"\") \"def\""
316
317 (pr-str "abc\ndef\nghi")
318 ;=>"\"abc\\ndef\\nghi\""
319
320 (pr-str "abc\\def\\ghi")
321 ;=>"\"abc\\\\def\\\\ghi\""
322
323 (pr-str (list))
324 ;=>"()"
325
326 ;; Testing str
327
328 (str)
329 ;=>""
330
331 (str "")
332 ;=>""
333
334 (str "abc")
335 ;=>"abc"
336
337 (str "\"")
338 ;=>"\""
339
340 (str 1 "abc" 3)
341 ;=>"1abc3"
342
343 (str "abc def" "ghi jkl")
344 ;=>"abc defghi jkl"
345
346 (str "abc\ndef\nghi")
347 ;=>"abc\ndef\nghi"
348
349 (str "abc\\def\\ghi")
350 ;=>"abc\\def\\ghi"
351
352 (str (list 1 2 "abc" "\"") "def")
353 ;=>"(1 2 abc \")def"
354
355 (str (list))
356 ;=>"()"
357
358 ;; Testing prn
359 (prn)
360 ;/
361 ;=>nil
362
363 (prn "")
364 ;/""
365 ;=>nil
366
367 (prn "abc")
368 ;/"abc"
369 ;=>nil
370
371 (prn "abc def" "ghi jkl")
372 ;/"abc def" "ghi jkl"
373
374 (prn "\"")
375 ;/"\\""
376 ;=>nil
377
378 (prn "abc\ndef\nghi")
379 ;/"abc\\ndef\\nghi"
380 ;=>nil
381
382 (prn "abc\\def\\ghi")
383 ;/"abc\\\\def\\\\ghi"
384 nil
385
386 (prn (list 1 2 "abc" "\"") "def")
387 ;/\(1 2 "abc" "\\""\) "def"
388 ;=>nil
389
390
391 ;; Testing println
392 (println)
393 ;/
394 ;=>nil
395
396 (println "")
397 ;/
398 ;=>nil
399
400 (println "abc")
401 ;/abc
402 ;=>nil
403
404 (println "abc def" "ghi jkl")
405 ;/abc def ghi jkl
406
407 (println "\"")
408 ;/"
409 ;=>nil
410
411 (println "abc\ndef\nghi")
412 ;/abc
413 ;/def
414 ;/ghi
415 ;=>nil
416
417 (println "abc\\def\\ghi")
418 ;/abc\\def\\ghi
419 ;=>nil
420
421 (println (list 1 2 "abc" "\"") "def")
422 ;/\(1 2 abc "\) def
423 ;=>nil
424
425
426 ;; Testing keywords
427 (= :abc :abc)
428 ;=>true
429 (= :abc :def)
430 ;=>false
431 (= :abc ":abc")
432 ;=>false
433 (= (list :abc) (list :abc))
434 ;=>true
435
436 ;; Testing vector truthiness
437 (if [] 7 8)
438 ;=>7
439
440 ;; Testing vector printing
441 (pr-str [1 2 "abc" "\""] "def")
442 ;=>"[1 2 \"abc\" \"\\\"\"] \"def\""
443
444 (pr-str [])
445 ;=>"[]"
446
447 (str [1 2 "abc" "\""] "def")
448 ;=>"[1 2 abc \"]def"
449
450 (str [])
451 ;=>"[]"
452
453
454 ;; Testing vector functions
455 (count [1 2 3])
456 ;=>3
457 (empty? [1 2 3])
458 ;=>false
459 (empty? [])
460 ;=>true
461 (list? [4 5 6])
462 ;=>false
463
464 ;; Testing vector equality
465 (= [] (list))
466 ;=>true
467 (= [7 8] [7 8])
468 ;=>true
469 (= [:abc] [:abc])
470 ;=>true
471 (= (list 1 2) [1 2])
472 ;=>true
473 (= (list 1) [])
474 ;=>false
475 (= [] [1])
476 ;=>false
477 (= 0 [])
478 ;=>false
479 (= [] 0)
480 ;=>false
481 (= [] "")
482 ;=>false
483 (= "" [])
484 ;=>false
485
486 ;; Testing vector parameter lists
487 ( (fn* [] 4) )
488 ;=>4
489 ( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
490 ;=>8
491
492 ;; Nested vector/list equality
493 (= [(list)] (list []))
494 ;=>true
495 (= [1 2 (list 3 4 [5 6])] (list 1 2 [3 4 (list 5 6)]))
496 ;=>true