Travis: split build and test into separate scripts.
[jackhill/mal.git] / tests / step9_try.mal
1 ;;
2 ;; Testing try*/catch*
3
4 (try* 123 (catch* e 456))
5 ;=>123
6
7 (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
8 ; "exc is:" "'abc' not found"
9 ;=>nil
10
11 ;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
12 ;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
13 ;;;; "exc is:" ["data" "foo"] ;;;;=>7
14 ;;;;=>7
15
16 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
17 ; "err:" (1 2 3)
18 ;=>7
19
20 (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
21 ; "exc:" "my exception"
22 ;=>7
23
24 ;;; Test that throw is a function:
25 (try* (map throw (list 7)) (catch* exc exc))
26 ;=>7
27
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
54 ;; Testing apply function with core functions
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 (apply prn 1 2 (list "3" (list)))
62 ; 1 2 "3" ()
63
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
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)
77 (map (fn* (x) (symbol? x)) (list 1 (symbol "two") "three"))
78 ;=>(false true false)
79
80 ;;
81 ;; ------- Optional Functionality ----------
82 ;; ------- (Needed for self-hosting) -------
83
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
102 (symbol "abc")
103 ;=>abc
104 ;;;TODO: all implementations should suppport this too
105 ;;;(keyword :abc)
106 ;;;;=>:abc
107 (keyword "abc")
108 ;=>:abc
109
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
122
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
135
136 ;; Testing map function with vectors
137 (map (fn* (a) (* 2 a)) [1 2 3])
138 ;=>(2 4 6)
139
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
149 (map? {})
150 ;=>true
151 (map? '())
152 ;=>false
153 (map? [])
154 ;=>false
155 (map? 'abc)
156 ;=>false
157 (map? :abc)
158 ;=>false
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
171 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
172 ;=>1
173
174 (def! hm1 (hash-map))
175 ;=>{}
176
177 (map? hm1)
178 ;=>true
179 (map? 1)
180 ;=>false
181 (map? "abc")
182 ;=>false
183
184 (get nil "a")
185 ;=>nil
186
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
208
209 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
210 (keys hm1)
211 ;=>()
212
213 (keys hm2)
214 ;=>("a")
215
216 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
217 (vals hm1)
218 ;=>()
219
220 (vals hm2)
221 ;=>(1)
222
223 (count (keys (assoc hm2 "b" 2 "c" 3)))
224 ;=>3
225
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
238 (dissoc hm3 "a" "b" "c")
239 ;=>{}
240
241 (count (keys hm3))
242 ;=>2
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 (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
264