rlm@10
|
1 ; Copyright (c) Rich Hickey. All rights reserved.
|
rlm@10
|
2 ; The use and distribution terms for this software are covered by the
|
rlm@10
|
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
4 ; which can be found in the file epl-v10.html at the root of this distribution.
|
rlm@10
|
5 ; By using this software in any fashion, you are agreeing to be bound by
|
rlm@10
|
6 ; the terms of this license.
|
rlm@10
|
7 ; You must not remove this notice, or any other, from this software.
|
rlm@10
|
8
|
rlm@10
|
9 ; Author: Frantisek Sodomka
|
rlm@10
|
10
|
rlm@10
|
11
|
rlm@10
|
12 (ns clojure.test-clojure.java-interop
|
rlm@10
|
13 (:use clojure.test))
|
rlm@10
|
14
|
rlm@10
|
15 ; http://clojure.org/java_interop
|
rlm@10
|
16 ; http://clojure.org/compilation
|
rlm@10
|
17
|
rlm@10
|
18
|
rlm@10
|
19 (deftest test-dot
|
rlm@10
|
20 ; (.instanceMember instance args*)
|
rlm@10
|
21 (are [x] (= x "FRED")
|
rlm@10
|
22 (.toUpperCase "fred")
|
rlm@10
|
23 (. "fred" toUpperCase)
|
rlm@10
|
24 (. "fred" (toUpperCase)) )
|
rlm@10
|
25
|
rlm@10
|
26 (are [x] (= x true)
|
rlm@10
|
27 (.startsWith "abcde" "ab")
|
rlm@10
|
28 (. "abcde" startsWith "ab")
|
rlm@10
|
29 (. "abcde" (startsWith "ab")) )
|
rlm@10
|
30
|
rlm@10
|
31 ; (.instanceMember Classname args*)
|
rlm@10
|
32 (are [x] (= x "java.lang.String")
|
rlm@10
|
33 (.getName String)
|
rlm@10
|
34 (. (identity String) getName)
|
rlm@10
|
35 (. (identity String) (getName)) )
|
rlm@10
|
36
|
rlm@10
|
37 ; (Classname/staticMethod args*)
|
rlm@10
|
38 (are [x] (= x 7)
|
rlm@10
|
39 (Math/abs -7)
|
rlm@10
|
40 (. Math abs -7)
|
rlm@10
|
41 (. Math (abs -7)) )
|
rlm@10
|
42
|
rlm@10
|
43 ; Classname/staticField
|
rlm@10
|
44 (are [x] (= x 2147483647)
|
rlm@10
|
45 Integer/MAX_VALUE
|
rlm@10
|
46 (. Integer MAX_VALUE) ))
|
rlm@10
|
47
|
rlm@10
|
48
|
rlm@10
|
49 (deftest test-double-dot
|
rlm@10
|
50 (is (= (.. System (getProperties) (get "os.name"))
|
rlm@10
|
51 (. (. System (getProperties)) (get "os.name")))))
|
rlm@10
|
52
|
rlm@10
|
53
|
rlm@10
|
54 (deftest test-doto
|
rlm@10
|
55 (let [m (doto (new java.util.HashMap)
|
rlm@10
|
56 (.put "a" 1)
|
rlm@10
|
57 (.put "b" 2))]
|
rlm@10
|
58 (are [x y] (= x y)
|
rlm@10
|
59 (class m) java.util.HashMap
|
rlm@10
|
60 m {"a" 1 "b" 2} )))
|
rlm@10
|
61
|
rlm@10
|
62
|
rlm@10
|
63 (deftest test-new
|
rlm@10
|
64 ; Integer
|
rlm@10
|
65 (are [expr cls value] (and (= (class expr) cls)
|
rlm@10
|
66 (= expr value))
|
rlm@10
|
67 (new java.lang.Integer 42) java.lang.Integer 42
|
rlm@10
|
68 (java.lang.Integer. 123) java.lang.Integer 123 )
|
rlm@10
|
69
|
rlm@10
|
70 ; Date
|
rlm@10
|
71 (are [x] (= (class x) java.util.Date)
|
rlm@10
|
72 (new java.util.Date)
|
rlm@10
|
73 (java.util.Date.) ))
|
rlm@10
|
74
|
rlm@10
|
75
|
rlm@10
|
76 (deftest test-instance?
|
rlm@10
|
77 ; evaluation
|
rlm@10
|
78 (are [x y] (= x y)
|
rlm@10
|
79 (instance? java.lang.Integer (+ 1 2)) true
|
rlm@10
|
80 (instance? java.lang.Long (+ 1 2)) false )
|
rlm@10
|
81
|
rlm@10
|
82 ; different types
|
rlm@10
|
83 (are [type literal] (instance? literal type)
|
rlm@10
|
84 1 java.lang.Integer
|
rlm@10
|
85 1.0 java.lang.Double
|
rlm@10
|
86 1M java.math.BigDecimal
|
rlm@10
|
87 \a java.lang.Character
|
rlm@10
|
88 "a" java.lang.String )
|
rlm@10
|
89
|
rlm@10
|
90 ; it is an int, nothing else
|
rlm@10
|
91 (are [x y] (= (instance? x 42) y)
|
rlm@10
|
92 java.lang.Integer true
|
rlm@10
|
93 java.lang.Long false
|
rlm@10
|
94 java.lang.Character false
|
rlm@10
|
95 java.lang.String false ))
|
rlm@10
|
96
|
rlm@10
|
97
|
rlm@10
|
98 ; set!
|
rlm@10
|
99
|
rlm@10
|
100 ; memfn
|
rlm@10
|
101
|
rlm@10
|
102
|
rlm@10
|
103 (deftest test-bean
|
rlm@10
|
104 (let [b (bean java.awt.Color/black)]
|
rlm@10
|
105 (are [x y] (= x y)
|
rlm@10
|
106 (map? b) true
|
rlm@10
|
107
|
rlm@10
|
108 (:red b) 0
|
rlm@10
|
109 (:green b) 0
|
rlm@10
|
110 (:blue b) 0
|
rlm@10
|
111 (:RGB b) -16777216
|
rlm@10
|
112
|
rlm@10
|
113 (:alpha b) 255
|
rlm@10
|
114 (:transparency b) 1
|
rlm@10
|
115
|
rlm@10
|
116 (:class b) java.awt.Color )))
|
rlm@10
|
117
|
rlm@10
|
118
|
rlm@10
|
119 ; proxy, proxy-super
|
rlm@10
|
120
|
rlm@10
|
121
|
rlm@10
|
122 (deftest test-bases
|
rlm@10
|
123 (are [x y] (= x y)
|
rlm@10
|
124 (bases java.lang.Math)
|
rlm@10
|
125 (list java.lang.Object)
|
rlm@10
|
126 (bases java.lang.Integer)
|
rlm@10
|
127 (list java.lang.Number java.lang.Comparable) ))
|
rlm@10
|
128
|
rlm@10
|
129 (deftest test-supers
|
rlm@10
|
130 (are [x y] (= x y)
|
rlm@10
|
131 (supers java.lang.Math)
|
rlm@10
|
132 #{java.lang.Object}
|
rlm@10
|
133 (supers java.lang.Integer)
|
rlm@10
|
134 #{java.lang.Number java.lang.Object
|
rlm@10
|
135 java.lang.Comparable java.io.Serializable} ))
|
rlm@10
|
136
|
rlm@10
|
137
|
rlm@10
|
138 ; Arrays: [alength] aget aset [make-array to-array into-array to-array-2d aclone]
|
rlm@10
|
139 ; [float-array, int-array, etc]
|
rlm@10
|
140 ; amap, areduce
|
rlm@10
|
141
|
rlm@10
|
142 (defmacro deftest-type-array [type-array type]
|
rlm@10
|
143 `(deftest ~(symbol (str "test-" type-array))
|
rlm@10
|
144 ; correct type
|
rlm@10
|
145 (is (= (class (first (~type-array [1 2]))) (class (~type 1))))
|
rlm@10
|
146
|
rlm@10
|
147 ; given size (and empty)
|
rlm@10
|
148 (are [x] (and (= (alength (~type-array x)) x)
|
rlm@10
|
149 (= (vec (~type-array x)) (repeat x 0)))
|
rlm@10
|
150 0 1 5 )
|
rlm@10
|
151
|
rlm@10
|
152 ; copy of a sequence
|
rlm@10
|
153 (are [x] (and (= (alength (~type-array x)) (count x))
|
rlm@10
|
154 (= (vec (~type-array x)) x))
|
rlm@10
|
155 []
|
rlm@10
|
156 [1]
|
rlm@10
|
157 [1 -2 3 0 5] )
|
rlm@10
|
158
|
rlm@10
|
159 ; given size and init-value
|
rlm@10
|
160 (are [x] (and (= (alength (~type-array x 42)) x)
|
rlm@10
|
161 (= (vec (~type-array x 42)) (repeat x 42)))
|
rlm@10
|
162 0 1 5 )
|
rlm@10
|
163
|
rlm@10
|
164 ; given size and init-seq
|
rlm@10
|
165 (are [x y z] (and (= (alength (~type-array x y)) x)
|
rlm@10
|
166 (= (vec (~type-array x y)) z))
|
rlm@10
|
167 0 [] []
|
rlm@10
|
168 0 [1] []
|
rlm@10
|
169 0 [1 2 3] []
|
rlm@10
|
170 1 [] [0]
|
rlm@10
|
171 1 [1] [1]
|
rlm@10
|
172 1 [1 2 3] [1]
|
rlm@10
|
173 5 [] [0 0 0 0 0]
|
rlm@10
|
174 5 [1] [1 0 0 0 0]
|
rlm@10
|
175 5 [1 2 3] [1 2 3 0 0]
|
rlm@10
|
176 5 [1 2 3 4 5] [1 2 3 4 5]
|
rlm@10
|
177 5 [1 2 3 4 5 6 7] [1 2 3 4 5] )))
|
rlm@10
|
178
|
rlm@10
|
179 (deftest-type-array int-array int)
|
rlm@10
|
180 (deftest-type-array long-array long)
|
rlm@10
|
181 (deftest-type-array float-array float)
|
rlm@10
|
182 (deftest-type-array double-array double)
|
rlm@10
|
183
|
rlm@10
|
184 ; separate test for exceptions (doesn't work with above macro...)
|
rlm@10
|
185 (deftest test-type-array-exceptions
|
rlm@10
|
186 (are [x] (thrown? NegativeArraySizeException x)
|
rlm@10
|
187 (int-array -1)
|
rlm@10
|
188 (long-array -1)
|
rlm@10
|
189 (float-array -1)
|
rlm@10
|
190 (double-array -1) ))
|
rlm@10
|
191
|
rlm@10
|
192
|
rlm@10
|
193 (deftest test-make-array
|
rlm@10
|
194 ; negative size
|
rlm@10
|
195 (is (thrown? NegativeArraySizeException (make-array Integer -1)))
|
rlm@10
|
196
|
rlm@10
|
197 ; one-dimensional
|
rlm@10
|
198 (are [x] (= (alength (make-array Integer x)) x)
|
rlm@10
|
199 0 1 5 )
|
rlm@10
|
200
|
rlm@10
|
201 (let [a (make-array Integer 5)]
|
rlm@10
|
202 (aset a 3 42)
|
rlm@10
|
203 (are [x y] (= x y)
|
rlm@10
|
204 (aget a 3) 42
|
rlm@10
|
205 (class (aget a 3)) Integer ))
|
rlm@10
|
206
|
rlm@10
|
207 ; multi-dimensional
|
rlm@10
|
208 (let [a (make-array Integer 3 2 4)]
|
rlm@10
|
209 (aset a 0 1 2 987)
|
rlm@10
|
210 (are [x y] (= x y)
|
rlm@10
|
211 (alength a) 3
|
rlm@10
|
212 (alength (first a)) 2
|
rlm@10
|
213 (alength (first (first a))) 4
|
rlm@10
|
214
|
rlm@10
|
215 (aget a 0 1 2) 987
|
rlm@10
|
216 (class (aget a 0 1 2)) Integer )))
|
rlm@10
|
217
|
rlm@10
|
218
|
rlm@10
|
219 (deftest test-to-array
|
rlm@10
|
220 (let [v [1 "abc" :kw \c []]
|
rlm@10
|
221 a (to-array v)]
|
rlm@10
|
222 (are [x y] (= x y)
|
rlm@10
|
223 ; length
|
rlm@10
|
224 (alength a) (count v)
|
rlm@10
|
225
|
rlm@10
|
226 ; content
|
rlm@10
|
227 (vec a) v
|
rlm@10
|
228 (class (aget a 0)) (class (nth v 0))
|
rlm@10
|
229 (class (aget a 1)) (class (nth v 1))
|
rlm@10
|
230 (class (aget a 2)) (class (nth v 2))
|
rlm@10
|
231 (class (aget a 3)) (class (nth v 3))
|
rlm@10
|
232 (class (aget a 4)) (class (nth v 4)) ))
|
rlm@10
|
233
|
rlm@10
|
234 ; different kinds of collections
|
rlm@10
|
235 (are [x] (and (= (alength (to-array x)) (count x))
|
rlm@10
|
236 (= (vec (to-array x)) (vec x)))
|
rlm@10
|
237 ()
|
rlm@10
|
238 '(1 2)
|
rlm@10
|
239 []
|
rlm@10
|
240 [1 2]
|
rlm@10
|
241 (sorted-set)
|
rlm@10
|
242 (sorted-set 1 2)
|
rlm@10
|
243
|
rlm@10
|
244 (int-array 0)
|
rlm@10
|
245 (int-array [1 2 3])
|
rlm@10
|
246
|
rlm@10
|
247 (to-array [])
|
rlm@10
|
248 (to-array [1 2 3]) ))
|
rlm@10
|
249
|
rlm@10
|
250
|
rlm@10
|
251 (deftest test-into-array
|
rlm@10
|
252 ; compatible types only
|
rlm@10
|
253 (is (thrown? IllegalArgumentException (into-array [1 "abc" :kw])))
|
rlm@10
|
254 (is (thrown? IllegalArgumentException (into-array [1.2 4])))
|
rlm@10
|
255 (is (thrown? IllegalArgumentException (into-array [(byte 2) (short 3)])))
|
rlm@10
|
256
|
rlm@10
|
257 ; simple case
|
rlm@10
|
258 (let [v [1 2 3 4 5]
|
rlm@10
|
259 a (into-array v)]
|
rlm@10
|
260 (are [x y] (= x y)
|
rlm@10
|
261 (alength a) (count v)
|
rlm@10
|
262 (vec a) v
|
rlm@10
|
263 (class (first a)) (class (first v)) ))
|
rlm@10
|
264
|
rlm@10
|
265 ; given type
|
rlm@10
|
266 (let [a (into-array Integer/TYPE [(byte 2) (short 3) (int 4)])]
|
rlm@10
|
267 (are [x] (= x Integer)
|
rlm@10
|
268 (class (aget a 0))
|
rlm@10
|
269 (class (aget a 1))
|
rlm@10
|
270 (class (aget a 2)) ))
|
rlm@10
|
271
|
rlm@10
|
272 ; different kinds of collections
|
rlm@10
|
273 (are [x] (and (= (alength (into-array x)) (count x))
|
rlm@10
|
274 (= (vec (into-array x)) (vec x))
|
rlm@10
|
275 (= (alength (into-array Integer/TYPE x)) (count x))
|
rlm@10
|
276 (= (vec (into-array Integer/TYPE x)) (vec x)))
|
rlm@10
|
277 ()
|
rlm@10
|
278 '(1 2)
|
rlm@10
|
279 []
|
rlm@10
|
280 [1 2]
|
rlm@10
|
281 (sorted-set)
|
rlm@10
|
282 (sorted-set 1 2)
|
rlm@10
|
283
|
rlm@10
|
284 (int-array 0)
|
rlm@10
|
285 (int-array [1 2 3])
|
rlm@10
|
286
|
rlm@10
|
287 (to-array [])
|
rlm@10
|
288 (to-array [1 2 3]) ))
|
rlm@10
|
289
|
rlm@10
|
290
|
rlm@10
|
291 (deftest test-to-array-2d
|
rlm@10
|
292 ; needs to be a collection of collection(s)
|
rlm@10
|
293 (is (thrown? Exception (to-array-2d [1 2 3])))
|
rlm@10
|
294
|
rlm@10
|
295 ; ragged array
|
rlm@10
|
296 (let [v [[1] [2 3] [4 5 6]]
|
rlm@10
|
297 a (to-array-2d v)]
|
rlm@10
|
298 (are [x y] (= x y)
|
rlm@10
|
299 (alength a) (count v)
|
rlm@10
|
300 (alength (aget a 0)) (count (nth v 0))
|
rlm@10
|
301 (alength (aget a 1)) (count (nth v 1))
|
rlm@10
|
302 (alength (aget a 2)) (count (nth v 2))
|
rlm@10
|
303
|
rlm@10
|
304 (vec (aget a 0)) (nth v 0)
|
rlm@10
|
305 (vec (aget a 1)) (nth v 1)
|
rlm@10
|
306 (vec (aget a 2)) (nth v 2) ))
|
rlm@10
|
307
|
rlm@10
|
308 ; empty array
|
rlm@10
|
309 (let [a (to-array-2d [])]
|
rlm@10
|
310 (are [x y] (= x y)
|
rlm@10
|
311 (alength a) 0
|
rlm@10
|
312 (vec a) [] )))
|
rlm@10
|
313
|
rlm@10
|
314
|
rlm@10
|
315 (deftest test-alength
|
rlm@10
|
316 (are [x] (= (alength x) 0)
|
rlm@10
|
317 (int-array 0)
|
rlm@10
|
318 (long-array 0)
|
rlm@10
|
319 (float-array 0)
|
rlm@10
|
320 (double-array 0)
|
rlm@10
|
321 (boolean-array 0)
|
rlm@10
|
322 (byte-array 0)
|
rlm@10
|
323 (char-array 0)
|
rlm@10
|
324 (short-array 0)
|
rlm@10
|
325 (make-array Integer/TYPE 0)
|
rlm@10
|
326 (to-array [])
|
rlm@10
|
327 (into-array [])
|
rlm@10
|
328 (to-array-2d []) )
|
rlm@10
|
329
|
rlm@10
|
330 (are [x] (= (alength x) 1)
|
rlm@10
|
331 (int-array 1)
|
rlm@10
|
332 (long-array 1)
|
rlm@10
|
333 (float-array 1)
|
rlm@10
|
334 (double-array 1)
|
rlm@10
|
335 (boolean-array 1)
|
rlm@10
|
336 (byte-array 1)
|
rlm@10
|
337 (char-array 1)
|
rlm@10
|
338 (short-array 1)
|
rlm@10
|
339 (make-array Integer/TYPE 1)
|
rlm@10
|
340 (to-array [1])
|
rlm@10
|
341 (into-array [1])
|
rlm@10
|
342 (to-array-2d [[1]]) )
|
rlm@10
|
343
|
rlm@10
|
344 (are [x] (= (alength x) 3)
|
rlm@10
|
345 (int-array 3)
|
rlm@10
|
346 (long-array 3)
|
rlm@10
|
347 (float-array 3)
|
rlm@10
|
348 (double-array 3)
|
rlm@10
|
349 (boolean-array 3)
|
rlm@10
|
350 (byte-array 3)
|
rlm@10
|
351 (char-array 3)
|
rlm@10
|
352 (short-array 3)
|
rlm@10
|
353 (make-array Integer/TYPE 3)
|
rlm@10
|
354 (to-array [1 "a" :k])
|
rlm@10
|
355 (into-array [1 2 3])
|
rlm@10
|
356 (to-array-2d [[1] [2 3] [4 5 6]]) ))
|
rlm@10
|
357
|
rlm@10
|
358
|
rlm@10
|
359 (deftest test-aclone
|
rlm@10
|
360 ; clone all arrays except 2D
|
rlm@10
|
361 (are [x] (and (= (alength (aclone x)) (alength x))
|
rlm@10
|
362 (= (vec (aclone x)) (vec x)))
|
rlm@10
|
363 (int-array 0)
|
rlm@10
|
364 (long-array 0)
|
rlm@10
|
365 (float-array 0)
|
rlm@10
|
366 (double-array 0)
|
rlm@10
|
367 (boolean-array 0)
|
rlm@10
|
368 (byte-array 0)
|
rlm@10
|
369 (char-array 0)
|
rlm@10
|
370 (short-array 0)
|
rlm@10
|
371 (make-array Integer/TYPE 0)
|
rlm@10
|
372 (to-array [])
|
rlm@10
|
373 (into-array [])
|
rlm@10
|
374
|
rlm@10
|
375 (int-array [1 2 3])
|
rlm@10
|
376 (long-array [1 2 3])
|
rlm@10
|
377 (float-array [1 2 3])
|
rlm@10
|
378 (double-array [1 2 3])
|
rlm@10
|
379 (boolean-array [true false])
|
rlm@10
|
380 (byte-array [(byte 1) (byte 2)])
|
rlm@10
|
381 (char-array [\a \b \c])
|
rlm@10
|
382 (short-array [(short 1) (short 2)])
|
rlm@10
|
383 (make-array Integer/TYPE 3)
|
rlm@10
|
384 (to-array [1 "a" :k])
|
rlm@10
|
385 (into-array [1 2 3]) )
|
rlm@10
|
386
|
rlm@10
|
387 ; clone 2D
|
rlm@10
|
388 (are [x] (and (= (alength (aclone x)) (alength x))
|
rlm@10
|
389 (= (map alength (aclone x)) (map alength x))
|
rlm@10
|
390 (= (map vec (aclone x)) (map vec x)))
|
rlm@10
|
391 (to-array-2d [])
|
rlm@10
|
392 (to-array-2d [[1] [2 3] [4 5 6]]) ))
|
rlm@10
|
393
|
rlm@10
|
394
|
rlm@10
|
395 ; Type Hints, *warn-on-reflection*
|
rlm@10
|
396 ; #^ints, #^floats, #^longs, #^doubles
|
rlm@10
|
397
|
rlm@10
|
398 ; Coercions: [int, long, float, double, char, boolean, short, byte]
|
rlm@10
|
399 ; num
|
rlm@10
|
400 ; ints/longs/floats/doubles
|
rlm@10
|
401
|
rlm@10
|
402 (deftest test-boolean
|
rlm@10
|
403 (are [x y] (and (instance? java.lang.Boolean (boolean x))
|
rlm@10
|
404 (= (boolean x) y))
|
rlm@10
|
405 nil false
|
rlm@10
|
406 false false
|
rlm@10
|
407 true true
|
rlm@10
|
408
|
rlm@10
|
409 0 true
|
rlm@10
|
410 1 true
|
rlm@10
|
411 () true
|
rlm@10
|
412 [1] true
|
rlm@10
|
413
|
rlm@10
|
414 "" true
|
rlm@10
|
415 \space true
|
rlm@10
|
416 :kw true ))
|
rlm@10
|
417
|
rlm@10
|
418
|
rlm@10
|
419 (deftest test-char
|
rlm@10
|
420 ; int -> char
|
rlm@10
|
421 (is (instance? java.lang.Character (char 65)))
|
rlm@10
|
422
|
rlm@10
|
423 ; char -> char
|
rlm@10
|
424 (is (instance? java.lang.Character (char \a)))
|
rlm@10
|
425 (is (= (char \a) \a)))
|
rlm@10
|
426
|
rlm@10
|
427 ;; Note: More coercions in numbers.clj
|