Skip to content
Browse files

Add containsKey and entryAt support to Vec, plus tests. Fixes #314.

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent f6c7464 commit dc9429f5cb6a0d881bc0e4ee1ae030543bb72655 @sattvik sattvik committed with stuarthalloway Apr 25, 2010
Showing with 36 additions and 0 deletions.
  1. +8 −0 src/clj/clojure/gvec.clj
  2. +28 −0 test/clojure/test_clojure/vectors.clj
View
8 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]
View
28 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))))

0 comments on commit dc9429f

Please sign in to comment.
Something went wrong with that request. Please try again.