Skip to content

Commit

Permalink
Add (vector a b c ...) like functionality to vector-of, plus tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@Orolo-2.local>
  • Loading branch information
sattvik authored and Stuart Halloway committed Mar 20, 2011
1 parent 8c783f1 commit 0245f15
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/clj/clojure/gvec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
50 changes: 50 additions & 0 deletions test/clojure/test_clojure/vectors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")))))

0 comments on commit 0245f15

Please sign in to comment.