diff options
author | Daniel Solano Gómez <clojure@sattvik.com> | 2011-02-25 16:09:06 -0600 |
---|---|---|
committer | Stuart Halloway <stu@Orolo-2.local> | 2011-03-20 09:34:28 -0400 |
commit | 0245f15c9c7bd2d043f0f6e59fff0a692d7466b1 (patch) | |
tree | a637d0aba0625b0228c7b6e9d87f11193417d950 | |
parent | 8c783f1c1a6fb0cf9c50e4737596e92bceb8012b (diff) |
Add (vector a b c ...) like functionality to vector-of, plus tests
Signed-off-by: Stuart Halloway <stu@Orolo-2.local>
-rw-r--r-- | src/clj/clojure/gvec.clj | 45 | ||||
-rw-r--r-- | test/clojure/test_clojure/vectors.clj | 50 |
2 files changed, 90 insertions, 5 deletions
diff --git a/src/clj/clojure/gvec.clj b/src/clj/clojure/gvec.clj index fa1ba03e..6aabd1eb 100644 --- a/src/clj/clojure/gvec.clj +++ b/src/clj/clojure/gvec.clj @@ -453,8 +453,43 @@ "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] + (let [am ^clojure.core.ArrayManager (ams t) + arr (.array am 1)] + (.aset am arr 0 x1) + (Vec. am 1 5 EMPTY-NODE arr nil))) + ([t x1 x2] + (let [am ^clojure.core.ArrayManager (ams t) + arr (.array am 2)] + (.aset am arr 0 x1) + (.aset am arr 1 x2) + (Vec. am 2 5 EMPTY-NODE arr nil))) + ([t x1 x2 x3] + (let [am ^clojure.core.ArrayManager (ams t) + arr (.array am 3)] + (.aset am arr 0 x1) + (.aset am arr 1 x2) + (.aset am arr 2 x3) + (Vec. am 3 5 EMPTY-NODE arr nil))) + ([t x1 x2 x3 x4] + (let [am ^clojure.core.ArrayManager (ams t) + arr (.array am 4)] + (.aset am arr 0 x1) + (.aset am arr 1 x2) + (.aset am arr 2 x3) + (.aset am arr 3 x4) + (Vec. am 4 5 EMPTY-NODE arr nil))) + ([t x1 x2 x3 x4 & xn] + (loop [v (vector-of t x1 x2 x3 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 fdf3ba54..2c6788c1 100644 --- a/test/clojure/test_clojure/vectors.clj +++ b/test/clojure/test_clojure/vectors.clj @@ -302,3 +302,53 @@ -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) + (apply vector (range 1000)) (apply vector-of :int (range 1000)) + [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"))))) |