Merge pull request #530 from mpritham/master
[jackhill/mal.git] / impls / tests / step9_try.mal
CommitLineData
dd7a4f55
JM
1;;
2;; Testing throw
3
4(throw "err1")
5;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*err1.*
dd7a4f55 6
31690700
JM
7;;
8;; Testing try*/catch*
9
f5223195
JM
10(try* 123 (catch* e 456))
11;=>123
12
0c240282
JM
13(try* abc (catch* exc (prn "exc is:" exc)))
14;/"exc is:" "'abc' not found"
15;=>nil
16
617bdb35 17(try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
f6f5d4f2 18;/"exc is:" "'abc' not found"
31690700
JM
19;=>nil
20
dd7a4f55 21;; Make sure error from core can be caught
64c11d81 22(try* (nth () 1) (catch* exc (prn "exc is:" exc)))
dd7a4f55
JM
23;/"exc is:".*(length|range|[Bb]ounds|beyond).*
24;=>nil
25
31690700 26(try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
f6f5d4f2 27;/"exc:" "my exception"
31690700
JM
28;=>7
29
5fb74d33
BH
30;; Test that exception handlers get restored correctly
31(try* (do (try* "t1" (catch* e "c1")) (throw "e1")) (catch* e "c2"))
32;=>"c2"
33(try* (try* (throw "e1") (catch* e (throw "e2"))) (catch* e "c2"))
34;=>"c2"
35
db11c740 36;;; Test that throw is a function:
c3a87f51
JM
37(try* (map throw (list "my err")) (catch* exc exc))
38;=>"my err"
db11c740 39
31690700
JM
40
41;;
42;; Testing builtin functions
43
44(symbol? 'abc)
45;=>true
46(symbol? "abc")
47;=>false
48
49(nil? nil)
50;=>true
51(nil? true)
52;=>false
53
54(true? true)
55;=>true
56(true? false)
57;=>false
58(true? true?)
59;=>false
60
61(false? false)
62;=>true
63(false? true)
64;=>false
65
58002e1b 66;; Testing apply function with core functions
31690700
JM
67(apply + (list 2 3))
68;=>5
69(apply + 4 (list 5))
70;=>9
71(apply prn (list 1 2 "3" (list)))
f6f5d4f2 72;/1 2 "3" \(\)
0067158f 73;=>nil
d90c7844 74(apply prn 1 2 (list "3" (list)))
f6f5d4f2 75;/1 2 "3" \(\)
0067158f 76;=>nil
a7ed71b9
JM
77(apply list (list))
78;=>()
55c25242
DM
79(apply symbol? (list (quote two)))
80;=>true
31690700 81
58002e1b
ST
82;; Testing apply function with user functions
83(apply (fn* (a b) (+ a b)) (list 2 3))
84;=>5
85(apply (fn* (a b) (+ a b)) 4 (list 5))
86;=>9
31690700
JM
87
88;; Testing map function
89(def! nums (list 1 2 3))
90(def! double (fn* (a) (* 2 a)))
91(double 3)
92;=>6
93(map double nums)
94;=>(2 4 6)
46e25689 95(map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
f947d503 96;=>(false true false)
31690700 97
a1eb30fc 98;>>> deferrable=True
9af8aee6 99;;
a1eb30fc 100;; ------- Deferrable Functionality ----------
f5223195 101;; ------- (Needed for self-hosting) -------
9af8aee6 102
1fcac410
BH
103;; Testing throwing a hash-map
104(throw {:msg "err2"})
105;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*msg.*err2.*
106
b8ee29b2
JM
107;; Testing symbol and keyword functions
108(symbol? :abc)
109;=>false
110(symbol? 'abc)
111;=>true
112(symbol? "abc")
113;=>false
114(symbol? (symbol "abc"))
115;=>true
116(keyword? :abc)
117;=>true
118(keyword? 'abc)
119;=>false
120(keyword? "abc")
121;=>false
8e32dbb6
JM
122(keyword? "")
123;=>false
b8ee29b2
JM
124(keyword? (keyword "abc"))
125;=>true
126
dbac60df
JM
127(symbol "abc")
128;=>abc
dbac60df
JM
129(keyword "abc")
130;=>:abc
131
9af8aee6
JM
132;; Testing sequential? function
133
134(sequential? (list 1 2 3))
135;=>true
136(sequential? [15])
137;=>true
138(sequential? sequential?)
139;=>false
140(sequential? nil)
141;=>false
142(sequential? "abc")
143;=>false
31690700 144
20c05e35
JM
145;; Testing apply function with core functions and arguments in vector
146(apply + 4 [5])
147;=>9
148(apply prn 1 2 ["3" 4])
f6f5d4f2 149;/1 2 "3" 4
20c05e35 150;=>nil
a7ed71b9
JM
151(apply list [])
152;=>()
20c05e35
JM
153;; Testing apply function with user functions and arguments in vector
154(apply (fn* (a b) (+ a b)) [2 3])
155;=>5
156(apply (fn* (a b) (+ a b)) 4 [5])
157;=>9
158
f5223195
JM
159
160;; Testing map function with vectors
161(map (fn* (a) (* 2 a)) [1 2 3])
162;=>(2 4 6)
163
f86d275f
BD
164(map (fn* [& args] (list? args)) [1 2])
165;=>(true true)
166
89bd4de1
JM
167;; Testing vector functions
168
169(vector? [10 11])
170;=>true
171(vector? '(12 13))
172;=>false
173(vector 3 4 5)
174;=>[3 4 5]
175
b8ee29b2
JM
176(map? {})
177;=>true
178(map? '())
179;=>false
9af8aee6
JM
180(map? [])
181;=>false
b8ee29b2
JM
182(map? 'abc)
183;=>false
184(map? :abc)
185;=>false
31690700 186
809d74cb 187
31690700
JM
188;;
189;; Testing hash-maps
190(hash-map "a" 1)
191;=>{"a" 1}
192
193{"a" 1}
194;=>{"a" 1}
195
196(assoc {} "a" 1)
197;=>{"a" 1}
198
86da00cc
JM
199(get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
200;=>1
201
31690700
JM
202(def! hm1 (hash-map))
203;=>{}
204
205(map? hm1)
206;=>true
207(map? 1)
208;=>false
9af8aee6 209(map? "abc")
31690700
JM
210;=>false
211
0027e8fe
JM
212(get nil "a")
213;=>nil
214
31690700
JM
215(get hm1 "a")
216;=>nil
217
218(contains? hm1 "a")
219;=>false
220
221(def! hm2 (assoc hm1 "a" 1))
222;=>{"a" 1}
223
224(get hm1 "a")
225;=>nil
226
227(contains? hm1 "a")
228;=>false
229
230(get hm2 "a")
231;=>1
232
233(contains? hm2 "a")
234;=>true
235
77b2da6c
JM
236
237;;; TODO: fix. Clojure returns nil but this breaks mal impl
238(keys hm1)
239;=>()
240
31690700
JM
241(keys hm2)
242;=>("a")
243
80512fcc
BH
244(keys {"1" 1})
245;=>("1")
246
77b2da6c
JM
247;;; TODO: fix. Clojure returns nil but this breaks mal impl
248(vals hm1)
249;=>()
250
31690700
JM
251(vals hm2)
252;=>(1)
253
9528bb14
JM
254(count (keys (assoc hm2 "b" 2 "c" 3)))
255;=>3
256
b8ee29b2
JM
257;; Testing keywords as hash-map keys
258(get {:abc 123} :abc)
259;=>123
260(contains? {:abc 123} :abc)
261;=>true
262(contains? {:abcd 123} :abc)
263;=>false
264(assoc {} :bcd 234)
265;=>{:bcd 234}
b8ee29b2
JM
266(keyword? (nth (keys {:abc 123 :def 456}) 0))
267;=>true
b8ee29b2
JM
268(keyword? (nth (vals {"a" :abc "b" :def}) 0))
269;=>true
270
3c7b63d2
VS
271;; Testing whether assoc updates properly
272(def! hm4 (assoc {:a 1 :b 2} :a 3 :c 1))
273(get hm4 :a)
274;=>3
275(get hm4 :b)
276;=>2
277(get hm4 :c)
278;=>1
279
6496cd1d
DM
280;; Testing nil as hash-map values
281(contains? {:abc nil} :abc)
282;=>true
283(assoc {} :bcd nil)
284;=>{:bcd nil}
35ea7a9e 285
c4cd2700
DM
286;;
287;; Additional str and pr-str tests
288
289(str "A" {:abc "val"} "Z")
290;=>"A{:abc val}Z"
291
292(str true "." false "." nil "." :keyw "." 'symb)
293;=>"true.false.nil.:keyw.symb"
294
295(pr-str "A" {:abc "val"} "Z")
296;=>"\"A\" {:abc \"val\"} \"Z\""
297
298(pr-str true "." false "." nil "." :keyw "." 'symb)
299;=>"true \".\" false \".\" nil \".\" :keyw \".\" symb"
300
301(def! s (str {:abc "val1" :def "val2"}))
26ced15b 302(cond (= s "{:abc val1 :def val2}") true (= s "{:def val2 :abc val1}") true)
c4cd2700
DM
303;=>true
304
305(def! p (pr-str {:abc "val1" :def "val2"}))
26ced15b 306(cond (= p "{:abc \"val1\" :def \"val2\"}") true (= p "{:def \"val2\" :abc \"val1\"}") true)
c4cd2700
DM
307;=>true
308
6205526a
DM
309;;
310;; Test extra function arguments as Mal List (bypassing TCO with apply)
311(apply (fn* (& more) (list? more)) [1 2 3])
312;=>true
313(apply (fn* (& more) (list? more)) [])
314;=>true
315(apply (fn* (a & more) (list? more)) [1])
316;=>true
317
46e25689
JM
318;>>> soft=True
319;>>> optional=True
c3a87f51
JM
320;;
321;; ------- Optional Functionality --------------
322;; ------- (Not needed for self-hosting) -------
c3a87f51
JM
323
324
325;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
326;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
327;;;; "exc is:" ["data" "foo"] ;;;;=>7
328;;;;=>7
329
dd7a4f55
JM
330;;
331;; Testing try* without catch*
332(try* xyz)
333;/.*\'?xyz\'? not found.*
334
37d75dc6 335;;
c3a87f51
JM
336;; Testing throwing non-strings
337(try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
f6f5d4f2 338;/"err:" \(1 2 3\)
c3a87f51
JM
339;=>7
340
37d75dc6
JM
341;;
342;; Testing dissoc
343(def! hm3 (assoc hm2 "b" 2))
344(count (keys hm3))
345;=>2
346(count (vals hm3))
347;=>2
348(dissoc hm3 "a")
349;=>{"b" 2}
350(dissoc hm3 "a" "b")
351;=>{}
352(dissoc hm3 "a" "b" "c")
353;=>{}
354(count (keys hm3))
355;=>2
356
357(dissoc {:cde 345 :fgh 456} :cde)
358;=>{:fgh 456}
359(dissoc {:cde nil :fgh 456} :cde)
360;=>{:fgh 456}
361
362;;
363;; Testing equality of hash-maps
364(= {} {})
365;=>true
366(= {:a 11 :b 22} (hash-map :b 22 :a 11))
367;=>true
368(= {:a 11 :b [22 33]} (hash-map :b [22 33] :a 11))
369;=>true
370(= {:a 11 :b {:c 33}} (hash-map :b {:c 33} :a 11))
371;=>true
372(= {:a 11 :b 22} (hash-map :b 23 :a 11))
373;=>false
374(= {:a 11 :b 22} (hash-map :a 11))
375;=>false
376(= {:a [11 22]} {:a (list 11 22)})
377;=>true
378(= {:a 11 :b 22} (list :a 11 :b 22))
379;=>false
380(= {} [])
381;=>false
382(= [] {})
383;=>false
384
8eed0a29
NB
385(keyword :abc)
386;=>:abc
387(keyword? (first (keys {":abc" 123 ":def" 456})))
388;=>false