Merge pull request #352 from bjh21/bjh21-defer-strings
[jackhill/mal.git] / tests / step9_try.mal
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
9 ;;
10 ;; Testing try*/catch*
11
12 (try* 123 (catch* e 456))
13 ;=>123
14
15 (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
16 ;/"exc is:" "'abc' not found"
17 ;=>nil
18
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
24 (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
25 ;/"exc:" "my exception"
26 ;=>7
27
28 ;;; Test that throw is a function:
29 (try* (map throw (list "my err")) (catch* exc exc))
30 ;=>"my err"
31
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
58 ;; Testing apply function with core functions
59 (apply + (list 2 3))
60 ;=>5
61 (apply + 4 (list 5))
62 ;=>9
63 (apply prn (list 1 2 "3" (list)))
64 ;/1 2 "3" \(\)
65 ;=>nil
66 (apply prn 1 2 (list "3" (list)))
67 ;/1 2 "3" \(\)
68 ;=>nil
69 (apply list (list))
70 ;=>()
71 (apply symbol? (list (quote two)))
72 ;=>true
73
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
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)
87 (map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
88 ;=>(false true false)
89
90 ;>>> deferrable=True
91 ;;
92 ;; ------- Deferrable Functionality ----------
93 ;; ------- (Needed for self-hosting) -------
94
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
110 (keyword? "")
111 ;=>false
112 (keyword? (keyword "abc"))
113 ;=>true
114
115 (symbol "abc")
116 ;=>abc
117 ;;;TODO: all implementations should suppport this too
118 ;;;(keyword :abc)
119 ;;;;=>:abc
120 (keyword "abc")
121 ;=>:abc
122
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
135
136 ;; Testing apply function with core functions and arguments in vector
137 (apply + 4 [5])
138 ;=>9
139 (apply prn 1 2 ["3" 4])
140 ;/1 2 "3" 4
141 ;=>nil
142 (apply list [])
143 ;=>()
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
150
151 ;; Testing map function with vectors
152 (map (fn* (a) (* 2 a)) [1 2 3])
153 ;=>(2 4 6)
154
155 (map (fn* [& args] (list? args)) [1 2])
156 ;=>(true true)
157
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
167 (map? {})
168 ;=>true
169 (map? '())
170 ;=>false
171 (map? [])
172 ;=>false
173 (map? 'abc)
174 ;=>false
175 (map? :abc)
176 ;=>false
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
189 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
190 ;=>1
191
192 (def! hm1 (hash-map))
193 ;=>{}
194
195 (map? hm1)
196 ;=>true
197 (map? 1)
198 ;=>false
199 (map? "abc")
200 ;=>false
201
202 (get nil "a")
203 ;=>nil
204
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
226
227 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
228 (keys hm1)
229 ;=>()
230
231 (keys hm2)
232 ;=>("a")
233
234 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
235 (vals hm1)
236 ;=>()
237
238 (vals hm2)
239 ;=>(1)
240
241 (count (keys (assoc hm2 "b" 2 "c" 3)))
242 ;=>3
243
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}
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
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
270 ;; Testing nil as hash-map values
271 (contains? {:abc nil} :abc)
272 ;=>true
273 (assoc {} :bcd nil)
274 ;=>{:bcd nil}
275
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
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
308 ;>>> soft=True
309 ;>>> optional=True
310 ;;
311 ;; ------- Optional Functionality --------------
312 ;; ------- (Not needed for self-hosting) -------
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
320 ;;
321 ;; Testing try* without catch*
322 (try* xyz)
323 ;/.*\'?xyz\'? not found.*
324
325 ;;
326 ;; Testing throwing non-strings
327 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
328 ;/"err:" \(1 2 3\)
329 ;=>7
330
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