Fix array iterators to adhere to Iterator contract

#KT-17453 Fixed

Change expected exceptions in tests.
This commit is contained in:
Ilya Gorbunov
2017-04-17 23:49:43 +03:00
parent 2380e0bacc
commit 4a5d8534c1
6 changed files with 46 additions and 28 deletions
@@ -16,7 +16,7 @@
package test.collections
import test.collections.behaviors.listBehavior
import test.collections.behaviors.*
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
import kotlin.test.*
import org.junit.Test
@@ -636,6 +636,24 @@ class ArraysTest {
assertArrayNotSameButEquals(booleanArrayOf(true, false, true), booleanArrayOf(true, false, true, true).sliceArray(coll))
}
@Test fun iterators() {
fun <T, E> checkContract(array: T, toList: T.() -> List<E>, iterator: T.() -> Iterator<E>) =
compare(array.toList().iterator(), array.iterator()) {
iteratorBehavior()
}
checkContract(arrayOf("a", "b", "c"), { toList() }, { iterator() })
checkContract(intArrayOf(), { toList() }, { iterator() })
checkContract(intArrayOf(1, 2, 3), { toList() }, { iterator() })
checkContract(shortArrayOf(1, 2, 3), { toList() }, { iterator() })
checkContract(byteArrayOf(1, 2, 3), { toList() }, { iterator() })
checkContract(longArrayOf(1L, 2L, 3L), { toList() }, { iterator() })
checkContract(doubleArrayOf(2.0, 3.0, 9.0), { toList() }, { iterator() })
checkContract(floatArrayOf(2f, 3f, 9f), { toList() }, { iterator() })
checkContract(charArrayOf('a', 'b', 'c'), { toList() }, { iterator() })
checkContract(booleanArrayOf(true, false), { toList() }, { iterator() })
}
@Test fun asIterable() {
val arr1 = intArrayOf(1, 2, 3, 4, 5)
val iter1 = arr1.asIterable()