All: don't ignore */mal. Fixes #99
[jackhill/mal.git] / tests / step4_if_fn_do.mal
1 ;; -----------------------------------------------------
2
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 (count (list))
18 ;=>0
19 (count nil)
20 ;=>0
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
46 (= (list) nil)
47 ;=>false
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
70 (= nil 1)
71 ;=>false
72 (= nil nil)
73 ;=>true
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
113 (= "abc" "")
114 ;=>false
115 (= "" "abc")
116 ;=>false
117 (= "abc" "def")
118 ;=>false
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
132 (= (list) "")
133 ;=>false
134 (= "" (list))
135 ;=>false
136
137
138 ;; Testing builtin and user defined functions
139 (+ 1 2)
140 ;=>3
141 ( (fn* (a b) (+ b a)) 3 4)
142 ;=>7
143 ( (fn* () 4) )
144 ;=>4
145
146 ( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)
147 ;=>8
148
149
150 ;; Testing closures
151 ( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)
152 ;=>12
153
154 (def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))
155 (def! plus5 (gen-plus5))
156 (plus5 7)
157 ;=>12
158
159 (def! gen-plusX (fn* (x) (fn* (b) (+ x b))))
160 (def! plus7 (gen-plusX 7))
161 (plus7 8)
162 ;=>15
163
164
165 ;; Testing variable length arguments
166
167 ( (fn* (& more) (count more)) 1 2 3)
168 ;=>3
169 ( (fn* (& more) (count more)) 1)
170 ;=>1
171 ( (fn* (& more) (count more)) )
172 ;=>0
173 ( (fn* (a & more) (count more)) 1 2 3)
174 ;=>2
175 ( (fn* (a & more) (count more)) 1)
176 ;=>0
177
178
179 ;; Testing language defined not function
180 (not false)
181 ;=>true
182 (not true)
183 ;=>false
184 (not "a")
185 ;=>false
186 (not 0)
187 ;=>false
188
189
190 ;; Testing do form
191 (do (prn "prn output1"))
192 ; "prn output1"
193 ;=>nil
194 (do (prn "prn output2") 7)
195 ; "prn output2"
196 ;=>7
197 (do (prn "prn output1") (prn "prn output2") (+ 1 2))
198 ; "prn output1"
199 ; "prn output2"
200 ;=>3
201
202 (do (def! a 6) 7 (+ a 8))
203 ;=>14
204 a
205 ;=>6
206
207
208 ;; Testing recursive sumdown function
209 (def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown (- N 1))) 0)))
210 (sumdown 1)
211 ;=>1
212 (sumdown 2)
213 ;=>3
214 (sumdown 6)
215 ;=>21
216
217
218 ;; Testing recursive fibonacci function
219 (def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))
220 (fib 1)
221 ;=>1
222 (fib 2)
223 ;=>2
224 (fib 4)
225 ;=>5
226 ;;; Too slow for bash, erlang, make and miniMAL
227 ;;;(fib 10)
228 ;;;;=>89
229
230 ;; -----------------------------------------------------
231
232 ;; Testing string quoting
233
234 ""
235 ;=>""
236
237 "abc"
238 ;=>"abc"
239
240 "abc def"
241 ;=>"abc def"
242
243 "\""
244 ;=>"\""
245
246
247 ;; Testing pr-str
248
249 (pr-str)
250 ;=>""
251
252 (pr-str "")
253 ;=>"\"\""
254
255 (pr-str "abc")
256 ;=>"\"abc\""
257
258 (pr-str "abc def" "ghi jkl")
259 ;=>"\"abc def\" \"ghi jkl\""
260
261 (pr-str "\"")
262 ;=>"\"\\\"\""
263
264 (pr-str (list 1 2 "abc" "\"") "def")
265 ;=>"(1 2 \"abc\" \"\\\"\") \"def\""
266
267
268 ;; Testing str
269
270 (str)
271 ;=>""
272
273 (str "")
274 ;=>""
275
276 (str "abc")
277 ;=>"abc"
278
279 (str "\"")
280 ;=>"\""
281
282 (str 1 "abc" 3)
283 ;=>"1abc3"
284
285 (str "abc def" "ghi jkl")
286 ;=>"abc defghi jkl"
287
288 ;;; TODO: get this working properly
289 ;;;(str (list 1 2 "abc" "\"") "def")
290 ;;;;=>"(1 2 \"abc\" \"\\\"\")def"
291
292
293 ;; Testing prn
294 (prn)
295 ;
296 ;=>nil
297
298 (prn "")
299 ; ""
300 ;=>nil
301
302 (prn "abc")
303 ; "abc"
304 ;=>nil
305
306 (prn "abc def" "ghi jkl")
307 ; "abc def" "ghi jkl"
308
309 (prn "\"")
310 ; "\""
311 ;=>nil
312
313 (prn (list 1 2 "abc" "\"") "def")
314 ; (1 2 "abc" "\"") "def"
315 ;=>nil
316
317
318 ;; Testing println
319 (println)
320 ;
321 ;=>nil
322
323 (println "")
324 ;
325 ;=>nil
326
327 (println "abc")
328 ; abc
329 ;=>nil
330
331 (println "abc def" "ghi jkl")
332 ; abc def ghi jkl
333
334 (println "\"")
335 ; "
336 ;=>nil
337
338 (println (list 1 2 "abc" "\"") "def")
339 ; (1 2 abc ") def
340 ;=>nil
341
342 ;;
343 ;; -------- Optional Functionality --------
344
345 ;; Testing keywords
346 (= :abc :abc)
347 ;=>true
348 (= :abc :def)
349 ;=>false
350 (= :abc ":abc")
351 ;=>false
352
353 ;; Testing vector truthiness
354 (if [] 7 8)
355 ;=>7
356
357 ;; Testing vector functions
358 (count [1 2 3])
359 ;=>3
360 (empty? [1 2 3])
361 ;=>false
362 (empty? [])
363 ;=>true
364 (list? [4 5 6])
365 ;=>false
366
367 ;; Testing vector equality
368 (= [] (list))
369 ;=>true
370 (= (list 1 2) [1 2])
371 ;=>true
372 (= (list 1) [])
373 ;=>false
374 (= [] [1])
375 ;=>false
376 (= 0 [])
377 ;=>false
378 (= [] 0)
379 ;=>false
380 (= [] "")
381 ;=>false
382 (= "" [])
383 ;=>false
384
385 ;; Testing vector parameter lists
386 ( (fn* [] 4) )
387 ;=>4
388 ( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
389 ;=>8