yorick: Fix evaluation of empty vectors in steps 2 and 3
[jackhill/mal.git] / impls / tests / stepA_mal.mal
1 ;;;
2 ;;; See IMPL/tests/stepA_mal.mal for implementation specific
3 ;;; interop tests.
4 ;;;
5
6
7 ;;
8 ;; Testing readline
9 (readline "mal-user> ")
10 "hello"
11 ;=>"\"hello\""
12
13 ;;
14 ;; Testing *host-language*
15 ;;; each impl is different, but this should return false
16 ;;; rather than throwing an exception
17 (= "something bogus" *host-language*)
18 ;=>false
19
20
21 ;>>> deferrable=True
22 ;;
23 ;; ------- Deferrable Functionality ----------
24 ;; ------- (Needed for self-hosting) -------
25
26 ;;
27 ;;
28 ;; Testing hash-map evaluation and atoms (i.e. an env)
29 (def! e (atom {"+" +}))
30 (swap! e assoc "-" -)
31 ( (get @e "+") 7 8)
32 ;=>15
33 ( (get @e "-") 11 8)
34 ;=>3
35 (swap! e assoc "foo" (list))
36 (get @e "foo")
37 ;=>()
38 (swap! e assoc "bar" '(1 2 3))
39 (get @e "bar")
40 ;=>(1 2 3)
41
42 ;; Testing for presence of optional functions
43 (do (list time-ms string? number? seq conj meta with-meta fn?) nil)
44 ;=>nil
45
46 ;; ------------------------------------------------------------------
47
48 ;>>> soft=True
49 ;>>> optional=True
50 ;;
51 ;; ------- Optional Functionality --------------
52 ;; ------- (Not needed for self-hosting) -------
53
54 ;; Testing metadata on functions
55
56 ;;
57 ;; Testing metadata on mal functions
58
59 (meta (fn* (a) a))
60 ;=>nil
61
62 (meta (with-meta (fn* (a) a) {"b" 1}))
63 ;=>{"b" 1}
64
65 (meta (with-meta (fn* (a) a) "abc"))
66 ;=>"abc"
67
68 (def! l-wm (with-meta (fn* (a) a) {"b" 2}))
69 (meta l-wm)
70 ;=>{"b" 2}
71
72 (meta (with-meta l-wm {"new_meta" 123}))
73 ;=>{"new_meta" 123}
74 (meta l-wm)
75 ;=>{"b" 2}
76
77 (def! f-wm (with-meta (fn* [a] (+ 1 a)) {"abc" 1}))
78 (meta f-wm)
79 ;=>{"abc" 1}
80
81 (meta (with-meta f-wm {"new_meta" 123}))
82 ;=>{"new_meta" 123}
83 (meta f-wm)
84 ;=>{"abc" 1}
85
86 (def! f-wm2 ^{"abc" 1} (fn* [a] (+ 1 a)))
87 (meta f-wm2)
88 ;=>{"abc" 1}
89
90 ;; Meta of native functions should return nil (not fail)
91 (meta +)
92 ;=>nil
93
94 ;;
95 ;; Make sure closures and metadata co-exist
96 (def! gen-plusX (fn* (x) (with-meta (fn* (b) (+ x b)) {"meta" 1})))
97 (def! plus7 (gen-plusX 7))
98 (def! plus8 (gen-plusX 8))
99 (plus7 8)
100 ;=>15
101 (meta plus7)
102 ;=>{"meta" 1}
103 (meta plus8)
104 ;=>{"meta" 1}
105 (meta (with-meta plus7 {"meta" 2}))
106 ;=>{"meta" 2}
107 (meta plus8)
108 ;=>{"meta" 1}
109
110 ;;
111 ;; Testing string? function
112 (string? "")
113 ;=>true
114 (string? 'abc)
115 ;=>false
116 (string? "abc")
117 ;=>true
118 (string? :abc)
119 ;=>false
120 (string? (keyword "abc"))
121 ;=>false
122 (string? 234)
123 ;=>false
124 (string? nil)
125 ;=>false
126
127 ;; Testing number? function
128 (number? 123)
129 ;=>true
130 (number? -1)
131 ;=>true
132 (number? nil)
133 ;=>false
134 (number? false)
135 ;=>false
136 (number? "123")
137 ;=>false
138
139 (def! add1 (fn* (x) (+ x 1)))
140
141 ;; Testing fn? function
142 (fn? +)
143 ;=>true
144 (fn? add1)
145 ;=>true
146 (fn? cond)
147 ;=>false
148 (fn? "+")
149 ;=>false
150 (fn? :+)
151 ;=>false
152 (fn? ^{"ismacro" true} (fn* () 0))
153 ;=>true
154
155 ;; Testing macro? function
156 (macro? cond)
157 ;=>true
158 (macro? +)
159 ;=>false
160 (macro? add1)
161 ;=>false
162 (macro? "+")
163 ;=>false
164 (macro? :+)
165 ;=>false
166 (macro? {})
167 ;=>false
168
169
170 ;;
171 ;; Testing conj function
172 (conj (list) 1)
173 ;=>(1)
174 (conj (list 1) 2)
175 ;=>(2 1)
176 (conj (list 2 3) 4)
177 ;=>(4 2 3)
178 (conj (list 2 3) 4 5 6)
179 ;=>(6 5 4 2 3)
180 (conj (list 1) (list 2 3))
181 ;=>((2 3) 1)
182
183 (conj [] 1)
184 ;=>[1]
185 (conj [1] 2)
186 ;=>[1 2]
187 (conj [2 3] 4)
188 ;=>[2 3 4]
189 (conj [2 3] 4 5 6)
190 ;=>[2 3 4 5 6]
191 (conj [1] [2 3])
192 ;=>[1 [2 3]]
193
194 ;;
195 ;; Testing seq function
196 (seq "abc")
197 ;=>("a" "b" "c")
198 (apply str (seq "this is a test"))
199 ;=>"this is a test"
200 (seq '(2 3 4))
201 ;=>(2 3 4)
202 (seq [2 3 4])
203 ;=>(2 3 4)
204
205 (seq "")
206 ;=>nil
207 (seq '())
208 ;=>nil
209 (seq [])
210 ;=>nil
211 (seq nil)
212 ;=>nil
213
214 ;;
215 ;; Testing metadata on collections
216
217 (meta [1 2 3])
218 ;=>nil
219
220 (with-meta [1 2 3] {"a" 1})
221 ;=>[1 2 3]
222
223 (meta (with-meta [1 2 3] {"a" 1}))
224 ;=>{"a" 1}
225
226 (vector? (with-meta [1 2 3] {"a" 1}))
227 ;=>true
228
229 (meta (with-meta [1 2 3] "abc"))
230 ;=>"abc"
231
232 (with-meta [] "abc")
233 ;=>[]
234
235 (meta (with-meta (list 1 2 3) {"a" 1}))
236 ;=>{"a" 1}
237
238 (list? (with-meta (list 1 2 3) {"a" 1}))
239 ;=>true
240
241 (with-meta (list) {"a" 1})
242 ;=>()
243
244 (empty? (with-meta (list) {"a" 1}))
245 ;=>true
246
247 (meta (with-meta {"abc" 123} {"a" 1}))
248 ;=>{"a" 1}
249
250 (map? (with-meta {"abc" 123} {"a" 1}))
251 ;=>true
252
253 (with-meta {} {"a" 1})
254 ;=>{}
255
256 (def! l-wm (with-meta [4 5 6] {"b" 2}))
257 ;=>[4 5 6]
258 (meta l-wm)
259 ;=>{"b" 2}
260
261 (meta (with-meta l-wm {"new_meta" 123}))
262 ;=>{"new_meta" 123}
263 (meta l-wm)
264 ;=>{"b" 2}
265
266 ;;
267 ;; Testing metadata on builtin functions
268 (meta +)
269 ;=>nil
270 (def! f-wm3 ^{"def" 2} +)
271 (meta f-wm3)
272 ;=>{"def" 2}
273 (meta +)
274 ;=>nil
275
276 ;; Loading sumdown from computations.mal
277 (load-file "../tests/computations.mal")
278 ;=>nil
279
280 ;;
281 ;; Testing time-ms function
282 (def! start-time (time-ms))
283 (= start-time 0)
284 ;=>false
285 (sumdown 10) ; Waste some time
286 ;=>55
287 (> (time-ms) start-time)
288 ;=>true
289
290 ;;
291 ;; Test that defining a macro does not mutate an existing function.
292 (def! f (fn* [x] (number? x)))
293 (defmacro! m f)
294 (f (+ 1 1))
295 ;=>true
296 (m (+ 1 1))
297 ;=>false