From 7c49f32c797503e058b5a462e46022a7b182f19b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Solano=20G=C3=B3mez?= Date: Fri, 25 Feb 2011 16:09:06 -0600 Subject: [PATCH] Add (vector a b c ...) like functionality to vector-of, plus tests --- src/clj/clojure/gvec.clj | 40 +++++++++++++++++++++++--- test/clojure/test_clojure/vectors.clj | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/clj/clojure/gvec.clj b/src/clj/clojure/gvec.clj index fa1ba03..d472ef0 100644 --- a/src/clj/clojure/gvec.clj +++ b/src/clj/clojure/gvec.clj @@ -453,8 +453,38 @@ "Creates a new vector of a single primitive type t, where t is one of :int :long :float :double :byte :short :char or :boolean. The resulting vector complies with the interface of vectors in general, - but stores the values unboxed internally." - {:added "1.2"} - [t] - (let [am ^clojure.core.ArrayManager (ams t)] - (Vec. am 0 5 EMPTY-NODE (.array am 0) nil))) + but stores the values unboxed internally. + + Optionally takes one or more elements to populate the vector." + {:added "1.2" + :arglists '([t] [t & elements])} + ([t] + (let [am ^clojure.core.ArrayManager (ams t)] + (Vec. am 0 5 EMPTY-NODE (.array am 0) nil))) + ([t x1] + (.cons (vector-of t) x1)) + ([t x1 x2] + (-> (vector-of t) + (.cons x1) + (.cons x2))) + ([t x1 x2 x3] + (-> (vector-of t) + (.cons x1) + (.cons x2) + (.cons x3))) + ([t x1 x2 x3 x4] + (-> (vector-of t) + (.cons x1) + (.cons x2) + (.cons x3) + (.cons x4))) + ([t x1 x2 x3 x4 & xn] + (loop [v (-> (vector-of t) + (.cons x1) + (.cons x2) + (.cons x3) + (.cons x4)) + xn xn] + (if xn + (recur (.cons v (first xn)) (next xn)) + v)))) diff --git a/test/clojure/test_clojure/vectors.clj b/test/clojure/test_clojure/vectors.clj index fdf3ba5..600b885 100644 --- a/test/clojure/test_clojure/vectors.clj +++ b/test/clojure/test_clojure/vectors.clj @@ -302,3 +302,52 @@ -5 -1 5 10 nil "") (are [idx] (nil? (.entryAt empty-v idx)) 0 1)))) + +(deftest test-vec-creation + (testing "Plain (vector-of :type)" + (are [x] (and (empty? x) (instance? clojure.core.Vec x)) + (vector-of :boolean) + (vector-of :byte) + (vector-of :short) + (vector-of :int) + (vector-of :long) + (vector-of :float) + (vector-of :double) + (vector-of :char)) + (testing "with invalid type argument" + (are [x] (thrown? NullPointerException x) + (vector-of nil) + (vector-of Float/TYPE) + (vector-of 'int) + (vector-of "")))) + (testing "vector-like (vector-of :type x1 x2 x3 … xn)" + (are [vec gvec] (and (instance? clojure.core.Vec gvec) + (= (into (vector-of :int) vec) gvec)) + [1] (vector-of :int 1) + [1 2] (vector-of :int 1 2) + [1 2 3] (vector-of :int 1 2 3) + [1 2 3 4] (vector-of :int 1 2 3 4) + [1 2 3 4 5] (vector-of :int 1 2 3 4 5) + [1 2 3 4 5 6] (vector-of :int 1 2 3 4 5 6) + [1 2 3] (vector-of :int 1M 2.0 3.1) + [97 98 99] (vector-of :int \a \b \c)) + (testing "with null values" + (are [x] (thrown? NullPointerException x) + (vector-of :int nil) + (vector-of :int 1 nil) + (vector-of :int 1 2 nil) + (vector-of :int 1 2 3 nil) + (vector-of :int 1 2 3 4 nil) + (vector-of :int 1 2 3 4 5 nil) + (vector-of :int 1 2 3 4 5 6 nil))) + (testing "with unsupported values" + (are [x] (thrown? ClassCastException x) + (vector-of :int true) + (vector-of :int 1 2 3 4 5 false) + (vector-of :int {:a 1 :b 2}) + (vector-of :int [1 2 3 4] [5 6]) + (vector-of :int '(1 2 3 4)) + (vector-of :int #{1 2 3 4}) + (vector-of :int (sorted-set 1 2 3 4)) + (vector-of :int 1 2 "3") + (vector-of :int "1" "2" "3"))))) -- 1.7.4.1