From 71cfb57499bda80f5fb0a810f37d0f9c54e1afd3 Mon Sep 17 00:00:00 2001 From: Jean-Remi Desjardins Date: Mon, 5 May 2014 09:34:15 -0400 Subject: [PATCH] SI-8475 Fix off by one in GroupedIterator when Streaming This also affected sliding and grouped since they defer to GroupedIterator --- src/library/scala/collection/Iterator.scala | 4 +++- .../junit/scala/collection/IteratorTest.scala | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/junit/scala/collection/IteratorTest.scala diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 1b496383a398..e321a6adbad8 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -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 } diff --git a/test/junit/scala/collection/IteratorTest.scala b/test/junit/scala/collection/IteratorTest.scala new file mode 100644 index 000000000000..cb7cbb40bc2e --- /dev/null +++ b/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) + } +}