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