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