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