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