diff options
author | Rich Hickey <richhickey@gmail.com> | 2010-04-28 13:02:17 -0400 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2010-04-28 13:02:17 -0400 |
commit | fab49c61d0b383d00270684a40bdf19bb2313ca6 (patch) | |
tree | 47975e7102c8933c2b0ddf62bc9f92055a50167e | |
parent | a55df92faa0c51c634d93a8d991ccfd2638b108b (diff) | |
parent | 57b320020e9387d1a2a6b7ff71ce2b5d6959fc10 (diff) |
Merge branch 'master' into seqfnsseqfns
-rw-r--r-- | src/clj/clojure/gvec.clj | 26 | ||||
-rw-r--r-- | test/clojure/test_clojure.clj | 1 | ||||
-rw-r--r-- | test/clojure/test_clojure/annotations.clj | 87 | ||||
-rw-r--r-- | test/clojure/test_clojure/vectors.clj | 226 |
4 files changed, 339 insertions, 1 deletions
diff --git a/src/clj/clojure/gvec.clj b/src/clj/clojure/gvec.clj index f9e65ba5..c8a70e47 100644 --- a/src/clj/clojure/gvec.clj +++ b/src/clj/clojure/gvec.clj @@ -245,6 +245,14 @@ (if (clojure.lang.Util/isInteger k) (.assocN this k v) (throw (IllegalArgumentException. "Key must be integer")))) + (containsKey [this k] + (and (clojure.lang.Util/isInteger k) + (<= 0 (int k)) + (< (int k) cnt))) + (entryAt [this k] + (if (.containsKey this k) + (clojure.lang.MapEntry. k (.nth this (int k))) + nil)) clojure.lang.ILookup (valAt [this k not-found] @@ -335,6 +343,24 @@ (aset arr subidx (.doAssoc this (- level (int 5)) (aget arr subidx) i val)) (VecNode. (.edit node) arr)))) + java.lang.Comparable + (compareTo [this o] + (if (identical? this o) + 0 + (let [#^clojure.lang.IPersistentVector v (cast clojure.lang.IPersistentVector o) + vcnt (.count v)] + (cond + (< cnt vcnt) -1 + (> cnt vcnt) 1 + :else + (loop [i (int 0)] + (if (= i cnt) + 0 + (let [comp (clojure.lang.Util/compare (.nth this i) (.nth v i))] + (if (= 0 comp) + (recur (inc i)) + comp)))))))) + java.lang.Iterable (iterator [this] (let [i (java.util.concurrent.atomic.AtomicInteger. 0)] diff --git a/test/clojure/test_clojure.clj b/test/clojure/test_clojure.clj index 1cc4ab94..fa9286d0 100644 --- a/test/clojure/test_clojure.clj +++ b/test/clojure/test_clojure.clj @@ -52,6 +52,7 @@ :genclass :main :vectors + ;:annotations ]) (def test-namespaces diff --git a/test/clojure/test_clojure/annotations.clj b/test/clojure/test_clojure/annotations.clj new file mode 100644 index 00000000..f5b4f4a2 --- /dev/null +++ b/test/clojure/test_clojure/annotations.clj @@ -0,0 +1,87 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +;; Authors: Stuart Halloway, Rich Hickey + +(ns clojure.test-clojure.annotations + (:use clojure.test)) + +(import [java.lang.annotation Annotation Retention RetentionPolicy Target ElementType] + [javax.xml.ws WebServiceRef WebServiceRefs]) +(definterface Foo (foo [])) + +;annotation on type +(deftype #^{Deprecated true + Retention RetentionPolicy/RUNTIME + javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"] + javax.xml.ws.soap.Addressing {:enabled false :required true} + WebServiceRefs [(WebServiceRef {:name "fred" :type String}) + (WebServiceRef {:name "ethel" :mappedName "lucy"})]} + Bar [#^int a + ;on field + #^{:tag int + Deprecated true + Retention RetentionPolicy/RUNTIME + javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"] + javax.xml.ws.soap.Addressing {:enabled false :required true} + WebServiceRefs [(WebServiceRef {:name "fred" :type String}) + (WebServiceRef {:name "ethel" :mappedName "lucy"})]} + b] + ;on method + Foo (#^{Deprecated true + Retention RetentionPolicy/RUNTIME + javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"] + javax.xml.ws.soap.Addressing {:enabled false :required true} + WebServiceRefs [(WebServiceRef {:name "fred" :type String}) + (WebServiceRef {:name "ethel" :mappedName "lucy"})]} + foo [this] 42)) + +(defn annotation->map + "Converts a Java annotation (which conceals data) + into a map (which makes is usable). Not lazy. + Works recursively. Returns non-annotations unscathed." + [#^java.lang.annotation.Annotation o] + (cond + (instance? Annotation o) + (let [type (.annotationType o) + itfs (-> (into #{type} (supers type)) (disj java.lang.annotation.Annotation)) + data-methods (into #{} (mapcat #(.getDeclaredMethods %) itfs))] + (into + {:annotation-type (.annotationType o)} + (map + (fn [m] [(keyword (.getName m)) (annotation->map (.invoke m o nil))]) + data-methods))) + (or (sequential? o) (.isArray (class o))) + (map annotation->map o) + :else o)) + +(def expected-annotations + #{{:annotation-type java.lang.annotation.Retention, :value RetentionPolicy/RUNTIME} + {:annotation-type javax.xml.ws.WebServiceRefs, + :value [{:annotation-type javax.xml.ws.WebServiceRef, :name "fred", :mappedName "", :type java.lang.String, :wsdlLocation "", :value java.lang.Object} + {:annotation-type javax.xml.ws.WebServiceRef, :name "ethel", :mappedName "lucy", :type java.lang.Object, :wsdlLocation "", :value java.lang.Object}]} + {:annotation-type javax.xml.ws.soap.Addressing, :enabled false, :required true} + {:annotation-type javax.annotation.processing.SupportedOptions, :value ["foo" "bar" "baz"]} + {:annotation-type java.lang.Deprecated}}) + +(deftest test-annotations-on-type + (is (= + expected-annotations + (into #{} (map annotation->map (.getAnnotations Bar)))))) + +(deftest test-annotations-on-field + (is (= + expected-annotations + (into #{} (map annotation->map (.getAnnotations (.getField Bar "b"))))))) + +(deftest test-annotations-on-method + (is (= + expected-annotations + (into #{} (map annotation->map (.getAnnotations (.getMethod Bar "foo" nil))))))) + + diff --git a/test/clojure/test_clojure/vectors.clj b/test/clojure/test_clojure/vectors.clj index 35341e85..45eb98ff 100644 --- a/test/clojure/test_clojure/vectors.clj +++ b/test/clojure/test_clojure/vectors.clj @@ -6,7 +6,7 @@ ; the terms of this license. ; You must not remove this notice, or any other, from this software. -; Author: Stuart Halloway +; Author: Stuart Halloway, Daniel Solano Gómez (ns clojure.test-clojure.vectors (:use clojure.test)) @@ -78,3 +78,227 @@ vs vs-32 vs-32 vs vs nil)))) + +(deftest test-vec + (let [ nums (range 1 100) + ; randomly replaces a single item with the given value + rand-replace (fn[val] + (let [r (rand-int 100)] + (concat (take r nums) [val] (drop (inc r) nums)))) + ; all num sequences in map + num-seqs {:standard nums + :empty '() + ; different lengths + :longer (concat nums [100]) + :shorter (drop-last nums) + ; greater by value + :first-greater (concat [100] (next nums)) + :last-greater (concat (drop-last nums) [100]) + :rand-greater-1 (rand-replace 100) + :rand-greater-2 (rand-replace 100) + :rand-greater-3 (rand-replace 100) + ; lesser by value + :first-lesser (concat [0] (next nums)) + :last-lesser (concat (drop-last nums) [0]) + :rand-lesser-1 (rand-replace 0) + :rand-lesser-2 (rand-replace 0) + :rand-lesser-3 (rand-replace 0)} + ; a way to create compare values based on num-seqs + create-vals (fn[base-val] + (zipmap (keys num-seqs) + (map #(into base-val %1) (vals num-seqs)))) + ; Vecs made of int primitives + int-vecs (create-vals (vector-of :int)) + ; Vecs made of long primitives + long-vecs (create-vals (vector-of :long)) + ; standard boxing vectors + regular-vecs (create-vals []) + ; the standard int Vec for comparisons + int-vec (:standard int-vecs)] + (testing "compare" + (testing "identical" + (is (= 0 (compare int-vec int-vec)))) + (testing "equivalent" + (are [x y] (= 0 (compare x y)) + ; standard + int-vec (:standard long-vecs) + (:standard long-vecs) int-vec + int-vec (:standard regular-vecs) + (:standard regular-vecs) int-vec + ; empty + (:empty int-vecs) (:empty long-vecs) + (:empty long-vecs) (:empty int-vecs))) + (testing "lesser" + (are [x] (= -1 (compare int-vec x)) + (:longer int-vecs) + (:longer long-vecs) + (:longer regular-vecs) + (:first-greater int-vecs) + (:first-greater long-vecs) + (:first-greater regular-vecs) + (:last-greater int-vecs) + (:last-greater long-vecs) + (:last-greater regular-vecs) + (:rand-greater-1 int-vecs) + (:rand-greater-1 long-vecs) + (:rand-greater-1 regular-vecs) + (:rand-greater-2 int-vecs) + (:rand-greater-2 long-vecs) + (:rand-greater-2 regular-vecs) + (:rand-greater-3 int-vecs) + (:rand-greater-3 long-vecs) + (:rand-greater-3 regular-vecs)) + (are [x] (= -1 (compare x int-vec)) + nil + (:empty int-vecs) + (:empty long-vecs) + (:empty regular-vecs) + (:shorter int-vecs) + (:shorter long-vecs) + (:shorter regular-vecs) + (:first-lesser int-vecs) + (:first-lesser long-vecs) + (:first-lesser regular-vecs) + (:last-lesser int-vecs) + (:last-lesser long-vecs) + (:last-lesser regular-vecs) + (:rand-lesser-1 int-vecs) + (:rand-lesser-1 long-vecs) + (:rand-lesser-1 regular-vecs) + (:rand-lesser-2 int-vecs) + (:rand-lesser-2 long-vecs) + (:rand-lesser-2 regular-vecs) + (:rand-lesser-3 int-vecs) + (:rand-lesser-3 long-vecs) + (:rand-lesser-3 regular-vecs))) + (testing "greater" + (are [x] (= 1 (compare int-vec x)) + nil + (:empty int-vecs) + (:empty long-vecs) + (:empty regular-vecs) + (:shorter int-vecs) + (:shorter long-vecs) + (:shorter regular-vecs) + (:first-lesser int-vecs) + (:first-lesser long-vecs) + (:first-lesser regular-vecs) + (:last-lesser int-vecs) + (:last-lesser long-vecs) + (:last-lesser regular-vecs) + (:rand-lesser-1 int-vecs) + (:rand-lesser-1 long-vecs) + (:rand-lesser-1 regular-vecs) + (:rand-lesser-2 int-vecs) + (:rand-lesser-2 long-vecs) + (:rand-lesser-2 regular-vecs) + (:rand-lesser-3 int-vecs) + (:rand-lesser-3 long-vecs) + (:rand-lesser-3 regular-vecs)) + (are [x] (= 1 (compare x int-vec)) + (:longer int-vecs) + (:longer long-vecs) + (:longer regular-vecs) + (:first-greater int-vecs) + (:first-greater long-vecs) + (:first-greater regular-vecs) + (:last-greater int-vecs) + (:last-greater long-vecs) + (:last-greater regular-vecs) + (:rand-greater-1 int-vecs) + (:rand-greater-1 long-vecs) + (:rand-greater-1 regular-vecs) + (:rand-greater-2 int-vecs) + (:rand-greater-2 long-vecs) + (:rand-greater-2 regular-vecs) + (:rand-greater-3 int-vecs) + (:rand-greater-3 long-vecs) + (:rand-greater-3 regular-vecs)))) + (testing "Comparable.compareTo" + (testing "incompatible" + (is (thrown? NullPointerException (.compareTo int-vec nil))) + (are [x] (thrown? ClassCastException (.compareTo int-vec x)) + '() + {} + #{} + (sorted-set) + (sorted-map) + nums + 1)) + (testing "identical" + (is (= 0 (.compareTo int-vec int-vec)))) + (testing "equivalent" + (are [x] (= 0 (.compareTo int-vec x)) + (:standard long-vecs) + (:standard regular-vecs))) + (testing "lesser" + (are [x] (= -1 (.compareTo int-vec x)) + (:longer int-vecs) + (:longer long-vecs) + (:longer regular-vecs) + (:first-greater int-vecs) + (:first-greater long-vecs) + (:first-greater regular-vecs) + (:last-greater int-vecs) + (:last-greater long-vecs) + (:last-greater regular-vecs) + (:rand-greater-1 int-vecs) + (:rand-greater-1 long-vecs) + (:rand-greater-1 regular-vecs) + (:rand-greater-2 int-vecs) + (:rand-greater-2 long-vecs) + (:rand-greater-2 regular-vecs) + (:rand-greater-3 int-vecs) + (:rand-greater-3 long-vecs) + (:rand-greater-3 regular-vecs))) + (testing "greater" + (are [x] (= 1 (.compareTo int-vec x)) + (:empty int-vecs) + (:empty long-vecs) + (:empty regular-vecs) + (:shorter int-vecs) + (:shorter long-vecs) + (:shorter regular-vecs) + (:first-lesser int-vecs) + (:first-lesser long-vecs) + (:first-lesser regular-vecs) + (:last-lesser int-vecs) + (:last-lesser long-vecs) + (:last-lesser regular-vecs) + (:rand-lesser-1 int-vecs) + (:rand-lesser-1 long-vecs) + (:rand-lesser-1 regular-vecs) + (:rand-lesser-2 int-vecs) + (:rand-lesser-2 long-vecs) + (:rand-lesser-2 regular-vecs) + (:rand-lesser-3 int-vecs) + (:rand-lesser-3 long-vecs) + (:rand-lesser-3 regular-vecs)))))) + +(deftest test-vec + (let [empty-v (vector-of :long) + v (into empty-v (range 1 6))] + (testing "Associative.containsKey" + (are [x] (.containsKey v x) + 0 1 2 3 4) + (are [x] (not (.containsKey v x)) + -1 -100 nil [] "" #"" #{} 5 100) + (are [x] (not (.containsKey empty-v x)) + 0 1)) + (testing "contains?" + (are [x] (contains? v x) + 0 2 4) + (are [x] (not (contains? v x)) + -1 -100 nil "" 5 100) + (are [x] (not (contains? empty-v x)) + 0 1)) + (testing "Associative.entryAt" + (are [idx val] (= (clojure.lang.MapEntry. idx val) + (.entryAt v idx)) + 0 1 + 2 3 + 4 5) + (are [idx] (nil? (.entryAt v idx)) + -5 -1 5 10 nil "") + (are [idx] (nil? (.entryAt empty-v idx)) + 0 1)))) |