Mercurial > lasercutter
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/test_clojure/java_interop.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,427 @@ 1.4 +; Copyright (c) Rich Hickey. All rights reserved. 1.5 +; The use and distribution terms for this software are covered by the 1.6 +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.7 +; which can be found in the file epl-v10.html at the root of this distribution. 1.8 +; By using this software in any fashion, you are agreeing to be bound by 1.9 +; the terms of this license. 1.10 +; You must not remove this notice, or any other, from this software. 1.11 + 1.12 +; Author: Frantisek Sodomka 1.13 + 1.14 + 1.15 +(ns clojure.test-clojure.java-interop 1.16 + (:use clojure.test)) 1.17 + 1.18 +; http://clojure.org/java_interop 1.19 +; http://clojure.org/compilation 1.20 + 1.21 + 1.22 +(deftest test-dot 1.23 + ; (.instanceMember instance args*) 1.24 + (are [x] (= x "FRED") 1.25 + (.toUpperCase "fred") 1.26 + (. "fred" toUpperCase) 1.27 + (. "fred" (toUpperCase)) ) 1.28 + 1.29 + (are [x] (= x true) 1.30 + (.startsWith "abcde" "ab") 1.31 + (. "abcde" startsWith "ab") 1.32 + (. "abcde" (startsWith "ab")) ) 1.33 + 1.34 + ; (.instanceMember Classname args*) 1.35 + (are [x] (= x "java.lang.String") 1.36 + (.getName String) 1.37 + (. (identity String) getName) 1.38 + (. (identity String) (getName)) ) 1.39 + 1.40 + ; (Classname/staticMethod args*) 1.41 + (are [x] (= x 7) 1.42 + (Math/abs -7) 1.43 + (. Math abs -7) 1.44 + (. Math (abs -7)) ) 1.45 + 1.46 + ; Classname/staticField 1.47 + (are [x] (= x 2147483647) 1.48 + Integer/MAX_VALUE 1.49 + (. Integer MAX_VALUE) )) 1.50 + 1.51 + 1.52 +(deftest test-double-dot 1.53 + (is (= (.. System (getProperties) (get "os.name")) 1.54 + (. (. System (getProperties)) (get "os.name"))))) 1.55 + 1.56 + 1.57 +(deftest test-doto 1.58 + (let [m (doto (new java.util.HashMap) 1.59 + (.put "a" 1) 1.60 + (.put "b" 2))] 1.61 + (are [x y] (= x y) 1.62 + (class m) java.util.HashMap 1.63 + m {"a" 1 "b" 2} ))) 1.64 + 1.65 + 1.66 +(deftest test-new 1.67 + ; Integer 1.68 + (are [expr cls value] (and (= (class expr) cls) 1.69 + (= expr value)) 1.70 + (new java.lang.Integer 42) java.lang.Integer 42 1.71 + (java.lang.Integer. 123) java.lang.Integer 123 ) 1.72 + 1.73 + ; Date 1.74 + (are [x] (= (class x) java.util.Date) 1.75 + (new java.util.Date) 1.76 + (java.util.Date.) )) 1.77 + 1.78 + 1.79 +(deftest test-instance? 1.80 + ; evaluation 1.81 + (are [x y] (= x y) 1.82 + (instance? java.lang.Integer (+ 1 2)) true 1.83 + (instance? java.lang.Long (+ 1 2)) false ) 1.84 + 1.85 + ; different types 1.86 + (are [type literal] (instance? literal type) 1.87 + 1 java.lang.Integer 1.88 + 1.0 java.lang.Double 1.89 + 1M java.math.BigDecimal 1.90 + \a java.lang.Character 1.91 + "a" java.lang.String ) 1.92 + 1.93 + ; it is an int, nothing else 1.94 + (are [x y] (= (instance? x 42) y) 1.95 + java.lang.Integer true 1.96 + java.lang.Long false 1.97 + java.lang.Character false 1.98 + java.lang.String false )) 1.99 + 1.100 + 1.101 +; set! 1.102 + 1.103 +; memfn 1.104 + 1.105 + 1.106 +(deftest test-bean 1.107 + (let [b (bean java.awt.Color/black)] 1.108 + (are [x y] (= x y) 1.109 + (map? b) true 1.110 + 1.111 + (:red b) 0 1.112 + (:green b) 0 1.113 + (:blue b) 0 1.114 + (:RGB b) -16777216 1.115 + 1.116 + (:alpha b) 255 1.117 + (:transparency b) 1 1.118 + 1.119 + (:class b) java.awt.Color ))) 1.120 + 1.121 + 1.122 +; proxy, proxy-super 1.123 + 1.124 + 1.125 +(deftest test-bases 1.126 + (are [x y] (= x y) 1.127 + (bases java.lang.Math) 1.128 + (list java.lang.Object) 1.129 + (bases java.lang.Integer) 1.130 + (list java.lang.Number java.lang.Comparable) )) 1.131 + 1.132 +(deftest test-supers 1.133 + (are [x y] (= x y) 1.134 + (supers java.lang.Math) 1.135 + #{java.lang.Object} 1.136 + (supers java.lang.Integer) 1.137 + #{java.lang.Number java.lang.Object 1.138 + java.lang.Comparable java.io.Serializable} )) 1.139 + 1.140 + 1.141 +; Arrays: [alength] aget aset [make-array to-array into-array to-array-2d aclone] 1.142 +; [float-array, int-array, etc] 1.143 +; amap, areduce 1.144 + 1.145 +(defmacro deftest-type-array [type-array type] 1.146 + `(deftest ~(symbol (str "test-" type-array)) 1.147 + ; correct type 1.148 + (is (= (class (first (~type-array [1 2]))) (class (~type 1)))) 1.149 + 1.150 + ; given size (and empty) 1.151 + (are [x] (and (= (alength (~type-array x)) x) 1.152 + (= (vec (~type-array x)) (repeat x 0))) 1.153 + 0 1 5 ) 1.154 + 1.155 + ; copy of a sequence 1.156 + (are [x] (and (= (alength (~type-array x)) (count x)) 1.157 + (= (vec (~type-array x)) x)) 1.158 + [] 1.159 + [1] 1.160 + [1 -2 3 0 5] ) 1.161 + 1.162 + ; given size and init-value 1.163 + (are [x] (and (= (alength (~type-array x 42)) x) 1.164 + (= (vec (~type-array x 42)) (repeat x 42))) 1.165 + 0 1 5 ) 1.166 + 1.167 + ; given size and init-seq 1.168 + (are [x y z] (and (= (alength (~type-array x y)) x) 1.169 + (= (vec (~type-array x y)) z)) 1.170 + 0 [] [] 1.171 + 0 [1] [] 1.172 + 0 [1 2 3] [] 1.173 + 1 [] [0] 1.174 + 1 [1] [1] 1.175 + 1 [1 2 3] [1] 1.176 + 5 [] [0 0 0 0 0] 1.177 + 5 [1] [1 0 0 0 0] 1.178 + 5 [1 2 3] [1 2 3 0 0] 1.179 + 5 [1 2 3 4 5] [1 2 3 4 5] 1.180 + 5 [1 2 3 4 5 6 7] [1 2 3 4 5] ))) 1.181 + 1.182 +(deftest-type-array int-array int) 1.183 +(deftest-type-array long-array long) 1.184 +(deftest-type-array float-array float) 1.185 +(deftest-type-array double-array double) 1.186 + 1.187 +; separate test for exceptions (doesn't work with above macro...) 1.188 +(deftest test-type-array-exceptions 1.189 + (are [x] (thrown? NegativeArraySizeException x) 1.190 + (int-array -1) 1.191 + (long-array -1) 1.192 + (float-array -1) 1.193 + (double-array -1) )) 1.194 + 1.195 + 1.196 +(deftest test-make-array 1.197 + ; negative size 1.198 + (is (thrown? NegativeArraySizeException (make-array Integer -1))) 1.199 + 1.200 + ; one-dimensional 1.201 + (are [x] (= (alength (make-array Integer x)) x) 1.202 + 0 1 5 ) 1.203 + 1.204 + (let [a (make-array Integer 5)] 1.205 + (aset a 3 42) 1.206 + (are [x y] (= x y) 1.207 + (aget a 3) 42 1.208 + (class (aget a 3)) Integer )) 1.209 + 1.210 + ; multi-dimensional 1.211 + (let [a (make-array Integer 3 2 4)] 1.212 + (aset a 0 1 2 987) 1.213 + (are [x y] (= x y) 1.214 + (alength a) 3 1.215 + (alength (first a)) 2 1.216 + (alength (first (first a))) 4 1.217 + 1.218 + (aget a 0 1 2) 987 1.219 + (class (aget a 0 1 2)) Integer ))) 1.220 + 1.221 + 1.222 +(deftest test-to-array 1.223 + (let [v [1 "abc" :kw \c []] 1.224 + a (to-array v)] 1.225 + (are [x y] (= x y) 1.226 + ; length 1.227 + (alength a) (count v) 1.228 + 1.229 + ; content 1.230 + (vec a) v 1.231 + (class (aget a 0)) (class (nth v 0)) 1.232 + (class (aget a 1)) (class (nth v 1)) 1.233 + (class (aget a 2)) (class (nth v 2)) 1.234 + (class (aget a 3)) (class (nth v 3)) 1.235 + (class (aget a 4)) (class (nth v 4)) )) 1.236 + 1.237 + ; different kinds of collections 1.238 + (are [x] (and (= (alength (to-array x)) (count x)) 1.239 + (= (vec (to-array x)) (vec x))) 1.240 + () 1.241 + '(1 2) 1.242 + [] 1.243 + [1 2] 1.244 + (sorted-set) 1.245 + (sorted-set 1 2) 1.246 + 1.247 + (int-array 0) 1.248 + (int-array [1 2 3]) 1.249 + 1.250 + (to-array []) 1.251 + (to-array [1 2 3]) )) 1.252 + 1.253 + 1.254 +(deftest test-into-array 1.255 + ; compatible types only 1.256 + (is (thrown? IllegalArgumentException (into-array [1 "abc" :kw]))) 1.257 + (is (thrown? IllegalArgumentException (into-array [1.2 4]))) 1.258 + (is (thrown? IllegalArgumentException (into-array [(byte 2) (short 3)]))) 1.259 + 1.260 + ; simple case 1.261 + (let [v [1 2 3 4 5] 1.262 + a (into-array v)] 1.263 + (are [x y] (= x y) 1.264 + (alength a) (count v) 1.265 + (vec a) v 1.266 + (class (first a)) (class (first v)) )) 1.267 + 1.268 + ; given type 1.269 + (let [a (into-array Integer/TYPE [(byte 2) (short 3) (int 4)])] 1.270 + (are [x] (= x Integer) 1.271 + (class (aget a 0)) 1.272 + (class (aget a 1)) 1.273 + (class (aget a 2)) )) 1.274 + 1.275 + ; different kinds of collections 1.276 + (are [x] (and (= (alength (into-array x)) (count x)) 1.277 + (= (vec (into-array x)) (vec x)) 1.278 + (= (alength (into-array Integer/TYPE x)) (count x)) 1.279 + (= (vec (into-array Integer/TYPE x)) (vec x))) 1.280 + () 1.281 + '(1 2) 1.282 + [] 1.283 + [1 2] 1.284 + (sorted-set) 1.285 + (sorted-set 1 2) 1.286 + 1.287 + (int-array 0) 1.288 + (int-array [1 2 3]) 1.289 + 1.290 + (to-array []) 1.291 + (to-array [1 2 3]) )) 1.292 + 1.293 + 1.294 +(deftest test-to-array-2d 1.295 + ; needs to be a collection of collection(s) 1.296 + (is (thrown? Exception (to-array-2d [1 2 3]))) 1.297 + 1.298 + ; ragged array 1.299 + (let [v [[1] [2 3] [4 5 6]] 1.300 + a (to-array-2d v)] 1.301 + (are [x y] (= x y) 1.302 + (alength a) (count v) 1.303 + (alength (aget a 0)) (count (nth v 0)) 1.304 + (alength (aget a 1)) (count (nth v 1)) 1.305 + (alength (aget a 2)) (count (nth v 2)) 1.306 + 1.307 + (vec (aget a 0)) (nth v 0) 1.308 + (vec (aget a 1)) (nth v 1) 1.309 + (vec (aget a 2)) (nth v 2) )) 1.310 + 1.311 + ; empty array 1.312 + (let [a (to-array-2d [])] 1.313 + (are [x y] (= x y) 1.314 + (alength a) 0 1.315 + (vec a) [] ))) 1.316 + 1.317 + 1.318 +(deftest test-alength 1.319 + (are [x] (= (alength x) 0) 1.320 + (int-array 0) 1.321 + (long-array 0) 1.322 + (float-array 0) 1.323 + (double-array 0) 1.324 + (boolean-array 0) 1.325 + (byte-array 0) 1.326 + (char-array 0) 1.327 + (short-array 0) 1.328 + (make-array Integer/TYPE 0) 1.329 + (to-array []) 1.330 + (into-array []) 1.331 + (to-array-2d []) ) 1.332 + 1.333 + (are [x] (= (alength x) 1) 1.334 + (int-array 1) 1.335 + (long-array 1) 1.336 + (float-array 1) 1.337 + (double-array 1) 1.338 + (boolean-array 1) 1.339 + (byte-array 1) 1.340 + (char-array 1) 1.341 + (short-array 1) 1.342 + (make-array Integer/TYPE 1) 1.343 + (to-array [1]) 1.344 + (into-array [1]) 1.345 + (to-array-2d [[1]]) ) 1.346 + 1.347 + (are [x] (= (alength x) 3) 1.348 + (int-array 3) 1.349 + (long-array 3) 1.350 + (float-array 3) 1.351 + (double-array 3) 1.352 + (boolean-array 3) 1.353 + (byte-array 3) 1.354 + (char-array 3) 1.355 + (short-array 3) 1.356 + (make-array Integer/TYPE 3) 1.357 + (to-array [1 "a" :k]) 1.358 + (into-array [1 2 3]) 1.359 + (to-array-2d [[1] [2 3] [4 5 6]]) )) 1.360 + 1.361 + 1.362 +(deftest test-aclone 1.363 + ; clone all arrays except 2D 1.364 + (are [x] (and (= (alength (aclone x)) (alength x)) 1.365 + (= (vec (aclone x)) (vec x))) 1.366 + (int-array 0) 1.367 + (long-array 0) 1.368 + (float-array 0) 1.369 + (double-array 0) 1.370 + (boolean-array 0) 1.371 + (byte-array 0) 1.372 + (char-array 0) 1.373 + (short-array 0) 1.374 + (make-array Integer/TYPE 0) 1.375 + (to-array []) 1.376 + (into-array []) 1.377 + 1.378 + (int-array [1 2 3]) 1.379 + (long-array [1 2 3]) 1.380 + (float-array [1 2 3]) 1.381 + (double-array [1 2 3]) 1.382 + (boolean-array [true false]) 1.383 + (byte-array [(byte 1) (byte 2)]) 1.384 + (char-array [\a \b \c]) 1.385 + (short-array [(short 1) (short 2)]) 1.386 + (make-array Integer/TYPE 3) 1.387 + (to-array [1 "a" :k]) 1.388 + (into-array [1 2 3]) ) 1.389 + 1.390 + ; clone 2D 1.391 + (are [x] (and (= (alength (aclone x)) (alength x)) 1.392 + (= (map alength (aclone x)) (map alength x)) 1.393 + (= (map vec (aclone x)) (map vec x))) 1.394 + (to-array-2d []) 1.395 + (to-array-2d [[1] [2 3] [4 5 6]]) )) 1.396 + 1.397 + 1.398 +; Type Hints, *warn-on-reflection* 1.399 +; #^ints, #^floats, #^longs, #^doubles 1.400 + 1.401 +; Coercions: [int, long, float, double, char, boolean, short, byte] 1.402 +; num 1.403 +; ints/longs/floats/doubles 1.404 + 1.405 +(deftest test-boolean 1.406 + (are [x y] (and (instance? java.lang.Boolean (boolean x)) 1.407 + (= (boolean x) y)) 1.408 + nil false 1.409 + false false 1.410 + true true 1.411 + 1.412 + 0 true 1.413 + 1 true 1.414 + () true 1.415 + [1] true 1.416 + 1.417 + "" true 1.418 + \space true 1.419 + :kw true )) 1.420 + 1.421 + 1.422 +(deftest test-char 1.423 + ; int -> char 1.424 + (is (instance? java.lang.Character (char 65))) 1.425 + 1.426 + ; char -> char 1.427 + (is (instance? java.lang.Character (char \a))) 1.428 + (is (= (char \a) \a))) 1.429 + 1.430 +;; Note: More coercions in numbers.clj