Skip to content

Commit cb4bf5a

Browse files
committed
merged fixes for scala#1691 and scala#1721 to 2.7.x branch
1 parent 80676bd commit cb4bf5a

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/library/scala/List.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ object List {
5454
* @return the sorted list of all integers in range [start;end).
5555
*/
5656
def range(start: Int, end: Int, step: Int): List[Int] = {
57+
if (step == 0)
58+
throw new IllegalArgumentException("step is zero")
5759
val b = new ListBuffer[Int]
5860
var i = start
5961
while ((step <= 0 || i < end) && (step >= 0 || i > end)) {
@@ -71,7 +73,9 @@ object List {
7173
*
7274
* @param start the start value of the list
7375
* @param end the end value of the list
74-
* @param step the increment function of the list, must be monotonically increasing or decreasing
76+
* @param step the increment function of the list, which given <code>v<sub>n</sub></code>,
77+
* computes <code>v<sub>n+1</sub></code>. Must be monotonically increasing
78+
* or decreasing.
7579
* @return the sorted list of all integers in range [start;end).
7680
*/
7781
def range(start: Int, end: Int, step: Int => Int): List[Int] = {
@@ -81,7 +85,10 @@ object List {
8185
var i = start
8286
while ((!up || i < end) && (!down || i > end)) {
8387
b += i
84-
i += step(i)
88+
val next = step(i)
89+
if (i == next)
90+
throw new IllegalArgumentException("the step function did not make any progress on "+ i)
91+
i = next
8592
}
8693
b.toList
8794
}

src/library/scala/Seq.scala

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,8 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
470470
/** @return true if this sequence end with that sequence
471471
* @see String.endsWith
472472
*/
473-
def endsWith[B](that: Seq[B]): Boolean = {
474-
val length = this.length
475-
val j = that.elements
476-
var i = 0
477-
var result = true
478-
while (result && i < length && j.hasNext)
479-
result = apply(length - i - 1) == j.next
480-
result && !j.hasNext
481-
}
473+
def endsWith[B](that: Seq[B]): Boolean =
474+
drop(length - that.length).sameElements(that)
482475

483476
/**
484477
* Searches for the argument sequence in the receiver object, returning

test/files/run/lists.scala

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import testing.SUnit._
1414
object Test extends TestConsoleMain {
1515
def suite = new TestSuite(
1616
Test1, //count, exists, filter, ..
17-
Test2 //#468
17+
Test2, //#468
18+
Test3, //#1691
19+
Test4 //#1721
1820
)
1921
}
2022

@@ -105,3 +107,36 @@ object Test2 extends TestCase("t0468") with Assert {
105107
assertEquals("check_++", 14, n2 + n3 + n4)
106108
}
107109
}
110+
111+
object Test3 extends TestCase("t1691") with Assert {
112+
override def enableStackTrace = false
113+
override def runTest {
114+
try {
115+
List.range(1, 10, 0)
116+
} catch {
117+
case e: IllegalArgumentException => ()
118+
case _ => throw new Error("List.range(1, 10, 0)")
119+
}
120+
try {
121+
List.range(1, 10, x => 4)
122+
} catch {
123+
case e: IllegalArgumentException => ()
124+
case _ => throw new Error("List.range(1, 10, x => 4)")
125+
}
126+
assertEquals(List.range(10, 0, x => x - 2),
127+
List(10, 8, 6, 4, 2))
128+
}
129+
}
130+
131+
object Test4 extends TestCase("t1721") with Assert {
132+
override def enableStackTrace = false
133+
override def runTest {
134+
assertTrue(List(1,2,3).endsWith(List(2,3)))
135+
assertFalse(List(1,2,3).endsWith(List(1,3)))
136+
assertTrue(List(1,2,3).endsWith(List()))
137+
assertFalse(List(1,2,3).endsWith(List(0,1,2,3)))
138+
assertTrue(List(1,2,3).endsWith(List(1,2,3)))
139+
assertFalse(List().endsWith(List(1,2,3)))
140+
assertTrue(List().endsWith(List()))
141+
}
142+
}

0 commit comments

Comments
 (0)