Skip to content

Commit

Permalink
Merge pull request #10331 from som-snytt/issue/12739-vector-slice
Browse files Browse the repository at this point in the history
Improve check for empty vector slice
  • Loading branch information
som-snytt committed Mar 15, 2023
2 parents 15538c7 + eaac91e commit 34e01b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/library/scala/collection/immutable/Vector.scala
Expand Up @@ -317,9 +317,8 @@ private sealed abstract class VectorImpl[+A](_prefix1: Arr1) extends Vector[A](_
override final def slice(from: Int, until: Int): Vector[A] = {
val lo = mmax(from, 0)
val hi = mmin(until, length)
val newlen = hi - lo
if(newlen == length) this
else if(hi <= lo) Vector0
if (hi <= lo) Vector0
else if (hi - lo == length) this
else slice0(lo, hi)
}
}
Expand Down
15 changes: 8 additions & 7 deletions test/junit/scala/collection/immutable/VectorTest.scala
Expand Up @@ -163,7 +163,7 @@ class VectorTest {
}

@Test
def testWierdAlignments1(): Unit = {
def testWeirdAlignments1(): Unit = {
val v3 = Vector.tabulate(2042)(i => (i - 42).toString).drop(42).asInstanceOf[Vector3[AnyRef]]
for (i <- Seq(0, 1, 5, 41, 42, 43, 123, 949, 950, 982, 1024, 1999, 2000)) {
val res = new VectorBuilder[AnyRef]
Expand All @@ -176,7 +176,7 @@ class VectorTest {
}

@Test
def testWierdAlignments2(): Unit = {
def testWeirdAlignments2(): Unit = {
val v3 = Vector.tabulate(2042)(i => (i - 42).toString).drop(42).asInstanceOf[Vector3[AnyRef]]
val v2 = Vector.tabulate(765)(i => (i - 123).toString).drop(123).asInstanceOf[Vector2[AnyRef]]
for (i <- Seq(-1234, -42, -1, 0, 1, 5, 41, 42, 43, 123, 949, 950, 982, 1024, 1999, 2000)) {
Expand All @@ -190,7 +190,7 @@ class VectorTest {
}

@Test
def testWierdAlignments3(): Unit = for (n <- allSizes; m <- verySmallSizes) {
def testWeirdAlignments3(): Unit = for (n <- allSizes; m <- verySmallSizes) {
val vPretend =
if (smallSizes.contains(n))
Vector.tabulate(n + 1337)(i => (i - 1337).toString).drop(1337)
Expand All @@ -212,7 +212,7 @@ class VectorTest {
}

@Test
def testWierdAlignments4(): Unit = {
def testWeirdAlignments4(): Unit = {
var lengths = Set[Int]()
for (
n <- allSizes.init :+ (allSizes.last - WIDTH5 - WIDTH3 + 41); // we need WIDTH5 for prefix, add 1+WIDTH3 and get 42 in suffix free
Expand Down Expand Up @@ -579,9 +579,10 @@ class VectorTest {
}
}

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

@Test
Expand Down

0 comments on commit 34e01b4

Please sign in to comment.