Merge pull request #532 from dubek/vhdl-fix-defmacro
[jackhill/mal.git] / impls / tests / step4_if_fn_do.mal
CommitLineData
31690700
JM
1;; -----------------------------------------------------
2
31690700
JM
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
b8ee29b2
JM
17(count (list))
18;=>0
19(count nil)
20;=>0
0c04a99f
BH
21(if (> (count (list 1 2 3)) 3) 89 78)
22;=>78
23(if (>= (count (list 1 2 3)) 3) 89 78)
24;=>89
31690700
JM
25
26
27;; Testing if form
28(if true 7 8)
29;=>7
30(if false 7 8)
31;=>8
a68c26af
JM
32(if false 7 false)
33;=>false
31690700
JM
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
31690700
JM
42(if (list) 7 8)
43;=>7
44(if (list 1 2 3) 7 8)
45;=>7
db11c740
C
46(= (list) nil)
47;=>false
31690700
JM
48
49
50;; Testing 1-way if form
51(if false (+ 1 7))
52;=>nil
57ccbb5e
AK
53(if nil 8)
54;=>nil
31690700
JM
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
c30efef4 72(= nil 1)
f947d503
JM
73;=>false
74(= nil nil)
c30efef4 75;=>true
31690700
JM
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
55c25242
DM
113(= true true)
114;=>true
115(= false false)
116;=>true
117(= nil nil)
118;=>true
31690700
JM
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
aeff9873
BH
132(= (list nil) (list))
133;=>false
31690700 134
31690700
JM
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
0027e8fe
JM
144( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)
145;=>8
146
31690700
JM
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
31690700 162;; Testing do form
0c04a99f
BH
163(do (prn 101))
164;/101
31690700 165;=>nil
0c04a99f
BH
166(do (prn 102) 7)
167;/102
31690700 168;=>7
0c04a99f
BH
169(do (prn 101) (prn 102) (+ 1 2))
170;/101
171;/102
31690700
JM
172;=>3
173
174(do (def! a 6) 7 (+ a 8))
175;=>14
176a
177;=>6
178
a7ed71b9 179;; Testing special form case-sensitivity
5e38bc99 180(def! DO (fn* (a) 7))
a7ed71b9
JM
181(DO 3)
182;=>7
31690700
JM
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
9af8aee6 202
46e25689 203
8c0d5c4d 204;; Testing recursive function in environment.
8734e2eb 205(let* (cst (fn* (n) (if (= n 0) nil (cst (- n 1))))) (cst 1))
8c0d5c4d 206;=>nil
8734e2eb 207(let* (f (fn* (n) (if (= n 0) 0 (g (- n 1)))) g (fn* (n) (f n))) (f 2))
8150202b 208;=>0
8c0d5c4d
NB
209
210
a1eb30fc 211;>>> deferrable=True
46e25689 212;;
a1eb30fc 213;; -------- Deferrable Functionality --------
46e25689 214
0c04a99f
BH
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
46e25689
JM
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
e2352e73
TM
262(not nil)
263;=>true
46e25689
JM
264(not true)
265;=>false
266(not "a")
267;=>false
268(not 0)
269;=>false
270
271
a5a66058
JM
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
8d78bc26
JM
288"abc\ndef\nghi"
289;=>"abc\ndef\nghi"
290
291"abc\\def\\ghi"
292;=>"abc\\def\\ghi"
a5a66058 293
8e59b715
DM
294"\\n"
295;=>"\\n"
296
a5a66058
JM
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
8d78bc26
JM
317(pr-str "abc\ndef\nghi")
318;=>"\"abc\\ndef\\nghi\""
319
320(pr-str "abc\\def\\ghi")
321;=>"\"abc\\\\def\\\\ghi\""
322
c4cd2700
DM
323(pr-str (list))
324;=>"()"
325
a5a66058
JM
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
8d78bc26
JM
346(str "abc\ndef\nghi")
347;=>"abc\ndef\nghi"
348
349(str "abc\\def\\ghi")
350;=>"abc\\def\\ghi"
351
c4cd2700
DM
352(str (list 1 2 "abc" "\"") "def")
353;=>"(1 2 abc \")def"
354
c4cd2700
DM
355(str (list))
356;=>"()"
a5a66058 357
a5a66058
JM
358;; Testing prn
359(prn)
f6f5d4f2 360;/
a5a66058
JM
361;=>nil
362
363(prn "")
f6f5d4f2 364;/""
a5a66058
JM
365;=>nil
366
367(prn "abc")
f6f5d4f2 368;/"abc"
a5a66058
JM
369;=>nil
370
371(prn "abc def" "ghi jkl")
f6f5d4f2 372;/"abc def" "ghi jkl"
a5a66058
JM
373
374(prn "\"")
f6f5d4f2 375;/"\\""
a5a66058
JM
376;=>nil
377
8d78bc26 378(prn "abc\ndef\nghi")
f6f5d4f2 379;/"abc\\ndef\\nghi"
8d78bc26
JM
380;=>nil
381
382(prn "abc\\def\\ghi")
f6f5d4f2 383;/"abc\\\\def\\\\ghi"
8d78bc26
JM
384nil
385
a5a66058 386(prn (list 1 2 "abc" "\"") "def")
f6f5d4f2 387;/\(1 2 "abc" "\\""\) "def"
a5a66058
JM
388;=>nil
389
390
391;; Testing println
392(println)
f6f5d4f2 393;/
a5a66058
JM
394;=>nil
395
396(println "")
f6f5d4f2 397;/
a5a66058
JM
398;=>nil
399
400(println "abc")
f6f5d4f2 401;/abc
a5a66058
JM
402;=>nil
403
404(println "abc def" "ghi jkl")
f6f5d4f2 405;/abc def ghi jkl
a5a66058
JM
406
407(println "\"")
f6f5d4f2 408;/"
a5a66058
JM
409;=>nil
410
8d78bc26 411(println "abc\ndef\nghi")
f6f5d4f2
JM
412;/abc
413;/def
414;/ghi
8d78bc26
JM
415;=>nil
416
417(println "abc\\def\\ghi")
f6f5d4f2 418;/abc\\def\\ghi
8d78bc26
JM
419;=>nil
420
a5a66058 421(println (list 1 2 "abc" "\"") "def")
f6f5d4f2 422;/\(1 2 abc "\) def
a5a66058
JM
423;=>nil
424
9af8aee6 425
b8ee29b2
JM
426;; Testing keywords
427(= :abc :abc)
428;=>true
429(= :abc :def)
430;=>false
431(= :abc ":abc")
432;=>false
b9d9e675
DM
433(= (list :abc) (list :abc))
434;=>true
b8ee29b2 435
9af8aee6
JM
436;; Testing vector truthiness
437(if [] 7 8)
438;=>7
439
0067158f
JM
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
ffd31966
JM
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
9af8aee6
JM
464;; Testing vector equality
465(= [] (list))
466;=>true
c46b421a
JM
467(= [7 8] [7 8])
468;=>true
b9d9e675
DM
469(= [:abc] [:abc])
470;=>true
9af8aee6
JM
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
06fef9b5
JM
486;; Testing vector parameter lists
487( (fn* [] 4) )
488;=>4
489( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
490;=>8
4b7f92e5 491
4b7f92e5
DM
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