Skip to content

Commit

Permalink
Floor for NewVectorIterator.slice limit
Browse files Browse the repository at this point in the history
Fixes scala/bug#12823
Adds a floor of 0 to the limit parameter of NewVectorIterator.slice.
This matches the base behavior of Iterator.slice and Vector.slice.
  • Loading branch information
kapunga committed Aug 28, 2023
1 parent 082a089 commit 7b6cb90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/library/scala/collection/immutable/Vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2389,12 +2389,14 @@ private final class NewVectorIterator[A](v: Vector[A], private[this] var totalLe
}

override def slice(from: Int, until: Int): Iterator[A] = {
val _until =
val _until = until max 0

val n =
if(from > 0) {
drop(from)
until - from
} else until
take(_until)
_until - from
} else _until
take(n)
}

override def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Int = {
Expand Down
6 changes: 6 additions & 0 deletions test/junit/scala/collection/immutable/VectorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,12 @@ class VectorTest {
assertTrue("slice max to min should be empty", Vector(42).slice(Int.MaxValue, Int.MinValue).isEmpty)
}

@Test def `test Vector#iterator slice to MinValue`: Unit = {
assertTrue(Vector(1, 2).iterator.slice(1, Int.MinValue).isEmpty)
assertTrue("slice almost max to min should be empty", Vector(1, 2).iterator.slice(Int.MaxValue - 1, Int.MinValue).isEmpty)
assertTrue("slice max to min should be empty", Vector(1, 2).iterator.slice(Int.MaxValue, Int.MinValue).isEmpty)
}

@Test
def testTail(): Unit = for(size <- verySmallSizes) {
var i = 0
Expand Down

0 comments on commit 7b6cb90

Please sign in to comment.