Skip to content

Commit

Permalink
Fixed into-array behavior #678
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information
alandipert authored and stuarthalloway committed Dec 17, 2010
1 parent 4e677b3 commit 2879523
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
29 changes: 26 additions & 3 deletions src/jvm/clojure/lang/RT.java
Expand Up @@ -1398,9 +1398,32 @@ static public Object seqToTypedArray(ISeq seq) throws Exception{
}

static public Object seqToTypedArray(Class type, ISeq seq) throws Exception{
Object ret = Array.newInstance(type, length(seq));
for(int i = 0; seq != null; ++i, seq = seq.next())
Array.set(ret, i, seq.first());
Object ret = Array.newInstance(type, length(seq));
if(type == Integer.TYPE){
for(int i = 0; seq != null; ++i, seq=seq.next()){
Array.set(ret, i, intCast(seq.first()));
}
} else if(type == Byte.TYPE) {
for(int i = 0; seq != null; ++i, seq=seq.next()){
Array.set(ret, i, byteCast(seq.first()));
}
} else if(type == Float.TYPE) {
for(int i = 0; seq != null; ++i, seq=seq.next()){
Array.set(ret, i, floatCast(seq.first()));
}
} else if(type == Short.TYPE) {
for(int i = 0; seq != null; ++i, seq=seq.next()){
Array.set(ret, i, shortCast(seq.first()));
}
} else if(type == Character.TYPE) {
for(int i = 0; seq != null; ++i, seq=seq.next()){
Array.set(ret, i, charCast(seq.first()));
}
} else {
for(int i = 0; seq != null; ++i, seq=seq.next()){
Array.set(ret, i, seq.first());
}
}
return ret;
}

Expand Down
26 changes: 18 additions & 8 deletions test/clojure/test_clojure/java_interop.clj
Expand Up @@ -269,7 +269,8 @@
(is (thrown? IllegalArgumentException (into-array [1 "abc" :kw])))
(is (thrown? IllegalArgumentException (into-array [1.2 4])))
(is (thrown? IllegalArgumentException (into-array [(byte 2) (short 3)])))

(is (thrown? IllegalArgumentException (into-array Byte/TYPE [100000000000000])))

; simple case
(let [v [1 2 3 4 5]
a (into-array v)]
Expand All @@ -278,13 +279,22 @@
(vec a) v
(class (first a)) (class (first v)) ))

; given type
#_(let [a (into-array Integer/TYPE [(byte 2) (short 3) (int 4)])]
(are [x] (= x Long)
(class (aget a 0))
(class (aget a 1))
(class (aget a 2)) ))

(is (= \a (aget (into-array Character/TYPE [\a \b \c]) 0)))

(let [types [Integer/TYPE
Byte/TYPE
Float/TYPE
Short/TYPE
Double/TYPE
Long/TYPE]
values [(byte 2) (short 3) (int 4) 5]]
(for [t types]
(let [a (into-array t values)]
(is (== (aget a 0) 2))
(is (== (aget a 1) 3))
(is (== (aget a 2) 4))
(is (== (aget a 3) 5)))))

; different kinds of collections
(are [x] (and (= (alength (into-array x)) (count x))
(= (vec (into-array x)) (vec x))
Expand Down

0 comments on commit 2879523

Please sign in to comment.