Travis: split build and test into separate scripts.
[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" ()
d90c7844
JM
61(apply prn 1 2 (list "3" (list)))
62; 1 2 "3" ()
31690700 63
58002e1b
ST
64;; Testing apply function with user functions
65(apply (fn* (a b) (+ a b)) (list 2 3))
66;=>5
67(apply (fn* (a b) (+ a b)) 4 (list 5))
68;=>9
31690700
JM
69
70;; Testing map function
71(def! nums (list 1 2 3))
72(def! double (fn* (a) (* 2 a)))
73(double 3)
74;=>6
75(map double nums)
76;=>(2 4 6)
f5223195 77(map (fn* (x) (symbol? x)) (list 1 (symbol "two") "three"))
f947d503 78;=>(false true false)
31690700 79
9af8aee6 80;;
f5223195
JM
81;; ------- Optional Functionality ----------
82;; ------- (Needed for self-hosting) -------
9af8aee6 83
b8ee29b2
JM
84;; Testing symbol and keyword functions
85(symbol? :abc)
86;=>false
87(symbol? 'abc)
88;=>true
89(symbol? "abc")
90;=>false
91(symbol? (symbol "abc"))
92;=>true
93(keyword? :abc)
94;=>true
95(keyword? 'abc)
96;=>false
97(keyword? "abc")
98;=>false
99(keyword? (keyword "abc"))
100;=>true
101
dbac60df
JM
102(symbol "abc")
103;=>abc
104;;;TODO: all implementations should suppport this too
105;;;(keyword :abc)
106;;;;=>:abc
107(keyword "abc")
108;=>:abc
109
9af8aee6
JM
110;; Testing sequential? function
111
112(sequential? (list 1 2 3))
113;=>true
114(sequential? [15])
115;=>true
116(sequential? sequential?)
117;=>false
118(sequential? nil)
119;=>false
120(sequential? "abc")
121;=>false
31690700 122
20c05e35
JM
123;; Testing apply function with core functions and arguments in vector
124(apply + 4 [5])
125;=>9
126(apply prn 1 2 ["3" 4])
127; 1 2 "3" 4
128;=>nil
129;; Testing apply function with user functions and arguments in vector
130(apply (fn* (a b) (+ a b)) [2 3])
131;=>5
132(apply (fn* (a b) (+ a b)) 4 [5])
133;=>9
134
f5223195
JM
135
136;; Testing map function with vectors
137(map (fn* (a) (* 2 a)) [1 2 3])
138;=>(2 4 6)
139
89bd4de1
JM
140;; Testing vector functions
141
142(vector? [10 11])
143;=>true
144(vector? '(12 13))
145;=>false
146(vector 3 4 5)
147;=>[3 4 5]
148
b8ee29b2
JM
149(map? {})
150;=>true
151(map? '())
152;=>false
9af8aee6
JM
153(map? [])
154;=>false
b8ee29b2
JM
155(map? 'abc)
156;=>false
157(map? :abc)
158;=>false
31690700
JM
159
160;;
161;; Testing hash-maps
162(hash-map "a" 1)
163;=>{"a" 1}
164
165{"a" 1}
166;=>{"a" 1}
167
168(assoc {} "a" 1)
169;=>{"a" 1}
170
86da00cc
JM
171(get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
172;=>1
173
31690700
JM
174(def! hm1 (hash-map))
175;=>{}
176
177(map? hm1)
178;=>true
179(map? 1)
180;=>false
9af8aee6 181(map? "abc")
31690700
JM
182;=>false
183
0027e8fe
JM
184(get nil "a")
185;=>nil
186
31690700
JM
187(get hm1 "a")
188;=>nil
189
190(contains? hm1 "a")
191;=>false
192
193(def! hm2 (assoc hm1 "a" 1))
194;=>{"a" 1}
195
196(get hm1 "a")
197;=>nil
198
199(contains? hm1 "a")
200;=>false
201
202(get hm2 "a")
203;=>1
204
205(contains? hm2 "a")
206;=>true
207
77b2da6c
JM
208
209;;; TODO: fix. Clojure returns nil but this breaks mal impl
210(keys hm1)
211;=>()
212
31690700
JM
213(keys hm2)
214;=>("a")
215
77b2da6c
JM
216;;; TODO: fix. Clojure returns nil but this breaks mal impl
217(vals hm1)
218;=>()
219
31690700
JM
220(vals hm2)
221;=>(1)
222
9528bb14
JM
223(count (keys (assoc hm2 "b" 2 "c" 3)))
224;=>3
225
31690700
JM
226(def! hm3 (assoc hm2 "b" 2))
227(count (keys hm3))
228;=>2
229(count (vals hm3))
230;=>2
231
232(dissoc hm3 "a")
233;=>{"b" 2}
234
235(dissoc hm3 "a" "b")
236;=>{}
237
5ce65382
JM
238(dissoc hm3 "a" "b" "c")
239;=>{}
240
31690700
JM
241(count (keys hm3))
242;=>2
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}
253(dissoc {:cde 345 :fgh 456} :cde)
254;=>{:fgh 456}
255(keyword? (nth (keys {:abc 123 :def 456}) 0))
256;=>true
257;;; TODO: support : in strings in make impl
258;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
259;;;;=>false
260(keyword? (nth (vals {"a" :abc "b" :def}) 0))
261;=>true
262
263
31690700 264