Skip to content

Commit

Permalink
CLJ-895 obey contract for toArray return type
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed Feb 20, 2012
1 parent f5bcf64 commit 7e49f98
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 65 deletions.
14 changes: 1 addition & 13 deletions src/jvm/clojure/lang/APersistentSet.java
Expand Up @@ -140,19 +140,7 @@ public boolean containsAll(Collection c){
}

public Object[] toArray(Object[] a){
if(a.length >= count())
{
ISeq s = seq();
for(int i = 0; s != null; ++i, s = s.next())
{
a[i] = s.first();
}
if(a.length > count())
a[count()] = null;
return a;
}
else
return toArray();
return RT.seqToPassedArray(seq(), a);
}

public int size(){
Expand Down
14 changes: 1 addition & 13 deletions src/jvm/clojure/lang/APersistentVector.java
Expand Up @@ -366,19 +366,7 @@ public boolean containsAll(Collection c){
}

public Object[] toArray(Object[] a){
if(a.length >= count())
{
ISeq s = seq();
for(int i = 0; s != null; ++i, s = s.next())
{
a[i] = s.first();
}
if(a.length > count())
a[count()] = null;
return a;
}
else
return toArray();
return RT.seqToPassedArray(seq(), a);
}

public int size(){
Expand Down
14 changes: 1 addition & 13 deletions src/jvm/clojure/lang/ASeq.java
Expand Up @@ -175,19 +175,7 @@ public boolean containsAll(Collection c){
}

public Object[] toArray(Object[] a){
if(a.length >= count())
{
ISeq s = seq();
for(int i = 0; s != null; ++i, s = s.next())
{
a[i] = s.first();
}
if(a.length > count())
a[count()] = null;
return a;
}
else
return toArray();
return RT.seqToPassedArray(seq(), a);
}

public int size(){
Expand Down
14 changes: 1 addition & 13 deletions src/jvm/clojure/lang/LazySeq.java
Expand Up @@ -174,19 +174,7 @@ public boolean containsAll(Collection c){
}

public Object[] toArray(Object[] a){
if(a.length >= count())
{
ISeq s = seq();
for(int i = 0; s != null; ++i, s = s.next())
{
a[i] = s.first();
}
if(a.length > count())
a[count()] = null;
return a;
}
else
return toArray();
return RT.seqToPassedArray(seq(), a);
}

public int size(){
Expand Down
14 changes: 1 addition & 13 deletions src/jvm/clojure/lang/PersistentQueue.java
Expand Up @@ -204,19 +204,7 @@ public boolean containsAll(Collection c){
}

public Object[] toArray(Object[] a){
if(a.length >= count())
{
ISeq s = seq();
for(int i = 0; s != null; ++i, s = s.next())
{
a[i] = s.first();
}
if(a.length >= count())
a[count()] = null;
return a;
}
else
return toArray();
return RT.seqToPassedArray(seq(), a);
}

public int size(){
Expand Down
15 changes: 15 additions & 0 deletions src/jvm/clojure/lang/RT.java
Expand Up @@ -1570,6 +1570,21 @@ static public Object[] seqToArray(ISeq seq){
return ret;
}

// supports java Collection.toArray(T[])
static public Object[] seqToPassedArray(ISeq seq, Object[] passed){
Object[] dest = passed;
int len = count(seq);
if (len > dest.length) {
dest = (Object[]) Array.newInstance(passed.getClass().getComponentType(), len);
}
for(int i = 0; seq != null; ++i, seq = seq.next())
dest[i] = seq.first();
if (len < passed.length) {
dest[len] = null;
}
return dest;
}

static public Object seqToTypedArray(ISeq seq) {
Class type = (seq != null) ? seq.first().getClass() : Object.class;
return seqToTypedArray(type, seq);
Expand Down
23 changes: 23 additions & 0 deletions test/clojure/test_clojure/java_interop.clj
Expand Up @@ -273,6 +273,29 @@
(to-array [])
(to-array [1 2 3]) ))

(defn queue [& contents]
(apply conj (clojure.lang.PersistentQueue/EMPTY) contents))

(defn array-typed-equals [expected actual]
(and (= (class expected) (class actual))
(java.util.Arrays/equals expected actual)))

(defmacro test-to-passed-array-for [collection-type]
`(deftest ~(symbol (str "test-to-passed-array-for-" collection-type))
(let [string-array# (make-array String 5)
shorter# (~collection-type "1" "2" "3")
same-length# (~collection-type "1" "2" "3" "4" "5")
longer# (~collection-type "1" "2" "3" "4" "5" "6")]
(are [expected actual] (array-typed-equals expected actual)
(into-array String ["1" "2" "3" nil nil]) (.toArray shorter# string-array#)
(into-array String ["1" "2" "3" "4" "5"]) (.toArray same-length# string-array#)
(into-array String ["1" "2" "3" "4" "5" "6"]) (.toArray longer# string-array#)))))


(test-to-passed-array-for vector)
(test-to-passed-array-for list)
(test-to-passed-array-for hash-set)
(test-to-passed-array-for queue)

(deftest test-into-array
; compatible types only
Expand Down

0 comments on commit 7e49f98

Please sign in to comment.