generate perf analysis
[jackhill/mal.git] / impls / tests / step9_try.mal
1 ;;
2 ;; Testing throw
3
4 (throw "err1")
5 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*err1.*
6
7 ;;
8 ;; Testing try*/catch*
9
10 (try* 123 (catch* e 456))
11 ;=>123
12
13 (try* abc (catch* exc (prn "exc is:" exc)))
14 ;/"exc is:" "'abc' not found"
15 ;=>nil
16
17 (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
18 ;/"exc is:" "'abc' not found"
19 ;=>nil
20
21 ;; Make sure error from core can be caught
22 (try* (nth () 1) (catch* exc (prn "exc is:" exc)))
23 ;/"exc is:".*(length|range|[Bb]ounds|beyond).*
24 ;=>nil
25
26 (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
27 ;/"exc:" "my exception"
28 ;=>7
29
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
36 ;;; Test that throw is a function:
37 (try* (map throw (list "my err")) (catch* exc exc))
38 ;=>"my err"
39
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
66 ;; Testing apply function with core functions
67 (apply + (list 2 3))
68 ;=>5
69 (apply + 4 (list 5))
70 ;=>9
71 (apply prn (list 1 2 "3" (list)))
72 ;/1 2 "3" \(\)
73 ;=>nil
74 (apply prn 1 2 (list "3" (list)))
75 ;/1 2 "3" \(\)
76 ;=>nil
77 (apply list (list))
78 ;=>()
79 (apply symbol? (list (quote two)))
80 ;=>true
81
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
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)
95 (map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
96 ;=>(false true false)
97
98 ;>>> deferrable=True
99 ;;
100 ;; ------- Deferrable Functionality ----------
101 ;; ------- (Needed for self-hosting) -------
102
103 ;; Testing throwing a hash-map
104 (throw {:msg "err2"})
105 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*msg.*err2.*
106
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
122 (keyword? "")
123 ;=>false
124 (keyword? (keyword "abc"))
125 ;=>true
126
127 (symbol "abc")
128 ;=>abc
129 (keyword "abc")
130 ;=>:abc
131
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
144
145 ;; Testing apply function with core functions and arguments in vector
146 (apply + 4 [5])
147 ;=>9
148 (apply prn 1 2 ["3" 4])
149 ;/1 2 "3" 4
150 ;=>nil
151 (apply list [])
152 ;=>()
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
159
160 ;; Testing map function with vectors
161 (map (fn* (a) (* 2 a)) [1 2 3])
162 ;=>(2 4 6)
163
164 (map (fn* [& args] (list? args)) [1 2])
165 ;=>(true true)
166
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
176 (map? {})
177 ;=>true
178 (map? '())
179 ;=>false
180 (map? [])
181 ;=>false
182 (map? 'abc)
183 ;=>false
184 (map? :abc)
185 ;=>false
186
187
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
199 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
200 ;=>1
201
202 (def! hm1 (hash-map))
203 ;=>{}
204
205 (map? hm1)
206 ;=>true
207 (map? 1)
208 ;=>false
209 (map? "abc")
210 ;=>false
211
212 (get nil "a")
213 ;=>nil
214
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
236
237 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
238 (keys hm1)
239 ;=>()
240
241 (keys hm2)
242 ;=>("a")
243
244 (keys {"1" 1})
245 ;=>("1")
246
247 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
248 (vals hm1)
249 ;=>()
250
251 (vals hm2)
252 ;=>(1)
253
254 (count (keys (assoc hm2 "b" 2 "c" 3)))
255 ;=>3
256
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}
266 (keyword? (nth (keys {:abc 123 :def 456}) 0))
267 ;=>true
268 (keyword? (nth (vals {"a" :abc "b" :def}) 0))
269 ;=>true
270
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
280 ;; Testing nil as hash-map values
281 (contains? {:abc nil} :abc)
282 ;=>true
283 (assoc {} :bcd nil)
284 ;=>{:bcd nil}
285
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"}))
302 (cond (= s "{:abc val1 :def val2}") true (= s "{:def val2 :abc val1}") true)
303 ;=>true
304
305 (def! p (pr-str {:abc "val1" :def "val2"}))
306 (cond (= p "{:abc \"val1\" :def \"val2\"}") true (= p "{:def \"val2\" :abc \"val1\"}") true)
307 ;=>true
308
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
318 ;>>> soft=True
319 ;>>> optional=True
320 ;;
321 ;; ------- Optional Functionality --------------
322 ;; ------- (Not needed for self-hosting) -------
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
330 ;;
331 ;; Testing try* without catch*
332 (try* xyz)
333 ;/.*\'?xyz\'? not found.*
334
335 ;;
336 ;; Testing throwing non-strings
337 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
338 ;/"err:" \(1 2 3\)
339 ;=>7
340
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
385 (keyword :abc)
386 ;=>:abc
387 (keyword? (first (keys {":abc" 123 ":def" 456})))
388 ;=>false