Mercurial > lasercutter
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
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. | |
8 | |
9 ; Author: Frantisek Sodomka | |
10 | |
11 | |
12 (ns clojure.test-clojure.java-interop | |
13 (:use clojure.test)) | |
14 | |
15 ; http://clojure.org/java_interop | |
16 ; http://clojure.org/compilation | |
17 | |
18 | |
19 (deftest test-dot | |
20 ; (.instanceMember instance args*) | |
21 (are [x] (= x "FRED") | |
22 (.toUpperCase "fred") | |
23 (. "fred" toUpperCase) | |
24 (. "fred" (toUpperCase)) ) | |
25 | |
26 (are [x] (= x true) | |
27 (.startsWith "abcde" "ab") | |
28 (. "abcde" startsWith "ab") | |
29 (. "abcde" (startsWith "ab")) ) | |
30 | |
31 ; (.instanceMember Classname args*) | |
32 (are [x] (= x "java.lang.String") | |
33 (.getName String) | |
34 (. (identity String) getName) | |
35 (. (identity String) (getName)) ) | |
36 | |
37 ; (Classname/staticMethod args*) | |
38 (are [x] (= x 7) | |
39 (Math/abs -7) | |
40 (. Math abs -7) | |
41 (. Math (abs -7)) ) | |
42 | |
43 ; Classname/staticField | |
44 (are [x] (= x 2147483647) | |
45 Integer/MAX_VALUE | |
46 (. Integer MAX_VALUE) )) | |
47 | |
48 | |
49 (deftest test-double-dot | |
50 (is (= (.. System (getProperties) (get "os.name")) | |
51 (. (. System (getProperties)) (get "os.name"))))) | |
52 | |
53 | |
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} ))) | |
61 | |
62 | |
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 ) | |
69 | |
70 ; Date | |
71 (are [x] (= (class x) java.util.Date) | |
72 (new java.util.Date) | |
73 (java.util.Date.) )) | |
74 | |
75 | |
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 ) | |
81 | |
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 ) | |
89 | |
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 )) | |
96 | |
97 | |
98 ; set! | |
99 | |
100 ; memfn | |
101 | |
102 | |
103 (deftest test-bean | |
104 (let [b (bean java.awt.Color/black)] | |
105 (are [x y] (= x y) | |
106 (map? b) true | |
107 | |
108 (:red b) 0 | |
109 (:green b) 0 | |
110 (:blue b) 0 | |
111 (:RGB b) -16777216 | |
112 | |
113 (:alpha b) 255 | |
114 (:transparency b) 1 | |
115 | |
116 (:class b) java.awt.Color ))) | |
117 | |
118 | |
119 ; proxy, proxy-super | |
120 | |
121 | |
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) )) | |
128 | |
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} )) | |
136 | |
137 | |
138 ; Arrays: [alength] aget aset [make-array to-array into-array to-array-2d aclone] | |
139 ; [float-array, int-array, etc] | |
140 ; amap, areduce | |
141 | |
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)))) | |
146 | |
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 ) | |
151 | |
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] ) | |
158 | |
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 ) | |
163 | |
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] ))) | |
178 | |
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) | |
183 | |
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) )) | |
191 | |
192 | |
193 (deftest test-make-array | |
194 ; negative size | |
195 (is (thrown? NegativeArraySizeException (make-array Integer -1))) | |
196 | |
197 ; one-dimensional | |
198 (are [x] (= (alength (make-array Integer x)) x) | |
199 0 1 5 ) | |
200 | |
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 )) | |
206 | |
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 | |
214 | |
215 (aget a 0 1 2) 987 | |
216 (class (aget a 0 1 2)) Integer ))) | |
217 | |
218 | |
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) | |
225 | |
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)) )) | |
233 | |
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) | |
243 | |
244 (int-array 0) | |
245 (int-array [1 2 3]) | |
246 | |
247 (to-array []) | |
248 (to-array [1 2 3]) )) | |
249 | |
250 | |
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)]))) | |
256 | |
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)) )) | |
264 | |
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)) )) | |
271 | |
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) | |
283 | |
284 (int-array 0) | |
285 (int-array [1 2 3]) | |
286 | |
287 (to-array []) | |
288 (to-array [1 2 3]) )) | |
289 | |
290 | |
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]))) | |
294 | |
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)) | |
303 | |
304 (vec (aget a 0)) (nth v 0) | |
305 (vec (aget a 1)) (nth v 1) | |
306 (vec (aget a 2)) (nth v 2) )) | |
307 | |
308 ; empty array | |
309 (let [a (to-array-2d [])] | |
310 (are [x y] (= x y) | |
311 (alength a) 0 | |
312 (vec a) [] ))) | |
313 | |
314 | |
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 []) ) | |
329 | |
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]]) ) | |
343 | |
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]]) )) | |
357 | |
358 | |
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 []) | |
374 | |
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]) ) | |
386 | |
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]]) )) | |
393 | |
394 | |
395 ; Type Hints, *warn-on-reflection* | |
396 ; #^ints, #^floats, #^longs, #^doubles | |
397 | |
398 ; Coercions: [int, long, float, double, char, boolean, short, byte] | |
399 ; num | |
400 ; ints/longs/floats/doubles | |
401 | |
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 | |
408 | |
409 0 true | |
410 1 true | |
411 () true | |
412 [1] true | |
413 | |
414 "" true | |
415 \space true | |
416 :kw true )) | |
417 | |
418 | |
419 (deftest test-char | |
420 ; int -> char | |
421 (is (instance? java.lang.Character (char 65))) | |
422 | |
423 ; char -> char | |
424 (is (instance? java.lang.Character (char \a))) | |
425 (is (= (char \a) \a))) | |
426 | |
427 ;; Note: More coercions in numbers.clj |