summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Solano Gómez <clojure@sattvik.com>2010-04-25 14:50:15 -0500
committerStuart Halloway <stu@thinkrelevance.com>2010-04-27 15:39:18 -0400
commitdc9429f5cb6a0d881bc0e4ee1ae030543bb72655 (patch)
tree570a1eb18a7fdc33a521c91a9a16567ce15d9fa4
parentf6c74643606461e119998421e39c1972e255d128 (diff)
Add containsKey and entryAt support to Vec, plus tests. Fixes #314.
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r--src/clj/clojure/gvec.clj8
-rw-r--r--test/clojure/test_clojure/vectors.clj28
2 files changed, 36 insertions, 0 deletions
diff --git a/src/clj/clojure/gvec.clj b/src/clj/clojure/gvec.clj
index 7da8e49e..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]
diff --git a/test/clojure/test_clojure/vectors.clj b/test/clojure/test_clojure/vectors.clj
index 00408d22..45eb98ff 100644
--- a/test/clojure/test_clojure/vectors.clj
+++ b/test/clojure/test_clojure/vectors.clj
@@ -274,3 +274,31 @@
(: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))))