Files
kotlin-fork/libraries/stdlib/jvm/test/collections/IndexOverflowJVMTest.kt
T
Ilya Gorbunov c44f62a3d4 Allow dropping and taking a lot of elements in very long sequences
For example allow dropping and taking Int.MAX_VALUE elements from
a sequence that is a result of another Int.MAX_VALUE dropping.
2018-08-30 14:57:03 +03:00

61 lines
1.6 KiB
Kotlin

/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package test.collections
import org.junit.Assume
import org.junit.Test
import kotlin.test.*
class IndexOverflowJVMTest {
@BeforeTest
fun checkIsNotIgnored() {
Assume.assumeTrue(System.getProperty("kotlin.stdlib.test.long.sequences")?.toBoolean() ?: false)
}
companion object {
fun <T> repeatCounted(value: T, count: Long = Int.MAX_VALUE + 1L): Sequence<T> = Sequence {
object : Iterator<T> {
var counter = count
override fun hasNext(): Boolean = counter > 0
override fun next(): T = value.also { counter-- }
}
}
val maxIndexSequence = repeatCounted("k", (Int.MAX_VALUE + 1L) + 1L) // here the last index is one greater than Int.MAX_VALUE
val maxIndexIterable = maxIndexSequence.asIterable()
val longCountSequence = Sequence {
object : Iterator<Long> {
var counter = 0L
override fun hasNext(): Boolean = true
override fun next(): Long = counter++
}
}
}
@Test
fun dropTwiceMaxValue() {
val halfMax = (1 shl 30) + 1
val dropOnce = longCountSequence.drop(halfMax)
val dropTwice = dropOnce.drop(halfMax)
val expectedEnd = halfMax.toLong() * 2
assertEquals(expectedEnd, dropTwice.first())
val dropTake = dropOnce.take(halfMax + 1)
assertEquals(expectedEnd, dropTake.last())
}
}