Fix progression iterators to respect the Iterator contract.

#KT-16923 Fixed
This commit is contained in:
Ilya Gorbunov
2017-03-18 02:52:51 +03:00
parent e5a28311bc
commit 87c055cc61
3 changed files with 14 additions and 4 deletions
@@ -23,15 +23,16 @@ package kotlin.ranges
* @property step the number by which the value is incremented on each step.
*/
internal class CharProgressionIterator(first: Char, last: Char, val step: Int) : CharIterator() {
private var next = first.toInt()
private val finalElement = last.toInt()
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first.toInt() else finalElement
override fun hasNext(): Boolean = hasNext
override fun nextChar(): Char {
val value = next
if (value == finalElement) {
if (!hasNext) throw kotlin.NoSuchElementException()
hasNext = false
}
else {
@@ -46,15 +47,16 @@ internal class CharProgressionIterator(first: Char, last: Char, val step: Int) :
* @property step the number by which the value is incremented on each step.
*/
internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : IntIterator() {
private var next = first
private val finalElement = last
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first else finalElement
override fun hasNext(): Boolean = hasNext
override fun nextInt(): Int {
val value = next
if (value == finalElement) {
if (!hasNext) throw kotlin.NoSuchElementException()
hasNext = false
}
else {
@@ -69,15 +71,16 @@ internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : In
* @property step the number by which the value is incremented on each step.
*/
internal class LongProgressionIterator(first: Long, last: Long, val step: Long) : LongIterator() {
private var next = first
private val finalElement = last
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first else finalElement
override fun hasNext(): Boolean = hasNext
override fun nextLong(): Long {
val value = next
if (value == finalElement) {
if (!hasNext) throw kotlin.NoSuchElementException()
hasNext = false
}
else {
@@ -36,15 +36,16 @@ fun integerProgressionIterator(kind: ProgressionKind): String {
* @property step the number by which the value is incremented on each step.
*/
internal class ${t}ProgressionIterator(first: $t, last: $t, val step: $incrementType) : ${t}Iterator() {
private var next = first$toInt
private val finalElement = last$toInt
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
private var next = if (hasNext) first$toInt else finalElement
override fun hasNext(): Boolean = hasNext
override fun next$t(): $t {
val value = next
if (value == finalElement) {
if (!hasNext) throw kotlin.NoSuchElementException()
hasNext = false
}
else {
@@ -1,6 +1,8 @@
package test.ranges
import org.junit.Test
import test.collections.behaviors.iteratorBehavior
import test.collections.compare
import kotlin.test.*
public open class RangeIterationTestBase {
@@ -41,6 +43,10 @@ public open class RangeIterationTestBase {
assertTrue(sequence.none())
else
assertEquals(expectedElements, sequence.toList())
compare(expectedElements.iterator(), sequence.iterator()) {
iteratorBehavior()
}
}
}