guide.md: step9,A stubs. TODO. More vector tests.
[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
31690700
JM
46
47
48;; Testing 1-way if form
49(if false (+ 1 7))
50;=>nil
51(if nil 8 7)
52;=>7
53(if true (+ 1 7))
54;=>8
55
56
57;; Testing basic conditionals
58(= 2 1)
59;=>false
60(= 1 1)
61;=>true
62(= 1 2)
63;=>false
64(= 1 (+ 1 1))
65;=>false
66(= 2 (+ 1 1))
67;=>true
c30efef4 68(= nil 1)
f947d503
JM
69;=>false
70(= nil nil)
c30efef4 71;=>true
31690700
JM
72
73(> 2 1)
74;=>true
75(> 1 1)
76;=>false
77(> 1 2)
78;=>false
79
80(>= 2 1)
81;=>true
82(>= 1 1)
83;=>true
84(>= 1 2)
85;=>false
86
87(< 2 1)
88;=>false
89(< 1 1)
90;=>false
91(< 1 2)
92;=>true
93
94(<= 2 1)
95;=>false
96(<= 1 1)
97;=>true
98(<= 1 2)
99;=>true
100
101
102;; Testing equality
103(= 1 1)
104;=>true
105(= 0 0)
106;=>true
107(= 1 0)
108;=>false
109(= "" "")
110;=>true
111(= "abc" "")
112;=>false
113(= "" "abc")
114;=>false
115(= "abc" "def")
116;=>false
117
118(= (list) (list))
119;=>true
120(= (list 1 2) (list 1 2))
121;=>true
122(= (list 1) (list))
123;=>false
124(= (list) (list 1))
125;=>false
126(= 0 (list))
127;=>false
128(= (list) 0)
129;=>false
130(= (list) "")
131;=>false
132(= "" (list))
133;=>false
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
162
163;; Testing variable length arguments
164
165( (fn* (& more) (count more)) 1 2 3)
166;=>3
167( (fn* (& more) (count more)) 1)
168;=>1
169( (fn* (& more) (count more)) )
170;=>0
171( (fn* (a & more) (count more)) 1 2 3)
172;=>2
173( (fn* (a & more) (count more)) 1)
174;=>0
175
176
177;; Testing language defined not function
178(not false)
179;=>true
180(not true)
181;=>false
182(not "a")
183;=>false
184(not 0)
185;=>false
186
187
188;; Testing do form
189(do (prn "prn output1"))
190; "prn output1"
191;=>nil
192(do (prn "prn output2") 7)
193; "prn output2"
194;=>7
195(do (prn "prn output1") (prn "prn output2") (+ 1 2))
196; "prn output1"
197; "prn output2"
198;=>3
199
200(do (def! a 6) 7 (+ a 8))
201;=>14
202a
203;=>6
204
205
206;; Testing recursive sumdown function
207(def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown (- N 1))) 0)))
208(sumdown 1)
209;=>1
210(sumdown 2)
211;=>3
212(sumdown 6)
213;=>21
214
215
216;; Testing recursive fibonacci function
217(def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))
218(fib 1)
219;=>1
220(fib 2)
221;=>2
222(fib 4)
223;=>5
224(fib 10)
225;=>89
9af8aee6 226
a5a66058
JM
227;; -----------------------------------------------------
228
229;; Testing string quoting
230
231""
232;=>""
233
234"abc"
235;=>"abc"
236
237"abc def"
238;=>"abc def"
239
240"\""
241;=>"\""
242
243
244;; Testing pr-str
245
246(pr-str)
247;=>""
248
249(pr-str "")
250;=>"\"\""
251
252(pr-str "abc")
253;=>"\"abc\""
254
255(pr-str "abc def" "ghi jkl")
256;=>"\"abc def\" \"ghi jkl\""
257
258(pr-str "\"")
259;=>"\"\\\"\""
260
261(pr-str (list 1 2 "abc" "\"") "def")
262;=>"(1 2 \"abc\" \"\\\"\") \"def\""
263
264
265;; Testing str
266
267(str)
268;=>""
269
270(str "")
271;=>""
272
273(str "abc")
274;=>"abc"
275
276(str "\"")
277;=>"\""
278
279(str 1 "abc" 3)
280;=>"1abc3"
281
282(str "abc def" "ghi jkl")
283;=>"abc defghi jkl"
284
285;;; TODO: get this working properly
286;;;(str (list 1 2 "abc" "\"") "def")
287;;;;=>"(1 2 \"abc\" \"\\\"\")def"
288
289
290;; Testing prn
291(prn)
292;
293;=>nil
294
295(prn "")
296; ""
297;=>nil
298
299(prn "abc")
300; "abc"
301;=>nil
302
303(prn "abc def" "ghi jkl")
304; "abc def" "ghi jkl"
305
306(prn "\"")
307; "\""
308;=>nil
309
310(prn (list 1 2 "abc" "\"") "def")
311; (1 2 "abc" "\"") "def"
312;=>nil
313
314
315;; Testing println
316(println)
317;
318;=>nil
319
320(println "")
321;
322;=>nil
323
324(println "abc")
325; abc
326;=>nil
327
328(println "abc def" "ghi jkl")
329; abc def ghi jkl
330
331(println "\"")
332; "
333;=>nil
334
335(println (list 1 2 "abc" "\"") "def")
336; (1 2 abc ") def
337;=>nil
338
9af8aee6
JM
339;;
340;; -------- Optional Functionality --------
341
b8ee29b2
JM
342;; Testing keywords
343(= :abc :abc)
344;=>true
345(= :abc :def)
346;=>false
347(= :abc ":abc")
348;=>false
349
9af8aee6
JM
350;; Testing vector truthiness
351(if [] 7 8)
352;=>7
353
ffd31966
JM
354;; Testing vector functions
355(count [1 2 3])
356;=>3
357(empty? [1 2 3])
358;=>false
359(empty? [])
360;=>true
361(list? [4 5 6])
362;=>false
363
9af8aee6
JM
364;; Testing vector equality
365(= [] (list))
366;=>true
367(= (list 1 2) [1 2])
368;=>true
369(= (list 1) [])
370;=>false
371(= [] [1])
372;=>false
373(= 0 [])
374;=>false
375(= [] 0)
376;=>false
377(= [] "")
378;=>false
379(= "" [])
380;=>false
381
06fef9b5
JM
382;; Testing vector parameter lists
383( (fn* [] 4) )
384;=>4
385( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
386;=>8