Skip to content

Commit 8ace59a

Browse files
authored
Merge pull request scala#7004 from joshlemer/issue/11049-2.13
[11049] Fixes scala/bug#11049
2 parents 477998f + 094a52f commit 8ace59a

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

src/library/scala/collection/IndexedSeqView.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ object IndexedSeqView {
2626
private[this] var current = 0
2727
override def knownSize: Int = self.size - current
2828
def hasNext = current < self.size
29-
def next(): A = {
30-
val r = self.apply(current)
31-
current += 1
32-
r
33-
}
29+
def next(): A =
30+
if (hasNext) {
31+
val r = self.apply(current)
32+
current += 1
33+
r
34+
} else Iterator.empty.next()
3435
}
3536

3637
/** An `IndexedSeqOps` whose collection type and collection type constructor are unknown */

src/library/scala/collection/mutable/ArrayBuffer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ object ArrayBuffer extends StrictOptimizedSeqFactory[ArrayBuffer] {
209209

210210
final class ArrayBufferView[A](val array: Array[AnyRef], val length: Int) extends AbstractIndexedSeqView[A] {
211211
@throws[ArrayIndexOutOfBoundsException]
212-
def apply(n: Int) = array(n).asInstanceOf[A]
212+
def apply(n: Int) = if (n < length) array(n).asInstanceOf[A] else throw new IndexOutOfBoundsException(n.toString)
213213
override protected[this] def className = "ArrayBufferView"
214214
}
215215

src/library/scala/collection/mutable/PriorityQueue.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,7 @@ sealed class PriorityQueue[A](implicit val ord: Ordering[A])
247247
*
248248
* @return an iterator over all the elements.
249249
*/
250-
override def iterator: Iterator[A] = new AbstractIterator[A] {
251-
private[this] var i = 1
252-
def hasNext: Boolean = i < resarr.p_size0
253-
def next(): A = {
254-
val n = resarr.p_array(i)
255-
i += 1
256-
toA(n)
257-
}
258-
}
250+
override def iterator: Iterator[A] = resarr.iterator.drop(1)
259251

260252
/** Returns the reverse of this priority queue. The new priority queue has
261253
* the same elements as the original, but the opposite ordering.

test/junit/scala/collection/mutable/ArrayBufferTest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import org.junit.runners.JUnit4
55
import org.junit.Test
66
import org.junit.Assert.assertEquals
77

8+
89
import scala.tools.testing.AssertUtil
10+
import scala.tools.testing.AssertUtil.assertThrows
911

1012
/* Test for scala/bug#9043 */
1113
@RunWith(classOf[JUnit4])
@@ -307,4 +309,10 @@ class ArrayBufferTest {
307309

308310
assertEquals(builder.result(), "4,3,2,1")
309311
}
312+
313+
@Test
314+
def emptyIteratorDropOneMustBeEmpty: Unit = {
315+
assertThrows[NoSuchElementException](new ArrayBuffer[Int].iterator.drop(1).next())
316+
}
317+
310318
}

test/junit/scala/collection/mutable/PriorityQueueTest.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package scala.collection.mutable
33
import org.junit.runner.RunWith
44
import org.junit.runners.JUnit4
55
import org.junit.Test
6+
67
import scala.collection.mutable
7-
import java.io.{ObjectInputStream, ByteArrayInputStream, ByteArrayOutputStream, ObjectOutputStream}
8+
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}
9+
10+
import scala.tools.testing.AssertUtil.assertThrows
811

912
@RunWith(classOf[JUnit4])
1013
/* Test for scala/bug#7568 */
@@ -42,4 +45,9 @@ class PriorityQueueTest {
4245
//correct sequencing is also tested here:
4346
assert(deserializedPriorityQueue.dequeueAll == elements.sorted.reverse)
4447
}
48+
@Test
49+
def lastOfEmptyThrowsException(): Unit = {
50+
assert(List(1,2,3,4,5).contains(collection.mutable.PriorityQueue[Int](1,2,3,4,5).last))
51+
assertThrows[NoSuchElementException](collection.mutable.PriorityQueue[Int]().last)
52+
}
4553
}

0 commit comments

Comments
 (0)