plpgsql: add steps7-9
[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
617bdb35 7(try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
16354bb4 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
e27782ea
JM
16(try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
17; "err:" (1 2 3)
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 24;;; Test that throw is a function:
a9cd6543 25(try* (map throw (list 7)) (catch* exc exc))
db11c740
C
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
58002e1b 54;; Testing apply function with core functions
31690700
JM
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" ()
0067158f 61;=>nil
d90c7844
JM
62(apply prn 1 2 (list "3" (list)))
63; 1 2 "3" ()
0067158f 64;=>nil
31690700 65
58002e1b
ST
66;; Testing apply function with user functions
67(apply (fn* (a b) (+ a b)) (list 2 3))
68;=>5
69(apply (fn* (a b) (+ a b)) 4 (list 5))
70;=>9
31690700
JM
71
72;; Testing map function
73(def! nums (list 1 2 3))
74(def! double (fn* (a) (* 2 a)))
75(double 3)
76;=>6
77(map double nums)
78;=>(2 4 6)
f5223195 79(map (fn* (x) (symbol? x)) (list 1 (symbol "two") "three"))
f947d503 80;=>(false true false)
31690700 81
9af8aee6 82;;
f5223195
JM
83;; ------- Optional Functionality ----------
84;; ------- (Needed for self-hosting) -------
9af8aee6 85
b8ee29b2
JM
86;; Testing symbol and keyword functions
87(symbol? :abc)
88;=>false
89(symbol? 'abc)
90;=>true
91(symbol? "abc")
92;=>false
93(symbol? (symbol "abc"))
94;=>true
95(keyword? :abc)
96;=>true
97(keyword? 'abc)
98;=>false
99(keyword? "abc")
100;=>false
8e32dbb6
JM
101(keyword? "")
102;=>false
b8ee29b2
JM
103(keyword? (keyword "abc"))
104;=>true
105
dbac60df
JM
106(symbol "abc")
107;=>abc
108;;;TODO: all implementations should suppport this too
109;;;(keyword :abc)
110;;;;=>:abc
111(keyword "abc")
112;=>:abc
113
9af8aee6
JM
114;; Testing sequential? function
115
116(sequential? (list 1 2 3))
117;=>true
118(sequential? [15])
119;=>true
120(sequential? sequential?)
121;=>false
122(sequential? nil)
123;=>false
124(sequential? "abc")
125;=>false
31690700 126
20c05e35
JM
127;; Testing apply function with core functions and arguments in vector
128(apply + 4 [5])
129;=>9
130(apply prn 1 2 ["3" 4])
131; 1 2 "3" 4
132;=>nil
133;; Testing apply function with user functions and arguments in vector
134(apply (fn* (a b) (+ a b)) [2 3])
135;=>5
136(apply (fn* (a b) (+ a b)) 4 [5])
137;=>9
138
f5223195
JM
139
140;; Testing map function with vectors
141(map (fn* (a) (* 2 a)) [1 2 3])
142;=>(2 4 6)
143
89bd4de1
JM
144;; Testing vector functions
145
146(vector? [10 11])
147;=>true
148(vector? '(12 13))
149;=>false
150(vector 3 4 5)
151;=>[3 4 5]
152
b8ee29b2
JM
153(map? {})
154;=>true
155(map? '())
156;=>false
9af8aee6
JM
157(map? [])
158;=>false
b8ee29b2
JM
159(map? 'abc)
160;=>false
161(map? :abc)
162;=>false
31690700
JM
163
164;;
165;; Testing hash-maps
166(hash-map "a" 1)
167;=>{"a" 1}
168
169{"a" 1}
170;=>{"a" 1}
171
172(assoc {} "a" 1)
173;=>{"a" 1}
174
86da00cc
JM
175(get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
176;=>1
177
31690700
JM
178(def! hm1 (hash-map))
179;=>{}
180
181(map? hm1)
182;=>true
183(map? 1)
184;=>false
9af8aee6 185(map? "abc")
31690700
JM
186;=>false
187
0027e8fe
JM
188(get nil "a")
189;=>nil
190
31690700
JM
191(get hm1 "a")
192;=>nil
193
194(contains? hm1 "a")
195;=>false
196
197(def! hm2 (assoc hm1 "a" 1))
198;=>{"a" 1}
199
200(get hm1 "a")
201;=>nil
202
203(contains? hm1 "a")
204;=>false
205
206(get hm2 "a")
207;=>1
208
209(contains? hm2 "a")
210;=>true
211
77b2da6c
JM
212
213;;; TODO: fix. Clojure returns nil but this breaks mal impl
214(keys hm1)
215;=>()
216
31690700
JM
217(keys hm2)
218;=>("a")
219
77b2da6c
JM
220;;; TODO: fix. Clojure returns nil but this breaks mal impl
221(vals hm1)
222;=>()
223
31690700
JM
224(vals hm2)
225;=>(1)
226
9528bb14
JM
227(count (keys (assoc hm2 "b" 2 "c" 3)))
228;=>3
229
31690700
JM
230(def! hm3 (assoc hm2 "b" 2))
231(count (keys hm3))
232;=>2
233(count (vals hm3))
234;=>2
235
236(dissoc hm3 "a")
237;=>{"b" 2}
238
239(dissoc hm3 "a" "b")
240;=>{}
241
5ce65382
JM
242(dissoc hm3 "a" "b" "c")
243;=>{}
244
31690700
JM
245(count (keys hm3))
246;=>2
247
b8ee29b2
JM
248;; Testing keywords as hash-map keys
249(get {:abc 123} :abc)
250;=>123
251(contains? {:abc 123} :abc)
252;=>true
253(contains? {:abcd 123} :abc)
254;=>false
255(assoc {} :bcd 234)
256;=>{:bcd 234}
257(dissoc {:cde 345 :fgh 456} :cde)
258;=>{:fgh 456}
259(keyword? (nth (keys {:abc 123 :def 456}) 0))
260;=>true
261;;; TODO: support : in strings in make impl
262;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
263;;;;=>false
264(keyword? (nth (vals {"a" :abc "b" :def}) 0))
265;=>true
266
6496cd1d
DM
267;; Testing nil as hash-map values
268(contains? {:abc nil} :abc)
269;=>true
270(assoc {} :bcd nil)
271;=>{:bcd nil}
272(dissoc {:cde nil :fgh 456} :cde)
273;=>{:fgh 456}
b8ee29b2 274
35ea7a9e
JM
275;; Testing equality of hash-maps
276(= {} {})
277;=>true
278(= {:a 11 :b 22} (hash-map :b 22 :a 11))
279;=>true
280(= {:a 11 :b [22 33]} (hash-map :b [22 33] :a 11))
281;=>true
282(= {:a 11 :b {:c 33}} (hash-map :b {:c 33} :a 11))
283;=>true
284(= {:a 11 :b 22} (hash-map :b 23 :a 11))
285;=>false
286(= {:a 11 :b 22} (hash-map :a 11))
287;=>false
288(= {:a [11 22]} {:a (list 11 22)})
289;=>true
290(= {:a 11 :b 22} (list :a 11 :b 22))
291;=>false
292(= {} [])
293;=>false
294(= [] {})
295;=>false
296
c4cd2700
DM
297;;
298;; Additional str and pr-str tests
299
300(str "A" {:abc "val"} "Z")
301;=>"A{:abc val}Z"
302
303(str true "." false "." nil "." :keyw "." 'symb)
304;=>"true.false.nil.:keyw.symb"
305
306(pr-str "A" {:abc "val"} "Z")
307;=>"\"A\" {:abc \"val\"} \"Z\""
308
309(pr-str true "." false "." nil "." :keyw "." 'symb)
310;=>"true \".\" false \".\" nil \".\" :keyw \".\" symb"
311
312(def! s (str {:abc "val1" :def "val2"}))
313(or (= s "{:abc val1 :def val2}") (= s "{:def val2 :abc val1}"))
314;=>true
315
316(def! p (pr-str {:abc "val1" :def "val2"}))
317(or (= p "{:abc \"val1\" :def \"val2\"}") (= p "{:def \"val2\" :abc \"val1\"}"))
318;=>true
319
6205526a
DM
320;;
321;; Test extra function arguments as Mal List (bypassing TCO with apply)
322(apply (fn* (& more) (list? more)) [1 2 3])
323;=>true
324(apply (fn* (& more) (list? more)) [])
325;=>true
326(apply (fn* (a & more) (list? more)) [1])
327;=>true
328