Skip to content

Commit eeea070

Browse files
authored
Merge pull request scala#7582 from rpmcdougall/ticket/11328
incorrect endsWith implementation on Range #11328
2 parents 511c02c + 9c77ddb commit eeea070

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/library/scala/collection/immutable/Range.scala

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,17 @@ private class RangeIterator(
609609
}
610610

611611
override def drop(n: Int): Iterator[Int] = {
612-
val longPos = _next.toLong + step * n
613-
if (step > 0) {
614-
_next = Math.min(lastElement, longPos).toInt
615-
_hasNext = longPos <= lastElement
616-
}
617-
else if (step < 0) {
618-
_next = Math.max(lastElement, longPos).toInt
619-
_hasNext = longPos >= lastElement
612+
if (n > 0) {
613+
val longPos = _next.toLong + step * n
614+
if (step > 0) {
615+
_next = Math.min(lastElement, longPos).toInt
616+
_hasNext = longPos <= lastElement
617+
}
618+
else if (step < 0) {
619+
_next = Math.max(lastElement, longPos).toInt
620+
_hasNext = longPos >= lastElement
621+
}
620622
}
621-
this
623+
this
622624
}
623625
}

test/junit/scala/collection/immutable/RangeTest.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,19 @@ class RangeTest {
8989
assertEquals(20, (20 to 1 by -1).min(Ordering.Int.reverse))
9090
assertEquals(1, (20 to 1 by -1).max(Ordering.Int.reverse))
9191
}
92+
93+
@Test
94+
def testRangeEndsWith(): Unit = {
95+
assertFalse((0 until 0).endsWith(List(-4, -3, -2, -1)))
96+
assertTrue((-8 until -1).endsWith((-8 until -1).takeRight(1)))
97+
assertTrue((0 until 0).endsWith(0 until 0))
98+
}
99+
100+
@Test
101+
def testRangeDrop(): Unit = {
102+
assertTrue((0 until 0).iterator.drop(-4).toList.isEmpty)
103+
assertEquals(4, (1 to 4).iterator.drop(-4).toList.size)
104+
}
105+
106+
92107
}

0 commit comments

Comments
 (0)