view src/clojure/test_clojure/java_interop.clj @ 10:ef7dbbd6452c

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