Mercurial > lasercutter
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 the3 ; 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 by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 ; Author: Frantisek Sodomka12 (ns clojure.test-clojure.java-interop13 (:use clojure.test))15 ; http://clojure.org/java_interop16 ; http://clojure.org/compilation19 (deftest test-dot20 ; (.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/staticField44 (are [x] (= x 2147483647)45 Integer/MAX_VALUE46 (. Integer MAX_VALUE) ))49 (deftest test-double-dot50 (is (= (.. System (getProperties) (get "os.name"))51 (. (. System (getProperties)) (get "os.name")))))54 (deftest test-doto55 (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.HashMap60 m {"a" 1 "b" 2} )))63 (deftest test-new64 ; Integer65 (are [expr cls value] (and (= (class expr) cls)66 (= expr value))67 (new java.lang.Integer 42) java.lang.Integer 4268 (java.lang.Integer. 123) java.lang.Integer 123 )70 ; Date71 (are [x] (= (class x) java.util.Date)72 (new java.util.Date)73 (java.util.Date.) ))76 (deftest test-instance?77 ; evaluation78 (are [x y] (= x y)79 (instance? java.lang.Integer (+ 1 2)) true80 (instance? java.lang.Long (+ 1 2)) false )82 ; different types83 (are [type literal] (instance? literal type)84 1 java.lang.Integer85 1.0 java.lang.Double86 1M java.math.BigDecimal87 \a java.lang.Character88 "a" java.lang.String )90 ; it is an int, nothing else91 (are [x y] (= (instance? x 42) y)92 java.lang.Integer true93 java.lang.Long false94 java.lang.Character false95 java.lang.String false ))98 ; set!100 ; memfn103 (deftest test-bean104 (let [b (bean java.awt.Color/black)]105 (are [x y] (= x y)106 (map? b) true108 (:red b) 0109 (:green b) 0110 (:blue b) 0111 (:RGB b) -16777216113 (:alpha b) 255114 (:transparency b) 1116 (:class b) java.awt.Color )))119 ; proxy, proxy-super122 (deftest test-bases123 (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-supers130 (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.Object135 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, areduce142 (defmacro deftest-type-array [type-array type]143 `(deftest ~(symbol (str "test-" type-array))144 ; correct type145 (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 sequence153 (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-value160 (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-seq165 (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-exceptions186 (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-array194 ; negative size195 (is (thrown? NegativeArraySizeException (make-array Integer -1)))197 ; one-dimensional198 (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) 42205 (class (aget a 3)) Integer ))207 ; multi-dimensional208 (let [a (make-array Integer 3 2 4)]209 (aset a 0 1 2 987)210 (are [x y] (= x y)211 (alength a) 3212 (alength (first a)) 2213 (alength (first (first a))) 4215 (aget a 0 1 2) 987216 (class (aget a 0 1 2)) Integer )))219 (deftest test-to-array220 (let [v [1 "abc" :kw \c []]221 a (to-array v)]222 (are [x y] (= x y)223 ; length224 (alength a) (count v)226 ; content227 (vec a) v228 (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 collections235 (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-array252 ; compatible types only253 (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 case258 (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) v263 (class (first a)) (class (first v)) ))265 ; given type266 (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 collections273 (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-2d292 ; needs to be a collection of collection(s)293 (is (thrown? Exception (to-array-2d [1 2 3])))295 ; ragged array296 (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 array309 (let [a (to-array-2d [])]310 (are [x y] (= x y)311 (alength a) 0312 (vec a) [] )))315 (deftest test-alength316 (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-aclone360 ; clone all arrays except 2D361 (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 2D388 (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, #^doubles398 ; Coercions: [int, long, float, double, char, boolean, short, byte]399 ; num400 ; ints/longs/floats/doubles402 (deftest test-boolean403 (are [x y] (and (instance? java.lang.Boolean (boolean x))404 (= (boolean x) y))405 nil false406 false false407 true true409 0 true410 1 true411 () true412 [1] true414 "" true415 \space true416 :kw true ))419 (deftest test-char420 ; int -> char421 (is (instance? java.lang.Character (char 65)))423 ; char -> char424 (is (instance? java.lang.Character (char \a)))425 (is (= (char \a) \a)))427 ;; Note: More coercions in numbers.clj