Skip to content

Commit 3e14902

Browse files
jafingerhutstuarthalloway
authored andcommitted
Make nth on subvec throw exception for negative indices
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
1 parent 92b4fc7 commit 3e14902

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/jvm/clojure/lang/APersistentVector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public SubVector(IPersistentMap meta, IPersistentVector v, int start, int end){
523523
public Iterator iterator(){return ((PersistentVector)v).rangedIterator(start,end);}
524524

525525
public Object nth(int i){
526-
if(start + i >= end)
526+
if((start + i >= end) || (i < 0))
527527
throw new IndexOutOfBoundsException();
528528
return v.nth(start + i);
529529
}

test/clojure/test_clojure/vectors.clj

+8
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,11 @@
370370
(are [r c1] (=vec r (filterv even? c1))
371371
[] [1 3 5]
372372
[2 4] [1 2 3 4 5]))
373+
374+
(deftest test-subvec
375+
(let [v1 (vec (range 100))
376+
v2 (subvec v1 50 57)]
377+
(is (thrown? IndexOutOfBoundsException (v2 -1)))
378+
(is (thrown? IndexOutOfBoundsException (v2 7)))
379+
(is (= (v1 50) (v2 0)))
380+
(is (= (v1 56) (v2 6)))))

0 commit comments

Comments
 (0)