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