Skip to content

Commit

Permalink
SI-8475 Fix off by one in GroupedIterator when Streaming
Browse files Browse the repository at this point in the history
This also affected sliding and grouped since they defer to GroupedIterator
  • Loading branch information
jedesah authored and retronym committed May 9, 2014
1 parent 251616a commit 71cfb57
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/library/scala/collection/Iterator.scala
Expand Up @@ -926,7 +926,9 @@ trait Iterator[+A] extends TraversableOnce[A] {
private def takeDestructively(size: Int): Seq[A] = {
val buf = new ArrayBuffer[A]
var i = 0
while (self.hasNext && i < size) {
// The order of terms in the following condition is important
// here as self.hasNext could be blocking
while (i < size && self.hasNext) {
buf += self.next
i += 1
}
Expand Down
20 changes: 20 additions & 0 deletions test/junit/scala/collection/IteratorTest.scala
@@ -0,0 +1,20 @@

package scala.collection

import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(classOf[JUnit4])
class IteratorTest {

@Test
def groupedIteratorShouldNotAskForUnneededElement(): Unit = {
var counter = 0
val it = new Iterator[Int] { var i = 0 ; def hasNext = { counter = i; true } ; def next = { i += 1; i } }
val slidingIt = it sliding 2
slidingIt.next
assertEquals("Counter should be one, that means we didn't look further than needed", 1, counter)
}
}

0 comments on commit 71cfb57

Please sign in to comment.