docker: build script executable. Skip matlab automatically.
[jackhill/mal.git] / tests / step9_try.mal
CommitLineData
31690700
JM
1;;
2;; Testing try*/catch*
3
f5223195
JM
4(try* 123 (catch* e 456))
5;=>123
6
16354bb4
JM
7(try* (abc 1 2) (catch* exc (prn "exc is:" exc))))
8; "exc is:" "'abc' not found"
31690700
JM
9;=>nil
10
16354bb4 11;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
8128c69a
JM
12;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
13;;;; "exc is:" ["data" "foo"] ;;;;=>7
6301e0b6 14;;;;=>7
16354bb4 15
e27782ea
JM
16(try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
17; "err:" (1 2 3)
16354bb4 18;=>7
31690700
JM
19
20(try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
21; "exc:" "my exception"
22;=>7
23
db11c740
C
24;;; Test that throw is a function:
25(try* (map throw [7]) (catch* exc exc))
26;=>7
27
31690700
JM
28
29;;
30;; Testing builtin functions
31
32(symbol? 'abc)
33;=>true
34(symbol? "abc")
35;=>false
36
37(nil? nil)
38;=>true
39(nil? true)
40;=>false
41
42(true? true)
43;=>true
44(true? false)
45;=>false
46(true? true?)
47;=>false
48
49(false? false)
50;=>true
51(false? true)
52;=>false
53
31690700
JM
54;; Testing apply function
55(apply + (list 2 3))
56;=>5
57(apply + 4 (list 5))
58;=>9
59(apply prn (list 1 2 "3" (list)))
60; 1 2 "3" ()
d90c7844
JM
61(apply prn 1 2 (list "3" (list)))
62; 1 2 "3" ()
31690700
JM
63;=>nil
64
65
66;; Testing map function
67(def! nums (list 1 2 3))
68(def! double (fn* (a) (* 2 a)))
69(double 3)
70;=>6
71(map double nums)
72;=>(2 4 6)
f5223195 73(map (fn* (x) (symbol? x)) (list 1 (symbol "two") "three"))
f947d503 74;=>(false true false)
31690700 75
9af8aee6
JM
76;;
77;; Testing read-str and eval
78(read-string "(1 2 (3 4) nil)")
79;=>(1 2 (3 4) nil)
80
89bd4de1
JM
81(read-string "7 ;; comment")
82;=>7
83
6301e0b6 84;;; Differing output, but make sure no fatal error
89bd4de1 85(read-string ";; comment")
6301e0b6 86
89bd4de1 87
9af8aee6
JM
88(eval (read-string "(+ 4 5)"))
89;=>9
90
91;;
92;; Testing readline
93(readline "mal-user> ")
94"hello"
95;=>"\"hello\""
96
97;;
f5223195
JM
98;; ------- Optional Functionality ----------
99;; ------- (Needed for self-hosting) -------
9af8aee6 100
b8ee29b2
JM
101;; Testing symbol and keyword functions
102(symbol? :abc)
103;=>false
104(symbol? 'abc)
105;=>true
106(symbol? "abc")
107;=>false
108(symbol? (symbol "abc"))
109;=>true
110(keyword? :abc)
111;=>true
112(keyword? 'abc)
113;=>false
114(keyword? "abc")
115;=>false
116(keyword? (keyword "abc"))
117;=>true
118
9af8aee6
JM
119;; Testing sequential? function
120
121(sequential? (list 1 2 3))
122;=>true
123(sequential? [15])
124;=>true
125(sequential? sequential?)
126;=>false
127(sequential? nil)
128;=>false
129(sequential? "abc")
130;=>false
31690700 131
f5223195
JM
132
133;; Testing map function with vectors
134(map (fn* (a) (* 2 a)) [1 2 3])
135;=>(2 4 6)
136
89bd4de1
JM
137;; Testing vector functions
138
139(vector? [10 11])
140;=>true
141(vector? '(12 13))
142;=>false
143(vector 3 4 5)
144;=>[3 4 5]
145
b8ee29b2
JM
146(map? {})
147;=>true
148(map? '())
149;=>false
9af8aee6
JM
150(map? [])
151;=>false
b8ee29b2
JM
152(map? 'abc)
153;=>false
154(map? :abc)
155;=>false
31690700
JM
156
157;;
158;; Testing hash-maps
159(hash-map "a" 1)
160;=>{"a" 1}
161
162{"a" 1}
163;=>{"a" 1}
164
165(assoc {} "a" 1)
166;=>{"a" 1}
167
168(def! hm1 (hash-map))
169;=>{}
170
171(map? hm1)
172;=>true
173(map? 1)
174;=>false
9af8aee6 175(map? "abc")
31690700
JM
176;=>false
177
0027e8fe
JM
178(get nil "a")
179;=>nil
180
31690700
JM
181(get hm1 "a")
182;=>nil
183
184(contains? hm1 "a")
185;=>false
186
187(def! hm2 (assoc hm1 "a" 1))
188;=>{"a" 1}
189
190(get hm1 "a")
191;=>nil
192
193(contains? hm1 "a")
194;=>false
195
196(get hm2 "a")
197;=>1
198
199(contains? hm2 "a")
200;=>true
201
77b2da6c
JM
202
203;;; TODO: fix. Clojure returns nil but this breaks mal impl
204(keys hm1)
205;=>()
206
31690700
JM
207(keys hm2)
208;=>("a")
209
77b2da6c
JM
210;;; TODO: fix. Clojure returns nil but this breaks mal impl
211(vals hm1)
212;=>()
213
31690700
JM
214(vals hm2)
215;=>(1)
216
9528bb14
JM
217(count (keys (assoc hm2 "b" 2 "c" 3)))
218;=>3
219
31690700
JM
220(def! hm3 (assoc hm2 "b" 2))
221(count (keys hm3))
222;=>2
223(count (vals hm3))
224;=>2
225
226(dissoc hm3 "a")
227;=>{"b" 2}
228
229(dissoc hm3 "a" "b")
230;=>{}
231
5ce65382
JM
232(dissoc hm3 "a" "b" "c")
233;=>{}
234
31690700
JM
235(count (keys hm3))
236;=>2
237
b8ee29b2
JM
238;; Testing keywords as hash-map keys
239(get {:abc 123} :abc)
240;=>123
241(contains? {:abc 123} :abc)
242;=>true
243(contains? {:abcd 123} :abc)
244;=>false
245(assoc {} :bcd 234)
246;=>{:bcd 234}
247(dissoc {:cde 345 :fgh 456} :cde)
248;=>{:fgh 456}
249(keyword? (nth (keys {:abc 123 :def 456}) 0))
250;=>true
251;;; TODO: support : in strings in make impl
252;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
253;;;;=>false
254(keyword? (nth (vals {"a" :abc "b" :def}) 0))
255;=>true
256
257
31690700 258
b12d98e4 259;;
f5223195 260;; Testing metadata on functions
b12d98e4 261
31690700 262;;
f5223195 263;; Testing metadata on mal functions
31690700 264
a34b0200
JM
265(meta (fn* (a) a))
266;=>nil
267
f5223195
JM
268(meta (with-meta (fn* (a) a) {"b" 1}))
269;=>{"b" 1}
31690700 270
f5223195 271(meta (with-meta (fn* (a) a) "abc"))
a34b0200
JM
272;=>"abc"
273
f5223195 274(def! l-wm (with-meta (fn* (a) a) {"b" 2}))
a34b0200
JM
275(meta l-wm)
276;=>{"b" 2}
277
278(meta (with-meta l-wm {"new_meta" 123}))
279;=>{"new_meta" 123}
280(meta l-wm)
281;=>{"b" 2}
282
31690700
JM
283(def! f-wm (with-meta (fn* [a] (+ 1 a)) {"abc" 1}))
284(meta f-wm)
285;=>{"abc" 1}
286
a34b0200
JM
287(meta (with-meta f-wm {"new_meta" 123}))
288;=>{"new_meta" 123}
17ae845e
JM
289(meta f-wm)
290;=>{"abc" 1}
291
b079f510 292(def! f-wm2 ^{"abc" 1} (fn* [a] (+ 1 a)))
5ce65382 293(meta f-wm2)
b079f510 294;=>{"abc" 1}
5ce65382 295
a34b0200
JM
296;;
297;; Make sure closures and metadata co-exist
298(def! gen-plusX (fn* (x) (with-meta (fn* (b) (+ x b)) {"meta" 1})))
299(def! plus7 (gen-plusX 7))
300(def! plus8 (gen-plusX 8))
301(plus7 8)
302;=>15
303(meta plus7)
304;=>{"meta" 1}
305(meta plus8)
306;=>{"meta" 1}
307(meta (with-meta plus7 {"meta" 2}))
308;=>{"meta" 2}
309(meta plus8)
310;=>{"meta" 1}
311
31690700
JM
312
313;;
314;; Testing atoms
315
316(def! inc3 (fn* (a) (+ 3 a)))
317
318(def! a (atom 2))
319;=>(atom 2)
320
321;;;(type a)
322;;;;=>"atom"
323
324(deref a)
325;=>2
326
327@a
328;=>2
329
330(reset! a 3)
331;=>3
332
333@a
334;=>3
335
336(swap! a inc3)
337;=>6
338
339@a
340;=>6
341
342(swap! a (fn* (a) a))
343;=>6
344
345(swap! a (fn* (a) (* 2 a)))
346;=>12
347
17ae845e
JM
348(swap! a (fn* (a b) (* a b)) 10)
349;=>120
350
5ce65382 351(swap! a + 3)
17ae845e 352;=>123
31690700 353
0027e8fe
JM
354;; Testing swap!/closure interaction
355(def! inc-it (fn* (a) (+ 1 a)))
356(def! atm (atom 7))
357(def! f (fn* [] (swap! atm inc-it)))
358(f)
359;=>8
360(f)
361;=>9
362
f5223195
JM
363
364;;
365;; ------- Optional Functionality --------------
366;; ------- (Not needed for self-hosting) -------
367
368;;
369;; Testing conj function
370(conj (list) 1)
371;=>(1)
372(conj (list 1) 2)
373;=>(2 1)
374(conj (list 2 3) 4)
375;=>(4 2 3)
376(conj (list 2 3) 4 5 6)
377;=>(6 5 4 2 3)
378(conj (list 1) (list 2 3))
379;=>((2 3) 1)
380
381(conj [] 1)
382;=>[1]
383(conj [1] 2)
384;=>[1 2]
385(conj [2 3] 4)
386;=>[2 3 4]
387(conj [2 3] 4 5 6)
388;=>[2 3 4 5 6]
389(conj [1] [2 3])
390;=>[1 [2 3]]
391
392
393;;
394;; Testing metadata on collections
395
396(meta [1 2 3])
397;=>nil
398
399(with-meta [1 2 3] {"a" 1})
400;=>[1 2 3]
401
402(meta (with-meta [1 2 3] {"a" 1}))
403;=>{"a" 1}
404
405(meta (with-meta [1 2 3] "abc"))
406;=>"abc"
407
408(meta (with-meta (list 1 2 3) {"a" 1}))
409;=>{"a" 1}
410
411(meta (with-meta {"abc" 123} {"a" 1}))
412;=>{"a" 1}
413
414;;; Not actually supported by Clojure
415;;;(meta (with-meta (atom 7) {"a" 1}))
416;;;;=>{"a" 1}
417
418(def! l-wm (with-meta [4 5 6] {"b" 2}))
419;=>[4 5 6]
420(meta l-wm)
421;=>{"b" 2}
422
423(meta (with-meta l-wm {"new_meta" 123}))
424;=>{"new_meta" 123}
425(meta l-wm)
426;=>{"b" 2}
427
428;;
429;; Testing metadata on builtin functions
430(meta +)
431;=>nil
432(def! f-wm3 ^{"def" 2} +)
433(meta f-wm3)
434;=>{"def" 2}
435(meta +)
436;=>nil
437