R: add step4_if_fn_do and step5_tco.
[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
17(if (> (count (list 1 2 3)) 3) "yes" "no")
18;=>"no"
19(if (>= (count (list 1 2 3)) 3) "yes" "no")
20;=>"yes"
21
22
23;; Testing if form
24(if true 7 8)
25;=>7
26(if false 7 8)
27;=>8
28(if true (+ 1 7) (+ 1 8))
29;=>8
30(if false (+ 1 7) (+ 1 8))
31;=>9
32(if nil 7 8)
33;=>8
34(if 0 7 8)
35;=>7
36(if "" 7 8)
37;=>7
38(if (list) 7 8)
39;=>7
40(if (list 1 2 3) 7 8)
41;=>7
31690700
JM
42
43
44;; Testing 1-way if form
45(if false (+ 1 7))
46;=>nil
47(if nil 8 7)
48;=>7
49(if true (+ 1 7))
50;=>8
51
52
53;; Testing basic conditionals
54(= 2 1)
55;=>false
56(= 1 1)
57;=>true
58(= 1 2)
59;=>false
60(= 1 (+ 1 1))
61;=>false
62(= 2 (+ 1 1))
63;=>true
64
65(> 2 1)
66;=>true
67(> 1 1)
68;=>false
69(> 1 2)
70;=>false
71
72(>= 2 1)
73;=>true
74(>= 1 1)
75;=>true
76(>= 1 2)
77;=>false
78
79(< 2 1)
80;=>false
81(< 1 1)
82;=>false
83(< 1 2)
84;=>true
85
86(<= 2 1)
87;=>false
88(<= 1 1)
89;=>true
90(<= 1 2)
91;=>true
92
93
94;; Testing equality
95(= 1 1)
96;=>true
97(= 0 0)
98;=>true
99(= 1 0)
100;=>false
101(= "" "")
102;=>true
103(= "abc" "")
104;=>false
105(= "" "abc")
106;=>false
107(= "abc" "def")
108;=>false
109
110(= (list) (list))
111;=>true
112(= (list 1 2) (list 1 2))
113;=>true
114(= (list 1) (list))
115;=>false
116(= (list) (list 1))
117;=>false
118(= 0 (list))
119;=>false
120(= (list) 0)
121;=>false
122(= (list) "")
123;=>false
124(= "" (list))
125;=>false
126
31690700
JM
127
128;; Testing builtin and user defined functions
129(+ 1 2)
130;=>3
131( (fn* (a b) (+ b a)) 3 4)
132;=>7
133( (fn* () 4) )
134;=>4
135
0027e8fe
JM
136( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)
137;=>8
138
31690700
JM
139
140;; Testing closures
141( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)
142;=>12
143
144(def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))
145(def! plus5 (gen-plus5))
146(plus5 7)
147;=>12
148
149(def! gen-plusX (fn* (x) (fn* (b) (+ x b))))
150(def! plus7 (gen-plusX 7))
151(plus7 8)
152;=>15
153
154
155;; Testing variable length arguments
156
157( (fn* (& more) (count more)) 1 2 3)
158;=>3
159( (fn* (& more) (count more)) 1)
160;=>1
161( (fn* (& more) (count more)) )
162;=>0
163( (fn* (a & more) (count more)) 1 2 3)
164;=>2
165( (fn* (a & more) (count more)) 1)
166;=>0
167
168
169;; Testing language defined not function
170(not false)
171;=>true
172(not true)
173;=>false
174(not "a")
175;=>false
176(not 0)
177;=>false
178
179
180;; Testing do form
181(do (prn "prn output1"))
182; "prn output1"
183;=>nil
184(do (prn "prn output2") 7)
185; "prn output2"
186;=>7
187(do (prn "prn output1") (prn "prn output2") (+ 1 2))
188; "prn output1"
189; "prn output2"
190;=>3
191
192(do (def! a 6) 7 (+ a 8))
193;=>14
194a
195;=>6
196
197
198;; Testing recursive sumdown function
199(def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown (- N 1))) 0)))
200(sumdown 1)
201;=>1
202(sumdown 2)
203;=>3
204(sumdown 6)
205;=>21
206
207
208;; Testing recursive fibonacci function
209(def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))
210(fib 1)
211;=>1
212(fib 2)
213;=>2
214(fib 4)
215;=>5
216(fib 10)
217;=>89
9af8aee6 218
a5a66058
JM
219;; -----------------------------------------------------
220
221;; Testing string quoting
222
223""
224;=>""
225
226"abc"
227;=>"abc"
228
229"abc def"
230;=>"abc def"
231
232"\""
233;=>"\""
234
235
236;; Testing pr-str
237
238(pr-str)
239;=>""
240
241(pr-str "")
242;=>"\"\""
243
244(pr-str "abc")
245;=>"\"abc\""
246
247(pr-str "abc def" "ghi jkl")
248;=>"\"abc def\" \"ghi jkl\""
249
250(pr-str "\"")
251;=>"\"\\\"\""
252
253(pr-str (list 1 2 "abc" "\"") "def")
254;=>"(1 2 \"abc\" \"\\\"\") \"def\""
255
256
257;; Testing str
258
259(str)
260;=>""
261
262(str "")
263;=>""
264
265(str "abc")
266;=>"abc"
267
268(str "\"")
269;=>"\""
270
271(str 1 "abc" 3)
272;=>"1abc3"
273
274(str "abc def" "ghi jkl")
275;=>"abc defghi jkl"
276
277;;; TODO: get this working properly
278;;;(str (list 1 2 "abc" "\"") "def")
279;;;;=>"(1 2 \"abc\" \"\\\"\")def"
280
281
282;; Testing prn
283(prn)
284;
285;=>nil
286
287(prn "")
288; ""
289;=>nil
290
291(prn "abc")
292; "abc"
293;=>nil
294
295(prn "abc def" "ghi jkl")
296; "abc def" "ghi jkl"
297
298(prn "\"")
299; "\""
300;=>nil
301
302(prn (list 1 2 "abc" "\"") "def")
303; (1 2 "abc" "\"") "def"
304;=>nil
305
306
307;; Testing println
308(println)
309;
310;=>nil
311
312(println "")
313;
314;=>nil
315
316(println "abc")
317; abc
318;=>nil
319
320(println "abc def" "ghi jkl")
321; abc def ghi jkl
322
323(println "\"")
324; "
325;=>nil
326
327(println (list 1 2 "abc" "\"") "def")
328; (1 2 abc ") def
329;=>nil
330
9af8aee6
JM
331;;
332;; -------- Optional Functionality --------
333
334;; Testing vector truthiness
335(if [] 7 8)
336;=>7
337
338;; Testing vector equality
339(= [] (list))
340;=>true
341(= (list 1 2) [1 2])
342;=>true
343(= (list 1) [])
344;=>false
345(= [] [1])
346;=>false
347(= 0 [])
348;=>false
349(= [] 0)
350;=>false
351(= [] "")
352;=>false
353(= "" [])
354;=>false
355
06fef9b5
JM
356;; Testing vector parameter lists
357( (fn* [] 4) )
358;=>4
359( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
360;=>8