generate perf analysis
[jackhill/mal.git] / impls / 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;;
28b63c0c
NB
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
dbac60df
JM
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
7e0bb668
JM
90;; Meta of native functions should return nil (not fail)
91(meta +)
92;=>nil
93
dbac60df
JM
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
59436f1a 110;;
393d1140
JM
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
2ff47a34 127;; Testing number? function
59436f1a
DM
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
69edbad7
DM
139(def! add1 (fn* (x) (+ x 1)))
140
28b63c0c
NB
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
59436f1a
DM
155;; Testing macro? function
156(macro? cond)
157;=>true
158(macro? +)
159;=>false
69edbad7 160(macro? add1)
59436f1a
DM
161;=>false
162(macro? "+")
163;=>false
164(macro? :+)
165;=>false
809d74cb
NB
166(macro? {})
167;=>false
59436f1a 168
329b6db3 169
dbac60df
JM
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
329b6db3
JM
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
dbac60df
JM
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
ba53efc2
ST
226(vector? (with-meta [1 2 3] {"a" 1}))
227;=>true
228
dbac60df
JM
229(meta (with-meta [1 2 3] "abc"))
230;=>"abc"
231
bfb315c8
BH
232(with-meta [] "abc")
233;=>[]
234
dbac60df
JM
235(meta (with-meta (list 1 2 3) {"a" 1}))
236;=>{"a" 1}
237
ba53efc2
ST
238(list? (with-meta (list 1 2 3) {"a" 1}))
239;=>true
240
bfb315c8
BH
241(with-meta (list) {"a" 1})
242;=>()
243
244(empty? (with-meta (list) {"a" 1}))
245;=>true
246
dbac60df
JM
247(meta (with-meta {"abc" 123} {"a" 1}))
248;=>{"a" 1}
249
ba53efc2
ST
250(map? (with-meta {"abc" 123} {"a" 1}))
251;=>true
252
bfb315c8
BH
253(with-meta {} {"a" 1})
254;=>{}
255
dbac60df
JM
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
83665b4f
NB
276;; Loading sumdown from computations.mal
277(load-file "../tests/computations.mal")
e6d41de4 278;=>nil
3e9b89d4 279
44c9a7d8
DM
280;;
281;; Testing time-ms function
282(def! start-time (time-ms))
bbd62dc9
JM
283(= start-time 0)
284;=>false
3e9b89d4 285(sumdown 10) ; Waste some time
44c9a7d8
DM
286;=>55
287(> (time-ms) start-time)
288;=>true
c714889a
NB
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