mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-02 15:51:50 +00:00
Compare commits
2 Commits
rr/selezne
...
rr/stdlib/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0dce36c958 | ||
|
|
eb6a51d5a1 |
@@ -151,24 +151,6 @@ class OrderingTest {
|
||||
assertEquals(v1, items[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf() {
|
||||
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE))
|
||||
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
|
||||
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE, 0, -1))
|
||||
|
||||
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE))
|
||||
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
|
||||
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE, 0, -1))
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
|
||||
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
assertEquals(0.0, maxOf(0.0, -0.0))
|
||||
assertEquals(0.0, maxOf(-0.0, 0.0))
|
||||
assertEquals(0.0, maxOf(-0.0, 0.0, 0.0, -0.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOfWith() {
|
||||
assertEquals(v1, maxOf(v1, v2, compareBy { it.name }))
|
||||
@@ -179,24 +161,6 @@ class OrderingTest {
|
||||
assertEquals(v4, maxOf(v4, v1, v2, v3, comparator = compareBy { it.rating }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf() {
|
||||
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE))
|
||||
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
|
||||
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE, 0, -1))
|
||||
|
||||
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE))
|
||||
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
|
||||
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE, 0, -1))
|
||||
|
||||
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
|
||||
assertEquals(Double.MIN_VALUE, minOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
assertEquals(-0.0, minOf(0.0, -0.0))
|
||||
assertEquals(-0.0, minOf(-0.0, 0.0))
|
||||
assertEquals(-0.0, minOf(-0.0, 0.0, 0.0, -0.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOfWith() {
|
||||
assertEquals(v2, minOf(v1, v2, compareBy { it.name }))
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
package test.collections
|
||||
|
||||
import test.assertStaticTypeIs
|
||||
import test.assertTypeEquals
|
||||
import test.collections.behaviors.*
|
||||
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
|
||||
import test.text.isAsciiLetter
|
||||
@@ -389,66 +388,6 @@ class ArraysTest {
|
||||
expect('b', { charArrayOf('a', 'b').maxOrNull() })
|
||||
}
|
||||
|
||||
@Test fun minWithOrNull() {
|
||||
assertEquals(null, arrayOf<Int>().minWithOrNull(naturalOrder()))
|
||||
assertEquals("a", arrayOf("a", "B").minWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test fun minWithOrNullInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().minWithOrNull(naturalOrder()) })
|
||||
expect(1, { intArrayOf(1).minWithOrNull(naturalOrder()) })
|
||||
expect(4, { intArrayOf(2, 3, 4).minWithOrNull(compareBy { it % 4 }) })
|
||||
}
|
||||
|
||||
@Test fun maxWithOrNull() {
|
||||
assertEquals(null, arrayOf<Int>().maxWithOrNull(naturalOrder()))
|
||||
assertEquals("B", arrayOf("a", "B").maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test fun maxWithOrNullInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().maxWithOrNull(naturalOrder()) })
|
||||
expect(1, { intArrayOf(1).maxWithOrNull(naturalOrder()) })
|
||||
expect(-4, { intArrayOf(2, 3, -4).maxWithOrNull(compareBy { it * it }) })
|
||||
}
|
||||
|
||||
@Test fun minByOrNull() {
|
||||
expect(null, { arrayOf<Int>().minByOrNull { it } })
|
||||
expect(1, { arrayOf(1).minByOrNull { it } })
|
||||
expect(3, { arrayOf(2, 3).minByOrNull { -it } })
|
||||
expect('a', { arrayOf('a', 'b').minByOrNull { "x$it" } })
|
||||
expect("b", { arrayOf("b", "abc").minByOrNull { it.length } })
|
||||
}
|
||||
|
||||
@Test fun minByOrNullInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().minByOrNull { it } })
|
||||
expect(1, { intArrayOf(1).minByOrNull { it } })
|
||||
expect(3, { intArrayOf(2, 3).minByOrNull { -it } })
|
||||
expect(2000000000000, { longArrayOf(3000000000000, 2000000000000).minByOrNull { it + 1 } })
|
||||
expect(1, { byteArrayOf(1, 3, 2).minByOrNull { it * it } })
|
||||
expect(3, { shortArrayOf(3, 2).minByOrNull { "a" } })
|
||||
expect(2.0F, { floatArrayOf(3.0F, 2.0F).minByOrNull { it.toString() } })
|
||||
expect(2.0, { doubleArrayOf(2.0, 3.0).minByOrNull { it * it } })
|
||||
}
|
||||
|
||||
@Test fun maxByOrNull() {
|
||||
expect(null, { arrayOf<Int>().maxByOrNull { it } })
|
||||
expect(1, { arrayOf(1).maxByOrNull { it } })
|
||||
expect(2, { arrayOf(2, 3).maxByOrNull { -it } })
|
||||
expect('b', { arrayOf('a', 'b').maxByOrNull { "x$it" } })
|
||||
expect("abc", { arrayOf("b", "abc").maxByOrNull { it.length } })
|
||||
}
|
||||
|
||||
@Test fun maxByOrNullInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().maxByOrNull { it } })
|
||||
expect(1, { intArrayOf(1).maxByOrNull { it } })
|
||||
expect(2, { intArrayOf(2, 3).maxByOrNull { -it } })
|
||||
expect(3000000000000, { longArrayOf(3000000000000, 2000000000000).maxByOrNull { it + 1 } })
|
||||
expect(3, { byteArrayOf(1, 3, 2).maxByOrNull { it * it } })
|
||||
expect(3, { shortArrayOf(3, 2).maxByOrNull { "a" } })
|
||||
expect(3.0F, { floatArrayOf(3.0F, 2.0F).maxByOrNull { it.toString() } })
|
||||
expect(3.0, { doubleArrayOf(2.0, 3.0).maxByOrNull { it * it } })
|
||||
}
|
||||
|
||||
@Test fun minIndex() {
|
||||
val a = intArrayOf(1, 7, 9, -42, 54, 93)
|
||||
expect(3, { a.indices.minByOrNull { a[it] } })
|
||||
@@ -502,66 +441,6 @@ class ArraysTest {
|
||||
// for each arr with size > 0 arr.average() = arr.sum().toDouble() / arr.size()
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
@Test fun indexOfInPrimitiveArrays() {
|
||||
expect(-1) { byteArrayOf(1, 2, 3).indexOf(0) }
|
||||
expect(0) { byteArrayOf(1, 2, 3).indexOf(1) }
|
||||
expect(1) { byteArrayOf(1, 2, 3).indexOf(2) }
|
||||
expect(2) { byteArrayOf(1, 2, 3).indexOf(3) }
|
||||
|
||||
expect(-1) { shortArrayOf(1, 2, 3).indexOf(0) }
|
||||
expect(0) { shortArrayOf(1, 2, 3).indexOf(1) }
|
||||
expect(1) { shortArrayOf(1, 2, 3).indexOf(2) }
|
||||
expect(2) { shortArrayOf(1, 2, 3).indexOf(3) }
|
||||
|
||||
expect(-1) { intArrayOf(1, 2, 3).indexOf(0) }
|
||||
expect(0) { intArrayOf(1, 2, 3).indexOf(1) }
|
||||
expect(1) { intArrayOf(1, 2, 3).indexOf(2) }
|
||||
expect(2) { intArrayOf(1, 2, 3).indexOf(3) }
|
||||
|
||||
expect(-1) { longArrayOf(1, 2, 3).indexOf(0) }
|
||||
expect(0) { longArrayOf(1, 2, 3).indexOf(1) }
|
||||
expect(1) { longArrayOf(1, 2, 3).indexOf(2) }
|
||||
expect(2) { longArrayOf(1, 2, 3).indexOf(3) }
|
||||
|
||||
expect(-1) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(0f) }
|
||||
expect(0) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(1.0f) }
|
||||
expect(1) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(2.0f) }
|
||||
expect(2) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(3.0f) }
|
||||
|
||||
expect(-1) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(0.0) }
|
||||
expect(0) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(1.0) }
|
||||
expect(1) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(2.0) }
|
||||
expect(2) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(3.0) }
|
||||
|
||||
expect(-1) { charArrayOf('a', 'b', 'c').indexOf('z') }
|
||||
expect(0) { charArrayOf('a', 'b', 'c').indexOf('a') }
|
||||
expect(1) { charArrayOf('a', 'b', 'c').indexOf('b') }
|
||||
expect(2) { charArrayOf('a', 'b', 'c').indexOf('c') }
|
||||
|
||||
expect(0) { booleanArrayOf(true, false).indexOf(true) }
|
||||
expect(1) { booleanArrayOf(true, false).indexOf(false) }
|
||||
expect(-1) { booleanArrayOf(true).indexOf(false) }
|
||||
}
|
||||
|
||||
@Test fun indexOf() {
|
||||
expect(-1) { arrayOf("cat", "dog", "bird").indexOf("mouse") }
|
||||
expect(0) { arrayOf("cat", "dog", "bird").indexOf("cat") }
|
||||
expect(1) { arrayOf("cat", "dog", "bird").indexOf("dog") }
|
||||
expect(2) { arrayOf("cat", "dog", "bird").indexOf("bird") }
|
||||
expect(0) { arrayOf(null, "dog", null).indexOf(null as String?)}
|
||||
|
||||
expect(-1) { arrayOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
|
||||
expect(0) { arrayOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
|
||||
expect(1) { arrayOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
|
||||
expect(2) { arrayOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
|
||||
|
||||
expect(-1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
|
||||
expect(0) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
|
||||
expect(1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
|
||||
expect(2) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
|
||||
}
|
||||
|
||||
@Test fun lastIndexOf() {
|
||||
expect(-1) { arrayOf("cat", "dog", "bird").lastIndexOf("mouse") }
|
||||
expect(0) { arrayOf("cat", "dog", "bird").lastIndexOf("cat") }
|
||||
@@ -927,84 +806,6 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test fun copyRangeInto() {
|
||||
fun <T> doTest(
|
||||
copyInto: T.(T, Int, Int, Int) -> T,
|
||||
assertTEquals: (T, T, String) -> Unit,
|
||||
toStringT: T.() -> String,
|
||||
dest: T, newValues: T,
|
||||
result1: T, result2: T, result3: T
|
||||
) {
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
assertTypeEquals(result1, dest)
|
||||
assertTEquals(result1, dest, "Copying from newValues: ${result1.toStringT()}, ${dest.toStringT()}")
|
||||
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
assertTEquals(result2, dest, "Overlapping backward copy: ${result2.toStringT()}, ${dest.toStringT()}")
|
||||
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
assertTEquals(result3, dest, "Overlapping forward copy: ${result2.toStringT()}, ${dest.toStringT()}")
|
||||
|
||||
for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0, 0 to -1)) {
|
||||
val bounds = "start: $start, end: $end"
|
||||
val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) }
|
||||
assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: $ex")
|
||||
}
|
||||
for (destIndex in listOf(-1, 2, 4)) {
|
||||
assertFailsWith<IndexOutOfBoundsException>("index: $destIndex") { newValues.copyInto(dest, destIndex, 0, 2) }
|
||||
}
|
||||
}
|
||||
|
||||
doTest(
|
||||
Array<String>::copyInto, { e, a, msg -> assertArrayNotSameButEquals(e, a, msg) }, Array<*>::contentToString,
|
||||
arrayOf("a", "b", "c"), arrayOf("e", "f", "g"),
|
||||
arrayOf("f", "g", "c"), arrayOf("g", "c", "c"), arrayOf("g", "g", "c")
|
||||
)
|
||||
|
||||
doTest(
|
||||
IntArray::copyInto, ::assertArrayNotSameButEquals, IntArray::contentToString,
|
||||
intArrayOf(1, 2, 3), intArrayOf(4, 5, 6),
|
||||
intArrayOf(5, 6, 3), intArrayOf(6, 3, 3), intArrayOf(6, 6, 3)
|
||||
)
|
||||
|
||||
doTest(
|
||||
LongArray::copyInto, ::assertArrayNotSameButEquals, LongArray::contentToString,
|
||||
longArrayOf(1, 2, 3), longArrayOf(4, 5, 6),
|
||||
longArrayOf(5, 6, 3), longArrayOf(6, 3, 3), longArrayOf(6, 6, 3)
|
||||
)
|
||||
|
||||
doTest(
|
||||
ByteArray::copyInto, ::assertArrayNotSameButEquals, ByteArray::contentToString,
|
||||
byteArrayOf(1, 2, 3), byteArrayOf(4, 5, 6),
|
||||
byteArrayOf(5, 6, 3), byteArrayOf(6, 3, 3), byteArrayOf(6, 6, 3)
|
||||
)
|
||||
|
||||
doTest(
|
||||
CharArray::copyInto, ::assertArrayNotSameButEquals, CharArray::contentToString,
|
||||
charArrayOf('a', 'b', 'c'), charArrayOf('e', 'f', 'g'),
|
||||
charArrayOf('f', 'g', 'c'), charArrayOf('g', 'c', 'c'), charArrayOf('g', 'g', 'c')
|
||||
)
|
||||
|
||||
doTest(
|
||||
UIntArray::copyInto, { e, a, msg -> assertTrue(e contentEquals a, msg) }, UIntArray::contentToString,
|
||||
uintArrayOf(1, 2, 3), uintArrayOf(4, 5, 6),
|
||||
uintArrayOf(5, 6, 3), uintArrayOf(6, 3, 3), uintArrayOf(6, 6, 3)
|
||||
)
|
||||
|
||||
doTest(
|
||||
ULongArray::copyInto, { e, a, msg -> assertTrue(e contentEquals a, msg) }, ULongArray::contentToString,
|
||||
ulongArrayOf(1, 2, 3), ulongArrayOf(4, 5, 6),
|
||||
ulongArrayOf(5, 6, 3), ulongArrayOf(6, 3, 3), ulongArrayOf(6, 6, 3)
|
||||
)
|
||||
|
||||
doTest(
|
||||
UByteArray::copyInto, { e, a, msg -> assertTrue(e contentEquals a, msg) }, UByteArray::contentToString,
|
||||
ubyteArrayOf(1, 2, 3), ubyteArrayOf(4, 5, 6),
|
||||
ubyteArrayOf(5, 6, 3), ubyteArrayOf(6, 3, 3), ubyteArrayOf(6, 6, 3)
|
||||
)
|
||||
}
|
||||
|
||||
@Test fun copyRangeIntoVarianceTest() {
|
||||
val sourceArr: Array<out Int> = arrayOf(1, 2, 3)
|
||||
val targetAnyArr: Array<Any?> = arrayOfNulls<Any?>(3)
|
||||
@@ -1449,101 +1250,6 @@ class ArraysTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test fun reverseInPlace() {
|
||||
|
||||
fun <TArray, T> doTest(build: Iterable<Int>.() -> TArray, reverse: TArray.() -> Unit, snapshot: TArray.() -> List<T>) {
|
||||
val arrays = (0..4).map { n -> (1..n).build() }
|
||||
for (array in arrays) {
|
||||
val original = array.snapshot()
|
||||
array.reverse()
|
||||
val reversed = array.snapshot()
|
||||
assertEquals(original.asReversed(), reversed)
|
||||
}
|
||||
}
|
||||
|
||||
doTest(build = { map {it}.toIntArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toLong()}.toLongArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toByte()}.toByteArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toShort()}.toShortArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toFloat()}.toFloatArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toDouble()}.toDoubleArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {'a' + it}.toCharArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it % 2 == 0}.toBooleanArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toString()}.toTypedArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toString()}.toTypedArray() as Array<out String> }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toUInt()}.toUIntArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toULong()}.toULongArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toUByte()}.toUByteArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toUShort()}.toUShortArray() }, reverse = { reverse() }, snapshot = { toList() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverseRangeInPlace() {
|
||||
|
||||
fun <TArray, T> doTest(
|
||||
build: Iterable<Int>.() -> TArray,
|
||||
reverse: TArray.(fromIndex: Int, toIndex: Int) -> Unit,
|
||||
snapshot: TArray.() -> List<T>
|
||||
) {
|
||||
val arrays = (0..7).map { n -> n to (0 until n).build() }
|
||||
for ((size, array) in arrays) {
|
||||
for (fromIndex in 0 until size) {
|
||||
for (toIndex in fromIndex..size) {
|
||||
val original = array.snapshot().toMutableList()
|
||||
array.reverse(fromIndex, toIndex)
|
||||
val reversed = array.snapshot()
|
||||
assertEquals(original.apply { subList(fromIndex, toIndex).reverse() }, reversed)
|
||||
}
|
||||
}
|
||||
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(-1, size) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(0, size + 1) }
|
||||
assertFailsWith<IllegalArgumentException> { array.reverse(0, -1) }
|
||||
}
|
||||
}
|
||||
|
||||
doTest(build = { map {it.toString()}.toTypedArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toString()}.toTypedArray() as Array<out String> }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
|
||||
doTest(build = { map {it}.toIntArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toLong()}.toLongArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toByte()}.toByteArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toShort()}.toShortArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toFloat()}.toFloatArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toDouble()}.toDoubleArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {'a' + it}.toCharArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it % 2 == 0}.toBooleanArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toUInt()}.toUIntArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toULong()}.toULongArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toUByte()}.toUByteArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
doTest(build = { map {it.toUShort()}.toUShortArray() }, reverse = { from, to -> reverse(from, to) }, snapshot = { toList() })
|
||||
}
|
||||
|
||||
|
||||
@Test fun reversed() {
|
||||
expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reversed() }
|
||||
expect(listOf<Byte>(3, 2, 1)) { byteArrayOf(1, 2, 3).reversed() }
|
||||
expect(listOf<Short>(3, 2, 1)) { shortArrayOf(1, 2, 3).reversed() }
|
||||
expect(listOf<Long>(3, 2, 1)) { longArrayOf(1, 2, 3).reversed() }
|
||||
expect(listOf(3F, 2F, 1F)) { floatArrayOf(1F, 2F, 3F).reversed() }
|
||||
expect(listOf(3.0, 2.0, 1.0)) { doubleArrayOf(1.0, 2.0, 3.0).reversed() }
|
||||
expect(listOf('3', '2', '1')) { charArrayOf('1', '2', '3').reversed() }
|
||||
expect(listOf(false, false, true)) { booleanArrayOf(true, false, false).reversed() }
|
||||
expect(listOf("3", "2", "1")) { arrayOf("1", "2", "3").reversed() }
|
||||
}
|
||||
|
||||
@Test fun reversedArray() {
|
||||
assertArrayNotSameButEquals(intArrayOf(3, 2, 1), intArrayOf(1, 2, 3).reversedArray())
|
||||
assertArrayNotSameButEquals(byteArrayOf(3, 2, 1), byteArrayOf(1, 2, 3).reversedArray())
|
||||
assertArrayNotSameButEquals(shortArrayOf(3, 2, 1), shortArrayOf(1, 2, 3).reversedArray())
|
||||
assertArrayNotSameButEquals(longArrayOf(3, 2, 1), longArrayOf(1, 2, 3).reversedArray())
|
||||
assertArrayNotSameButEquals(floatArrayOf(3F, 2F, 1F), floatArrayOf(1F, 2F, 3F).reversedArray())
|
||||
assertArrayNotSameButEquals(doubleArrayOf(3.0, 2.0, 1.0), doubleArrayOf(1.0, 2.0, 3.0).reversedArray())
|
||||
assertArrayNotSameButEquals(charArrayOf('3', '2', '1'), charArrayOf('1', '2', '3').reversedArray())
|
||||
assertArrayNotSameButEquals(booleanArrayOf(false, false, true), booleanArrayOf(true, false, false).reversedArray())
|
||||
assertArrayNotSameButEquals(arrayOf("3", "2", "1"), arrayOf("1", "2", "3").reversedArray())
|
||||
assertArrayNotSameButEquals(arrayOf("3", "2", "1"), (arrayOf("1", "2", "3") as Array<out String>).reversedArray())
|
||||
}
|
||||
|
||||
@Test fun onEach() {
|
||||
var count = 0
|
||||
@@ -1865,33 +1571,6 @@ class ArraysTest {
|
||||
assertEquals(Array(0, { "" }).asList(), emptyList<String>())
|
||||
}
|
||||
|
||||
@Test fun sort() {
|
||||
val intArr = intArrayOf(5, 2, 1, 9, 80, Int.MIN_VALUE, Int.MAX_VALUE)
|
||||
intArr.sort()
|
||||
assertArrayNotSameButEquals(intArrayOf(Int.MIN_VALUE, 1, 2, 5, 9, 80, Int.MAX_VALUE), intArr)
|
||||
intArr.sortDescending()
|
||||
assertArrayNotSameButEquals(intArrayOf(Int.MAX_VALUE, 80, 9, 5, 2, 1, Int.MIN_VALUE), intArr)
|
||||
|
||||
val longArr = longArrayOf(200, 2, 1, 4, 3, Long.MIN_VALUE, Long.MAX_VALUE)
|
||||
longArr.sort()
|
||||
assertArrayNotSameButEquals(longArrayOf(Long.MIN_VALUE, 1, 2, 3, 4, 200, Long.MAX_VALUE), longArr)
|
||||
longArr.sortDescending()
|
||||
assertArrayNotSameButEquals(longArrayOf(Long.MAX_VALUE, 200, 4, 3, 2, 1, Long.MIN_VALUE), longArr)
|
||||
|
||||
val charArr = charArrayOf('d', 'c', 'E', 'a', '\u0000', '\uFFFF')
|
||||
charArr.sort()
|
||||
assertArrayNotSameButEquals(charArrayOf('\u0000', 'E', 'a', 'c', 'd', '\uFFFF'), charArr)
|
||||
charArr.sortDescending()
|
||||
assertArrayNotSameButEquals(charArrayOf('\uFFFF', 'd', 'c', 'a', 'E', '\u0000'), charArr)
|
||||
|
||||
|
||||
val strArr = arrayOf("9", "80", "all", "Foo")
|
||||
strArr.sort()
|
||||
assertArrayNotSameButEquals(arrayOf("80", "9", "Foo", "all"), strArr)
|
||||
strArr.sortDescending()
|
||||
assertArrayNotSameButEquals(arrayOf("all", "Foo", "9", "80"), strArr)
|
||||
}
|
||||
|
||||
@Test fun sortRange() {
|
||||
|
||||
fun <TArray, T : Comparable<T>> doTest(
|
||||
@@ -1977,50 +1656,6 @@ class ArraysTest {
|
||||
doTest(build = { map {it.toUShort()}.toUShortArray() }, sortDescending = { from, to -> sortDescending(from, to) }, snapshot = { toList() })
|
||||
}
|
||||
|
||||
@Test fun sortedTests() {
|
||||
assertTrue(arrayOf<Long>().sorted().none())
|
||||
assertEquals(listOf(1), arrayOf(1).sorted())
|
||||
|
||||
fun <A, T: Comparable<T>> arrayData(vararg values: T, toArray: Array<out T>.() -> A) = ArraySortedChecker<A, T>(values.toArray(), naturalOrder())
|
||||
|
||||
with (arrayData("ac", "aD", "aba") { toList().toTypedArray() }) {
|
||||
checkSorted<List<String>>({ sorted() }, { sortedDescending() }, { iterator() })
|
||||
checkSorted<Array<String>>({ sortedArray() }, { sortedArrayDescending()}, { iterator() } )
|
||||
}
|
||||
|
||||
with (arrayData("ac", "aD", "aba") { toList().toTypedArray() as Array<out String> }) {
|
||||
checkSorted<List<String>>({ sorted() }, { sortedDescending() }, { iterator() })
|
||||
checkSorted<Array<out String>>({ sortedArray() }, { sortedArrayDescending()}, { iterator() } )
|
||||
}
|
||||
|
||||
with (arrayData(3, 7, 1) { toIntArray() }) {
|
||||
checkSorted<List<Int>>( { sorted() }, { sortedDescending() }, { iterator() })
|
||||
checkSorted<IntArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
|
||||
}
|
||||
|
||||
|
||||
with (arrayData(1L, Long.MIN_VALUE, Long.MAX_VALUE) { toLongArray() }) {
|
||||
checkSorted<List<Long>>( { sorted() }, { sortedDescending() }, { iterator() })
|
||||
checkSorted<LongArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
|
||||
}
|
||||
|
||||
with (arrayData('a', 'D', 'c') { toCharArray() }) {
|
||||
checkSorted<List<Char>>( { sorted() }, { sortedDescending() }, { iterator() })
|
||||
checkSorted<CharArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
|
||||
}
|
||||
|
||||
with (arrayData(1.toByte(), Byte.MAX_VALUE, Byte.MIN_VALUE) { toByteArray() }) {
|
||||
checkSorted<List<Byte>>( { sorted() }, { sortedDescending() }, { iterator() })
|
||||
checkSorted<ByteArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
|
||||
}
|
||||
|
||||
with(arrayData(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, -Double.MAX_VALUE,
|
||||
1.0, -1.0, Double.MIN_VALUE, -Double.MIN_VALUE, 0.0, -0.0, Double.NaN) { toDoubleArray() }) {
|
||||
checkSorted<List<Double>>( { sorted() }, { sortedDescending() }, { iterator() })
|
||||
checkSorted<DoubleArray>( { sortedArray() }, { sortedArrayDescending() }, { iterator() })
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun sortStable() {
|
||||
val keyRange = 'A'..'D'
|
||||
for (size in listOf(10, 100, 2000)) {
|
||||
@@ -2036,25 +1671,6 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun sortByInPlace() {
|
||||
val data = arrayOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.sortBy { it.second }
|
||||
assertArrayNotSameButEquals(arrayOf("ab" to 3, "aa" to 3, "aa" to 20), data)
|
||||
|
||||
data.sortBy { it.first }
|
||||
assertArrayNotSameButEquals(arrayOf("aa" to 3, "aa" to 20, "ab" to 3), data)
|
||||
|
||||
data.sortByDescending { (it.first + it.second).length }
|
||||
assertArrayNotSameButEquals(arrayOf("aa" to 20, "aa" to 3, "ab" to 3), data)
|
||||
}
|
||||
|
||||
@Test fun sortedBy() {
|
||||
val values = arrayOf("ac", "aD", "aba")
|
||||
val indices = values.indices.toList().toIntArray()
|
||||
|
||||
assertEquals(listOf(1, 2, 0), indices.sortedBy { values[it] })
|
||||
}
|
||||
|
||||
@Test fun sortByStable() {
|
||||
val keyRange = 'A'..'D'
|
||||
for (size in listOf(10, 100, 2000)) {
|
||||
@@ -2082,13 +1698,6 @@ class ArraysTest {
|
||||
|
||||
@Test fun sortedWith() {
|
||||
val comparator = compareBy { it: Int -> it % 3 }.thenByDescending { it }
|
||||
fun <A, T> arrayData(array: A, comparator: Comparator<T>) = ArraySortedChecker<A, T>(array, comparator)
|
||||
|
||||
arrayData(intArrayOf(0, 1, 2, 3, 4, 5), comparator)
|
||||
.checkSorted<List<Int>>( { sortedWith(comparator) }, { sortedWith(comparator.reversed()) }, { iterator() })
|
||||
|
||||
arrayData(arrayOf(0, 1, 2, 3, 4, 5), comparator)
|
||||
.checkSorted<Array<out Int>>( { sortedArrayWith(comparator) }, { sortedArrayWith(comparator.reversed()) }, { iterator() })
|
||||
|
||||
// in-place
|
||||
val array = Array(6) { it }
|
||||
@@ -2151,64 +1760,6 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun <T> testShuffle(array: T, shuffle: T.() -> Unit, toList: T.() -> List<*>) {
|
||||
val original = array.toList()
|
||||
array.shuffle()
|
||||
val shuffled = array.toList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(original.groupBy { it }, shuffled.groupBy { it })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffle() {
|
||||
val numbers = List(100) { it }
|
||||
testShuffle(numbers.map(Int::toInt).toIntArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toLong).toLongArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toByte).toByteArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toShort).toShortArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toFloat).toFloatArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toDouble).toDoubleArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toChar).toCharArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map { it % 2 == 0 }.toBooleanArray(), { shuffle() }, { toList() })
|
||||
|
||||
testShuffle(numbers.map(Int::toUInt).toUIntArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toULong).toULongArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toUByte).toUByteArray(), { shuffle() }, { toList() })
|
||||
testShuffle(numbers.map(Int::toUShort).toUShortArray(), { shuffle() }, { toList() })
|
||||
|
||||
testShuffle(arrayOf(1, "x", null, Any(), 'a', 2u, 5.0), { shuffle() }, { toList() })
|
||||
}
|
||||
|
||||
private inline fun <T> testShuffleR(array: T, shuffle: T.(Random) -> Unit, toList: T.() -> List<*>) {
|
||||
val seed = Random.nextInt()
|
||||
val original = array.toList()
|
||||
val originalShuffled = original.shuffled(Random(seed))
|
||||
array.shuffle(Random(seed))
|
||||
val shuffled = array.toList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(originalShuffled, shuffled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shufflePredictably() {
|
||||
val numbers = List(16) { it }
|
||||
testShuffleR(numbers.map(Int::toInt).toIntArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toLong).toLongArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toByte).toByteArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toShort).toShortArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toFloat).toFloatArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toDouble).toDoubleArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toChar).toCharArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map { it % 2 == 0 }.toBooleanArray(), { r -> shuffle(r) }, { toList() })
|
||||
|
||||
testShuffleR(numbers.map(Int::toUInt).toUIntArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toULong).toULongArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toUByte).toUByteArray(), { r -> shuffle(r) }, { toList() })
|
||||
testShuffleR(numbers.map(Int::toUShort).toUShortArray(), { r -> shuffle(r) }, { toList() })
|
||||
|
||||
testShuffleR(arrayOf(1, "x", null, Any(), 'a', 2u, 5.0), { r -> shuffle(r) }, { toList() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun elementAt() {
|
||||
expect(0) { byteArrayOf(0, 1, 2).elementAt(0) }
|
||||
@@ -2299,7 +1850,7 @@ private fun IntArray.toStringArray() = Array(size) { get(it).toString() }
|
||||
fun <K : Comparable<K>> Array<out Sortable<K>>.assertStableSorted(descending: Boolean = false) =
|
||||
iterator().assertStableSorted(descending = descending)
|
||||
|
||||
private class ArraySortedChecker<A, T>(val array: A, val comparator: Comparator<in T>) {
|
||||
internal class ArraySortedChecker<A, T>(val array: A, val comparator: Comparator<in T>) {
|
||||
public fun <R> checkSorted(sorted: A.() -> R, sortedDescending: A.() -> R, iterator: R.() -> Iterator<T>) {
|
||||
array.sorted().iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
|
||||
array.sortedDescending().iterator().assertSorted { a, b -> comparator.compare(a, b) >= 0 }
|
||||
|
||||
@@ -152,23 +152,6 @@ class CollectionTest {
|
||||
assertEquals(0, charValues.size)
|
||||
}
|
||||
|
||||
@Test fun foldIndexed() {
|
||||
expect(42) {
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.foldIndexed(0) { index, a, b -> index * (a + b) }
|
||||
}
|
||||
|
||||
expect(0) {
|
||||
val numbers = arrayListOf<Int>()
|
||||
numbers.foldIndexed(0) { index, a, b -> index * (a + b) }
|
||||
}
|
||||
|
||||
expect("11234") {
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun foldIndexedWithDifferentTypes() {
|
||||
expect(10) {
|
||||
val numbers = listOf("a", "ab", "abc")
|
||||
@@ -188,13 +171,6 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun foldRightIndexed() {
|
||||
expect("12343210") {
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun foldRightIndexedWithDifferentTypes() {
|
||||
expect("12343210") {
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
@@ -851,40 +827,6 @@ class CollectionTest {
|
||||
assertIsPositiveZero(listOf(0.0F, -0.0F).shuffled().maxOrNull()!!.toDouble())
|
||||
}
|
||||
|
||||
@Test fun minWithOrNull() {
|
||||
expect(null, { listOf<Int>().minWithOrNull(naturalOrder()) })
|
||||
expect(1, { listOf(1).minWithOrNull(naturalOrder()) })
|
||||
expect("a", { listOf("a", "B").minWithOrNull(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
expect("a", { listOf("a", "B").asSequence().minWithOrNull(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
}
|
||||
|
||||
@Test fun maxWithOrNull() {
|
||||
expect(null, { listOf<Int>().maxWithOrNull(naturalOrder()) })
|
||||
expect(1, { listOf(1).maxWithOrNull(naturalOrder()) })
|
||||
expect("B", { listOf("a", "B").maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
expect("B", { listOf("a", "B").asSequence().maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
}
|
||||
|
||||
@Test fun minByOrNull() {
|
||||
expect(null, { listOf<Int>().minByOrNull { it } })
|
||||
expect(1, { listOf(1).minByOrNull { it } })
|
||||
expect(3, { listOf(2, 3).minByOrNull { -it } })
|
||||
expect('a', { listOf('a', 'b').minByOrNull { "x$it" } })
|
||||
expect("b", { listOf("b", "abc").minByOrNull { it.length } })
|
||||
expect(null, { listOf<Int>().asSequence().minByOrNull { it } })
|
||||
expect(3, { listOf(2, 3).asSequence().minByOrNull { -it } })
|
||||
}
|
||||
|
||||
@Test fun maxByOrNull() {
|
||||
expect(null, { listOf<Int>().maxByOrNull { it } })
|
||||
expect(1, { listOf(1).maxByOrNull { it } })
|
||||
expect(2, { listOf(2, 3).maxByOrNull { -it } })
|
||||
expect('b', { listOf('a', 'b').maxByOrNull { "x$it" } })
|
||||
expect("abc", { listOf("b", "abc").maxByOrNull { it.length } })
|
||||
expect(null, { listOf<Int>().asSequence().maxByOrNull { it } })
|
||||
expect(2, { listOf(2, 3).asSequence().maxByOrNull { -it } })
|
||||
}
|
||||
|
||||
@Test fun minByOrNullEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).minByOrNull { c++; it * it } })
|
||||
@@ -1037,18 +979,6 @@ class CollectionTest {
|
||||
-0.0, -Double.MIN_VALUE, -1.0, -Double.MAX_VALUE, Double.NEGATIVE_INFINITY), dataDouble.sortedDescending())
|
||||
}
|
||||
|
||||
@Test fun sortByInPlace() {
|
||||
val data = arrayListOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.sortBy { it.second }
|
||||
assertEquals(listOf("ab" to 3, "aa" to 3, "aa" to 20), data)
|
||||
|
||||
data.sortBy { it.first }
|
||||
assertEquals(listOf("aa" to 3, "aa" to 20, "ab" to 3), data)
|
||||
|
||||
data.sortByDescending { (it.first + it.second).length }
|
||||
assertEquals(listOf("aa" to 20, "aa" to 3, "ab" to 3), data)
|
||||
}
|
||||
|
||||
@Test fun sortStable() {
|
||||
val keyRange = 'A'..'D'
|
||||
for (size in listOf(10, 100, 2000)) {
|
||||
@@ -1064,12 +994,6 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun sortedBy() {
|
||||
assertEquals(listOf("two" to 3, "three" to 20), listOf("three" to 20, "two" to 3).sortedBy { it.second })
|
||||
assertEquals(listOf("three" to 20, "two" to 3), listOf("three" to 20, "two" to 3).sortedBy { it.first })
|
||||
assertEquals(listOf("three", "two"), listOf("two", "three").sortedByDescending { it.length })
|
||||
}
|
||||
|
||||
@Test fun sortedNullableBy() {
|
||||
fun String.nullIfEmpty() = if (isEmpty()) null else this
|
||||
listOf(null, "", "a").let {
|
||||
@@ -1088,15 +1012,6 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun sortedWith() {
|
||||
val comparator = compareBy<String> { it.toUpperCase().reversed() }
|
||||
val data = listOf("cat", "dad", "BAD")
|
||||
|
||||
expect(listOf("BAD", "dad", "cat")) { data.sortedWith(comparator) }
|
||||
expect(listOf("cat", "dad", "BAD")) { data.sortedWith(comparator.reversed()) }
|
||||
expect(listOf("BAD", "dad", "cat")) { data.sortedWith(comparator.reversed().reversed()) }
|
||||
}
|
||||
|
||||
@Test fun sortByStable() {
|
||||
val keyRange = 'A'..'D'
|
||||
for (size in listOf(10, 100, 2000)) {
|
||||
|
||||
@@ -247,22 +247,6 @@ class UnsignedArraysTest {
|
||||
assertArrayContentEquals(uintArrayOf(1u, 2u, 3u, 4u), uintArrayOf(1u, 2u) + uintArrayOf(3u, 4u))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOf() {
|
||||
expect(-1) { ubyteArrayOf(1, 2, 3).indexOf(0) }
|
||||
expect(0) { ushortArrayOf(1, 2, 3).indexOf(1) }
|
||||
expect(1) { uintArrayOf(1, 2, 3).indexOf(2) }
|
||||
expect(2) { ulongArrayOf(1, 2, 3).indexOf(3) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfFirst() {
|
||||
expect(-1) { ubyteArrayOf(1, 2, 3).indexOfFirst { it == 5.toUByte() } }
|
||||
expect(0) { ushortArrayOf(1, 2, 3).indexOfFirst { it % 2u == 1u } }
|
||||
expect(1) { uintArrayOf(1, 2, 3).indexOfFirst { it % 2u == 0u } }
|
||||
expect(2) { ulongArrayOf(1, 2, 3).indexOfFirst { it == 3.toULong() } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun lastIndexOf() {
|
||||
expect(-1) { ubyteArrayOf(1, 2, 3).lastIndexOf(0) }
|
||||
|
||||
1947
libraries/stdlib/test/generated/_ArraysTest.kt
Normal file
1947
libraries/stdlib/test/generated/_ArraysTest.kt
Normal file
File diff suppressed because it is too large
Load Diff
234
libraries/stdlib/test/generated/_CollectionsTest.kt
Normal file
234
libraries/stdlib/test/generated/_CollectionsTest.kt
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLibTests.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
|
||||
import kotlin.random.*
|
||||
import kotlin.test.*
|
||||
|
||||
class _CollectionsTest {
|
||||
@Test
|
||||
fun foldIndexed_Iterable() {
|
||||
expect(8) { listOf<Int>(1, 2, 3).foldIndexed(0) { i, acc, e -> acc + i.toInt() * e } }
|
||||
expect(10) { listOf<Int>(1, 2, 3).foldIndexed(1) { i, acc, e -> acc + i + e.toInt() } }
|
||||
expect(15) { listOf<Int>(1, 2, 3).foldIndexed(1) { i, acc, e -> acc * (i.toInt() + e) } }
|
||||
expect(" 0-1 1-2 2-3") { listOf<Int>(1, 2, 3).foldIndexed("") { i, acc, e -> "$acc $i-$e" } }
|
||||
expect(42) {
|
||||
val numbers = listOf<Int>(1, 2, 3, 4)
|
||||
numbers.foldIndexed(0) { index, a, b -> index.toInt() * (a + b) }
|
||||
}
|
||||
expect(0) {
|
||||
val numbers = listOf<Int>()
|
||||
numbers.foldIndexed(0) { index, a, b -> index.toInt() * (a + b) }
|
||||
}
|
||||
expect("11234") {
|
||||
val numbers = listOf<Int>(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldRightIndexed_List() {
|
||||
expect(8) { listOf<Int>(1, 2, 3).foldRightIndexed(0) { i, e, acc -> acc + i.toInt() * e } }
|
||||
expect(10) { listOf<Int>(1, 2, 3).foldRightIndexed(1) { i, e, acc -> acc + i + e.toInt() } }
|
||||
expect(15) { listOf<Int>(1, 2, 3).foldRightIndexed(1) { i, e, acc -> acc * (i.toInt() + e) } }
|
||||
expect(" 2-3 1-2 0-1") { listOf<Int>(1, 2, 3).foldRightIndexed("") { i, e, acc -> "$acc $i-$e" } }
|
||||
expect("12343210") {
|
||||
val numbers = listOf<Int>(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxByOrNull_Iterable() {
|
||||
assertEquals(null, listOf<Int>().maxByOrNull { it })
|
||||
assertEquals(1, listOf<Int>(1).maxByOrNull { it })
|
||||
assertEquals(3, listOf<Int>(3, 2).maxByOrNull { it * it })
|
||||
assertEquals(3, listOf<Int>(3, 2).maxByOrNull { "a" })
|
||||
assertEquals(3, listOf<Int>(3, 2).maxByOrNull { it.toString() })
|
||||
assertEquals(2, listOf<Int>(2, 3).maxByOrNull { -it })
|
||||
assertEquals('b', listOf('a', 'b').maxByOrNull { "x$it" })
|
||||
assertEquals("abc", listOf("b", "abc").maxByOrNull { it.length })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxWithOrNull_Iterable() {
|
||||
assertEquals(null, listOf<Int>().maxWithOrNull(naturalOrder()))
|
||||
assertEquals(1, listOf<Int>(1).maxWithOrNull(naturalOrder()))
|
||||
assertEquals(3, listOf<Int>(2, 3, 4).maxWithOrNull(compareBy { it % 4 }))
|
||||
assertEquals("B", listOf("a", "B").maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minByOrNull_Iterable() {
|
||||
assertEquals(null, listOf<Int>().minByOrNull { it })
|
||||
assertEquals(1, listOf<Int>(1).minByOrNull { it })
|
||||
assertEquals(2, listOf<Int>(3, 2).minByOrNull { it * it })
|
||||
assertEquals(3, listOf<Int>(3, 2).minByOrNull { "a" })
|
||||
assertEquals(2, listOf<Int>(3, 2).minByOrNull { it.toString() })
|
||||
assertEquals(3, listOf<Int>(2, 3).minByOrNull { -it })
|
||||
assertEquals('a', listOf('a', 'b').minByOrNull { "x$it" })
|
||||
assertEquals("b", listOf("b", "abc").minByOrNull { it.length })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minWithOrNull_Iterable() {
|
||||
assertEquals(null, listOf<Int>().minWithOrNull(naturalOrder()))
|
||||
assertEquals(1, listOf<Int>(1).minWithOrNull(naturalOrder()))
|
||||
assertEquals(4, listOf<Int>(2, 3, 4).minWithOrNull(compareBy { it % 4 }))
|
||||
assertEquals("a", listOf("a", "B").minWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOf_List() {
|
||||
expect(-1) { listOf<Int>(1, 2, 3).indexOf(0) }
|
||||
expect(0) { listOf<Int>(1, 2, 3).indexOf(1) }
|
||||
expect(1) { listOf<Int>(1, 2, 3).indexOf(2) }
|
||||
expect(2) { listOf<Int>(1, 2, 3).indexOf(3) }
|
||||
expect(-1) { listOf("cat", "dog", "bird").indexOf("mouse") }
|
||||
expect(0) { listOf("cat", "dog", "bird").indexOf("cat") }
|
||||
expect(1) { listOf("cat", "dog", "bird").indexOf("dog") }
|
||||
expect(2) { listOf("cat", "dog", "bird").indexOf("bird") }
|
||||
expect(0) { listOf(null, "dog", null).indexOf(null as String?)}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfFirst_List() {
|
||||
expect(-1) { listOf<Int>(1, 2, 3).indexOfFirst { it == 0 } }
|
||||
expect(0) { listOf<Int>(1, 2, 3).indexOfFirst { it % 2 == 1 } }
|
||||
expect(1) { listOf<Int>(1, 2, 3).indexOfFirst { it % 2 == 0 } }
|
||||
expect(2) { listOf<Int>(1, 2, 3).indexOfFirst { it == 3 } }
|
||||
expect(-1) { listOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
|
||||
expect(0) { listOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
|
||||
expect(1) { listOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
|
||||
expect(2) { listOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversed_Iterable() {
|
||||
assertEquals(listOf<Int>(3, 2, 1), listOf<Int>(1, 2, 3).reversed())
|
||||
assertEquals(listOf("3", "2", "1"), listOf("1", "2", "3").reversed())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffle_List() {
|
||||
fun test(data: MutableList<*>) {
|
||||
val original = data.toMutableList()
|
||||
data.shuffle()
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(original.groupBy { it }, shuffled.groupBy { it })
|
||||
}
|
||||
test(MutableList(100) { it.toInt() })
|
||||
test(mutableListOf(1, "x", null, Any(), 'a', 2u, 5.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffleRandom_List() {
|
||||
fun test(data: MutableList<*>) {
|
||||
val seed = Random.nextInt()
|
||||
val original = data.toMutableList()
|
||||
val originalShuffled = original.shuffled(Random(seed))
|
||||
data.shuffle(Random(seed))
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(originalShuffled, shuffled)
|
||||
}
|
||||
test(MutableList(16) { it.toInt() })
|
||||
test(mutableListOf(1, "x", null, Any(), 'a', 2u, 5.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sort_List() {
|
||||
val data = mutableListOf(5, 2, 1, 9, 80, Int.MIN_VALUE, Int.MAX_VALUE)
|
||||
data.sort()
|
||||
assertEquals(listOf(Int.MIN_VALUE, 1, 2, 5, 9, 80, Int.MAX_VALUE), data)
|
||||
val strings = mutableListOf("9", "80", "all", "Foo")
|
||||
strings.sort()
|
||||
assertEquals(listOf("80", "9", "Foo", "all"), strings)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortBy_List() {
|
||||
val data = mutableListOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.sortBy { it.second }
|
||||
assertEquals(listOf("ab" to 3, "aa" to 3, "aa" to 20), data)
|
||||
data.sortBy { it.first }
|
||||
assertEquals(listOf("aa" to 3, "aa" to 20, "ab" to 3), data)
|
||||
data.sortBy { (it.first + it.second).length }
|
||||
assertEquals(listOf("aa" to 3, "ab" to 3, "aa" to 20), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortByDescending_List() {
|
||||
val data = mutableListOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.sortByDescending { it.second }
|
||||
assertEquals(listOf("aa" to 20, "ab" to 3, "aa" to 3), data)
|
||||
data.sortByDescending { it.first }
|
||||
assertEquals(listOf("ab" to 3, "aa" to 20, "aa" to 3), data)
|
||||
data.sortByDescending { (it.first + it.second).length }
|
||||
assertEquals(listOf("aa" to 20, "ab" to 3, "aa" to 3), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortDescending_List() {
|
||||
val data = mutableListOf(5, 2, 1, 9, 80, Int.MIN_VALUE, Int.MAX_VALUE)
|
||||
data.sortDescending()
|
||||
assertEquals(listOf(Int.MIN_VALUE, 1, 2, 5, 9, 80, Int.MAX_VALUE).reversed(), data)
|
||||
val strings = mutableListOf("9", "80", "all", "Foo")
|
||||
strings.sortDescending()
|
||||
assertEquals(listOf("80", "9", "Foo", "all").reversed(), strings)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sorted_Iterable() {
|
||||
listOf<Int>(3, 7, 1).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
listOf(1, Int.MAX_VALUE, Int.MIN_VALUE).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
listOf("ac", "aD", "aba").sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedBy_List() {
|
||||
val values = arrayOf("ac", "aD", "aba")
|
||||
val indices = listOf<Int>(0, 1, 2)
|
||||
assertEquals(listOf<Int>(1, 2, 0), indices.sortedBy { values[it.toInt()] })
|
||||
assertEquals(listOf("two" to 3, "three" to 20), listOf("three" to 20, "two" to 3).sortedBy { it.second })
|
||||
assertEquals(listOf("three" to 20, "two" to 3), listOf("three" to 20, "two" to 3).sortedBy { it.first })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedByDescending_List() {
|
||||
val values = arrayOf("ac", "aD", "aba")
|
||||
val indices = listOf<Int>(0, 1, 2)
|
||||
assertEquals(listOf<Int>(1, 2, 0).reversed(), indices.sortedByDescending { values[it.toInt()] })
|
||||
assertEquals(listOf("two" to 3, "three" to 20).reversed(), listOf("three" to 20, "two" to 3).sortedByDescending { it.second })
|
||||
assertEquals(listOf("three" to 20, "two" to 3).reversed(), listOf("three" to 20, "two" to 3).sortedByDescending { it.first })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedDescending_Iterable() {
|
||||
listOf<Int>(3, 7, 1).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
listOf(1, Int.MAX_VALUE, Int.MIN_VALUE).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
listOf("ac", "aD", "aba").sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedWith_Iterable() {
|
||||
val comparator = compareBy { it: Int -> it % 3 }.thenByDescending { it }
|
||||
listOf<Int>(0, 1, 2, 3, 4, 5).sortedWith(comparator).iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
|
||||
val comparator1 = compareBy<String> { it.toUpperCase().reversed() }
|
||||
val data = listOf("cat", "dad", "BAD")
|
||||
assertEquals(listOf("BAD", "dad", "cat"), data.sortedWith(comparator1))
|
||||
assertEquals(listOf("cat", "dad", "BAD"), data.sortedWith(comparator1.reversed()))
|
||||
assertEquals(listOf("BAD", "dad", "cat"), data.sortedWith(comparator1.reversed().reversed()))
|
||||
}
|
||||
|
||||
}
|
||||
463
libraries/stdlib/test/generated/_ComparisonsTest.kt
Normal file
463
libraries/stdlib/test/generated/_ComparisonsTest.kt
Normal file
@@ -0,0 +1,463 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.comparisons
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLibTests.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import kotlin.random.*
|
||||
import kotlin.test.*
|
||||
|
||||
class _ComparisonsTest {
|
||||
@Test
|
||||
fun maxOf_2_Generic() {
|
||||
expect(2) { maxOf<Int>(2, 1) }
|
||||
expect(126) { maxOf<Int>(58, 126) }
|
||||
expect(23) { maxOf<Int>(Random.nextInt(Int.MIN_VALUE, 23), 23) }
|
||||
expect(Int.MAX_VALUE) { maxOf<Int>(Int.MIN_VALUE, Int.MAX_VALUE) }
|
||||
expect(Int.MIN_VALUE) { maxOf<Int>(Int.MIN_VALUE, Int.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_Int() {
|
||||
expect(2) { maxOf(2, 1) }
|
||||
expect(126) { maxOf(58, 126) }
|
||||
expect(23) { maxOf(Random.nextInt(Int.MIN_VALUE, 23), 23) }
|
||||
expect(Int.MAX_VALUE) { maxOf(Int.MIN_VALUE, Int.MAX_VALUE) }
|
||||
expect(Int.MIN_VALUE) { maxOf(Int.MIN_VALUE, Int.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_Long() {
|
||||
expect(2L) { maxOf(2L, 1L) }
|
||||
expect(126L) { maxOf(58L, 126L) }
|
||||
expect(23L) { maxOf(Random.nextLong(Long.MIN_VALUE, 23L), 23L) }
|
||||
expect(Long.MAX_VALUE) { maxOf(Long.MIN_VALUE, Long.MAX_VALUE) }
|
||||
expect(Long.MIN_VALUE) { maxOf(Long.MIN_VALUE, Long.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_Byte() {
|
||||
expect(2.toByte()) { maxOf(2.toByte(), 1.toByte()) }
|
||||
expect(126.toByte()) { maxOf(58.toByte(), 126.toByte()) }
|
||||
expect(23.toByte()) { maxOf(Random.nextInt(Byte.MIN_VALUE.toInt(), 23).toByte(), 23.toByte()) }
|
||||
expect(Byte.MAX_VALUE) { maxOf(Byte.MIN_VALUE, Byte.MAX_VALUE) }
|
||||
expect(Byte.MIN_VALUE) { maxOf(Byte.MIN_VALUE, Byte.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_Short() {
|
||||
expect(2.toShort()) { maxOf(2.toShort(), 1.toShort()) }
|
||||
expect(126.toShort()) { maxOf(58.toShort(), 126.toShort()) }
|
||||
expect(23.toShort()) { maxOf(Random.nextInt(Short.MIN_VALUE.toInt(), 23).toShort(), 23.toShort()) }
|
||||
expect(Short.MAX_VALUE) { maxOf(Short.MIN_VALUE, Short.MAX_VALUE) }
|
||||
expect(Short.MIN_VALUE) { maxOf(Short.MIN_VALUE, Short.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_Double() {
|
||||
expect(2.0) { maxOf(2.0, 1.0) }
|
||||
expect(126.0) { maxOf(58.0, 126.0) }
|
||||
expect(23.0) { maxOf(Random.nextDouble(Double.MIN_VALUE, 23.0), 23.0) }
|
||||
expect(Double.MAX_VALUE) { maxOf(Double.MIN_VALUE, Double.MAX_VALUE) }
|
||||
expect(Double.MIN_VALUE) { maxOf(Double.MIN_VALUE, Double.MIN_VALUE) }
|
||||
assertEquals(0.0, maxOf(0.0, -0.0))
|
||||
assertEquals(0.0, maxOf(-0.0, 0.0))
|
||||
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_Float() {
|
||||
expect(2.0f) { maxOf(2.0f, 1.0f) }
|
||||
expect(126.0f) { maxOf(58.0f, 126.0f) }
|
||||
expect(23.0f) { maxOf(Random.nextDouble(Float.MIN_VALUE.toDouble(), 23.0).toFloat(), 23.0f) }
|
||||
expect(Float.MAX_VALUE) { maxOf(Float.MIN_VALUE, Float.MAX_VALUE) }
|
||||
expect(Float.MIN_VALUE) { maxOf(Float.MIN_VALUE, Float.MIN_VALUE) }
|
||||
assertEquals(0.0f, maxOf(0.0f, -0.0f))
|
||||
assertEquals(0.0f, maxOf(-0.0f, 0.0f))
|
||||
assertEquals(Float.POSITIVE_INFINITY, maxOf(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_Generic() {
|
||||
expect(3) { maxOf<Int>(2, 1, 3) }
|
||||
expect(126) { maxOf<Int>(58, 126, 55) }
|
||||
expect(23) { maxOf<Int>(Random.nextInt(Int.MIN_VALUE, 23), 23, Random.nextInt(Int.MIN_VALUE, 23)) }
|
||||
expect(Int.MIN_VALUE) { maxOf<Int>(Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE) }
|
||||
expect(Int.MAX_VALUE) { maxOf<Int>(Int.MIN_VALUE, Int.MAX_VALUE, 0) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_Int() {
|
||||
expect(3) { maxOf(2, 1, 3) }
|
||||
expect(126) { maxOf(58, 126, 55) }
|
||||
expect(23) { maxOf(Random.nextInt(Int.MIN_VALUE, 23), 23, Random.nextInt(Int.MIN_VALUE, 23)) }
|
||||
expect(Int.MIN_VALUE) { maxOf(Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE) }
|
||||
expect(Int.MAX_VALUE) { maxOf(Int.MIN_VALUE, Int.MAX_VALUE, 0) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_Long() {
|
||||
expect(3L) { maxOf(2L, 1L, 3L) }
|
||||
expect(126L) { maxOf(58L, 126L, 55L) }
|
||||
expect(23L) { maxOf(Random.nextLong(Long.MIN_VALUE, 23L), 23L, Random.nextLong(Long.MIN_VALUE, 23L)) }
|
||||
expect(Long.MIN_VALUE) { maxOf(Long.MIN_VALUE, Long.MIN_VALUE, Long.MIN_VALUE) }
|
||||
expect(Long.MAX_VALUE) { maxOf(Long.MIN_VALUE, Long.MAX_VALUE, 0L) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_Byte() {
|
||||
expect(3.toByte()) { maxOf(2.toByte(), 1.toByte(), 3.toByte()) }
|
||||
expect(126.toByte()) { maxOf(58.toByte(), 126.toByte(), 55.toByte()) }
|
||||
expect(23.toByte()) { maxOf(Random.nextInt(Byte.MIN_VALUE.toInt(), 23).toByte(), 23.toByte(), Random.nextInt(Byte.MIN_VALUE.toInt(), 23).toByte()) }
|
||||
expect(Byte.MIN_VALUE) { maxOf(Byte.MIN_VALUE, Byte.MIN_VALUE, Byte.MIN_VALUE) }
|
||||
expect(Byte.MAX_VALUE) { maxOf(Byte.MIN_VALUE, Byte.MAX_VALUE, 0.toByte()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_Short() {
|
||||
expect(3.toShort()) { maxOf(2.toShort(), 1.toShort(), 3.toShort()) }
|
||||
expect(126.toShort()) { maxOf(58.toShort(), 126.toShort(), 55.toShort()) }
|
||||
expect(23.toShort()) { maxOf(Random.nextInt(Short.MIN_VALUE.toInt(), 23).toShort(), 23.toShort(), Random.nextInt(Short.MIN_VALUE.toInt(), 23).toShort()) }
|
||||
expect(Short.MIN_VALUE) { maxOf(Short.MIN_VALUE, Short.MIN_VALUE, Short.MIN_VALUE) }
|
||||
expect(Short.MAX_VALUE) { maxOf(Short.MIN_VALUE, Short.MAX_VALUE, 0.toShort()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_Double() {
|
||||
expect(3.0) { maxOf(2.0, 1.0, 3.0) }
|
||||
expect(126.0) { maxOf(58.0, 126.0, 55.0) }
|
||||
expect(23.0) { maxOf(Random.nextDouble(Double.MIN_VALUE, 23.0), 23.0, Random.nextDouble(Double.MIN_VALUE, 23.0)) }
|
||||
expect(Double.MIN_VALUE) { maxOf(Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE) }
|
||||
expect(Double.MAX_VALUE) { maxOf(Double.MIN_VALUE, Double.MAX_VALUE, 0.0) }
|
||||
assertEquals(0.0, maxOf(0.0, -0.0, -0.0))
|
||||
assertEquals(0.0, maxOf(-0.0, 0.0, 0.0))
|
||||
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_Float() {
|
||||
expect(3.0f) { maxOf(2.0f, 1.0f, 3.0f) }
|
||||
expect(126.0f) { maxOf(58.0f, 126.0f, 55.0f) }
|
||||
expect(23.0f) { maxOf(Random.nextDouble(Float.MIN_VALUE.toDouble(), 23.0).toFloat(), 23.0f, Random.nextDouble(Float.MIN_VALUE.toDouble(), 23.0).toFloat()) }
|
||||
expect(Float.MIN_VALUE) { maxOf(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE) }
|
||||
expect(Float.MAX_VALUE) { maxOf(Float.MIN_VALUE, Float.MAX_VALUE, 0.0f) }
|
||||
assertEquals(0.0f, maxOf(0.0f, -0.0f, -0.0f))
|
||||
assertEquals(0.0f, maxOf(-0.0f, 0.0f, 0.0f))
|
||||
assertEquals(Float.POSITIVE_INFINITY, maxOf(Float.POSITIVE_INFINITY, Float.MAX_VALUE, Float.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_Generic() {
|
||||
expect(10) { maxOf<Int>(2, 1, 3, 10) }
|
||||
expect(126) { maxOf<Int>(58, 126, 55, 87) }
|
||||
expect(23) { maxOf<Int>(Random.nextInt(Int.MIN_VALUE, 23), 23, Random.nextInt(Int.MIN_VALUE, 23), 21) }
|
||||
expect(Int.MIN_VALUE) { maxOf<Int>(Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE) }
|
||||
expect(Int.MAX_VALUE) { maxOf<Int>(Int.MIN_VALUE, Int.MAX_VALUE, 0, 1) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_Int() {
|
||||
expect(10) { maxOf(2, 1, 3, 10) }
|
||||
expect(126) { maxOf(58, 126, 55, 87) }
|
||||
expect(23) { maxOf(Random.nextInt(Int.MIN_VALUE, 23), 23, Random.nextInt(Int.MIN_VALUE, 23), 21) }
|
||||
expect(Int.MIN_VALUE) { maxOf(Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE) }
|
||||
expect(Int.MAX_VALUE) { maxOf(Int.MIN_VALUE, Int.MAX_VALUE, 0, 1) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_Long() {
|
||||
expect(10L) { maxOf(2L, 1L, 3L, 10L) }
|
||||
expect(126L) { maxOf(58L, 126L, 55L, 87L) }
|
||||
expect(23L) { maxOf(Random.nextLong(Long.MIN_VALUE, 23L), 23L, Random.nextLong(Long.MIN_VALUE, 23L), 21L) }
|
||||
expect(Long.MIN_VALUE) { maxOf(Long.MIN_VALUE, Long.MIN_VALUE, Long.MIN_VALUE, Long.MIN_VALUE) }
|
||||
expect(Long.MAX_VALUE) { maxOf(Long.MIN_VALUE, Long.MAX_VALUE, 0L, 1L) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_Byte() {
|
||||
expect(10.toByte()) { maxOf(2.toByte(), 1.toByte(), 3.toByte(), 10.toByte()) }
|
||||
expect(126.toByte()) { maxOf(58.toByte(), 126.toByte(), 55.toByte(), 87.toByte()) }
|
||||
expect(23.toByte()) { maxOf(Random.nextInt(Byte.MIN_VALUE.toInt(), 23).toByte(), 23.toByte(), Random.nextInt(Byte.MIN_VALUE.toInt(), 23).toByte(), 21.toByte()) }
|
||||
expect(Byte.MIN_VALUE) { maxOf(Byte.MIN_VALUE, Byte.MIN_VALUE, Byte.MIN_VALUE, Byte.MIN_VALUE) }
|
||||
expect(Byte.MAX_VALUE) { maxOf(Byte.MIN_VALUE, Byte.MAX_VALUE, 0.toByte(), 1.toByte()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_Short() {
|
||||
expect(10.toShort()) { maxOf(2.toShort(), 1.toShort(), 3.toShort(), 10.toShort()) }
|
||||
expect(126.toShort()) { maxOf(58.toShort(), 126.toShort(), 55.toShort(), 87.toShort()) }
|
||||
expect(23.toShort()) { maxOf(Random.nextInt(Short.MIN_VALUE.toInt(), 23).toShort(), 23.toShort(), Random.nextInt(Short.MIN_VALUE.toInt(), 23).toShort(), 21.toShort()) }
|
||||
expect(Short.MIN_VALUE) { maxOf(Short.MIN_VALUE, Short.MIN_VALUE, Short.MIN_VALUE, Short.MIN_VALUE) }
|
||||
expect(Short.MAX_VALUE) { maxOf(Short.MIN_VALUE, Short.MAX_VALUE, 0.toShort(), 1.toShort()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_Double() {
|
||||
expect(10.0) { maxOf(2.0, 1.0, 3.0, 10.0) }
|
||||
expect(126.0) { maxOf(58.0, 126.0, 55.0, 87.0) }
|
||||
expect(23.0) { maxOf(Random.nextDouble(Double.MIN_VALUE, 23.0), 23.0, Random.nextDouble(Double.MIN_VALUE, 23.0), 21.0) }
|
||||
expect(Double.MIN_VALUE) { maxOf(Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE) }
|
||||
expect(Double.MAX_VALUE) { maxOf(Double.MIN_VALUE, Double.MAX_VALUE, 0.0, 1.0) }
|
||||
assertEquals(0.0, maxOf(0.0, -0.0, -0.0, 0.0))
|
||||
assertEquals(0.0, maxOf(-0.0, 0.0, 0.0, -0.0))
|
||||
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_Float() {
|
||||
expect(10.0f) { maxOf(2.0f, 1.0f, 3.0f, 10.0f) }
|
||||
expect(126.0f) { maxOf(58.0f, 126.0f, 55.0f, 87.0f) }
|
||||
expect(23.0f) { maxOf(Random.nextDouble(Float.MIN_VALUE.toDouble(), 23.0).toFloat(), 23.0f, Random.nextDouble(Float.MIN_VALUE.toDouble(), 23.0).toFloat(), 21.0f) }
|
||||
expect(Float.MIN_VALUE) { maxOf(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE) }
|
||||
expect(Float.MAX_VALUE) { maxOf(Float.MIN_VALUE, Float.MAX_VALUE, 0.0f, 1.0f) }
|
||||
assertEquals(0.0f, maxOf(0.0f, -0.0f, -0.0f, 0.0f))
|
||||
assertEquals(0.0f, maxOf(-0.0f, 0.0f, 0.0f, -0.0f))
|
||||
assertEquals(Float.POSITIVE_INFINITY, maxOf(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.MAX_VALUE, Float.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_Generic() {
|
||||
expect(1) { minOf<Int>(2, 1) }
|
||||
expect(58) { minOf<Int>(58, 126) }
|
||||
expect(23) { minOf<Int>(Random.nextInt(23..Int.MAX_VALUE), 23) }
|
||||
expect(Int.MIN_VALUE) { minOf<Int>(Int.MIN_VALUE, Int.MAX_VALUE) }
|
||||
expect(Int.MAX_VALUE) { minOf<Int>(Int.MAX_VALUE, Int.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_Int() {
|
||||
expect(1) { minOf(2, 1) }
|
||||
expect(58) { minOf(58, 126) }
|
||||
expect(23) { minOf(Random.nextInt(23..Int.MAX_VALUE), 23) }
|
||||
expect(Int.MIN_VALUE) { minOf(Int.MIN_VALUE, Int.MAX_VALUE) }
|
||||
expect(Int.MAX_VALUE) { minOf(Int.MAX_VALUE, Int.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_Long() {
|
||||
expect(1L) { minOf(2L, 1L) }
|
||||
expect(58L) { minOf(58L, 126L) }
|
||||
expect(23L) { minOf(Random.nextLong(23L..Long.MAX_VALUE), 23L) }
|
||||
expect(Long.MIN_VALUE) { minOf(Long.MIN_VALUE, Long.MAX_VALUE) }
|
||||
expect(Long.MAX_VALUE) { minOf(Long.MAX_VALUE, Long.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_Byte() {
|
||||
expect(1.toByte()) { minOf(2.toByte(), 1.toByte()) }
|
||||
expect(58.toByte()) { minOf(58.toByte(), 126.toByte()) }
|
||||
expect(23.toByte()) { minOf(Random.nextInt(23..Byte.MAX_VALUE.toInt()).toByte(), 23.toByte()) }
|
||||
expect(Byte.MIN_VALUE) { minOf(Byte.MIN_VALUE, Byte.MAX_VALUE) }
|
||||
expect(Byte.MAX_VALUE) { minOf(Byte.MAX_VALUE, Byte.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_Short() {
|
||||
expect(1.toShort()) { minOf(2.toShort(), 1.toShort()) }
|
||||
expect(58.toShort()) { minOf(58.toShort(), 126.toShort()) }
|
||||
expect(23.toShort()) { minOf(Random.nextInt(23..Short.MAX_VALUE.toInt()).toShort(), 23.toShort()) }
|
||||
expect(Short.MIN_VALUE) { minOf(Short.MIN_VALUE, Short.MAX_VALUE) }
|
||||
expect(Short.MAX_VALUE) { minOf(Short.MAX_VALUE, Short.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_Double() {
|
||||
expect(1.0) { minOf(2.0, 1.0) }
|
||||
expect(58.0) { minOf(58.0, 126.0) }
|
||||
expect(23.0) { minOf(Random.nextDouble(23.0, Double.MAX_VALUE), 23.0) }
|
||||
expect(Double.MIN_VALUE) { minOf(Double.MIN_VALUE, Double.MAX_VALUE) }
|
||||
expect(Double.MAX_VALUE) { minOf(Double.MAX_VALUE, Double.MAX_VALUE) }
|
||||
assertEquals(-0.0, minOf(0.0, -0.0))
|
||||
assertEquals(-0.0, minOf(-0.0, 0.0))
|
||||
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_Float() {
|
||||
expect(1.0f) { minOf(2.0f, 1.0f) }
|
||||
expect(58.0f) { minOf(58.0f, 126.0f) }
|
||||
expect(23.0f) { minOf(Random.nextDouble(23.0, Float.MAX_VALUE.toDouble()).toFloat(), 23.0f) }
|
||||
expect(Float.MIN_VALUE) { minOf(Float.MIN_VALUE, Float.MAX_VALUE) }
|
||||
expect(Float.MAX_VALUE) { minOf(Float.MAX_VALUE, Float.MAX_VALUE) }
|
||||
assertEquals(-0.0f, minOf(0.0f, -0.0f))
|
||||
assertEquals(-0.0f, minOf(-0.0f, 0.0f))
|
||||
assertEquals(Float.NEGATIVE_INFINITY, minOf(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_Generic() {
|
||||
expect(1) { minOf<Int>(2, 1, 3) }
|
||||
expect(55) { minOf<Int>(58, 126, 55) }
|
||||
expect(23) { minOf<Int>(Random.nextInt(23..Int.MAX_VALUE), 23, Random.nextInt(23..Int.MAX_VALUE)) }
|
||||
expect(Int.MAX_VALUE) { minOf<Int>(Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE) }
|
||||
expect(Int.MIN_VALUE) { minOf<Int>(Int.MIN_VALUE, Int.MAX_VALUE, 0) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_Int() {
|
||||
expect(1) { minOf(2, 1, 3) }
|
||||
expect(55) { minOf(58, 126, 55) }
|
||||
expect(23) { minOf(Random.nextInt(23..Int.MAX_VALUE), 23, Random.nextInt(23..Int.MAX_VALUE)) }
|
||||
expect(Int.MAX_VALUE) { minOf(Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE) }
|
||||
expect(Int.MIN_VALUE) { minOf(Int.MIN_VALUE, Int.MAX_VALUE, 0) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_Long() {
|
||||
expect(1L) { minOf(2L, 1L, 3L) }
|
||||
expect(55L) { minOf(58L, 126L, 55L) }
|
||||
expect(23L) { minOf(Random.nextLong(23L..Long.MAX_VALUE), 23L, Random.nextLong(23L..Long.MAX_VALUE)) }
|
||||
expect(Long.MAX_VALUE) { minOf(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE) }
|
||||
expect(Long.MIN_VALUE) { minOf(Long.MIN_VALUE, Long.MAX_VALUE, 0L) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_Byte() {
|
||||
expect(1.toByte()) { minOf(2.toByte(), 1.toByte(), 3.toByte()) }
|
||||
expect(55.toByte()) { minOf(58.toByte(), 126.toByte(), 55.toByte()) }
|
||||
expect(23.toByte()) { minOf(Random.nextInt(23..Byte.MAX_VALUE.toInt()).toByte(), 23.toByte(), Random.nextInt(23..Byte.MAX_VALUE.toInt()).toByte()) }
|
||||
expect(Byte.MAX_VALUE) { minOf(Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE) }
|
||||
expect(Byte.MIN_VALUE) { minOf(Byte.MIN_VALUE, Byte.MAX_VALUE, 0) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_Short() {
|
||||
expect(1.toShort()) { minOf(2.toShort(), 1.toShort(), 3.toShort()) }
|
||||
expect(55.toShort()) { minOf(58.toShort(), 126.toShort(), 55.toShort()) }
|
||||
expect(23.toShort()) { minOf(Random.nextInt(23..Short.MAX_VALUE.toInt()).toShort(), 23.toShort(), Random.nextInt(23..Short.MAX_VALUE.toInt()).toShort()) }
|
||||
expect(Short.MAX_VALUE) { minOf(Short.MAX_VALUE, Short.MAX_VALUE, Short.MAX_VALUE) }
|
||||
expect(Short.MIN_VALUE) { minOf(Short.MIN_VALUE, Short.MAX_VALUE, 0) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_Double() {
|
||||
expect(1.0) { minOf(2.0, 1.0, 3.0) }
|
||||
expect(55.0) { minOf(58.0, 126.0, 55.0) }
|
||||
expect(23.0) { minOf(Random.nextDouble(23.0, Double.MAX_VALUE), 23.0, Random.nextDouble(23.0, Double.MAX_VALUE)) }
|
||||
expect(Double.MAX_VALUE) { minOf(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE) }
|
||||
expect(0.0) { minOf(Double.MIN_VALUE, Double.MAX_VALUE, 0.0) }
|
||||
assertEquals(-0.0, minOf(0.0, -0.0, -0.0))
|
||||
assertEquals(-0.0, minOf(-0.0, 0.0, 0.0))
|
||||
assertEquals(Double.MIN_VALUE, minOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_Float() {
|
||||
expect(1.0f) { minOf(2.0f, 1.0f, 3.0f) }
|
||||
expect(55.0f) { minOf(58.0f, 126.0f, 55.0f) }
|
||||
expect(23.0f) { minOf(Random.nextDouble(23.0, Float.MAX_VALUE.toDouble()).toFloat(), 23.0f, Random.nextDouble(23.0, Float.MAX_VALUE.toDouble()).toFloat()) }
|
||||
expect(Float.MAX_VALUE) { minOf(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE) }
|
||||
expect(0.0f) { minOf(Float.MIN_VALUE, Float.MAX_VALUE, 0.0f) }
|
||||
assertEquals(-0.0f, minOf(0.0f, -0.0f, -0.0f))
|
||||
assertEquals(-0.0f, minOf(-0.0f, 0.0f, 0.0f))
|
||||
assertEquals(Float.MIN_VALUE, minOf(Float.POSITIVE_INFINITY, Float.MAX_VALUE, Float.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_Generic() {
|
||||
expect(1) { minOf<Int>(2, 1, 3, 10) }
|
||||
expect(55) { minOf<Int>(58, 126, 55, 87) }
|
||||
expect(21) { minOf<Int>(Random.nextInt(23..Int.MAX_VALUE), 23, Random.nextInt(23..Int.MAX_VALUE), 21) }
|
||||
expect(Int.MAX_VALUE) { minOf<Int>(Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE) }
|
||||
assertEquals(Int.MIN_VALUE, minOf<Int>(Int.MIN_VALUE, Int.MAX_VALUE, 0, 1))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_Int() {
|
||||
expect(1) { minOf(2, 1, 3, 10) }
|
||||
expect(55) { minOf(58, 126, 55, 87) }
|
||||
expect(21) { minOf(Random.nextInt(23..Int.MAX_VALUE), 23, Random.nextInt(23..Int.MAX_VALUE), 21) }
|
||||
expect(Int.MAX_VALUE) { minOf(Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE) }
|
||||
assertEquals(Int.MIN_VALUE, minOf(Int.MIN_VALUE, Int.MAX_VALUE, 0, 1))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_Long() {
|
||||
expect(1L) { minOf(2L, 1L, 3L, 10L) }
|
||||
expect(55L) { minOf(58L, 126L, 55L, 87L) }
|
||||
expect(21L) { minOf(Random.nextLong(23L..Long.MAX_VALUE), 23L, Random.nextLong(23L..Long.MAX_VALUE), 21L) }
|
||||
expect(Long.MAX_VALUE) { minOf(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE) }
|
||||
assertEquals(Long.MIN_VALUE, minOf(Long.MIN_VALUE, Long.MAX_VALUE, 0L, 1L))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_Byte() {
|
||||
expect(1.toByte()) { minOf(2.toByte(), 1.toByte(), 3.toByte(), 10.toByte()) }
|
||||
expect(55.toByte()) { minOf(58.toByte(), 126.toByte(), 55.toByte(), 87.toByte()) }
|
||||
expect(21.toByte()) { minOf(Random.nextInt(23..Byte.MAX_VALUE.toInt()).toByte(), 23.toByte(), Random.nextInt(23..Byte.MAX_VALUE.toInt()).toByte(), 21.toByte()) }
|
||||
expect(Byte.MAX_VALUE) { minOf(Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE) }
|
||||
assertEquals(Byte.MIN_VALUE, minOf(Byte.MIN_VALUE, Byte.MAX_VALUE, 0.toByte(), 1.toByte()))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_Short() {
|
||||
expect(1.toShort()) { minOf(2.toShort(), 1.toShort(), 3.toShort(), 10.toShort()) }
|
||||
expect(55.toShort()) { minOf(58.toShort(), 126.toShort(), 55.toShort(), 87.toShort()) }
|
||||
expect(21.toShort()) { minOf(Random.nextInt(23..Short.MAX_VALUE.toInt()).toShort(), 23.toShort(), Random.nextInt(23..Short.MAX_VALUE.toInt()).toShort(), 21.toShort()) }
|
||||
expect(Short.MAX_VALUE) { minOf(Short.MAX_VALUE, Short.MAX_VALUE, Short.MAX_VALUE, Short.MAX_VALUE) }
|
||||
assertEquals(Short.MIN_VALUE, minOf(Short.MIN_VALUE, Short.MAX_VALUE, 0.toShort(), 1.toShort()))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_Double() {
|
||||
expect(1.0) { minOf(2.0, 1.0, 3.0, 10.0) }
|
||||
expect(55.0) { minOf(58.0, 126.0, 55.0, 87.0) }
|
||||
expect(21.0) { minOf(Random.nextDouble(23.0, Double.MAX_VALUE), 23.0, Random.nextDouble(23.0, Double.MAX_VALUE), 21.0) }
|
||||
expect(Double.MAX_VALUE) { minOf(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE) }
|
||||
assertEquals(0.0, minOf(Double.MIN_VALUE, Double.MAX_VALUE, 0.0, 1.0))
|
||||
assertEquals(-0.0, minOf(0.0, -0.0, -0.0, 0.0))
|
||||
assertEquals(-0.0, minOf(-0.0, 0.0, 0.0, -0.0))
|
||||
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_Float() {
|
||||
expect(1.0f) { minOf(2.0f, 1.0f, 3.0f, 10.0f) }
|
||||
expect(55.0f) { minOf(58.0f, 126.0f, 55.0f, 87.0f) }
|
||||
expect(21.0f) { minOf(Random.nextDouble(23.0, Float.MAX_VALUE.toDouble()).toFloat(), 23.0f, Random.nextDouble(23.0, Float.MAX_VALUE.toDouble()).toFloat(), 21.0f) }
|
||||
expect(Float.MAX_VALUE) { minOf(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE) }
|
||||
assertEquals(0.0f, minOf(Float.MIN_VALUE, Float.MAX_VALUE, 0.0f, 1.0f))
|
||||
assertEquals(-0.0f, minOf(0.0f, -0.0f, -0.0f, 0.0f))
|
||||
assertEquals(-0.0f, minOf(-0.0f, 0.0f, 0.0f, -0.0f))
|
||||
assertEquals(Float.NEGATIVE_INFINITY, minOf(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.MAX_VALUE, Float.MIN_VALUE))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
194
libraries/stdlib/test/generated/_PrimitivesTest.kt
Normal file
194
libraries/stdlib/test/generated/_PrimitivesTest.kt
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.numbers
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLibTests.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import kotlin.random.*
|
||||
import kotlin.test.*
|
||||
|
||||
class _PrimitivesTest {
|
||||
@Test
|
||||
fun bits_Byte() {
|
||||
fun test(value: Byte, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Byte.SIZE_BITS) 1.shl(Byte.SIZE_BITS - leadingZeroes - 1).toByte() else 0
|
||||
val lowestBit = if (trailingZeroes < Byte.SIZE_BITS) 1.shl(trailingZeroes).toByte() else 0
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
test(0, 0, 8, 8)
|
||||
test(1, 1, 7, 0)
|
||||
test(2, 1, 6, 1)
|
||||
test(0x44, 2, 1, 2)
|
||||
test(0x80.toByte(), 1, 0, 7)
|
||||
test(0xF0.toByte(), 4, 0, 4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bits_Short() {
|
||||
fun test(value: Short, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Short.SIZE_BITS) 1.shl(Short.SIZE_BITS - leadingZeroes - 1).toShort() else 0
|
||||
val lowestBit = if (trailingZeroes < Short.SIZE_BITS) 1.shl(trailingZeroes).toShort() else 0
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
test(0, 0, 16, 16)
|
||||
test(1, 1, 15, 0)
|
||||
test(2, 1, 14, 1)
|
||||
test(0xF2, 5, 8, 1)
|
||||
test(0x8000.toShort(), 1, 0, 15)
|
||||
test(0xF200.toShort(), 5, 0, 9)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bits_Int() {
|
||||
fun test(value: Int, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Int.SIZE_BITS) 1.shl(Int.SIZE_BITS - leadingZeroes - 1).toInt() else 0
|
||||
val lowestBit = if (trailingZeroes < Int.SIZE_BITS) 1.shl(trailingZeroes).toInt() else 0
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
test(0, 0, 32, 32)
|
||||
test(1, 1, 31, 0)
|
||||
test(2, 1, 30, 1)
|
||||
test(0xF002, 5, 16, 1)
|
||||
test(0xF00F0000.toInt(), 8, 0, 16)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bits_Long() {
|
||||
fun test(value: Long, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Long.SIZE_BITS) 1L.shl(Long.SIZE_BITS - leadingZeroes - 1).toLong() else 0L
|
||||
val lowestBit = if (trailingZeroes < Long.SIZE_BITS) 1L.shl(trailingZeroes).toLong() else 0L
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
test(0L, 0, 64, 64)
|
||||
test(1L, 1, 63, 0)
|
||||
test(2L, 1, 62, 1)
|
||||
test(0xF002, 5, 48, 1)
|
||||
test(0xF00F0000L, 8, 32, 16)
|
||||
test(0x1111_3333_EEEE_0000L, 4 + 8 + 12, 3, 17)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rotate_Byte() {
|
||||
fun test(value: Byte, n: Int, expected: Byte) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
fun testCyclic(value: Byte) {
|
||||
for (n in -2 * Byte.SIZE_BITS..2 * Byte.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Byte.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Byte.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
test(0x73, 4, 0x37)
|
||||
test(0x73, -3, 0x6E)
|
||||
test(0x73, 1, 0xE6.toByte())
|
||||
test(0xE6.toByte(), 1, 0xCD.toByte())
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt().toByte())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rotate_Short() {
|
||||
fun test(value: Short, n: Int, expected: Short) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
fun testCyclic(value: Short) {
|
||||
for (n in -2 * Short.SIZE_BITS..2 * Short.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Short.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Short.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
test(0x7361, 4, 0x3617)
|
||||
test(0x7361, -3, 0b001_0111_0011_0110_0)
|
||||
test(0x7361, 1, 0b111_0011_0110_0001_0.toShort())
|
||||
test(0xE6C2.toShort(), 1, 0b11_0011_0110_0001_01.toShort())
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt().toShort())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rotate_Int() {
|
||||
fun test(value: Int, n: Int, expected: Int) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
fun testCyclic(value: Int) {
|
||||
for (n in -2 * Int.SIZE_BITS..2 * Int.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Int.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Int.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
test(0x7_3422345, 4, 0x3422345_7)
|
||||
test(0x7342234_5, -4, 0x5_7342234)
|
||||
test(0x73422345, 1, 0xE684468A.toInt())
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rotate_Long() {
|
||||
fun test(value: Long, n: Int, expected: Long) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
fun testCyclic(value: Long) {
|
||||
for (n in -2 * Long.SIZE_BITS..2 * Long.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Long.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Long.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
test(0x7372ABAC_DEEF0123, 4, 0x372ABAC_DEEF01237)
|
||||
test(0x88888888_44444444U.toLong(), -3, 0x91111111_08888888u.toLong())
|
||||
test(0x88888888_44444444U.toLong(), 1, 0x11111110_88888889)
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextLong())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
123
libraries/stdlib/test/generated/_SequencesTest.kt
Normal file
123
libraries/stdlib/test/generated/_SequencesTest.kt
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.sequences
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLibTests.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
|
||||
import test.collections.assertSorted
|
||||
import kotlin.test.*
|
||||
|
||||
class _SequencesTest {
|
||||
@Test
|
||||
fun foldIndexed_Sequence() {
|
||||
expect(8) { sequenceOf<Int>(1, 2, 3).foldIndexed(0) { i, acc, e -> acc + i.toInt() * e } }
|
||||
expect(10) { sequenceOf<Int>(1, 2, 3).foldIndexed(1) { i, acc, e -> acc + i + e.toInt() } }
|
||||
expect(15) { sequenceOf<Int>(1, 2, 3).foldIndexed(1) { i, acc, e -> acc * (i.toInt() + e) } }
|
||||
expect(" 0-1 1-2 2-3") { sequenceOf<Int>(1, 2, 3).foldIndexed("") { i, acc, e -> "$acc $i-$e" } }
|
||||
expect(42) {
|
||||
val numbers = sequenceOf<Int>(1, 2, 3, 4)
|
||||
numbers.foldIndexed(0) { index, a, b -> index.toInt() * (a + b) }
|
||||
}
|
||||
expect(0) {
|
||||
val numbers = sequenceOf<Int>()
|
||||
numbers.foldIndexed(0) { index, a, b -> index.toInt() * (a + b) }
|
||||
}
|
||||
expect("11234") {
|
||||
val numbers = sequenceOf<Int>(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxByOrNull_Sequence() {
|
||||
assertEquals(null, sequenceOf<Int>().maxByOrNull { it })
|
||||
assertEquals(1, sequenceOf<Int>(1).maxByOrNull { it })
|
||||
assertEquals(3, sequenceOf<Int>(3, 2).maxByOrNull { it * it })
|
||||
assertEquals(3, sequenceOf<Int>(3, 2).maxByOrNull { "a" })
|
||||
assertEquals(3, sequenceOf<Int>(3, 2).maxByOrNull { it.toString() })
|
||||
assertEquals(2, sequenceOf<Int>(2, 3).maxByOrNull { -it })
|
||||
assertEquals('b', sequenceOf('a', 'b').maxByOrNull { "x$it" })
|
||||
assertEquals("abc", sequenceOf("b", "abc").maxByOrNull { it.length })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxWithOrNull_Sequence() {
|
||||
assertEquals(null, sequenceOf<Int>().maxWithOrNull(naturalOrder()))
|
||||
assertEquals(1, sequenceOf<Int>(1).maxWithOrNull(naturalOrder()))
|
||||
assertEquals(3, sequenceOf<Int>(2, 3, 4).maxWithOrNull(compareBy { it % 4 }))
|
||||
assertEquals("B", sequenceOf("a", "B").maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minByOrNull_Sequence() {
|
||||
assertEquals(null, sequenceOf<Int>().minByOrNull { it })
|
||||
assertEquals(1, sequenceOf<Int>(1).minByOrNull { it })
|
||||
assertEquals(2, sequenceOf<Int>(3, 2).minByOrNull { it * it })
|
||||
assertEquals(3, sequenceOf<Int>(3, 2).minByOrNull { "a" })
|
||||
assertEquals(2, sequenceOf<Int>(3, 2).minByOrNull { it.toString() })
|
||||
assertEquals(3, sequenceOf<Int>(2, 3).minByOrNull { -it })
|
||||
assertEquals('a', sequenceOf('a', 'b').minByOrNull { "x$it" })
|
||||
assertEquals("b", sequenceOf("b", "abc").minByOrNull { it.length })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minWithOrNull_Sequence() {
|
||||
assertEquals(null, sequenceOf<Int>().minWithOrNull(naturalOrder()))
|
||||
assertEquals(1, sequenceOf<Int>(1).minWithOrNull(naturalOrder()))
|
||||
assertEquals(4, sequenceOf<Int>(2, 3, 4).minWithOrNull(compareBy { it % 4 }))
|
||||
assertEquals("a", sequenceOf("a", "B").minWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOf_Sequence() {
|
||||
expect(-1) { sequenceOf<Int>(1, 2, 3).indexOf(0) }
|
||||
expect(0) { sequenceOf<Int>(1, 2, 3).indexOf(1) }
|
||||
expect(1) { sequenceOf<Int>(1, 2, 3).indexOf(2) }
|
||||
expect(2) { sequenceOf<Int>(1, 2, 3).indexOf(3) }
|
||||
expect(-1) { sequenceOf("cat", "dog", "bird").indexOf("mouse") }
|
||||
expect(0) { sequenceOf("cat", "dog", "bird").indexOf("cat") }
|
||||
expect(1) { sequenceOf("cat", "dog", "bird").indexOf("dog") }
|
||||
expect(2) { sequenceOf("cat", "dog", "bird").indexOf("bird") }
|
||||
expect(0) { sequenceOf(null, "dog", null).indexOf(null as String?)}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfFirst_Sequence() {
|
||||
expect(-1) { sequenceOf<Int>(1, 2, 3).indexOfFirst { it == 0 } }
|
||||
expect(0) { sequenceOf<Int>(1, 2, 3).indexOfFirst { it % 2 == 1 } }
|
||||
expect(1) { sequenceOf<Int>(1, 2, 3).indexOfFirst { it % 2 == 0 } }
|
||||
expect(2) { sequenceOf<Int>(1, 2, 3).indexOfFirst { it == 3 } }
|
||||
expect(-1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
|
||||
expect(0) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
|
||||
expect(1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
|
||||
expect(2) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sorted_Sequence() {
|
||||
sequenceOf<Int>(3, 7, 1).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
sequenceOf(1, Int.MAX_VALUE, Int.MIN_VALUE).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
sequenceOf("ac", "aD", "aba").sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedDescending_Sequence() {
|
||||
sequenceOf<Int>(3, 7, 1).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
sequenceOf(1, Int.MAX_VALUE, Int.MIN_VALUE).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
sequenceOf("ac", "aD", "aba").sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedWith_Sequence() {
|
||||
val comparator = compareBy { it: Int -> it % 3 }.thenByDescending { it }
|
||||
sequenceOf<Int>(0, 1, 2, 3, 4, 5).sortedWith(comparator).iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
|
||||
}
|
||||
|
||||
}
|
||||
842
libraries/stdlib/test/generated/_UArraysTest.kt
Normal file
842
libraries/stdlib/test/generated/_UArraysTest.kt
Normal file
@@ -0,0 +1,842 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLibTests.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import kotlin.random.*
|
||||
import test.assertArrayContentEquals
|
||||
import kotlin.test.*
|
||||
|
||||
class _UArraysTest {
|
||||
@Test
|
||||
fun foldIndexed_UIntArray() {
|
||||
expect(8u) { uintArrayOf(1u, 2u, 3u).foldIndexed(0u) { i, acc, e -> acc + i.toUInt() * e } }
|
||||
expect(10) { uintArrayOf(1u, 2u, 3u).foldIndexed(1) { i, acc, e -> acc + i + e.toInt() } }
|
||||
expect(15u) { uintArrayOf(1u, 2u, 3u).foldIndexed(1u) { i, acc, e -> acc * (i.toUInt() + e) } }
|
||||
expect(" 0-1 1-2 2-3") { uintArrayOf(1u, 2u, 3u).foldIndexed("") { i, acc, e -> "$acc $i-$e" } }
|
||||
expect(42u) {
|
||||
val numbers = uintArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.foldIndexed(0u) { index, a, b -> index.toUInt() * (a + b) }
|
||||
}
|
||||
expect(0u) {
|
||||
val numbers = uintArrayOf()
|
||||
numbers.foldIndexed(0u) { index, a, b -> index.toUInt() * (a + b) }
|
||||
}
|
||||
expect("11234") {
|
||||
val numbers = uintArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldIndexed_ULongArray() {
|
||||
expect(8uL) { ulongArrayOf(1uL, 2uL, 3uL).foldIndexed(0uL) { i, acc, e -> acc + i.toULong() * e } }
|
||||
expect(10) { ulongArrayOf(1uL, 2uL, 3uL).foldIndexed(1) { i, acc, e -> acc + i + e.toInt() } }
|
||||
expect(15uL) { ulongArrayOf(1uL, 2uL, 3uL).foldIndexed(1uL) { i, acc, e -> acc * (i.toULong() + e) } }
|
||||
expect(" 0-1 1-2 2-3") { ulongArrayOf(1uL, 2uL, 3uL).foldIndexed("") { i, acc, e -> "$acc $i-$e" } }
|
||||
expect(42uL) {
|
||||
val numbers = ulongArrayOf(1uL, 2uL, 3uL, 4uL)
|
||||
numbers.foldIndexed(0uL) { index, a, b -> index.toULong() * (a + b) }
|
||||
}
|
||||
expect(0uL) {
|
||||
val numbers = ulongArrayOf()
|
||||
numbers.foldIndexed(0uL) { index, a, b -> index.toULong() * (a + b) }
|
||||
}
|
||||
expect("11234") {
|
||||
val numbers = ulongArrayOf(1uL, 2uL, 3uL, 4uL)
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldIndexed_UByteArray() {
|
||||
expect(8u) { ubyteArrayOf(1u, 2u, 3u).foldIndexed(0u) { i, acc, e -> acc + i.toUByte() * e } }
|
||||
expect(10) { ubyteArrayOf(1u, 2u, 3u).foldIndexed(1) { i, acc, e -> acc + i + e.toInt() } }
|
||||
expect(15u) { ubyteArrayOf(1u, 2u, 3u).foldIndexed(1u) { i, acc, e -> acc * (i.toUByte() + e) } }
|
||||
expect(" 0-1 1-2 2-3") { ubyteArrayOf(1u, 2u, 3u).foldIndexed("") { i, acc, e -> "$acc $i-$e" } }
|
||||
expect(42u) {
|
||||
val numbers = ubyteArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.foldIndexed(0u) { index, a, b -> index.toUByte() * (a + b) }
|
||||
}
|
||||
expect(0u) {
|
||||
val numbers = ubyteArrayOf()
|
||||
numbers.foldIndexed(0u) { index, a, b -> index.toUByte() * (a + b) }
|
||||
}
|
||||
expect("11234") {
|
||||
val numbers = ubyteArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldIndexed_UShortArray() {
|
||||
expect(8u) { ushortArrayOf(1u, 2u, 3u).foldIndexed(0u) { i, acc, e -> acc + i.toUShort() * e } }
|
||||
expect(10) { ushortArrayOf(1u, 2u, 3u).foldIndexed(1) { i, acc, e -> acc + i + e.toInt() } }
|
||||
expect(15u) { ushortArrayOf(1u, 2u, 3u).foldIndexed(1u) { i, acc, e -> acc * (i.toUShort() + e) } }
|
||||
expect(" 0-1 1-2 2-3") { ushortArrayOf(1u, 2u, 3u).foldIndexed("") { i, acc, e -> "$acc $i-$e" } }
|
||||
expect(42u) {
|
||||
val numbers = ushortArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.foldIndexed(0u) { index, a, b -> index.toUShort() * (a + b) }
|
||||
}
|
||||
expect(0u) {
|
||||
val numbers = ushortArrayOf()
|
||||
numbers.foldIndexed(0u) { index, a, b -> index.toUShort() * (a + b) }
|
||||
}
|
||||
expect("11234") {
|
||||
val numbers = ushortArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldRightIndexed_UIntArray() {
|
||||
expect(8u) { uintArrayOf(1u, 2u, 3u).foldRightIndexed(0u) { i, e, acc -> acc + i.toUInt() * e } }
|
||||
expect(10) { uintArrayOf(1u, 2u, 3u).foldRightIndexed(1) { i, e, acc -> acc + i + e.toInt() } }
|
||||
expect(15u) { uintArrayOf(1u, 2u, 3u).foldRightIndexed(1u) { i, e, acc -> acc * (i.toUInt() + e) } }
|
||||
expect(" 2-3 1-2 0-1") { uintArrayOf(1u, 2u, 3u).foldRightIndexed("") { i, e, acc -> "$acc $i-$e" } }
|
||||
expect("12343210") {
|
||||
val numbers = uintArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldRightIndexed_ULongArray() {
|
||||
expect(8uL) { ulongArrayOf(1uL, 2uL, 3uL).foldRightIndexed(0uL) { i, e, acc -> acc + i.toULong() * e } }
|
||||
expect(10) { ulongArrayOf(1uL, 2uL, 3uL).foldRightIndexed(1) { i, e, acc -> acc + i + e.toInt() } }
|
||||
expect(15uL) { ulongArrayOf(1uL, 2uL, 3uL).foldRightIndexed(1uL) { i, e, acc -> acc * (i.toULong() + e) } }
|
||||
expect(" 2-3 1-2 0-1") { ulongArrayOf(1uL, 2uL, 3uL).foldRightIndexed("") { i, e, acc -> "$acc $i-$e" } }
|
||||
expect("12343210") {
|
||||
val numbers = ulongArrayOf(1uL, 2uL, 3uL, 4uL)
|
||||
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldRightIndexed_UByteArray() {
|
||||
expect(8u) { ubyteArrayOf(1u, 2u, 3u).foldRightIndexed(0u) { i, e, acc -> acc + i.toUByte() * e } }
|
||||
expect(10) { ubyteArrayOf(1u, 2u, 3u).foldRightIndexed(1) { i, e, acc -> acc + i + e.toInt() } }
|
||||
expect(15u) { ubyteArrayOf(1u, 2u, 3u).foldRightIndexed(1u) { i, e, acc -> acc * (i.toUByte() + e) } }
|
||||
expect(" 2-3 1-2 0-1") { ubyteArrayOf(1u, 2u, 3u).foldRightIndexed("") { i, e, acc -> "$acc $i-$e" } }
|
||||
expect("12343210") {
|
||||
val numbers = ubyteArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun foldRightIndexed_UShortArray() {
|
||||
expect(8u) { ushortArrayOf(1u, 2u, 3u).foldRightIndexed(0u) { i, e, acc -> acc + i.toUShort() * e } }
|
||||
expect(10) { ushortArrayOf(1u, 2u, 3u).foldRightIndexed(1) { i, e, acc -> acc + i + e.toInt() } }
|
||||
expect(15u) { ushortArrayOf(1u, 2u, 3u).foldRightIndexed(1u) { i, e, acc -> acc * (i.toUShort() + e) } }
|
||||
expect(" 2-3 1-2 0-1") { ushortArrayOf(1u, 2u, 3u).foldRightIndexed("") { i, e, acc -> "$acc $i-$e" } }
|
||||
expect("12343210") {
|
||||
val numbers = ushortArrayOf(1u, 2u, 3u, 4u)
|
||||
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxByOrNull_UIntArray() {
|
||||
assertEquals(null, uintArrayOf().maxByOrNull { it })
|
||||
assertEquals(1u, uintArrayOf(1u).maxByOrNull { it })
|
||||
assertEquals(3u, uintArrayOf(3u, 2u).maxByOrNull { it * it })
|
||||
assertEquals(3u, uintArrayOf(3u, 2u).maxByOrNull { "a" })
|
||||
assertEquals(3u, uintArrayOf(3u, 2u).maxByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxByOrNull_ULongArray() {
|
||||
assertEquals(null, ulongArrayOf().maxByOrNull { it })
|
||||
assertEquals(1uL, ulongArrayOf(1uL).maxByOrNull { it })
|
||||
assertEquals(3uL, ulongArrayOf(3uL, 2uL).maxByOrNull { it * it })
|
||||
assertEquals(3uL, ulongArrayOf(3uL, 2uL).maxByOrNull { "a" })
|
||||
assertEquals(3uL, ulongArrayOf(3uL, 2uL).maxByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxByOrNull_UByteArray() {
|
||||
assertEquals(null, ubyteArrayOf().maxByOrNull { it })
|
||||
assertEquals(1u, ubyteArrayOf(1u).maxByOrNull { it })
|
||||
assertEquals(3u, ubyteArrayOf(3u, 2u).maxByOrNull { it * it })
|
||||
assertEquals(3u, ubyteArrayOf(3u, 2u).maxByOrNull { "a" })
|
||||
assertEquals(3u, ubyteArrayOf(3u, 2u).maxByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxByOrNull_UShortArray() {
|
||||
assertEquals(null, ushortArrayOf().maxByOrNull { it })
|
||||
assertEquals(1u, ushortArrayOf(1u).maxByOrNull { it })
|
||||
assertEquals(3u, ushortArrayOf(3u, 2u).maxByOrNull { it * it })
|
||||
assertEquals(3u, ushortArrayOf(3u, 2u).maxByOrNull { "a" })
|
||||
assertEquals(3u, ushortArrayOf(3u, 2u).maxByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxWithOrNull_UIntArray() {
|
||||
assertEquals(null, uintArrayOf().maxWithOrNull(naturalOrder()))
|
||||
assertEquals(1u, uintArrayOf(1u).maxWithOrNull(naturalOrder()))
|
||||
assertEquals(3u, uintArrayOf(2u, 3u, 4u).maxWithOrNull(compareBy { it % 4u }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxWithOrNull_ULongArray() {
|
||||
assertEquals(null, ulongArrayOf().maxWithOrNull(naturalOrder()))
|
||||
assertEquals(1uL, ulongArrayOf(1uL).maxWithOrNull(naturalOrder()))
|
||||
assertEquals(3uL, ulongArrayOf(2uL, 3uL, 4uL).maxWithOrNull(compareBy { it % 4uL }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxWithOrNull_UByteArray() {
|
||||
assertEquals(null, ubyteArrayOf().maxWithOrNull(naturalOrder()))
|
||||
assertEquals(1u, ubyteArrayOf(1u).maxWithOrNull(naturalOrder()))
|
||||
assertEquals(3u, ubyteArrayOf(2u, 3u, 4u).maxWithOrNull(compareBy { it % 4u }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxWithOrNull_UShortArray() {
|
||||
assertEquals(null, ushortArrayOf().maxWithOrNull(naturalOrder()))
|
||||
assertEquals(1u, ushortArrayOf(1u).maxWithOrNull(naturalOrder()))
|
||||
assertEquals(3u, ushortArrayOf(2u, 3u, 4u).maxWithOrNull(compareBy { it % 4u }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minByOrNull_UIntArray() {
|
||||
assertEquals(null, uintArrayOf().minByOrNull { it })
|
||||
assertEquals(1u, uintArrayOf(1u).minByOrNull { it })
|
||||
assertEquals(2u, uintArrayOf(3u, 2u).minByOrNull { it * it })
|
||||
assertEquals(3u, uintArrayOf(3u, 2u).minByOrNull { "a" })
|
||||
assertEquals(2u, uintArrayOf(3u, 2u).minByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minByOrNull_ULongArray() {
|
||||
assertEquals(null, ulongArrayOf().minByOrNull { it })
|
||||
assertEquals(1uL, ulongArrayOf(1uL).minByOrNull { it })
|
||||
assertEquals(2uL, ulongArrayOf(3uL, 2uL).minByOrNull { it * it })
|
||||
assertEquals(3uL, ulongArrayOf(3uL, 2uL).minByOrNull { "a" })
|
||||
assertEquals(2uL, ulongArrayOf(3uL, 2uL).minByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minByOrNull_UByteArray() {
|
||||
assertEquals(null, ubyteArrayOf().minByOrNull { it })
|
||||
assertEquals(1u, ubyteArrayOf(1u).minByOrNull { it })
|
||||
assertEquals(2u, ubyteArrayOf(3u, 2u).minByOrNull { it * it })
|
||||
assertEquals(3u, ubyteArrayOf(3u, 2u).minByOrNull { "a" })
|
||||
assertEquals(2u, ubyteArrayOf(3u, 2u).minByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minByOrNull_UShortArray() {
|
||||
assertEquals(null, ushortArrayOf().minByOrNull { it })
|
||||
assertEquals(1u, ushortArrayOf(1u).minByOrNull { it })
|
||||
assertEquals(2u, ushortArrayOf(3u, 2u).minByOrNull { it * it })
|
||||
assertEquals(3u, ushortArrayOf(3u, 2u).minByOrNull { "a" })
|
||||
assertEquals(2u, ushortArrayOf(3u, 2u).minByOrNull { it.toString() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minWithOrNull_UIntArray() {
|
||||
assertEquals(null, uintArrayOf().minWithOrNull(naturalOrder()))
|
||||
assertEquals(1u, uintArrayOf(1u).minWithOrNull(naturalOrder()))
|
||||
assertEquals(4u, uintArrayOf(2u, 3u, 4u).minWithOrNull(compareBy { it % 4u }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minWithOrNull_ULongArray() {
|
||||
assertEquals(null, ulongArrayOf().minWithOrNull(naturalOrder()))
|
||||
assertEquals(1uL, ulongArrayOf(1uL).minWithOrNull(naturalOrder()))
|
||||
assertEquals(4uL, ulongArrayOf(2uL, 3uL, 4uL).minWithOrNull(compareBy { it % 4uL }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minWithOrNull_UByteArray() {
|
||||
assertEquals(null, ubyteArrayOf().minWithOrNull(naturalOrder()))
|
||||
assertEquals(1u, ubyteArrayOf(1u).minWithOrNull(naturalOrder()))
|
||||
assertEquals(4u, ubyteArrayOf(2u, 3u, 4u).minWithOrNull(compareBy { it % 4u }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minWithOrNull_UShortArray() {
|
||||
assertEquals(null, ushortArrayOf().minWithOrNull(naturalOrder()))
|
||||
assertEquals(1u, ushortArrayOf(1u).minWithOrNull(naturalOrder()))
|
||||
assertEquals(4u, ushortArrayOf(2u, 3u, 4u).minWithOrNull(compareBy { it % 4u }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOf_UIntArray() {
|
||||
expect(-1) { uintArrayOf(1u, 2u, 3u).indexOf(0u) }
|
||||
expect(0) { uintArrayOf(1u, 2u, 3u).indexOf(1u) }
|
||||
expect(1) { uintArrayOf(1u, 2u, 3u).indexOf(2u) }
|
||||
expect(2) { uintArrayOf(1u, 2u, 3u).indexOf(3u) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOf_ULongArray() {
|
||||
expect(-1) { ulongArrayOf(1uL, 2uL, 3uL).indexOf(0uL) }
|
||||
expect(0) { ulongArrayOf(1uL, 2uL, 3uL).indexOf(1uL) }
|
||||
expect(1) { ulongArrayOf(1uL, 2uL, 3uL).indexOf(2uL) }
|
||||
expect(2) { ulongArrayOf(1uL, 2uL, 3uL).indexOf(3uL) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOf_UByteArray() {
|
||||
expect(-1) { ubyteArrayOf(1u, 2u, 3u).indexOf(0u) }
|
||||
expect(0) { ubyteArrayOf(1u, 2u, 3u).indexOf(1u) }
|
||||
expect(1) { ubyteArrayOf(1u, 2u, 3u).indexOf(2u) }
|
||||
expect(2) { ubyteArrayOf(1u, 2u, 3u).indexOf(3u) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOf_UShortArray() {
|
||||
expect(-1) { ushortArrayOf(1u, 2u, 3u).indexOf(0u) }
|
||||
expect(0) { ushortArrayOf(1u, 2u, 3u).indexOf(1u) }
|
||||
expect(1) { ushortArrayOf(1u, 2u, 3u).indexOf(2u) }
|
||||
expect(2) { ushortArrayOf(1u, 2u, 3u).indexOf(3u) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfFirst_UIntArray() {
|
||||
expect(-1) { uintArrayOf(1u, 2u, 3u).indexOfFirst { it == 0u } }
|
||||
expect(0) { uintArrayOf(1u, 2u, 3u).indexOfFirst { it % 2u == 1u } }
|
||||
expect(1) { uintArrayOf(1u, 2u, 3u).indexOfFirst { it % 2u == 0u } }
|
||||
expect(2) { uintArrayOf(1u, 2u, 3u).indexOfFirst { it == 3u } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfFirst_ULongArray() {
|
||||
expect(-1) { ulongArrayOf(1uL, 2uL, 3uL).indexOfFirst { it == 0uL } }
|
||||
expect(0) { ulongArrayOf(1uL, 2uL, 3uL).indexOfFirst { it % 2uL == 1uL } }
|
||||
expect(1) { ulongArrayOf(1uL, 2uL, 3uL).indexOfFirst { it % 2uL == 0uL } }
|
||||
expect(2) { ulongArrayOf(1uL, 2uL, 3uL).indexOfFirst { it == 3uL } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfFirst_UByteArray() {
|
||||
expect(-1) { ubyteArrayOf(1u, 2u, 3u).indexOfFirst { it == 0.toUByte() } }
|
||||
expect(0) { ubyteArrayOf(1u, 2u, 3u).indexOfFirst { it % 2u == 1u } }
|
||||
expect(1) { ubyteArrayOf(1u, 2u, 3u).indexOfFirst { it % 2u == 0u } }
|
||||
expect(2) { ubyteArrayOf(1u, 2u, 3u).indexOfFirst { it == 3.toUByte() } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfFirst_UShortArray() {
|
||||
expect(-1) { ushortArrayOf(1u, 2u, 3u).indexOfFirst { it == 0.toUShort() } }
|
||||
expect(0) { ushortArrayOf(1u, 2u, 3u).indexOfFirst { it % 2u == 1u } }
|
||||
expect(1) { ushortArrayOf(1u, 2u, 3u).indexOfFirst { it % 2u == 0u } }
|
||||
expect(2) { ushortArrayOf(1u, 2u, 3u).indexOfFirst { it == 3.toUShort() } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyInto_UIntArray() {
|
||||
val dest = uintArrayOf(1u, 2u, 3u)
|
||||
val newValues = uintArrayOf(4u, 5u, 6u)
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
val result1 = uintArrayOf(5u, 6u, 3u)
|
||||
assertTrue(result1 contentEquals dest, "Copying from newValues: ${result1.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
val result2 = uintArrayOf(6u, 3u, 3u)
|
||||
assertTrue(result2 contentEquals dest, "Overlapping backward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
val result3 = uintArrayOf(6u, 6u, 3u)
|
||||
assertTrue(result3 contentEquals dest, "Overlapping forward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0, 0 to -1)) {
|
||||
val bounds = "start: $start, end: $end"
|
||||
val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) }
|
||||
assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: $ex")
|
||||
}
|
||||
for (destIndex in listOf(-1, 2, 4)) {
|
||||
assertFailsWith<IndexOutOfBoundsException>("index: $destIndex") { newValues.copyInto(dest, destIndex, 0, 2) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyInto_ULongArray() {
|
||||
val dest = ulongArrayOf(1uL, 2uL, 3uL)
|
||||
val newValues = ulongArrayOf(4uL, 5uL, 6uL)
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
val result1 = ulongArrayOf(5uL, 6uL, 3uL)
|
||||
assertTrue(result1 contentEquals dest, "Copying from newValues: ${result1.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
val result2 = ulongArrayOf(6uL, 3uL, 3uL)
|
||||
assertTrue(result2 contentEquals dest, "Overlapping backward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
val result3 = ulongArrayOf(6uL, 6uL, 3uL)
|
||||
assertTrue(result3 contentEquals dest, "Overlapping forward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0, 0 to -1)) {
|
||||
val bounds = "start: $start, end: $end"
|
||||
val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) }
|
||||
assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: $ex")
|
||||
}
|
||||
for (destIndex in listOf(-1, 2, 4)) {
|
||||
assertFailsWith<IndexOutOfBoundsException>("index: $destIndex") { newValues.copyInto(dest, destIndex, 0, 2) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyInto_UByteArray() {
|
||||
val dest = ubyteArrayOf(1u, 2u, 3u)
|
||||
val newValues = ubyteArrayOf(4u, 5u, 6u)
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
val result1 = ubyteArrayOf(5u, 6u, 3u)
|
||||
assertTrue(result1 contentEquals dest, "Copying from newValues: ${result1.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
val result2 = ubyteArrayOf(6u, 3u, 3u)
|
||||
assertTrue(result2 contentEquals dest, "Overlapping backward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
val result3 = ubyteArrayOf(6u, 6u, 3u)
|
||||
assertTrue(result3 contentEquals dest, "Overlapping forward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0, 0 to -1)) {
|
||||
val bounds = "start: $start, end: $end"
|
||||
val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) }
|
||||
assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: $ex")
|
||||
}
|
||||
for (destIndex in listOf(-1, 2, 4)) {
|
||||
assertFailsWith<IndexOutOfBoundsException>("index: $destIndex") { newValues.copyInto(dest, destIndex, 0, 2) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyInto_UShortArray() {
|
||||
val dest = ushortArrayOf(1u, 2u, 3u)
|
||||
val newValues = ushortArrayOf(4u, 5u, 6u)
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
val result1 = ushortArrayOf(5u, 6u, 3u)
|
||||
assertTrue(result1 contentEquals dest, "Copying from newValues: ${result1.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
val result2 = ushortArrayOf(6u, 3u, 3u)
|
||||
assertTrue(result2 contentEquals dest, "Overlapping backward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
val result3 = ushortArrayOf(6u, 6u, 3u)
|
||||
assertTrue(result3 contentEquals dest, "Overlapping forward copy: ${result2.contentToString()}, ${dest.contentToString()}")
|
||||
for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0, 0 to -1)) {
|
||||
val bounds = "start: $start, end: $end"
|
||||
val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) }
|
||||
assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: $ex")
|
||||
}
|
||||
for (destIndex in listOf(-1, 2, 4)) {
|
||||
assertFailsWith<IndexOutOfBoundsException>("index: $destIndex") { newValues.copyInto(dest, destIndex, 0, 2) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverse_UIntArray() {
|
||||
val arrays = (0..4).map { n -> (1..n).map { it.toUInt() }.toUIntArray() }
|
||||
for (array in arrays) {
|
||||
val original = array.toList()
|
||||
array.reverse()
|
||||
val reversed = array.toList()
|
||||
assertEquals(original.asReversed(), reversed)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverse_ULongArray() {
|
||||
val arrays = (0..4).map { n -> (1..n).map { it.toULong() }.toULongArray() }
|
||||
for (array in arrays) {
|
||||
val original = array.toList()
|
||||
array.reverse()
|
||||
val reversed = array.toList()
|
||||
assertEquals(original.asReversed(), reversed)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverse_UByteArray() {
|
||||
val arrays = (0..4).map { n -> (1..n).map { it.toUByte() }.toUByteArray() }
|
||||
for (array in arrays) {
|
||||
val original = array.toList()
|
||||
array.reverse()
|
||||
val reversed = array.toList()
|
||||
assertEquals(original.asReversed(), reversed)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverse_UShortArray() {
|
||||
val arrays = (0..4).map { n -> (1..n).map { it.toUShort() }.toUShortArray() }
|
||||
for (array in arrays) {
|
||||
val original = array.toList()
|
||||
array.reverse()
|
||||
val reversed = array.toList()
|
||||
assertEquals(original.asReversed(), reversed)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverseRange_UIntArray() {
|
||||
val arrays = (0..7).map { n -> n to (0 until n).map { it.toUInt() }.toUIntArray() }
|
||||
for ((size, array) in arrays) {
|
||||
for (fromIndex in 0 until size) {
|
||||
for (toIndex in fromIndex..size) {
|
||||
val original = array.toMutableList()
|
||||
array.reverse(fromIndex, toIndex)
|
||||
val reversed = array.toMutableList()
|
||||
assertEquals(original.apply { subList(fromIndex, toIndex).reverse() }, reversed)
|
||||
}
|
||||
}
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(-1, size) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(0, size + 1) }
|
||||
assertFailsWith<IllegalArgumentException> { array.reverse(0, -1) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverseRange_ULongArray() {
|
||||
val arrays = (0..7).map { n -> n to (0 until n).map { it.toULong() }.toULongArray() }
|
||||
for ((size, array) in arrays) {
|
||||
for (fromIndex in 0 until size) {
|
||||
for (toIndex in fromIndex..size) {
|
||||
val original = array.toMutableList()
|
||||
array.reverse(fromIndex, toIndex)
|
||||
val reversed = array.toMutableList()
|
||||
assertEquals(original.apply { subList(fromIndex, toIndex).reverse() }, reversed)
|
||||
}
|
||||
}
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(-1, size) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(0, size + 1) }
|
||||
assertFailsWith<IllegalArgumentException> { array.reverse(0, -1) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverseRange_UByteArray() {
|
||||
val arrays = (0..7).map { n -> n to (0 until n).map { it.toUByte() }.toUByteArray() }
|
||||
for ((size, array) in arrays) {
|
||||
for (fromIndex in 0 until size) {
|
||||
for (toIndex in fromIndex..size) {
|
||||
val original = array.toMutableList()
|
||||
array.reverse(fromIndex, toIndex)
|
||||
val reversed = array.toMutableList()
|
||||
assertEquals(original.apply { subList(fromIndex, toIndex).reverse() }, reversed)
|
||||
}
|
||||
}
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(-1, size) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(0, size + 1) }
|
||||
assertFailsWith<IllegalArgumentException> { array.reverse(0, -1) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reverseRange_UShortArray() {
|
||||
val arrays = (0..7).map { n -> n to (0 until n).map { it.toUShort() }.toUShortArray() }
|
||||
for ((size, array) in arrays) {
|
||||
for (fromIndex in 0 until size) {
|
||||
for (toIndex in fromIndex..size) {
|
||||
val original = array.toMutableList()
|
||||
array.reverse(fromIndex, toIndex)
|
||||
val reversed = array.toMutableList()
|
||||
assertEquals(original.apply { subList(fromIndex, toIndex).reverse() }, reversed)
|
||||
}
|
||||
}
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(-1, size) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(0, size + 1) }
|
||||
assertFailsWith<IllegalArgumentException> { array.reverse(0, -1) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversed_UIntArray() {
|
||||
assertEquals(listOf<UInt>(3u, 2u, 1u), uintArrayOf(1u, 2u, 3u).reversed())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversed_ULongArray() {
|
||||
assertEquals(listOf<ULong>(3uL, 2uL, 1uL), ulongArrayOf(1uL, 2uL, 3uL).reversed())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversed_UByteArray() {
|
||||
assertEquals(listOf<UByte>(3u, 2u, 1u), ubyteArrayOf(1u, 2u, 3u).reversed())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversed_UShortArray() {
|
||||
assertEquals(listOf<UShort>(3u, 2u, 1u), ushortArrayOf(1u, 2u, 3u).reversed())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversedArray_UIntArray() {
|
||||
assertArrayContentEquals(uintArrayOf(3u, 2u, 1u), uintArrayOf(1u, 2u, 3u).reversedArray())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversedArray_ULongArray() {
|
||||
assertArrayContentEquals(ulongArrayOf(3uL, 2uL, 1uL), ulongArrayOf(1uL, 2uL, 3uL).reversedArray())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversedArray_UByteArray() {
|
||||
assertArrayContentEquals(ubyteArrayOf(3u, 2u, 1u), ubyteArrayOf(1u, 2u, 3u).reversedArray())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reversedArray_UShortArray() {
|
||||
assertArrayContentEquals(ushortArrayOf(3u, 2u, 1u), ushortArrayOf(1u, 2u, 3u).reversedArray())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffle_UIntArray() {
|
||||
fun test(data: UIntArray) {
|
||||
val original = data.toMutableList()
|
||||
data.shuffle()
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(original.groupBy { it }, shuffled.groupBy { it })
|
||||
}
|
||||
test(UIntArray(100) { it.toUInt() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffle_ULongArray() {
|
||||
fun test(data: ULongArray) {
|
||||
val original = data.toMutableList()
|
||||
data.shuffle()
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(original.groupBy { it }, shuffled.groupBy { it })
|
||||
}
|
||||
test(ULongArray(100) { it.toULong() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffle_UByteArray() {
|
||||
fun test(data: UByteArray) {
|
||||
val original = data.toMutableList()
|
||||
data.shuffle()
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(original.groupBy { it }, shuffled.groupBy { it })
|
||||
}
|
||||
test(UByteArray(100) { it.toUByte() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffle_UShortArray() {
|
||||
fun test(data: UShortArray) {
|
||||
val original = data.toMutableList()
|
||||
data.shuffle()
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(original.groupBy { it }, shuffled.groupBy { it })
|
||||
}
|
||||
test(UShortArray(100) { it.toUShort() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffleRandom_UIntArray() {
|
||||
fun test(data: UIntArray) {
|
||||
val seed = Random.nextInt()
|
||||
val original = data.toMutableList()
|
||||
val originalShuffled = original.shuffled(Random(seed))
|
||||
data.shuffle(Random(seed))
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(originalShuffled, shuffled)
|
||||
}
|
||||
test(UIntArray(16) { it.toUInt() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffleRandom_ULongArray() {
|
||||
fun test(data: ULongArray) {
|
||||
val seed = Random.nextInt()
|
||||
val original = data.toMutableList()
|
||||
val originalShuffled = original.shuffled(Random(seed))
|
||||
data.shuffle(Random(seed))
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(originalShuffled, shuffled)
|
||||
}
|
||||
test(ULongArray(16) { it.toULong() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffleRandom_UByteArray() {
|
||||
fun test(data: UByteArray) {
|
||||
val seed = Random.nextInt()
|
||||
val original = data.toMutableList()
|
||||
val originalShuffled = original.shuffled(Random(seed))
|
||||
data.shuffle(Random(seed))
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(originalShuffled, shuffled)
|
||||
}
|
||||
test(UByteArray(16) { it.toUByte() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shuffleRandom_UShortArray() {
|
||||
fun test(data: UShortArray) {
|
||||
val seed = Random.nextInt()
|
||||
val original = data.toMutableList()
|
||||
val originalShuffled = original.shuffled(Random(seed))
|
||||
data.shuffle(Random(seed))
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(originalShuffled, shuffled)
|
||||
}
|
||||
test(UShortArray(16) { it.toUShort() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sort_UIntArray() {
|
||||
val data = uintArrayOf(5u, 2u, 1u, 9u, 80u, UInt.MIN_VALUE, UInt.MAX_VALUE)
|
||||
data.sort()
|
||||
assertArrayContentEquals(uintArrayOf(UInt.MIN_VALUE, 1u, 2u, 5u, 9u, 80u, UInt.MAX_VALUE), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sort_ULongArray() {
|
||||
val data = ulongArrayOf(5uL, 2uL, 1uL, 9uL, 80uL, ULong.MIN_VALUE, ULong.MAX_VALUE)
|
||||
data.sort()
|
||||
assertArrayContentEquals(ulongArrayOf(ULong.MIN_VALUE, 1uL, 2uL, 5uL, 9uL, 80uL, ULong.MAX_VALUE), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sort_UByteArray() {
|
||||
val data = ubyteArrayOf(5u, 2u, 1u, 9u, 80u, UByte.MIN_VALUE, UByte.MAX_VALUE)
|
||||
data.sort()
|
||||
assertArrayContentEquals(ubyteArrayOf(UByte.MIN_VALUE, 1u, 2u, 5u, 9u, 80u, UByte.MAX_VALUE), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sort_UShortArray() {
|
||||
val data = ushortArrayOf(5u, 2u, 1u, 9u, 80u, UShort.MIN_VALUE, UShort.MAX_VALUE)
|
||||
data.sort()
|
||||
assertArrayContentEquals(ushortArrayOf(UShort.MIN_VALUE, 1u, 2u, 5u, 9u, 80u, UShort.MAX_VALUE), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortDescending_UIntArray() {
|
||||
val data = uintArrayOf(5u, 2u, 1u, 9u, 80u, UInt.MIN_VALUE, UInt.MAX_VALUE)
|
||||
data.sortDescending()
|
||||
assertArrayContentEquals(uintArrayOf(UInt.MIN_VALUE, 1u, 2u, 5u, 9u, 80u, UInt.MAX_VALUE).reversedArray(), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortDescending_ULongArray() {
|
||||
val data = ulongArrayOf(5uL, 2uL, 1uL, 9uL, 80uL, ULong.MIN_VALUE, ULong.MAX_VALUE)
|
||||
data.sortDescending()
|
||||
assertArrayContentEquals(ulongArrayOf(ULong.MIN_VALUE, 1uL, 2uL, 5uL, 9uL, 80uL, ULong.MAX_VALUE).reversedArray(), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortDescending_UByteArray() {
|
||||
val data = ubyteArrayOf(5u, 2u, 1u, 9u, 80u, UByte.MIN_VALUE, UByte.MAX_VALUE)
|
||||
data.sortDescending()
|
||||
assertArrayContentEquals(ubyteArrayOf(UByte.MIN_VALUE, 1u, 2u, 5u, 9u, 80u, UByte.MAX_VALUE).reversedArray(), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortDescending_UShortArray() {
|
||||
val data = ushortArrayOf(5u, 2u, 1u, 9u, 80u, UShort.MIN_VALUE, UShort.MAX_VALUE)
|
||||
data.sortDescending()
|
||||
assertArrayContentEquals(ushortArrayOf(UShort.MIN_VALUE, 1u, 2u, 5u, 9u, 80u, UShort.MAX_VALUE).reversedArray(), data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sorted_UIntArray() {
|
||||
uintArrayOf(3u, 7u, 1u).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sorted_ULongArray() {
|
||||
ulongArrayOf(3uL, 7uL, 1uL).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sorted_UByteArray() {
|
||||
ubyteArrayOf(3u, 7u, 1u).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sorted_UShortArray() {
|
||||
ushortArrayOf(3u, 7u, 1u).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).sorted().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArray_UIntArray() {
|
||||
uintArrayOf(3u, 7u, 1u).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArray_ULongArray() {
|
||||
ulongArrayOf(3uL, 7uL, 1uL).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArray_UByteArray() {
|
||||
ubyteArrayOf(3u, 7u, 1u).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArray_UShortArray() {
|
||||
ushortArrayOf(3u, 7u, 1u).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).sortedArray().iterator().assertSorted { a, b -> a.compareTo(b) <= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArrayDescending_UIntArray() {
|
||||
uintArrayOf(3u, 7u, 1u).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArrayDescending_ULongArray() {
|
||||
ulongArrayOf(3uL, 7uL, 1uL).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArrayDescending_UByteArray() {
|
||||
ubyteArrayOf(3u, 7u, 1u).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedArrayDescending_UShortArray() {
|
||||
ushortArrayOf(3u, 7u, 1u).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).sortedArrayDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedDescending_UIntArray() {
|
||||
uintArrayOf(3u, 7u, 1u).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedDescending_ULongArray() {
|
||||
ulongArrayOf(3uL, 7uL, 1uL).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedDescending_UByteArray() {
|
||||
ubyteArrayOf(3u, 7u, 1u).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortedDescending_UShortArray() {
|
||||
ushortArrayOf(3u, 7u, 1u).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).sortedDescending().iterator().assertSorted { a, b -> a.compareTo(b) >= 0 }
|
||||
}
|
||||
|
||||
}
|
||||
249
libraries/stdlib/test/generated/_UComparisonsTest.kt
Normal file
249
libraries/stdlib/test/generated/_UComparisonsTest.kt
Normal file
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.unsigned
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLibTests.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import kotlin.random.*
|
||||
import kotlin.test.*
|
||||
|
||||
class _UComparisonsTest {
|
||||
@Test
|
||||
fun maxOf_2_UInt() {
|
||||
expect(2u) { maxOf(2u, 1u) }
|
||||
expect(126u) { maxOf(58u, 126u) }
|
||||
expect(23u) { maxOf(Random.nextUInt(UInt.MIN_VALUE, 23u), 23u) }
|
||||
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE) }
|
||||
expect(UInt.MIN_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_ULong() {
|
||||
expect(2uL) { maxOf(2uL, 1uL) }
|
||||
expect(126uL) { maxOf(58uL, 126uL) }
|
||||
expect(23uL) { maxOf(Random.nextULong(ULong.MIN_VALUE, 23uL), 23uL) }
|
||||
expect(ULong.MAX_VALUE) { maxOf(ULong.MIN_VALUE, ULong.MAX_VALUE) }
|
||||
expect(ULong.MIN_VALUE) { maxOf(ULong.MIN_VALUE, ULong.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_UByte() {
|
||||
expect(2.toUByte()) { maxOf(2.toUByte(), 1.toUByte()) }
|
||||
expect(126.toUByte()) { maxOf(58.toUByte(), 126.toUByte()) }
|
||||
expect(23.toUByte()) { maxOf(Random.nextInt(UByte.MIN_VALUE.toInt(), 23).toUByte(), 23.toUByte()) }
|
||||
expect(UByte.MAX_VALUE) { maxOf(UByte.MIN_VALUE, UByte.MAX_VALUE) }
|
||||
expect(UByte.MIN_VALUE) { maxOf(UByte.MIN_VALUE, UByte.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2_UShort() {
|
||||
expect(2.toUShort()) { maxOf(2.toUShort(), 1.toUShort()) }
|
||||
expect(126.toUShort()) { maxOf(58.toUShort(), 126.toUShort()) }
|
||||
expect(23.toUShort()) { maxOf(Random.nextInt(UShort.MIN_VALUE.toInt(), 23).toUShort(), 23.toUShort()) }
|
||||
expect(UShort.MAX_VALUE) { maxOf(UShort.MIN_VALUE, UShort.MAX_VALUE) }
|
||||
expect(UShort.MIN_VALUE) { maxOf(UShort.MIN_VALUE, UShort.MIN_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_UInt() {
|
||||
expect(3u) { maxOf(2u, 1u, 3u) }
|
||||
expect(126u) { maxOf(58u, 126u, 55u) }
|
||||
expect(23u) { maxOf(Random.nextUInt(UInt.MIN_VALUE, 23u), 23u, Random.nextUInt(UInt.MIN_VALUE, 23u)) }
|
||||
expect(UInt.MIN_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MIN_VALUE, UInt.MIN_VALUE) }
|
||||
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_ULong() {
|
||||
expect(3uL) { maxOf(2uL, 1uL, 3uL) }
|
||||
expect(126uL) { maxOf(58uL, 126uL, 55uL) }
|
||||
expect(23uL) { maxOf(Random.nextULong(ULong.MIN_VALUE, 23uL), 23uL, Random.nextULong(ULong.MIN_VALUE, 23uL)) }
|
||||
expect(ULong.MIN_VALUE) { maxOf(ULong.MIN_VALUE, ULong.MIN_VALUE, ULong.MIN_VALUE) }
|
||||
expect(ULong.MAX_VALUE) { maxOf(ULong.MIN_VALUE, ULong.MAX_VALUE, 0uL) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_UByte() {
|
||||
expect(3.toUByte()) { maxOf(2.toUByte(), 1.toUByte(), 3.toUByte()) }
|
||||
expect(126.toUByte()) { maxOf(58.toUByte(), 126.toUByte(), 55.toUByte()) }
|
||||
expect(23.toUByte()) { maxOf(Random.nextInt(UByte.MIN_VALUE.toInt(), 23).toUByte(), 23.toUByte(), Random.nextInt(UByte.MIN_VALUE.toInt(), 23).toUByte()) }
|
||||
expect(UByte.MIN_VALUE) { maxOf(UByte.MIN_VALUE, UByte.MIN_VALUE, UByte.MIN_VALUE) }
|
||||
expect(UByte.MAX_VALUE) { maxOf(UByte.MIN_VALUE, UByte.MAX_VALUE, 0.toUByte()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3_UShort() {
|
||||
expect(3.toUShort()) { maxOf(2.toUShort(), 1.toUShort(), 3.toUShort()) }
|
||||
expect(126.toUShort()) { maxOf(58.toUShort(), 126.toUShort(), 55.toUShort()) }
|
||||
expect(23.toUShort()) { maxOf(Random.nextInt(UShort.MIN_VALUE.toInt(), 23).toUShort(), 23.toUShort(), Random.nextInt(UShort.MIN_VALUE.toInt(), 23).toUShort()) }
|
||||
expect(UShort.MIN_VALUE) { maxOf(UShort.MIN_VALUE, UShort.MIN_VALUE, UShort.MIN_VALUE) }
|
||||
expect(UShort.MAX_VALUE) { maxOf(UShort.MIN_VALUE, UShort.MAX_VALUE, 0.toUShort()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_UInt() {
|
||||
expect(10u) { maxOf(2u, 1u, 3u, 10u) }
|
||||
expect(126u) { maxOf(58u, 126u, 55u, 87u) }
|
||||
expect(23u) { maxOf(Random.nextUInt(UInt.MIN_VALUE, 23u), 23u, Random.nextUInt(UInt.MIN_VALUE, 23u), 21u) }
|
||||
expect(UInt.MIN_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MIN_VALUE, UInt.MIN_VALUE, UInt.MIN_VALUE) }
|
||||
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u, 1u) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_ULong() {
|
||||
expect(10uL) { maxOf(2uL, 1uL, 3uL, 10uL) }
|
||||
expect(126uL) { maxOf(58uL, 126uL, 55uL, 87uL) }
|
||||
expect(23uL) { maxOf(Random.nextULong(ULong.MIN_VALUE, 23uL), 23uL, Random.nextULong(ULong.MIN_VALUE, 23uL), 21uL) }
|
||||
expect(ULong.MIN_VALUE) { maxOf(ULong.MIN_VALUE, ULong.MIN_VALUE, ULong.MIN_VALUE, ULong.MIN_VALUE) }
|
||||
expect(ULong.MAX_VALUE) { maxOf(ULong.MIN_VALUE, ULong.MAX_VALUE, 0uL, 1uL) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_UByte() {
|
||||
expect(10.toUByte()) { maxOf(2.toUByte(), 1.toUByte(), 3.toUByte(), 10.toUByte()) }
|
||||
expect(126.toUByte()) { maxOf(58.toUByte(), 126.toUByte(), 55.toUByte(), 87.toUByte()) }
|
||||
expect(23.toUByte()) { maxOf(Random.nextInt(UByte.MIN_VALUE.toInt(), 23).toUByte(), 23.toUByte(), Random.nextInt(UByte.MIN_VALUE.toInt(), 23).toUByte(), 21.toUByte()) }
|
||||
expect(UByte.MIN_VALUE) { maxOf(UByte.MIN_VALUE, UByte.MIN_VALUE, UByte.MIN_VALUE, UByte.MIN_VALUE) }
|
||||
expect(UByte.MAX_VALUE) { maxOf(UByte.MIN_VALUE, UByte.MAX_VALUE, 0.toUByte(), 1.toUByte()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg_UShort() {
|
||||
expect(10.toUShort()) { maxOf(2.toUShort(), 1.toUShort(), 3.toUShort(), 10.toUShort()) }
|
||||
expect(126.toUShort()) { maxOf(58.toUShort(), 126.toUShort(), 55.toUShort(), 87.toUShort()) }
|
||||
expect(23.toUShort()) { maxOf(Random.nextInt(UShort.MIN_VALUE.toInt(), 23).toUShort(), 23.toUShort(), Random.nextInt(UShort.MIN_VALUE.toInt(), 23).toUShort(), 21.toUShort()) }
|
||||
expect(UShort.MIN_VALUE) { maxOf(UShort.MIN_VALUE, UShort.MIN_VALUE, UShort.MIN_VALUE, UShort.MIN_VALUE) }
|
||||
expect(UShort.MAX_VALUE) { maxOf(UShort.MIN_VALUE, UShort.MAX_VALUE, 0.toUShort(), 1.toUShort()) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_UInt() {
|
||||
expect(1u) { minOf(2u, 1u) }
|
||||
expect(58u) { minOf(58u, 126u) }
|
||||
expect(23u) { minOf(Random.nextUInt(23u..UInt.MAX_VALUE), 23u) }
|
||||
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE) }
|
||||
expect(UInt.MAX_VALUE) { minOf(UInt.MAX_VALUE, UInt.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_ULong() {
|
||||
expect(1uL) { minOf(2uL, 1uL) }
|
||||
expect(58uL) { minOf(58uL, 126uL) }
|
||||
expect(23uL) { minOf(Random.nextULong(23uL..ULong.MAX_VALUE), 23uL) }
|
||||
expect(ULong.MIN_VALUE) { minOf(ULong.MIN_VALUE, ULong.MAX_VALUE) }
|
||||
expect(ULong.MAX_VALUE) { minOf(ULong.MAX_VALUE, ULong.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_UByte() {
|
||||
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte()) }
|
||||
expect(58.toUByte()) { minOf(58.toUByte(), 126.toUByte()) }
|
||||
expect(23.toUByte()) { minOf(Random.nextInt(23..UByte.MAX_VALUE.toInt()).toUByte(), 23.toUByte()) }
|
||||
expect(UByte.MIN_VALUE) { minOf(UByte.MIN_VALUE, UByte.MAX_VALUE) }
|
||||
expect(UByte.MAX_VALUE) { minOf(UByte.MAX_VALUE, UByte.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_2_UShort() {
|
||||
expect(1.toUShort()) { minOf(2.toUShort(), 1.toUShort()) }
|
||||
expect(58.toUShort()) { minOf(58.toUShort(), 126.toUShort()) }
|
||||
expect(23.toUShort()) { minOf(Random.nextInt(23..UShort.MAX_VALUE.toInt()).toUShort(), 23.toUShort()) }
|
||||
expect(UShort.MIN_VALUE) { minOf(UShort.MIN_VALUE, UShort.MAX_VALUE) }
|
||||
expect(UShort.MAX_VALUE) { minOf(UShort.MAX_VALUE, UShort.MAX_VALUE) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_UInt() {
|
||||
expect(1u) { minOf(2u, 1u, 3u) }
|
||||
expect(55u) { minOf(58u, 126u, 55u) }
|
||||
expect(23u) { minOf(Random.nextUInt(23u..UInt.MAX_VALUE), 23u, Random.nextUInt(23u..UInt.MAX_VALUE)) }
|
||||
expect(UInt.MAX_VALUE) { minOf(UInt.MAX_VALUE, UInt.MAX_VALUE, UInt.MAX_VALUE) }
|
||||
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_ULong() {
|
||||
expect(1uL) { minOf(2uL, 1uL, 3uL) }
|
||||
expect(55uL) { minOf(58uL, 126uL, 55uL) }
|
||||
expect(23uL) { minOf(Random.nextULong(23uL..ULong.MAX_VALUE), 23uL, Random.nextULong(23uL..ULong.MAX_VALUE)) }
|
||||
expect(ULong.MAX_VALUE) { minOf(ULong.MAX_VALUE, ULong.MAX_VALUE, ULong.MAX_VALUE) }
|
||||
expect(ULong.MIN_VALUE) { minOf(ULong.MIN_VALUE, ULong.MAX_VALUE, 0uL) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_UByte() {
|
||||
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte(), 3.toUByte()) }
|
||||
expect(55.toUByte()) { minOf(58.toUByte(), 126.toUByte(), 55.toUByte()) }
|
||||
expect(23.toUByte()) { minOf(Random.nextInt(23..UByte.MAX_VALUE.toInt()).toUByte(), 23.toUByte(), Random.nextInt(23..UByte.MAX_VALUE.toInt()).toUByte()) }
|
||||
expect(UByte.MAX_VALUE) { minOf(UByte.MAX_VALUE, UByte.MAX_VALUE, UByte.MAX_VALUE) }
|
||||
expect(UByte.MIN_VALUE) { minOf(UByte.MIN_VALUE, UByte.MAX_VALUE, 0u) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3_UShort() {
|
||||
expect(1.toUShort()) { minOf(2.toUShort(), 1.toUShort(), 3.toUShort()) }
|
||||
expect(55.toUShort()) { minOf(58.toUShort(), 126.toUShort(), 55.toUShort()) }
|
||||
expect(23.toUShort()) { minOf(Random.nextInt(23..UShort.MAX_VALUE.toInt()).toUShort(), 23.toUShort(), Random.nextInt(23..UShort.MAX_VALUE.toInt()).toUShort()) }
|
||||
expect(UShort.MAX_VALUE) { minOf(UShort.MAX_VALUE, UShort.MAX_VALUE, UShort.MAX_VALUE) }
|
||||
expect(UShort.MIN_VALUE) { minOf(UShort.MIN_VALUE, UShort.MAX_VALUE, 0u) }
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_UInt() {
|
||||
expect(1u) { minOf(2u, 1u, 3u, 10u) }
|
||||
expect(55u) { minOf(58u, 126u, 55u, 87u) }
|
||||
expect(21u) { minOf(Random.nextUInt(23u..UInt.MAX_VALUE), 23u, Random.nextUInt(23u..UInt.MAX_VALUE), 21u) }
|
||||
expect(UInt.MAX_VALUE) { minOf(UInt.MAX_VALUE, UInt.MAX_VALUE, UInt.MAX_VALUE, UInt.MAX_VALUE) }
|
||||
assertEquals(UInt.MIN_VALUE, minOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u, 1u))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_ULong() {
|
||||
expect(1uL) { minOf(2uL, 1uL, 3uL, 10uL) }
|
||||
expect(55uL) { minOf(58uL, 126uL, 55uL, 87uL) }
|
||||
expect(21uL) { minOf(Random.nextULong(23uL..ULong.MAX_VALUE), 23uL, Random.nextULong(23uL..ULong.MAX_VALUE), 21uL) }
|
||||
expect(ULong.MAX_VALUE) { minOf(ULong.MAX_VALUE, ULong.MAX_VALUE, ULong.MAX_VALUE, ULong.MAX_VALUE) }
|
||||
assertEquals(ULong.MIN_VALUE, minOf(ULong.MIN_VALUE, ULong.MAX_VALUE, 0uL, 1uL))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_UByte() {
|
||||
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte(), 3.toUByte(), 10.toUByte()) }
|
||||
expect(55.toUByte()) { minOf(58.toUByte(), 126.toUByte(), 55.toUByte(), 87.toUByte()) }
|
||||
expect(21.toUByte()) { minOf(Random.nextInt(23..UByte.MAX_VALUE.toInt()).toUByte(), 23.toUByte(), Random.nextInt(23..UByte.MAX_VALUE.toInt()).toUByte(), 21.toUByte()) }
|
||||
expect(UByte.MAX_VALUE) { minOf(UByte.MAX_VALUE, UByte.MAX_VALUE, UByte.MAX_VALUE, UByte.MAX_VALUE) }
|
||||
assertEquals(UByte.MIN_VALUE, minOf(UByte.MIN_VALUE, UByte.MAX_VALUE, 0.toUByte(), 1.toUByte()))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg_UShort() {
|
||||
expect(1.toUShort()) { minOf(2.toUShort(), 1.toUShort(), 3.toUShort(), 10.toUShort()) }
|
||||
expect(55.toUShort()) { minOf(58.toUShort(), 126.toUShort(), 55.toUShort(), 87.toUShort()) }
|
||||
expect(21.toUShort()) { minOf(Random.nextInt(23..UShort.MAX_VALUE.toInt()).toUShort(), 23.toUShort(), Random.nextInt(23..UShort.MAX_VALUE.toInt()).toUShort(), 21.toUShort()) }
|
||||
expect(UShort.MAX_VALUE) { minOf(UShort.MAX_VALUE, UShort.MAX_VALUE, UShort.MAX_VALUE, UShort.MAX_VALUE) }
|
||||
assertEquals(UShort.MIN_VALUE, minOf(UShort.MIN_VALUE, UShort.MAX_VALUE, 0.toUShort(), 1.toUShort()))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +1,58 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.unsigned
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLibTests.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import kotlin.random.*
|
||||
import kotlin.test.*
|
||||
|
||||
|
||||
class NumbersTest {
|
||||
class _UnsignedTest {
|
||||
@Test
|
||||
fun bits_UInt() {
|
||||
fun test(value: UInt, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < UInt.SIZE_BITS) 1u.shl(UInt.SIZE_BITS - leadingZeroes - 1).toUInt() else 0u
|
||||
val lowestBit = if (trailingZeroes < UInt.SIZE_BITS) 1u.shl(trailingZeroes).toUInt() else 0u
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
test(0u, 0, 32, 32)
|
||||
test(1u, 1, 31, 0)
|
||||
test(2u, 1, 30, 1)
|
||||
test(0xF002u, 5, 16, 1)
|
||||
test(0xF00F0000u, 8, 0, 16)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ubyteBits() {
|
||||
fun bits_ULong() {
|
||||
fun test(value: ULong, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < ULong.SIZE_BITS) 1uL.shl(ULong.SIZE_BITS - leadingZeroes - 1).toULong() else 0uL
|
||||
val lowestBit = if (trailingZeroes < ULong.SIZE_BITS) 1uL.shl(trailingZeroes).toULong() else 0uL
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
test(0uL, 0, 64, 64)
|
||||
test(1uL, 1, 63, 0)
|
||||
test(2uL, 1, 62, 1)
|
||||
test(0xF002uL, 5, 48, 1)
|
||||
test(0xF00F0000uL, 8, 32, 16)
|
||||
test(0x1111_3333_EEEE_0000uL, 4 + 8 + 12, 3, 17)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bits_UByte() {
|
||||
fun test(value: UByte, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
@@ -22,7 +62,6 @@ class NumbersTest {
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0u, 0, 8, 8)
|
||||
test(1u, 1, 7, 0)
|
||||
test(2u, 1, 6, 1)
|
||||
@@ -32,7 +71,7 @@ class NumbersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ushortBits() {
|
||||
fun bits_UShort() {
|
||||
fun test(value: UShort, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
@@ -42,7 +81,6 @@ class NumbersTest {
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0u, 0, 16, 16)
|
||||
test(1u, 1, 15, 0)
|
||||
test(2u, 1, 14, 1)
|
||||
@@ -52,52 +90,11 @@ class NumbersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun uintBits() {
|
||||
fun test(value: UInt, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < UInt.SIZE_BITS) 1u.shl(UInt.SIZE_BITS - leadingZeroes - 1) else 0u
|
||||
val lowestBit = if (trailingZeroes < UInt.SIZE_BITS) 1u.shl(trailingZeroes) else 0u
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0u, 0, 32, 32)
|
||||
test(1u, 1, 31, 0)
|
||||
test(2u, 1, 30, 1)
|
||||
test(0xF002u, 5, 16, 1)
|
||||
test(0xF00F0000u, 8, 0, 16)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ulongBits() {
|
||||
fun test(value: ULong, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < ULong.SIZE_BITS) 1uL.shl(ULong.SIZE_BITS - leadingZeroes - 1).toULong() else 0u
|
||||
val lowestBit = if (trailingZeroes < ULong.SIZE_BITS) 1uL.shl(trailingZeroes).toULong() else 0u
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0uL, 0, 64, 64)
|
||||
test(1uL, 1, 63, 0)
|
||||
test(2uL, 1, 62, 1)
|
||||
test(0xF002uL, 5, 48, 1)
|
||||
test(0xF00F0000uL, 8, 32, 16)
|
||||
test(0x1111_3333_EEEE_0000uL, 4 + 8 + 12, 3, 17)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun uintRotate() {
|
||||
fun rotate_UInt() {
|
||||
fun test(value: UInt, n: Int, expected: UInt) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: UInt) {
|
||||
for (n in -2 * UInt.SIZE_BITS..2 * UInt.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
@@ -109,7 +106,6 @@ class NumbersTest {
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x7_3422345u, 4, 0x3422345_7u)
|
||||
test(0x7342234_5u, -4, 0x5_7342234u)
|
||||
test(0x73422345u, 1, 0xE684468Au)
|
||||
@@ -119,40 +115,11 @@ class NumbersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ubyteRotate() {
|
||||
fun test(value: UByte, n: Int, expected: UByte) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: UByte) {
|
||||
for (n in -2 * UByte.SIZE_BITS..2 * UByte.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % UByte.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % UByte.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x73u, 4, 0x37u)
|
||||
test(0x73u, -3, 0x6Eu)
|
||||
test(0x73u, 1, 0xE6u)
|
||||
test(0xE6u, 1, 0xCDu)
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt().toUByte())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ulongRotate() {
|
||||
fun rotate_ULong() {
|
||||
fun test(value: ULong, n: Int, expected: ULong) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: ULong) {
|
||||
for (n in -2 * ULong.SIZE_BITS..2 * ULong.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
@@ -164,7 +131,6 @@ class NumbersTest {
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x7372ABAC_DEEF0123uL, 4, 0x372ABAC_DEEF01237uL)
|
||||
test(0x88888888_44444444uL, -3, 0x91111111_08888888uL)
|
||||
test(0x88888888_44444444uL, 1, 0x11111110_88888889uL)
|
||||
@@ -174,12 +140,37 @@ class NumbersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ushortRotate() {
|
||||
fun rotate_UByte() {
|
||||
fun test(value: UByte, n: Int, expected: UByte) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
fun testCyclic(value: UByte) {
|
||||
for (n in -2 * UByte.SIZE_BITS..2 * UByte.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % UByte.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % UByte.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
test(0x73u, 4, 0x37u)
|
||||
test(0x73u, -3, 0x6Eu)
|
||||
test(0x73u, 1, 0xE6u)
|
||||
test(0xE6u, 1, 0xCDu)
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt().toUByte())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rotate_UShort() {
|
||||
fun test(value: UShort, n: Int, expected: UShort) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: UShort) {
|
||||
for (n in -2 * UShort.SIZE_BITS..2 * UShort.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
@@ -191,7 +182,6 @@ class NumbersTest {
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x7361u, 4, 0x3617u)
|
||||
test(0x7361u, -3, 0b001_0111_0011_0110_0u)
|
||||
test(0x7361u, 1, 0b111_0011_0110_0001_0u)
|
||||
@@ -201,4 +191,4 @@ class NumbersTest {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@
|
||||
package test.numbers
|
||||
|
||||
import test.isFloat32RangeEnforced
|
||||
import kotlin.random.Random
|
||||
import kotlin.test.*
|
||||
|
||||
object NumbersTestConstants {
|
||||
@@ -252,194 +251,4 @@ class NumbersTest {
|
||||
testSizes(ULong, ULong.SIZE_BYTES, ULong.SIZE_BITS, 8)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun byteBits() {
|
||||
fun test(value: Byte, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Byte.SIZE_BITS) 1.shl(Byte.SIZE_BITS - leadingZeroes - 1).toByte() else 0
|
||||
val lowestBit = if (trailingZeroes < Byte.SIZE_BITS) 1.shl(trailingZeroes).toByte() else 0
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0, 0, 8, 8)
|
||||
test(1, 1, 7, 0)
|
||||
test(2, 1, 6, 1)
|
||||
test(0x44, 2, 1, 2)
|
||||
test(0x80.toByte(), 1, 0, 7)
|
||||
test(0xF0.toByte(), 4, 0, 4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shortBits() {
|
||||
fun test(value: Short, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Short.SIZE_BITS) 1.shl(Short.SIZE_BITS - leadingZeroes - 1).toShort() else 0
|
||||
val lowestBit = if (trailingZeroes < Short.SIZE_BITS) 1.shl(trailingZeroes).toShort() else 0
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0, 0, 16, 16)
|
||||
test(1, 1, 15, 0)
|
||||
test(2, 1, 14, 1)
|
||||
test(0xF2, 5, 8, 1)
|
||||
test(0x8000.toShort(), 1, 0, 15)
|
||||
test(0xF200.toShort(), 5, 0, 9)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun intBits() {
|
||||
fun test(value: Int, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Int.SIZE_BITS) 1.shl(Int.SIZE_BITS - leadingZeroes - 1) else 0
|
||||
val lowestBit = if (trailingZeroes < Int.SIZE_BITS) 1.shl(trailingZeroes) else 0
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0, 0, 32, 32)
|
||||
test(1, 1, 31, 0)
|
||||
test(2, 1, 30, 1)
|
||||
test(0xF002, 5, 16, 1)
|
||||
test(0xF00F0000.toInt(), 8, 0, 16)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longBits() {
|
||||
fun test(value: Long, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < Long.SIZE_BITS) 1L.shl(Long.SIZE_BITS - leadingZeroes - 1).toLong() else 0
|
||||
val lowestBit = if (trailingZeroes < Long.SIZE_BITS) 1L.shl(trailingZeroes).toLong() else 0
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(0, 0, 64, 64)
|
||||
test(1, 1, 63, 0)
|
||||
test(2, 1, 62, 1)
|
||||
test(0xF002, 5, 48, 1)
|
||||
test(0xF00F0000L, 8, 32, 16)
|
||||
test(0x1111_3333_EEEE_0000L, 4 + 8 + 12, 3, 17)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun intRotate() {
|
||||
fun test(value: Int, n: Int, expected: Int) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: Int) {
|
||||
for (n in -2 * Int.SIZE_BITS..2 * Int.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Int.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Int.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x7_3422345, 4, 0x3422345_7)
|
||||
test(0x7342234_5, -4, 0x5_7342234)
|
||||
test(0x73422345, 1, 0xE684468A.toInt())
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun byteRotate() {
|
||||
fun test(value: Byte, n: Int, expected: Byte) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: Byte) {
|
||||
for (n in -2 * Byte.SIZE_BITS..2 * Byte.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Byte.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Byte.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x73, 4, 0x37)
|
||||
test(0x73, -3, 0x6E)
|
||||
test(0x73, 1, 0xE6.toByte())
|
||||
test(0xE6.toByte(), 1, 0xCD.toByte())
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt().toByte())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longRotate() {
|
||||
fun test(value: Long, n: Int, expected: Long) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: Long) {
|
||||
for (n in -2 * Long.SIZE_BITS..2 * Long.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Long.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Long.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x7372ABAC_DEEF0123, 4, 0x372ABAC_DEEF01237)
|
||||
test(0x88888888_44444444U.toLong(), -3, 0x91111111_08888888u.toLong())
|
||||
test(0x88888888_44444444U.toLong(), 1, 0x11111110_88888889)
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextLong())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shortRotate() {
|
||||
fun test(value: Short, n: Int, expected: Short) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: Short) {
|
||||
for (n in -2 * Short.SIZE_BITS..2 * Short.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % Short.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % Short.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
|
||||
test(0x7361, 4, 0x3617)
|
||||
test(0x7361, -3, 0b001_0111_0011_0110_0)
|
||||
test(0x7361, 1, 0b111_0011_0110_0001_0.toShort())
|
||||
test(0xE6C2.toShort(), 1, 0b11_0011_0110_0001_01.toShort())
|
||||
repeat(100) {
|
||||
testCyclic(Random.nextInt().toShort())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,8 +23,16 @@ inline fun <reified T> assertStaticAndRuntimeTypeIs(value: @kotlin.internal.NoIn
|
||||
}
|
||||
|
||||
|
||||
fun assertArrayContentEquals(expected: ByteArray, actual: ByteArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: CharArray, actual: CharArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun <T> assertArrayContentEquals(expected: Array<out T>, actual: Array<out T>, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
|
||||
fun assertArrayContentEquals(expected: ByteArray, actual: ByteArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: ShortArray, actual: ShortArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: IntArray, actual: IntArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: LongArray, actual: LongArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: FloatArray, actual: FloatArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: DoubleArray, actual: DoubleArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: BooleanArray, actual: BooleanArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: CharArray, actual: CharArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
|
||||
fun assertArrayContentEquals(expected: UIntArray, actual: UIntArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
fun assertArrayContentEquals(expected: ULongArray, actual: ULongArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package unsigned
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.expect
|
||||
|
||||
class UComparisonsTest {
|
||||
|
||||
@Test
|
||||
fun minOf_2() {
|
||||
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte()) }
|
||||
expect(58.toUShort()) { minOf(58.toUShort(), 32768.toUShort()) }
|
||||
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE) }
|
||||
expect(42312uL) { minOf(42312uL, 42312uL) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_3() {
|
||||
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte(), 3.toUByte()) }
|
||||
expect(55.toUShort()) { minOf(58.toUShort(), 32768.toUShort(), 55.toUShort()) }
|
||||
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u) }
|
||||
expect(42312uL) { minOf(42312uL, 42312uL, 42312uL) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOf_vararg() {
|
||||
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte(), 3.toUByte(), 2.toUByte(), 10.toUByte()) }
|
||||
expect(55.toUShort()) { minOf(58.toUShort(), 32768.toUShort(), 55.toUShort(), 2423.toUShort()) }
|
||||
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u, 1000u, (-1).toUInt(), Int.MAX_VALUE.toUInt()) }
|
||||
expect(42312uL) { minOf(42312uL, 42312uL, 42312uL, 42312uL) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_2() {
|
||||
expect(2.toUByte()) { maxOf(2.toUByte(), 1.toUByte()) }
|
||||
expect(32768.toUShort()) { maxOf(58.toUShort(), 32768.toUShort()) }
|
||||
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE) }
|
||||
expect(42312uL) { maxOf(42312uL, 42312uL) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_3() {
|
||||
expect(3.toUByte()) { maxOf(2.toUByte(), 1.toUByte(), 3.toUByte()) }
|
||||
expect(32768.toUShort()) { maxOf(58.toUShort(), 32768.toUShort(), 55.toUShort()) }
|
||||
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u) }
|
||||
expect(42312uL) { maxOf(42312uL, 42312uL, 42312uL) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxOf_vararg() {
|
||||
expect(10.toUByte()) { maxOf(2.toUByte(), 1.toUByte(), 3.toUByte(), 2.toUByte(), 10.toUByte()) }
|
||||
expect(32768.toUShort()) { maxOf(58.toUShort(), 32768.toUShort(), 55.toUShort(), 2423.toUShort()) }
|
||||
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u, 1000u, (-1).toUInt(), Int.MAX_VALUE.toUInt()) }
|
||||
expect(42312uL) { maxOf(42312uL, 42312uL, 42312uL, 42312uL) }
|
||||
}
|
||||
}
|
||||
@@ -34,4 +34,12 @@ task run(type: JavaExec) {
|
||||
classpath sourceSets.main.runtimeClasspath
|
||||
args = ["${rootDir}"]
|
||||
systemProperty 'line.separator', '\n'
|
||||
}
|
||||
|
||||
task genTests(type: JavaExec) {
|
||||
group 'application'
|
||||
main 'generators.GenerateStandardLibTestsKt'
|
||||
classpath sourceSets.main.runtimeClasspath
|
||||
args = ["${rootDir}"]
|
||||
systemProperty 'line.separator', '\n'
|
||||
}
|
||||
@@ -7,7 +7,7 @@ package generators
|
||||
|
||||
import java.io.*
|
||||
import templates.*
|
||||
import kotlin.system.exitProcess
|
||||
import templates.TemplateWriter
|
||||
|
||||
/**
|
||||
* Generates methods in the standard library which are mostly identical
|
||||
@@ -17,7 +17,7 @@ import kotlin.system.exitProcess
|
||||
* at runtime.
|
||||
*/
|
||||
fun main(args: Array<String>) {
|
||||
val templateGroups = sequenceOf<TemplateGroup>(
|
||||
val templateGroups = sequenceOf(
|
||||
Elements,
|
||||
Filtering,
|
||||
Ordering,
|
||||
@@ -35,44 +35,74 @@ fun main(args: Array<String>) {
|
||||
ComparableOps
|
||||
)
|
||||
|
||||
COPYRIGHT_NOTICE = readCopyrightNoticeFromProfile { Thread.currentThread().contextClassLoader.getResourceAsStream("apache.xml").reader() }
|
||||
|
||||
val targetBaseDirs = mutableMapOf<KotlinTarget, File>()
|
||||
|
||||
when (args.size) {
|
||||
1 -> {
|
||||
val baseDir = File(args.first())
|
||||
targetBaseDirs[KotlinTarget.Common] = baseDir.resolveExistingDir("libraries/stdlib/common/src/generated")
|
||||
targetBaseDirs[KotlinTarget.JVM] = baseDir.resolveExistingDir("libraries/stdlib/jvm/src/generated")
|
||||
targetBaseDirs[KotlinTarget.JS] = baseDir.resolveExistingDir("libraries/stdlib/js/src/generated")
|
||||
targetBaseDirs[KotlinTarget.JS_IR] = baseDir.resolveExistingDir("libraries/stdlib/js-ir/src/generated")
|
||||
}
|
||||
2 -> {
|
||||
val (targetName, targetDir) = args
|
||||
val target = KotlinTarget.values.singleOrNull { it.name.equals(targetName, ignoreCase = true) } ?: error("Invalid target: $targetName")
|
||||
targetBaseDirs[target] = File(targetDir).also { it.requireExistingDir() }
|
||||
}
|
||||
else -> {
|
||||
println("""Parameters:
|
||||
<kotlin-base-dir> - generates sources for common, jvm, js, ir-js targets using paths derived from specified base path
|
||||
<target> <target-dir> - generates source for the specified target in the specified target directory
|
||||
""")
|
||||
exitProcess(1)
|
||||
}
|
||||
}
|
||||
|
||||
templateGroups.groupByFileAndWrite(targetsToGenerate = targetBaseDirs.keys) { (target, source) ->
|
||||
val targetDir = targetBaseDirs[target] ?: error("Target $target directory is not configured")
|
||||
val platformSuffix = when (val platform = target.platform) {
|
||||
Platform.Common -> ""
|
||||
else -> platform.name.toLowerCase().capitalize()
|
||||
}
|
||||
targetDir.resolve("_${source.name.capitalize()}$platformSuffix.kt")
|
||||
}
|
||||
templateGroups.generateSources(args, StandardLibGeneratorStrategy())
|
||||
}
|
||||
|
||||
fun File.resolveExistingDir(subpath: String) = resolve(subpath).also { it.requireExistingDir() }
|
||||
private class StandardLibGeneratorStrategy : SourceGeneratorStrategy<MemberBuilder> {
|
||||
override val supportedTargets: List<KotlinTarget>
|
||||
get() = KotlinTarget.values - KotlinTarget.Native
|
||||
|
||||
fun File.requireExistingDir() {
|
||||
require(isDirectory) { "Directory $this doesn't exist"}
|
||||
override fun baseDir(target: KotlinTarget): String = when (target) {
|
||||
KotlinTarget.Common -> "libraries/stdlib/common/src/generated"
|
||||
KotlinTarget.JVM -> "libraries/stdlib/jvm/src/generated"
|
||||
KotlinTarget.JS -> "libraries/stdlib/js/src/generated"
|
||||
KotlinTarget.JS_IR -> "libraries/stdlib/js-ir/src/generated"
|
||||
else -> error("Invalid target: $target")
|
||||
}
|
||||
|
||||
override fun fileName(target: KotlinTarget, source: SourceFile): String {
|
||||
val platformSuffix = if (target.platform == Platform.Common) "" else target.platform.name.toLowerCase().capitalize()
|
||||
return "_${source.name.capitalize()}$platformSuffix.kt"
|
||||
}
|
||||
|
||||
override val writer: TemplateWriter<MemberBuilder>
|
||||
get() = MemberWriter()
|
||||
}
|
||||
|
||||
private class MemberWriter : TemplateWriter<MemberBuilder> {
|
||||
override fun writeTo(file: File, builders: List<MemberBuilder>, targetedSource: TargetedSourceFile) {
|
||||
val (target, sourceFile) = targetedSource
|
||||
println("Generating file: $file")
|
||||
file.parentFile.mkdirs()
|
||||
FileWriter(file).use { writer ->
|
||||
writer.appendln(copyrightNotice())
|
||||
|
||||
when (target.platform) {
|
||||
Platform.Common, Platform.JVM -> {
|
||||
if (sourceFile.multifile) {
|
||||
writer.appendln("@file:kotlin.jvm.JvmMultifileClass")
|
||||
}
|
||||
|
||||
writer.appendln("@file:kotlin.jvm.JvmName(\"${sourceFile.jvmClassName}\")")
|
||||
sourceFile.jvmPackageName?.let {
|
||||
writer.appendln("@file:kotlin.jvm.JvmPackageName(\"$it\")")
|
||||
}
|
||||
writer.appendln()
|
||||
}
|
||||
}
|
||||
|
||||
writer.append("package ${sourceFile.packageName ?: "kotlin"}\n\n")
|
||||
writer.append("${autogeneratedWarning("GenerateStandardLib.kt")}\n\n")
|
||||
if (target.platform == Platform.JS) {
|
||||
writer.appendln("import kotlin.js.*")
|
||||
if (sourceFile == SourceFile.Arrays) {
|
||||
writer.appendln("import primitiveArrayConcat")
|
||||
writer.appendln("import withType")
|
||||
}
|
||||
}
|
||||
if (target.platform == Platform.Common) {
|
||||
writer.appendln("import kotlin.random.*")
|
||||
}
|
||||
if (sourceFile.packageName == "kotlin.collections") {
|
||||
writer.appendln("import kotlin.ranges.contains")
|
||||
writer.appendln("import kotlin.ranges.reversed")
|
||||
}
|
||||
|
||||
writer.appendln()
|
||||
|
||||
for (f in builders) {
|
||||
f.build(writer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package generators
|
||||
|
||||
import templates.*
|
||||
import templates.test.*
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
|
||||
/**
|
||||
* Generates methods in the standard library which are mostly identical
|
||||
* but just using a different input kind.
|
||||
*
|
||||
* Kinda like mimicking source macros here, but this avoids the inefficiency of type conversions
|
||||
* at runtime.
|
||||
*/
|
||||
fun main(args: Array<String>) {
|
||||
val templateGroups = sequenceOf(
|
||||
AggregatesTest,
|
||||
NumbersTest,
|
||||
ComparablesTest,
|
||||
ElementsTest,
|
||||
ArraysTest,
|
||||
OrderingTest
|
||||
)
|
||||
|
||||
templateGroups.generateSources(args, TestGeneratorStrategy())
|
||||
}
|
||||
|
||||
|
||||
private class TestGeneratorStrategy : SourceGeneratorStrategy<TestBuilder> {
|
||||
override val supportedTargets: List<KotlinTarget>
|
||||
get() = listOf(KotlinTarget.Common)
|
||||
|
||||
override fun baseDir(target: KotlinTarget): String = when (target) {
|
||||
KotlinTarget.Common -> "libraries/stdlib/test/generated"
|
||||
// KotlinTarget.JVM -> "libraries/stdlib/jvm/test/generated"
|
||||
// KotlinTarget.JS -> "libraries/stdlib/js/test/generated"
|
||||
// KotlinTarget.JS_IR -> "libraries/stdlib/js-ir/test/generated"
|
||||
else -> error("Invalid target: $target")
|
||||
}
|
||||
|
||||
override fun fileName(target: KotlinTarget, source: SourceFile): String {
|
||||
val platformSuffix = if (target.platform == Platform.Common) "" else target.platform.name
|
||||
return "_${source.name.capitalize() + platformSuffix}Test.kt"
|
||||
}
|
||||
|
||||
override val writer: TemplateWriter<TestBuilder>
|
||||
get() = TestWriter()
|
||||
}
|
||||
|
||||
private class TestWriter : TemplateWriter<TestBuilder> {
|
||||
override fun writeTo(file: File, builders: List<TestBuilder>, targetedSource: TargetedSourceFile) {
|
||||
val (target, sourceFile) = targetedSource
|
||||
println("Generating file: $file")
|
||||
file.parentFile.mkdirs()
|
||||
FileWriter(file).use { writer ->
|
||||
writer.appendln(copyrightNotice())
|
||||
|
||||
writer.append("package ${sourceFile.testPackageName ?: "test"}\n\n")
|
||||
writer.append("${autogeneratedWarning("GenerateStandardLibTests.kt")}\n\n")
|
||||
|
||||
if (sourceFile in setOf(SourceFile.Arrays, SourceFile.Sequences, SourceFile.Collections)) {
|
||||
writer.appendLine("import test.comparisons.STRING_CASE_INSENSITIVE_ORDER")
|
||||
}
|
||||
if (sourceFile in setOf(
|
||||
SourceFile.Primitives,
|
||||
SourceFile.Unsigned,
|
||||
SourceFile.Comparisons,
|
||||
SourceFile.UComparisons,
|
||||
SourceFile.Arrays,
|
||||
SourceFile.UArrays,
|
||||
SourceFile.Collections
|
||||
)
|
||||
) {
|
||||
writer.appendLine("import kotlin.random.*")
|
||||
}
|
||||
if (sourceFile == SourceFile.Arrays || sourceFile == SourceFile.UArrays) {
|
||||
writer.appendLine("import test.assertArrayContentEquals")
|
||||
}
|
||||
if (sourceFile == SourceFile.Sequences) {
|
||||
writer.appendLine("import test.collections.assertSorted")
|
||||
}
|
||||
|
||||
writer.append("import kotlin.test.*\n\n")
|
||||
|
||||
writer.append("class ${file.nameWithoutExtension} {\n")
|
||||
|
||||
for (f in builders) {
|
||||
f.build(writer)
|
||||
}
|
||||
|
||||
writer.append("}\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import templates.DocExtensions.prefixWithArticle
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object Aggregates : TemplateGroupBase() {
|
||||
object Aggregates : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -10,7 +10,7 @@ import templates.Family.*
|
||||
import templates.Ordering.appendStableSortNote
|
||||
import templates.Ordering.stableSortNote
|
||||
|
||||
object ArrayOps : TemplateGroupBase() {
|
||||
object ArrayOps : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -7,7 +7,7 @@ package templates
|
||||
|
||||
import templates.Family.*
|
||||
|
||||
object ComparableOps : TemplateGroupBase() {
|
||||
object ComparableOps : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object Elements : TemplateGroupBase() {
|
||||
object Elements : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object Filtering : TemplateGroupBase() {
|
||||
object Filtering : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
val terminalOperationPattern = Regex("^\\w+To")
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object Generators : TemplateGroupBase() {
|
||||
object Generators : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object Guards : TemplateGroupBase() {
|
||||
object Guards : MemberTemplateGroupBase() {
|
||||
private val THIS = "\$this"
|
||||
|
||||
val f_requireNoNulls = fn("requireNoNulls()") {
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object Mapping : TemplateGroupBase() {
|
||||
object Mapping : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
val terminalOperationPattern = Regex("^\\w+To")
|
||||
|
||||
@@ -7,7 +7,7 @@ package templates
|
||||
|
||||
import templates.Family.*
|
||||
|
||||
object Numeric : TemplateGroupBase() {
|
||||
object Numeric : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -9,7 +9,7 @@ import templates.ArrayOps.rangeDoc
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object Ordering : TemplateGroupBase() {
|
||||
object Ordering : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.PrimitiveType.Companion.maxByCapacity
|
||||
|
||||
object RangeOps : TemplateGroupBase() {
|
||||
object RangeOps : MemberTemplateGroupBase() {
|
||||
|
||||
private val rangePrimitives = PrimitiveType.rangePrimitives
|
||||
private fun rangeElementType(fromType: PrimitiveType, toType: PrimitiveType) =
|
||||
|
||||
@@ -7,7 +7,7 @@ package templates
|
||||
|
||||
import templates.Family.*
|
||||
|
||||
object SequenceOps : TemplateGroupBase() {
|
||||
object SequenceOps : MemberTemplateGroupBase() {
|
||||
|
||||
val f_asIterable = fn("asIterable()") {
|
||||
include(Iterables, ArraysOfObjects, ArraysOfPrimitives, Sequences, CharSequences, Maps)
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object SetOps : TemplateGroupBase() {
|
||||
object SetOps : MemberTemplateGroupBase() {
|
||||
|
||||
val f_toMutableSet = fn("toMutableSet()") {
|
||||
includeDefault()
|
||||
|
||||
@@ -7,7 +7,7 @@ package templates
|
||||
|
||||
import templates.Family.*
|
||||
|
||||
object Snapshots : TemplateGroupBase() {
|
||||
object Snapshots : MemberTemplateGroupBase() {
|
||||
|
||||
init {
|
||||
defaultBuilder {
|
||||
|
||||
@@ -8,7 +8,7 @@ package templates
|
||||
import templates.Family.*
|
||||
import templates.SequenceClass.*
|
||||
|
||||
object StringJoinOps : TemplateGroupBase() {
|
||||
object StringJoinOps : MemberTemplateGroupBase() {
|
||||
|
||||
val f_joinTo = fn("joinTo(buffer: A, separator: CharSequence = \", \", prefix: CharSequence = \"\", postfix: CharSequence = \"\", limit: Int = -1, truncated: CharSequence = \"...\", transform: ((T) -> CharSequence)? = null)") {
|
||||
includeDefault()
|
||||
|
||||
@@ -9,38 +9,17 @@ import templates.Family.*
|
||||
import java.io.StringReader
|
||||
import java.util.StringTokenizer
|
||||
|
||||
private fun getDefaultSourceFile(f: Family): SourceFile = when (f) {
|
||||
Iterables, Collections, Lists -> SourceFile.Collections
|
||||
Sequences -> SourceFile.Sequences
|
||||
Sets -> SourceFile.Sets
|
||||
Ranges, RangesOfPrimitives, ProgressionsOfPrimitives -> SourceFile.Ranges
|
||||
ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives -> SourceFile.Arrays
|
||||
ArraysOfUnsigned -> SourceFile.UArrays
|
||||
Maps -> SourceFile.Maps
|
||||
Strings -> SourceFile.Strings
|
||||
CharSequences -> SourceFile.Strings
|
||||
Primitives, Generic, Unsigned -> SourceFile.Misc
|
||||
}
|
||||
|
||||
@TemplateDsl
|
||||
class MemberBuilder(
|
||||
val allowedPlatforms: Set<Platform>,
|
||||
val target: KotlinTarget,
|
||||
var family: Family,
|
||||
var sourceFile: SourceFile = getDefaultSourceFile(family),
|
||||
var primitive: PrimitiveType? = null
|
||||
) {
|
||||
allowedPlatforms: Set<Platform>,
|
||||
target: KotlinTarget,
|
||||
family: Family,
|
||||
primitive: PrimitiveType? = null
|
||||
) : TemplateBuilderBase(allowedPlatforms, target, family, primitive) {
|
||||
lateinit var keyword: Keyword // fun/val/var
|
||||
lateinit var signature: String // name and params
|
||||
|
||||
var sortingSignature: String? = null
|
||||
get() = field ?: signature
|
||||
private set
|
||||
|
||||
val f get() = family
|
||||
|
||||
private val legacyMode = false
|
||||
var hasPlatformSpecializations: Boolean = legacyMode
|
||||
override var hasPlatformSpecializations: Boolean = legacyMode
|
||||
private set
|
||||
|
||||
var doc: String? = null; private set
|
||||
@@ -66,10 +45,6 @@ class MemberBuilder(
|
||||
var returns: String? = null; private set
|
||||
val throwsExceptions = mutableListOf<ThrowsException>()
|
||||
var body: String? = null; private set
|
||||
val annotations: MutableSet<String> = mutableSetOf()
|
||||
val suppressions: MutableList<String> = mutableListOf()
|
||||
|
||||
fun sourceFile(file: SourceFile) { sourceFile = file }
|
||||
|
||||
fun deprecate(value: Deprecation) { deprecate = value }
|
||||
fun deprecate(value: String) { deprecate = Deprecation(value) }
|
||||
@@ -97,10 +72,6 @@ class MemberBuilder(
|
||||
fun receiver(value: String) { customReceiver = value }
|
||||
@Deprecated("Use receiver()", ReplaceWith("receiver(value)"))
|
||||
fun customReceiver(value: String) = receiver(value)
|
||||
fun signature(value: String, notForSorting: Boolean = false) {
|
||||
if (notForSorting) sortingSignature = signature
|
||||
signature = value
|
||||
}
|
||||
fun returns(type: String) { returns = type }
|
||||
@Deprecated("Use specialFor", ReplaceWith("specialFor(*fs) { returns(run(valueBuilder)) }"))
|
||||
fun returns(vararg fs: Family, valueBuilder: () -> String) = specialFor(*fs) { returns(run(valueBuilder)) }
|
||||
@@ -115,14 +86,6 @@ class MemberBuilder(
|
||||
}
|
||||
}
|
||||
|
||||
fun annotation(annotation: String) {
|
||||
annotations += annotation
|
||||
}
|
||||
|
||||
fun suppress(diagnostic: String) {
|
||||
suppressions += diagnostic
|
||||
}
|
||||
|
||||
fun sequenceClassification(vararg sequenceClass: SequenceClass) {
|
||||
sequenceClassification += sequenceClass
|
||||
}
|
||||
@@ -163,18 +126,8 @@ class MemberBuilder(
|
||||
if (target.backend == backend) action()
|
||||
}
|
||||
|
||||
fun specialFor(f: Family, action: () -> Unit) {
|
||||
if (family == f)
|
||||
action()
|
||||
}
|
||||
fun specialFor(vararg families: Family, action: () -> Unit) {
|
||||
require(families.isNotEmpty())
|
||||
if (family in families)
|
||||
action()
|
||||
}
|
||||
|
||||
|
||||
fun build(builder: Appendable) {
|
||||
override fun build(builder: Appendable) {
|
||||
val headerOnly: Boolean
|
||||
val isImpl: Boolean
|
||||
if (!legacyMode) {
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
|
||||
package templates
|
||||
|
||||
enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = true, val packageName: String? = null, val jvmPackageName: String? = null) {
|
||||
enum class SourceFile(
|
||||
jvmClassName: String? = null,
|
||||
val multifile: Boolean = true,
|
||||
val packageName: String? = null,
|
||||
testPackageName: String? = null,
|
||||
val jvmPackageName: String? = null
|
||||
) {
|
||||
|
||||
Arrays(packageName = "kotlin.collections"),
|
||||
UArrays(packageName = "kotlin.collections", jvmPackageName = "kotlin.collections.unsigned"),
|
||||
@@ -18,10 +24,13 @@ enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = tru
|
||||
Ranges(packageName = "kotlin.ranges"),
|
||||
URanges(packageName = "kotlin.ranges"),
|
||||
Comparisons(packageName = "kotlin.comparisons"),
|
||||
UComparisons(packageName = "kotlin.comparisons"),
|
||||
UComparisons(packageName = "kotlin.comparisons", testPackageName = "test.unsigned"),
|
||||
Strings(packageName = "kotlin.text"),
|
||||
Misc(),
|
||||
Primitives(testPackageName = "test.numbers"),
|
||||
Unsigned(testPackageName = "test.unsigned"),
|
||||
Misc()
|
||||
;
|
||||
|
||||
val jvmClassName = jvmClassName ?: (name.capitalize() + "Kt")
|
||||
val testPackageName = testPackageName ?: packageName?.replaceFirst("kotlin.", "test.")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates
|
||||
|
||||
import templates.Family.*
|
||||
|
||||
private fun getDefaultSourceFile(f: Family): SourceFile = when (f) {
|
||||
Iterables, Collections, Lists -> SourceFile.Collections
|
||||
Sequences -> SourceFile.Sequences
|
||||
Sets -> SourceFile.Sets
|
||||
Ranges, RangesOfPrimitives, ProgressionsOfPrimitives -> SourceFile.Ranges
|
||||
ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives -> SourceFile.Arrays
|
||||
ArraysOfUnsigned -> SourceFile.UArrays
|
||||
Maps -> SourceFile.Maps
|
||||
Strings -> SourceFile.Strings
|
||||
CharSequences -> SourceFile.Strings
|
||||
Primitives -> SourceFile.Primitives
|
||||
Unsigned -> SourceFile.Unsigned
|
||||
Generic -> SourceFile.Misc
|
||||
}
|
||||
|
||||
@TemplateDsl
|
||||
abstract class TemplateBuilderBase(
|
||||
val allowedPlatforms: Set<Platform>,
|
||||
val target: KotlinTarget,
|
||||
var family: Family,
|
||||
var primitive: PrimitiveType? = null,
|
||||
var sourceFile: SourceFile = getDefaultSourceFile(family)
|
||||
) {
|
||||
lateinit var signature: String // name and params
|
||||
|
||||
var sortingSignature: String? = null
|
||||
get() = field ?: signature
|
||||
private set
|
||||
|
||||
val f get() = family
|
||||
|
||||
val annotations: MutableList<String> = mutableListOf()
|
||||
val suppressions: MutableList<String> = mutableListOf()
|
||||
|
||||
fun sourceFile(file: SourceFile) { sourceFile = file }
|
||||
|
||||
fun signature(value: String, notForSorting: Boolean = false) {
|
||||
if (notForSorting) sortingSignature = signature
|
||||
signature = value
|
||||
}
|
||||
|
||||
fun annotation(annotation: String) {
|
||||
annotations += annotation
|
||||
}
|
||||
|
||||
fun suppress(diagnostic: String) {
|
||||
suppressions += diagnostic
|
||||
}
|
||||
|
||||
fun specialFor(f: Family, action: () -> Unit) {
|
||||
if (family == f)
|
||||
action()
|
||||
}
|
||||
|
||||
fun specialFor(vararg families: Family, action: () -> Unit) {
|
||||
require(families.isNotEmpty())
|
||||
if (family in families)
|
||||
action()
|
||||
}
|
||||
|
||||
abstract val hasPlatformSpecializations: Boolean
|
||||
abstract fun build(builder: Appendable)
|
||||
}
|
||||
@@ -8,29 +8,27 @@ package templates
|
||||
import kotlin.reflect.KTypeProjection
|
||||
import kotlin.reflect.full.createType
|
||||
import kotlin.reflect.full.isSubtypeOf
|
||||
import kotlin.reflect.full.starProjectedType
|
||||
|
||||
|
||||
typealias TemplateGroup = () -> Sequence<MemberTemplate>
|
||||
typealias TemplateGroup<TBuilder> = () -> Sequence<SourceTemplate<TBuilder>>
|
||||
|
||||
fun templateGroupOf(vararg templates: MemberTemplate): TemplateGroup = { templates.asSequence() }
|
||||
abstract class TemplateGroupBase<TBuilder> : TemplateGroup<TBuilder> {
|
||||
|
||||
abstract class TemplateGroupBase : TemplateGroup {
|
||||
|
||||
override fun invoke(): Sequence<MemberTemplate> = sequence {
|
||||
override fun invoke(): Sequence<SourceTemplate<TBuilder>> = sequence {
|
||||
with(this@TemplateGroupBase) {
|
||||
this::class.members.filter { it.name.startsWith("f_") }.forEach {
|
||||
require(it.parameters.size == 1) { "Member $it violates naming convention" }
|
||||
require(it.parameters.size == 1) { "Template $it violates naming convention" }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
when {
|
||||
it.returnType.isSubtypeOf(typeMemberTemplate) ->
|
||||
yield(it.call(this) as MemberTemplate)
|
||||
it.returnType.isSubtypeOf(typeIterableOfMemberTemplates) ->
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
yieldAll(it.call(this) as Iterable<MemberTemplate>)
|
||||
it.returnType.isSubtypeOf(typeSequenceOfMemberTemplates) ->
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
yieldAll(it.call(this) as Sequence<MemberTemplate>)
|
||||
it.returnType.isSubtypeOf(typeSourceTemplate) ->
|
||||
yield(it.call(this) as SourceTemplate<TBuilder>)
|
||||
it.returnType.isSubtypeOf(typeIterableOfSourceTemplates) ->
|
||||
yieldAll(it.call(this) as Iterable<SourceTemplate<TBuilder>>)
|
||||
it.returnType.isSubtypeOf(typeSequenceOfSourceTemplates) ->
|
||||
yieldAll(it.call(this) as Sequence<SourceTemplate<TBuilder>>)
|
||||
else ->
|
||||
error("Member $it violates naming convention")
|
||||
error("Template $it violates naming convention")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,16 +36,19 @@ abstract class TemplateGroupBase : TemplateGroup {
|
||||
if (defaultActions.isEmpty()) this else onEach { t -> defaultActions.forEach(t::builder) }
|
||||
}
|
||||
|
||||
private val defaultActions = mutableListOf<MemberBuildAction>()
|
||||
private val defaultActions = mutableListOf<Action<TBuilder>>()
|
||||
|
||||
fun defaultBuilder(builderAction: MemberBuildAction) {
|
||||
fun defaultBuilder(builderAction: Action<TBuilder>) {
|
||||
defaultActions += builderAction
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val typeMemberTemplate = MemberTemplate::class.createType()
|
||||
private val typeIterableOfMemberTemplates = Iterable::class.createType(arguments = listOf(KTypeProjection.invariant(typeMemberTemplate)))
|
||||
private val typeSequenceOfMemberTemplates = Sequence::class.createType(arguments = listOf(KTypeProjection.invariant(typeMemberTemplate)))
|
||||
private val typeSourceTemplate = SourceTemplate::class.starProjectedType
|
||||
private val typeIterableOfSourceTemplates = Iterable::class.createType(arguments = listOf(KTypeProjection.invariant(typeSourceTemplate)))
|
||||
private val typeSequenceOfSourceTemplates = Sequence::class.createType(arguments = listOf(KTypeProjection.invariant(typeSourceTemplate)))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
typealias MemberTemplateGroupBase = TemplateGroupBase<MemberBuilder>
|
||||
typealias TestTemplateGroupBase = TemplateGroupBase<TestBuilder>
|
||||
@@ -14,15 +14,16 @@ enum class Keyword(val value: String) {
|
||||
Variable("var");
|
||||
}
|
||||
|
||||
typealias MemberBuildAction = MemberBuilder.() -> Unit
|
||||
typealias MemberBuildActionP<TParam> = MemberBuilder.(TParam) -> Unit
|
||||
typealias Action<TBuilder> = TBuilder.() -> Unit
|
||||
typealias ActionP<TBuilder, TParam> = TBuilder.(TParam) -> Unit
|
||||
|
||||
private fun def(signature: String, memberKind: Keyword): MemberBuildAction = {
|
||||
|
||||
private fun def(signature: String, memberKind: Keyword): Action<MemberBuilder> = {
|
||||
this.signature = signature
|
||||
this.keyword = memberKind
|
||||
}
|
||||
|
||||
fun fn(defaultSignature: String): MemberBuildAction = def(defaultSignature, Keyword.Function)
|
||||
fun fn(defaultSignature: String): Action<MemberBuilder> = def(defaultSignature, Keyword.Function)
|
||||
|
||||
fun fn(defaultSignature: String, setup: FamilyPrimitiveMemberDefinition.() -> Unit): FamilyPrimitiveMemberDefinition =
|
||||
FamilyPrimitiveMemberDefinition().apply {
|
||||
@@ -30,7 +31,7 @@ fun fn(defaultSignature: String, setup: FamilyPrimitiveMemberDefinition.() -> Un
|
||||
setup()
|
||||
}
|
||||
|
||||
fun MemberBuildAction.byTwoPrimitives(setup: PairPrimitiveMemberDefinition.() -> Unit): PairPrimitiveMemberDefinition =
|
||||
fun Action<MemberBuilder>.byTwoPrimitives(setup: PairPrimitiveMemberDefinition.() -> Unit): PairPrimitiveMemberDefinition =
|
||||
PairPrimitiveMemberDefinition().apply {
|
||||
builder(this@byTwoPrimitives)
|
||||
setup()
|
||||
@@ -48,35 +49,45 @@ fun pvar(name: String, setup: FamilyPrimitiveMemberDefinition.() -> Unit): Famil
|
||||
setup()
|
||||
}
|
||||
|
||||
|
||||
interface MemberTemplate {
|
||||
/** Specifies which platforms this member template should be generated for */
|
||||
fun platforms(vararg platforms: Platform)
|
||||
|
||||
fun instantiate(targets: Collection<KotlinTarget> = KotlinTarget.values): Sequence<MemberBuilder>
|
||||
|
||||
/** Registers parameterless member builder function */
|
||||
fun builder(b: MemberBuildAction)
|
||||
fun test(defaultSignature: String): Action<TestBuilder> = {
|
||||
this.signature = defaultSignature
|
||||
}
|
||||
|
||||
infix fun <MT: MemberTemplate> MT.builder(b: MemberBuildAction): MT = apply { builder(b) }
|
||||
infix fun <TParam, MT : MemberTemplateDefinition<TParam>> MT.builderWith(b: MemberBuildActionP<TParam>): MT = apply { builderWith(b) }
|
||||
fun test(defaultSignature: String, setup: FamilyPrimitiveTestDefinition.() -> Unit): FamilyPrimitiveTestDefinition =
|
||||
FamilyPrimitiveTestDefinition().apply {
|
||||
builder(test(defaultSignature))
|
||||
setup()
|
||||
}
|
||||
|
||||
abstract class MemberTemplateDefinition<TParam> : MemberTemplate {
|
||||
|
||||
sealed class BuildAction {
|
||||
class Generic(val action: MemberBuildAction) : BuildAction() {
|
||||
operator fun invoke(builder: MemberBuilder) { action(builder) }
|
||||
interface SourceTemplate<TBuilder> {
|
||||
/** Specifies which platforms this source template should be generated for */
|
||||
fun platforms(vararg platforms: Platform)
|
||||
|
||||
fun instantiate(targets: Collection<KotlinTarget> = KotlinTarget.values): Sequence<TBuilder>
|
||||
|
||||
/** Registers parameterless source builder function */
|
||||
fun builder(b: Action<TBuilder>)
|
||||
}
|
||||
|
||||
infix fun <TBuilder, MT : SourceTemplate<TBuilder>> MT.builder(b: Action<TBuilder>): MT = apply { builder(b) }
|
||||
infix fun <TBuilder, TParam, MT : SourceTemplateDefinition<TBuilder, TParam>> MT.builderWith(b: ActionP<TBuilder, TParam>): MT = apply { builderWith(b) }
|
||||
|
||||
abstract class SourceTemplateDefinition<TBuilder : TemplateBuilderBase, TParam> : SourceTemplate<TBuilder> {
|
||||
|
||||
sealed class BuildAction<TBuilder> {
|
||||
class Generic<TBuilder>(val action: Action<TBuilder>) : BuildAction<TBuilder>() {
|
||||
operator fun invoke(builder: TBuilder) { action(builder) }
|
||||
}
|
||||
class Parametrized(val action: MemberBuildActionP<*>) : BuildAction() {
|
||||
class Parametrized<TBuilder>(val action: ActionP<TBuilder, *>) : BuildAction<TBuilder>() {
|
||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "UNCHECKED_CAST")
|
||||
operator fun <TParam> invoke(builder: MemberBuilder, p: @kotlin.internal.NoInfer TParam) {
|
||||
(action as MemberBuildActionP<TParam>).invoke(builder, p)
|
||||
operator fun <TParam> invoke(builder: TBuilder, p: @kotlin.internal.NoInfer TParam) {
|
||||
(action as ActionP<TBuilder, TParam>).invoke(builder, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val buildActions = mutableListOf<BuildAction>()
|
||||
private val buildActions = mutableListOf<BuildAction<TBuilder>>()
|
||||
|
||||
private var allowedPlatforms = setOf(*Platform.values())
|
||||
override fun platforms(vararg platforms: Platform) {
|
||||
@@ -90,9 +101,9 @@ abstract class MemberTemplateDefinition<TParam> : MemberTemplate {
|
||||
this.filterPredicate = predicate
|
||||
}
|
||||
|
||||
override fun builder(b: MemberBuildAction) { buildActions += BuildAction.Generic(b) }
|
||||
override fun builder(b: Action<TBuilder>) { buildActions += BuildAction.Generic(b) }
|
||||
/** Registers member builder function with the parameter(s) of this DSL */
|
||||
fun builderWith(b: MemberBuildActionP<TParam>) { buildActions += BuildAction.Parametrized(b) }
|
||||
fun builderWith(b: ActionP<TBuilder, TParam>) { buildActions += BuildAction.Parametrized(b) }
|
||||
|
||||
|
||||
|
||||
@@ -105,43 +116,44 @@ abstract class MemberTemplateDefinition<TParam> : MemberTemplate {
|
||||
} ?: this
|
||||
|
||||
|
||||
override fun instantiate(targets: Collection<KotlinTarget>): Sequence<MemberBuilder> {
|
||||
override fun instantiate(targets: Collection<KotlinTarget>): Sequence<TBuilder> {
|
||||
val resultingTargets = targets.filter { it.platform in allowedPlatforms }
|
||||
val resultingPlatforms = resultingTargets.map { it.platform }.distinct()
|
||||
val specificTargets by lazy { resultingTargets - KotlinTarget.Common }
|
||||
|
||||
fun platformMemberBuilders(family: Family, p: TParam) =
|
||||
if (Platform.Common in allowedPlatforms) {
|
||||
val commonMemberBuilder = createMemberBuilder(KotlinTarget.Common, family, p)
|
||||
mutableListOf<MemberBuilder>().also { builders ->
|
||||
if (Platform.Common in resultingPlatforms) builders.add(commonMemberBuilder)
|
||||
if (commonMemberBuilder.hasPlatformSpecializations) {
|
||||
specificTargets.mapTo(builders) {
|
||||
createMemberBuilder(it, family, p)
|
||||
}
|
||||
fun platformBuilders(family: Family, p: TParam) =
|
||||
if (Platform.Common in allowedPlatforms) {
|
||||
val commonBuilder = createAndSetUpBuilder(KotlinTarget.Common, family, p)
|
||||
mutableListOf<TBuilder>().also { builders ->
|
||||
if (Platform.Common in resultingPlatforms) builders.add(commonBuilder)
|
||||
if (commonBuilder.hasPlatformSpecializations) {
|
||||
specificTargets.mapTo(builders) {
|
||||
createAndSetUpBuilder(it, family, p)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resultingTargets.map { createMemberBuilder(it, family, p) }
|
||||
}
|
||||
} else {
|
||||
resultingTargets.map { createAndSetUpBuilder(it, family, p) }
|
||||
}
|
||||
|
||||
return parametrize()
|
||||
.applyFilter()
|
||||
.map { (family, p) -> platformMemberBuilders(family, p) }
|
||||
.flatten()
|
||||
.applyFilter()
|
||||
.map { (family, p) -> platformBuilders(family, p) }
|
||||
.flatten()
|
||||
}
|
||||
|
||||
private fun createMemberBuilder(target: KotlinTarget, family: Family, p: TParam): MemberBuilder {
|
||||
return MemberBuilder(allowedPlatforms, target, family).also { builder ->
|
||||
private fun createAndSetUpBuilder(target: KotlinTarget, family: Family, p: TParam): TBuilder {
|
||||
return createBuilder(allowedPlatforms, target, family).also { builder ->
|
||||
for (action in buildActions) {
|
||||
when (action) {
|
||||
is BuildAction.Generic -> action(builder)
|
||||
is BuildAction.Parametrized -> action<TParam>(builder, p)
|
||||
is BuildAction.Generic<TBuilder> -> action(builder)
|
||||
is BuildAction.Parametrized<TBuilder> -> action<TParam>(builder, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun createBuilder(allowedPlatforms: Set<Platform>, target: KotlinTarget, family: Family): TBuilder
|
||||
}
|
||||
|
||||
|
||||
@@ -154,7 +166,7 @@ private fun defaultPrimitives(f: Family): Set<PrimitiveType> =
|
||||
}
|
||||
|
||||
@TemplateDsl
|
||||
class FamilyPrimitiveMemberDefinition : MemberTemplateDefinition<PrimitiveType?>() {
|
||||
abstract class FamilyPrimitiveTemplateDefinitionBase<TBuilder : TemplateBuilderBase> : SourceTemplateDefinition<TBuilder, PrimitiveType?>() {
|
||||
|
||||
private val familyPrimitives = mutableMapOf<Family, Set<PrimitiveType?>>()
|
||||
|
||||
@@ -198,7 +210,21 @@ class FamilyPrimitiveMemberDefinition : MemberTemplateDefinition<PrimitiveType?>
|
||||
}
|
||||
|
||||
@TemplateDsl
|
||||
class PairPrimitiveMemberDefinition : MemberTemplateDefinition<Pair<PrimitiveType, PrimitiveType>>() {
|
||||
class FamilyPrimitiveMemberDefinition : FamilyPrimitiveTemplateDefinitionBase<MemberBuilder>() {
|
||||
override fun createBuilder(allowedPlatforms: Set<Platform>, target: KotlinTarget, family: Family): MemberBuilder {
|
||||
return MemberBuilder(allowedPlatforms, target, family)
|
||||
}
|
||||
}
|
||||
|
||||
@TemplateDsl
|
||||
class FamilyPrimitiveTestDefinition : FamilyPrimitiveTemplateDefinitionBase<TestBuilder>() {
|
||||
override fun createBuilder(allowedPlatforms: Set<Platform>, target: KotlinTarget, family: Family): TestBuilder {
|
||||
return TestBuilder(allowedPlatforms, target, family)
|
||||
}
|
||||
}
|
||||
|
||||
@TemplateDsl
|
||||
class PairPrimitiveMemberDefinition : SourceTemplateDefinition<MemberBuilder, Pair<PrimitiveType, PrimitiveType>>() {
|
||||
|
||||
private val familyPrimitives = mutableMapOf<Family, Set<Pair<PrimitiveType, PrimitiveType>>>()
|
||||
|
||||
@@ -212,6 +238,10 @@ class PairPrimitiveMemberDefinition : MemberTemplateDefinition<Pair<PrimitiveTyp
|
||||
.asSequence()
|
||||
}
|
||||
|
||||
override fun createBuilder(allowedPlatforms: Set<Platform>, target: KotlinTarget, family: Family): MemberBuilder {
|
||||
return MemberBuilder(allowedPlatforms, target, family)
|
||||
}
|
||||
|
||||
init {
|
||||
builderWith { (p1, p2) -> primitive = p1 }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates
|
||||
|
||||
import templates.Family.*
|
||||
import java.lang.IllegalArgumentException
|
||||
import java.util.*
|
||||
|
||||
@TemplateDsl
|
||||
class TestBuilder(
|
||||
allowedPlatforms: Set<Platform>,
|
||||
target: KotlinTarget,
|
||||
family: Family,
|
||||
primitive: PrimitiveType? = null
|
||||
) : TemplateBuilderBase(allowedPlatforms, target, family, primitive) {
|
||||
|
||||
override val hasPlatformSpecializations: Boolean get() = false
|
||||
|
||||
var body: String? = null; private set
|
||||
|
||||
fun body(valueBuilder: TestExtension.() -> String) {
|
||||
body = valueBuilder(test)
|
||||
}
|
||||
fun body(f: Family, valueBuilder: TestExtension.() -> String) {
|
||||
if (family == f) {
|
||||
body(valueBuilder)
|
||||
}
|
||||
}
|
||||
fun body(p: PrimitiveType, valueBuilder: TestExtension.() -> String) {
|
||||
if (primitive == p) {
|
||||
body(valueBuilder)
|
||||
}
|
||||
}
|
||||
fun body(vararg families: Family, valueBuilder: TestExtension.() -> String) {
|
||||
if (family in families) {
|
||||
body(valueBuilder)
|
||||
}
|
||||
}
|
||||
fun body(vararg primitives: PrimitiveType, valueBuilder: TestExtension.() -> String) {
|
||||
if (primitive in primitives) {
|
||||
body(valueBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun bodyAppend(valueBuilder: TestExtension.() -> String) {
|
||||
body += "\n" + valueBuilder(test)
|
||||
}
|
||||
fun bodyAppend(f: Family, valueBuilder: TestExtension.() -> String) {
|
||||
if (family == f) {
|
||||
bodyAppend(valueBuilder)
|
||||
}
|
||||
}
|
||||
fun bodyAppend(p: PrimitiveType, valueBuilder: TestExtension.() -> String) {
|
||||
if (primitive == p) {
|
||||
bodyAppend(valueBuilder)
|
||||
}
|
||||
}
|
||||
fun bodyAppend(vararg families: Family, valueBuilder: TestExtension.() -> String) {
|
||||
if (family in families) {
|
||||
bodyAppend(valueBuilder)
|
||||
}
|
||||
}
|
||||
fun bodyAppend(vararg primitives: PrimitiveType, valueBuilder: TestExtension.() -> String) {
|
||||
if (primitive in primitives) {
|
||||
bodyAppend(valueBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
fun on(platform: Platform, action: () -> Unit) {
|
||||
require(platform in allowedPlatforms) { "Platform $platform is not in the list of allowed platforms $allowedPlatforms" }
|
||||
if (target.platform == platform)
|
||||
action()
|
||||
}
|
||||
|
||||
// toPrimitiveArray
|
||||
// runningFold
|
||||
// reverseRange
|
||||
// sortedTests
|
||||
// fill
|
||||
|
||||
override fun build(builder: Appendable) {
|
||||
|
||||
fun Appendable.appendIndented(csq: CharSequence): Appendable = append(" ").append(csq)
|
||||
|
||||
annotations.forEach { builder.appendIndented(it.trimIndent()).append('\n') }
|
||||
|
||||
if (suppressions.isNotEmpty()) {
|
||||
suppressions.joinTo(builder, separator = ", ", prefix = "@Suppress(", postfix = ")\n") {
|
||||
""""$it""""
|
||||
}
|
||||
}
|
||||
|
||||
builder.appendIndented("@Test\n")
|
||||
|
||||
val testNameSuffix = when (f) {
|
||||
ArraysOfPrimitives, ArraysOfUnsigned, ArraysOfObjects -> (primitive?.name ?: "") + "Array"
|
||||
Primitives, Unsigned -> primitive!!.name
|
||||
InvariantArraysOfObjects -> throw IllegalArgumentException("$signature test: invariant arrays are not supported yet")
|
||||
else -> f.name.let { if (it.last() == 's') it.dropLast(1) else it }
|
||||
}
|
||||
|
||||
val fullSignature = signature.indexOf('(').let { "${signature.substring(0 until it)}_$testNameSuffix${signature.substring(it)}" }
|
||||
|
||||
builder.appendIndented("fun $fullSignature {")
|
||||
|
||||
val body = (body ?:
|
||||
"""TODO("Body is not provided")""".also { System.err.println("ERROR: $fullSignature for ${target.fullName}: no body specified for ${family to primitive}") }
|
||||
).trim('\n')
|
||||
val indent: Int = body.takeWhile { it == ' ' }.length
|
||||
|
||||
builder.append('\n')
|
||||
body.lineSequence().forEach {
|
||||
var count = indent
|
||||
val line = it.dropWhile { count-- > 0 && it == ' ' }.replaceKeywords()
|
||||
if (line.isNotEmpty()) {
|
||||
builder.appendIndented(" ").append(line).append("\n")
|
||||
}
|
||||
}
|
||||
|
||||
builder.appendIndented("}\n\n")
|
||||
}
|
||||
|
||||
private fun String.replaceKeywords(): String {
|
||||
val t = StringTokenizer(this, " \t\n,:()<>?.", true)
|
||||
val answer = StringBuilder()
|
||||
|
||||
while (t.hasMoreTokens()) {
|
||||
val token = t.nextToken()
|
||||
answer.append(
|
||||
when (token) {
|
||||
"PRIMITIVE" -> primitive?.name ?: token
|
||||
"ZERO" -> test.literal(0)
|
||||
"-ZERO" -> "-" + test.literal(0)
|
||||
"ONE" -> test.literal(1)
|
||||
"-ONE" -> test.literal(-1)
|
||||
"TWO" -> test.literal(2)
|
||||
"THREE" -> test.literal(3)
|
||||
"MAX_VALUE" -> test.P + ".MAX_VALUE"
|
||||
"MIN_VALUE" -> test.P + ".MIN_VALUE"
|
||||
"-MAX_VALUE" -> "-" + test.P + ".MAX_VALUE"
|
||||
"-MIN_VALUE" -> "-" + test.P + ".MIN_VALUE"
|
||||
"POSITIVE_INFINITY" -> test.P + ".POSITIVE_INFINITY"
|
||||
"NEGATIVE_INFINITY" -> test.P + ".NEGATIVE_INFINITY"
|
||||
else -> token
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
return answer.toString()
|
||||
}
|
||||
|
||||
val test = TestExtension()
|
||||
|
||||
inner class TestExtension {
|
||||
fun Family.of(vararg values: Int): String = when (this) {
|
||||
ArraysOfPrimitives, ArraysOfUnsigned -> "$of(${values.joinToString { literal(it) }})"
|
||||
else -> "$of<$P>(${values.joinToString { literal(it) }})"
|
||||
}
|
||||
|
||||
fun of(vararg values: Int): String = family.of(*values)
|
||||
|
||||
val Family.of: String
|
||||
get() = when (this) {
|
||||
ArraysOfPrimitives, ArraysOfUnsigned -> "${primitive!!.name.toLowerCase()}ArrayOf"
|
||||
ArraysOfObjects -> "arrayOf"
|
||||
Iterables, Lists -> "listOf"
|
||||
Sequences -> "sequenceOf"
|
||||
else -> throw IllegalArgumentException(this.toString())
|
||||
}
|
||||
|
||||
val of: String
|
||||
get() = family.of
|
||||
|
||||
val mutableOf: String
|
||||
get() = when (family) {
|
||||
Lists, Iterables -> "mutableListOf"
|
||||
ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned -> of
|
||||
else -> throw IllegalArgumentException(this.toString())
|
||||
}
|
||||
|
||||
val P: String
|
||||
get() = primitive?.name ?: "Int"
|
||||
|
||||
val Family.assertEquals: String
|
||||
get() = when (this) {
|
||||
ArraysOfPrimitives, ArraysOfUnsigned, ArraysOfObjects -> "assertArrayContentEquals"
|
||||
else -> "assertEquals"
|
||||
}
|
||||
|
||||
val assertEquals: String
|
||||
get() = family.assertEquals
|
||||
|
||||
fun literal(value: Int): String {
|
||||
return when (primitive) {
|
||||
PrimitiveType.Byte, PrimitiveType.Short, PrimitiveType.Int -> "$value"
|
||||
PrimitiveType.Long -> "${value}L"
|
||||
PrimitiveType.UByte, PrimitiveType.UShort, PrimitiveType.UInt -> "${value}u"
|
||||
PrimitiveType.ULong -> "${value}uL"
|
||||
PrimitiveType.Float -> "$value.0f"
|
||||
PrimitiveType.Double -> "$value.0"
|
||||
else -> "$value"
|
||||
}
|
||||
}
|
||||
|
||||
fun toP(value: Int): String {
|
||||
return when (primitive) {
|
||||
PrimitiveType.Byte, PrimitiveType.Short, PrimitiveType.UByte, PrimitiveType.UShort -> "$value.to$P()"
|
||||
else -> literal(value)
|
||||
}
|
||||
}
|
||||
|
||||
fun toString(value: Int): String {
|
||||
return when (primitive) {
|
||||
PrimitiveType.Float, PrimitiveType.Double -> "$value.0"
|
||||
else -> "$value"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UseExperimental(ExperimentalUnsignedTypes::class)
|
||||
val PrimitiveType.SIZE_BITS: Int
|
||||
get() = when (this) {
|
||||
PrimitiveType.Byte -> Byte.SIZE_BITS
|
||||
PrimitiveType.Short -> Short.SIZE_BITS
|
||||
PrimitiveType.Int -> Int.SIZE_BITS
|
||||
PrimitiveType.Long -> Long.SIZE_BITS
|
||||
PrimitiveType.Float -> Float.SIZE_BITS
|
||||
PrimitiveType.Double -> Double.SIZE_BITS
|
||||
PrimitiveType.Boolean -> throw IllegalArgumentException(this.toString())
|
||||
PrimitiveType.Char -> Char.SIZE_BITS
|
||||
PrimitiveType.UByte -> UByte.SIZE_BITS
|
||||
PrimitiveType.UShort -> UShort.SIZE_BITS
|
||||
PrimitiveType.UInt -> UInt.SIZE_BITS
|
||||
PrimitiveType.ULong -> ULong.SIZE_BITS
|
||||
}
|
||||
|
||||
fun PrimitiveType.randomNext(): String = randomNext(range = "")
|
||||
|
||||
fun PrimitiveType.randomNextFrom(from: Int): String =
|
||||
if (this == PrimitiveType.Boolean)
|
||||
throw IllegalArgumentException(this.toString())
|
||||
else if (name.endsWith("Int") || name.endsWith("Long"))
|
||||
randomNext(range = "${literal(from)}..MAX_VALUE")
|
||||
else if (this == PrimitiveType.Double)
|
||||
randomNext(range = "$from.0, MAX_VALUE")
|
||||
else if (this == PrimitiveType.Float)
|
||||
randomNext(range = "$from.0, MAX_VALUE.toDouble()")
|
||||
else
|
||||
randomNext(range = "$from..MAX_VALUE.toInt()")
|
||||
|
||||
fun PrimitiveType.randomNextUntil(until: Int): String =
|
||||
if (this == PrimitiveType.Boolean)
|
||||
throw IllegalArgumentException(this.toString())
|
||||
else if (name.endsWith("Int") || name.endsWith("Long"))
|
||||
randomNext(range = "MIN_VALUE, ${literal(until)}")
|
||||
else if (this == PrimitiveType.Double)
|
||||
randomNext(range = "MIN_VALUE, $until.0")
|
||||
else if (this == PrimitiveType.Float)
|
||||
randomNext(range = "MIN_VALUE.toDouble(), $until.0")
|
||||
else
|
||||
randomNext(range = "MIN_VALUE.toInt(), $until")
|
||||
|
||||
private fun PrimitiveType.randomNext(range: String): String =
|
||||
"Random." + if (name.endsWith("Int") || name.endsWith("Long") || this == PrimitiveType.Double)
|
||||
"next$name($range)"
|
||||
else if (this == PrimitiveType.Boolean) {
|
||||
check(range.isEmpty())
|
||||
"nextBoolean()"
|
||||
} else if (this == PrimitiveType.Float)
|
||||
if (range.isEmpty()) "nextFloat()" else "nextDouble($range).toFloat()"
|
||||
else
|
||||
"nextInt($range).to$name()"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun Family.isArray(): Boolean = when (this) {
|
||||
ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
@@ -7,20 +7,28 @@ package templates
|
||||
|
||||
import org.xml.sax.InputSource
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.Reader
|
||||
import javax.xml.xpath.XPathFactory
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
val COMMON_AUTOGENERATED_WARNING: String = """//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
|
||||
fun autogeneratedWarning(generator: String): String = """//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the $generator
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//"""
|
||||
|
||||
lateinit var COPYRIGHT_NOTICE: String
|
||||
|
||||
fun readCopyrightNoticeFromProfile(copyrightProfile: File): String = readCopyrightNoticeFromProfile { copyrightProfile.reader() }
|
||||
private var copyrightNotice: String? = null
|
||||
|
||||
fun readCopyrightNoticeFromProfile(getCopyrightReader: () -> Reader): String {
|
||||
fun copyrightNotice(): String {
|
||||
val cur = copyrightNotice
|
||||
return if (cur == null) {
|
||||
val new = readCopyrightNoticeFromProfile { Thread.currentThread().contextClassLoader.getResourceAsStream("apache.xml").reader() }
|
||||
copyrightNotice = new
|
||||
return new
|
||||
} else cur
|
||||
}
|
||||
|
||||
private fun readCopyrightNoticeFromProfile(getCopyrightReader: () -> Reader): String {
|
||||
val template = getCopyrightReader().use { reader ->
|
||||
XPathFactory.newInstance().newXPath().evaluate("/component/copyright/option[@name='notice']/@value", InputSource(reader))
|
||||
}
|
||||
@@ -32,86 +40,82 @@ fun readCopyrightNoticeFromProfile(getCopyrightReader: () -> Reader): String {
|
||||
}
|
||||
|
||||
|
||||
interface SourceGeneratorStrategy<TBuilder> {
|
||||
val supportedTargets: List<KotlinTarget>
|
||||
fun baseDir(target: KotlinTarget): String
|
||||
fun fileName(target: KotlinTarget, source: SourceFile): String
|
||||
val writer: TemplateWriter<TBuilder>
|
||||
}
|
||||
|
||||
interface TemplateWriter<TBuilder> {
|
||||
fun writeTo(file: File, builders: List<TBuilder>, targetedSource: TargetedSourceFile)
|
||||
}
|
||||
|
||||
data class TargetedSourceFile(
|
||||
val target: KotlinTarget,
|
||||
val sourceFile: SourceFile
|
||||
)
|
||||
|
||||
@JvmName("groupByFileAndWriteGroups")
|
||||
fun Sequence<TemplateGroup>.groupByFileAndWrite(
|
||||
targetsToGenerate: Collection<KotlinTarget>,
|
||||
fileNameBuilder: (TargetedSourceFile) -> File
|
||||
|
||||
fun <TBuilder : TemplateBuilderBase> Sequence<TemplateGroup<TBuilder>>.generateSources(args: Array<String>, strategy: SourceGeneratorStrategy<TBuilder>) {
|
||||
val targetBaseDirs = parseTargetBaseDirs(args, strategy)
|
||||
|
||||
this.groupByFileAndWriteWith(writer = strategy.writer, targetsToGenerate = targetBaseDirs.keys) { (target, source) ->
|
||||
val targetDir = targetBaseDirs[target] ?: error("Target $target directory is not configured")
|
||||
val fileName = strategy.fileName(target, source)
|
||||
targetDir.resolve(fileName)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseTargetBaseDirs(args: Array<String>, strategy: SourceGeneratorStrategy<*>): Map<KotlinTarget, File> = when (args.size) {
|
||||
1 -> {
|
||||
val baseDir = File(args.first())
|
||||
val targetsToGenerate = strategy.supportedTargets
|
||||
targetsToGenerate.associateWith { baseDir.resolveExistingDir(strategy.baseDir(it)) }
|
||||
}
|
||||
2 -> {
|
||||
val (targetName, targetDir) = args
|
||||
val target = KotlinTarget.values.singleOrNull { it.name.equals(targetName, ignoreCase = true) } ?: error("Invalid target: $targetName")
|
||||
mapOf(target to File(targetDir).also { it.requireExistingDir() })
|
||||
}
|
||||
else -> {
|
||||
println(
|
||||
"""
|
||||
Parameters:
|
||||
<kotlin-base-dir> - generates sources for common, jvm, js, ir-js targets using paths derived from specified base path
|
||||
<target> <target-dir> - generates source for the specified target in the specified target directory
|
||||
"""
|
||||
)
|
||||
exitProcess(1)
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.resolveExistingDir(subpath: String) = resolve(subpath).also { it.requireExistingDir() }
|
||||
|
||||
private fun File.requireExistingDir() {
|
||||
require(isDirectory) { "Directory $this doesn't exist"}
|
||||
}
|
||||
|
||||
private fun <TBuilder : TemplateBuilderBase> Sequence<TemplateGroup<TBuilder>>.groupByFileAndWriteWith(
|
||||
writer: TemplateWriter<TBuilder>,
|
||||
targetsToGenerate: Collection<KotlinTarget>,
|
||||
fileNameBuilder: (TargetedSourceFile) -> File
|
||||
) {
|
||||
flatMap { group ->
|
||||
group.invoke()
|
||||
.flatMap { it.instantiate(targetsToGenerate) }
|
||||
.sortedBy { it.sortingSignature }
|
||||
}.groupByFileAndWrite(fileNameBuilder)
|
||||
}.groupByFileAndWriteWith(writer, fileNameBuilder)
|
||||
}
|
||||
|
||||
@JvmName("groupByFileAndWriteTemplates")
|
||||
fun Sequence<MemberTemplate>.groupByFileAndWrite(
|
||||
targetsToGenerate: Collection<KotlinTarget>,
|
||||
fileNameBuilder: (TargetedSourceFile) -> File
|
||||
private fun <TBuilder : TemplateBuilderBase> Sequence<TBuilder>.groupByFileAndWriteWith(
|
||||
writer: TemplateWriter<TBuilder>,
|
||||
fileNameBuilder: (TargetedSourceFile) -> File
|
||||
) {
|
||||
flatMap { it.instantiate(targetsToGenerate) }
|
||||
.groupByFileAndWrite(fileNameBuilder)
|
||||
}
|
||||
val groupedBuilders = groupBy { TargetedSourceFile(it.target, it.sourceFile) }
|
||||
|
||||
@JvmName("groupByFileAndWriteMembers")
|
||||
fun Sequence<MemberBuilder>.groupByFileAndWrite(
|
||||
fileNameBuilder: (TargetedSourceFile) -> File
|
||||
) {
|
||||
val groupedMembers = groupBy { TargetedSourceFile(it.target, it.sourceFile) }
|
||||
|
||||
for ((psf, members) in groupedMembers) {
|
||||
for ((psf, builders) in groupedBuilders) {
|
||||
val file = fileNameBuilder(psf)
|
||||
members.writeTo(file, psf)
|
||||
}
|
||||
}
|
||||
|
||||
fun List<MemberBuilder>.writeTo(file: File, targetedSource: TargetedSourceFile) {
|
||||
val (target, sourceFile) = targetedSource
|
||||
println("Generating file: $file")
|
||||
file.parentFile.mkdirs()
|
||||
FileWriter(file).use { writer ->
|
||||
writer.appendln(COPYRIGHT_NOTICE)
|
||||
|
||||
when (target.platform) {
|
||||
Platform.Common, Platform.JVM -> {
|
||||
if (sourceFile.multifile) {
|
||||
writer.appendln("@file:kotlin.jvm.JvmMultifileClass")
|
||||
}
|
||||
|
||||
writer.appendln("@file:kotlin.jvm.JvmName(\"${sourceFile.jvmClassName}\")")
|
||||
sourceFile.jvmPackageName?.let {
|
||||
writer.appendln("@file:kotlin.jvm.JvmPackageName(\"$it\")")
|
||||
}
|
||||
writer.appendln()
|
||||
}
|
||||
}
|
||||
|
||||
writer.append("package ${sourceFile.packageName ?: "kotlin"}\n\n")
|
||||
writer.append("${COMMON_AUTOGENERATED_WARNING}\n\n")
|
||||
if (target.platform == Platform.JS) {
|
||||
writer.appendln("import kotlin.js.*")
|
||||
if (sourceFile == SourceFile.Arrays) {
|
||||
writer.appendln("import primitiveArrayConcat")
|
||||
writer.appendln("import withType")
|
||||
}
|
||||
}
|
||||
if (target.platform == Platform.Common) {
|
||||
writer.appendln("import kotlin.random.*")
|
||||
}
|
||||
if (sourceFile.packageName == "kotlin.collections") {
|
||||
writer.appendln("import kotlin.ranges.contains")
|
||||
writer.appendln("import kotlin.ranges.reversed")
|
||||
}
|
||||
|
||||
writer.appendln()
|
||||
|
||||
for (f in this) {
|
||||
f.build(writer)
|
||||
}
|
||||
writer.writeTo(file, builders, psf)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates.test
|
||||
|
||||
import templates.*
|
||||
import templates.Family.*
|
||||
|
||||
object AggregatesTest : TestTemplateGroupBase() {
|
||||
|
||||
val f_minByOrNull = test("minByOrNull()") {
|
||||
includeDefault()
|
||||
include(ArraysOfUnsigned)
|
||||
} builder {
|
||||
|
||||
body {
|
||||
"""
|
||||
assertEquals(null, ${of()}.minByOrNull { it })
|
||||
assertEquals(ONE, ${of(1)}.minByOrNull { it })
|
||||
assertEquals(TWO, ${of(3, 2)}.minByOrNull { it * it })
|
||||
assertEquals(THREE, ${of(3, 2)}.minByOrNull { "a" })
|
||||
assertEquals(TWO, ${of(3, 2)}.minByOrNull { it.toString() })
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
assertEquals(THREE, ${of(2, 3)}.minByOrNull { -it })
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects) {
|
||||
"""
|
||||
assertEquals('a', $of('a', 'b').minByOrNull { "x${"$"}it" })
|
||||
assertEquals("b", $of("b", "abc").minByOrNull { it.length })
|
||||
"""
|
||||
}
|
||||
|
||||
bodyAppend(PrimitiveType.Long) {
|
||||
"""
|
||||
assertEquals(2000000000000, longArrayOf(3000000000000, 2000000000000).minByOrNull { it + 1 })
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Boolean) {
|
||||
"""
|
||||
assertEquals(false, booleanArrayOf(true, false).minByOrNull { it.toString() })
|
||||
assertEquals(true, booleanArrayOf(true, false).minByOrNull { it.toString().length })
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"""
|
||||
assertEquals('a', charArrayOf('a', 'b').minByOrNull { "x${"$"}it" })
|
||||
assertEquals('b', charArrayOf('b', 'a').minByOrNull { "${"$"}it".length })
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_maxByOrNull = test("maxByOrNull()") {
|
||||
includeDefault()
|
||||
include(ArraysOfUnsigned)
|
||||
} builder {
|
||||
|
||||
body {
|
||||
"""
|
||||
assertEquals(null, ${of()}.maxByOrNull { it })
|
||||
assertEquals(ONE, ${of(1)}.maxByOrNull { it })
|
||||
assertEquals(THREE, ${of(3, 2)}.maxByOrNull { it * it })
|
||||
assertEquals(THREE, ${of(3, 2)}.maxByOrNull { "a" })
|
||||
assertEquals(THREE, ${of(3, 2)}.maxByOrNull { it.toString() })
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
assertEquals(TWO, ${of(2, 3)}.maxByOrNull { -it })
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects) {
|
||||
"""
|
||||
assertEquals('b', $of('a', 'b').maxByOrNull { "x${"$"}it" })
|
||||
assertEquals("abc", $of("b", "abc").maxByOrNull { it.length })
|
||||
"""
|
||||
}
|
||||
|
||||
bodyAppend(PrimitiveType.Long) {
|
||||
"""
|
||||
assertEquals(3000000000000, longArrayOf(3000000000000, 2000000000000).maxByOrNull { it + 1 })
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Boolean) {
|
||||
"""
|
||||
assertEquals(true, booleanArrayOf(true, false).maxByOrNull { it.toString() })
|
||||
assertEquals(false, booleanArrayOf(true, false).maxByOrNull { it.toString().length })
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"""
|
||||
assertEquals('b', charArrayOf('a', 'b').maxByOrNull { "x${"$"}it" })
|
||||
assertEquals('b', charArrayOf('b', 'a').maxByOrNull { "${"$"}it".length })
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_minWithOrNull = test("minWithOrNull()") {
|
||||
includeDefault()
|
||||
include(ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean, PrimitiveType.Char)
|
||||
} builder {
|
||||
|
||||
body {
|
||||
"""
|
||||
assertEquals(null, ${of()}.minWithOrNull(naturalOrder()))
|
||||
assertEquals(ONE, ${of(1)}.minWithOrNull(naturalOrder()))
|
||||
assertEquals(${literal(4)}, ${of(2, 3, 4)}.minWithOrNull(compareBy { it % ${literal(4)} }))
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects) {
|
||||
"""
|
||||
assertEquals("a", $of("a", "B").minWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_maxWithOrNull = test("maxWithOrNull()") {
|
||||
includeDefault()
|
||||
include(ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean, PrimitiveType.Char)
|
||||
} builder {
|
||||
|
||||
body {
|
||||
"""
|
||||
assertEquals(null, ${of()}.maxWithOrNull(naturalOrder()))
|
||||
assertEquals(ONE, ${of(1)}.maxWithOrNull(naturalOrder()))
|
||||
assertEquals(${literal(3)}, ${of(2, 3, 4)}.maxWithOrNull(compareBy { it % ${literal(4)} }))
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects) {
|
||||
"""
|
||||
assertEquals("B", $of("a", "B").maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER))
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_foldIndexed = test("foldIndexed()") {
|
||||
includeDefault()
|
||||
include(ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean, PrimitiveType.Char)
|
||||
} builder {
|
||||
|
||||
body {
|
||||
"""
|
||||
expect(${literal(8)}) { ${of(1, 2, 3)}.foldIndexed(ZERO) { i, acc, e -> acc + i.to$P() * e } }
|
||||
expect(10) { ${of(1, 2, 3)}.foldIndexed(1) { i, acc, e -> acc + i + e.toInt() } }
|
||||
expect(${literal(15)}) { ${of(1, 2, 3)}.foldIndexed(ONE) { i, acc, e -> acc * (i.to$P() + e) } }
|
||||
expect(" 0-${toString(1)} 1-${toString(2)} 2-${toString(3)}") { ${of(1, 2, 3)}.foldIndexed("") { i, acc, e -> "${"$"}acc ${"$"}i-${"$"}e" } }
|
||||
|
||||
expect(${literal(42)}) {
|
||||
val numbers = ${of(1, 2, 3, 4)}
|
||||
numbers.foldIndexed(ZERO) { index, a, b -> index.to$P() * (a + b) }
|
||||
}
|
||||
|
||||
expect(ZERO) {
|
||||
val numbers = ${of()}
|
||||
numbers.foldIndexed(ZERO) { index, a, b -> index.to$P() * (a + b) }
|
||||
}
|
||||
|
||||
expect("${toString(1)}${toString(1)}${toString(2)}${toString(3)}${toString(4)}") {
|
||||
val numbers = ${of(1, 2, 3, 4)}
|
||||
numbers.map { it.toString() }.foldIndexed("") { index, a, b -> if (index == 0) a + b + b else a + b }
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_foldRightIndexed = test("foldRightIndexed()") {
|
||||
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean, PrimitiveType.Char)
|
||||
} builder {
|
||||
|
||||
body {
|
||||
"""
|
||||
expect(${literal(8)}) { ${of(1, 2, 3)}.foldRightIndexed(ZERO) { i, e, acc -> acc + i.to$P() * e } }
|
||||
expect(10) { ${of(1, 2, 3)}.foldRightIndexed(1) { i, e, acc -> acc + i + e.toInt() } }
|
||||
expect(${literal(15)}) { ${of(1, 2, 3)}.foldRightIndexed(ONE) { i, e, acc -> acc * (i.to$P() + e) } }
|
||||
expect(" 2-${toString(3)} 1-${toString(2)} 0-${toString(1)}") { ${of(1, 2, 3)}.foldRightIndexed("") { i, e, acc -> "${"$"}acc ${"$"}i-${"$"}e" } }
|
||||
|
||||
expect("${toString(1)}${toString(2)}${toString(3)}${toString(4)}3210") {
|
||||
val numbers = ${of(1, 2, 3, 4)}
|
||||
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates.test
|
||||
|
||||
import templates.*
|
||||
import templates.Family.*
|
||||
|
||||
object ArraysTest : TestTemplateGroupBase() {
|
||||
|
||||
val f_copyInto = test("copyInto()") {
|
||||
include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
} builder {
|
||||
body {
|
||||
"""
|
||||
val dest = ${of(1, 2, 3)}
|
||||
val newValues = ${of(4, 5, 6)}
|
||||
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
val result1 = ${of(5, 6, 3)}
|
||||
assertTrue(result1 contentEquals dest, "Copying from newValues: ${"$"}{result1.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
val result2 = ${of(6, 3, 3)}
|
||||
assertTrue(result2 contentEquals dest, "Overlapping backward copy: ${"$"}{result2.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
val result3 = ${of(6, 6, 3)}
|
||||
assertTrue(result3 contentEquals dest, "Overlapping forward copy: ${"$"}{result2.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"""
|
||||
val dest = $of('a', 'b', 'c')
|
||||
val newValues = $of('e', 'f', 'g')
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
|
||||
val result1 = $of('f', 'g', 'c')
|
||||
assertTrue(result1 contentEquals dest, "Copying from newValues: ${"$"}{result1.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
val result2 = $of('g', 'c', 'c')
|
||||
assertTrue(result2 contentEquals dest, "Overlapping backward copy: ${"$"}{result2.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
val result3 = $of('g', 'g', 'c')
|
||||
assertTrue(result3 contentEquals dest, "Overlapping forward copy: ${"$"}{result2.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
"""
|
||||
}
|
||||
body(ArraysOfObjects) {
|
||||
"""
|
||||
val dest = arrayOf("a", "b", "c")
|
||||
val newValues = arrayOf("e", "f", "g")
|
||||
newValues.copyInto(dest, 0, 1, 3)
|
||||
|
||||
val result1 = arrayOf("f", "g", "c")
|
||||
assertTrue(result1 contentEquals dest, "Copying from newValues: ${"$"}{result1.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
|
||||
dest.copyInto(dest, 0, 1, 3)
|
||||
val result2 = arrayOf("g", "c", "c")
|
||||
assertTrue(result2 contentEquals dest, "Overlapping backward copy: ${"$"}{result2.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
|
||||
dest.copyInto(dest, 1, 0, 2)
|
||||
val result3 = arrayOf("g", "g", "c")
|
||||
assertTrue(result3 contentEquals dest, "Overlapping forward copy: ${"$"}{result2.contentToString()}, ${"$"}{dest.contentToString()}")
|
||||
"""
|
||||
}
|
||||
bodyAppend {
|
||||
"""
|
||||
for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0, 0 to -1)) {
|
||||
val bounds = "start: ${"$"}start, end: ${"$"}end"
|
||||
val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) }
|
||||
assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: ${"$"}ex")
|
||||
}
|
||||
for (destIndex in listOf(-1, 2, 4)) {
|
||||
assertFailsWith<IndexOutOfBoundsException>("index: ${"$"}destIndex") { newValues.copyInto(dest, destIndex, 0, 2) }
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates.test
|
||||
|
||||
import templates.*
|
||||
import templates.Family.*
|
||||
|
||||
object ComparablesTest : TestTemplateGroupBase() {
|
||||
|
||||
private val Family.sourceFileComparisons: SourceFile
|
||||
get() = when (this) {
|
||||
Generic, Primitives -> SourceFile.Comparisons
|
||||
Unsigned -> SourceFile.UComparisons
|
||||
else -> error(this)
|
||||
}
|
||||
|
||||
|
||||
val f_minOf_2 = test("minOf_2()") {
|
||||
include(Generic)
|
||||
include(Primitives, PrimitiveType.numericPrimitives)
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
sourceFile(f.sourceFileComparisons)
|
||||
|
||||
val p = primitive ?: PrimitiveType.Int
|
||||
val minOf = if (f == Generic) "minOf<${p.name}>" else "minOf"
|
||||
body {
|
||||
"""
|
||||
expect(${toP(1)}) { $minOf(${toP(2)}, ${toP(1)}) }
|
||||
expect(${toP(58)}) { $minOf(${toP(58)}, ${toP(126)}) }
|
||||
expect(${toP(23)}) { $minOf(${p.randomNextFrom(23)}, ${toP(23)}) }
|
||||
expect(MIN_VALUE) { $minOf(MIN_VALUE, MAX_VALUE) }
|
||||
expect(MAX_VALUE) { $minOf(MAX_VALUE, MAX_VALUE) }
|
||||
"""
|
||||
}
|
||||
if (p.isFloatingPoint()) {
|
||||
bodyAppend {
|
||||
"""
|
||||
assertEquals(-ZERO, $minOf(ZERO, -ZERO))
|
||||
assertEquals(-ZERO, $minOf(-ZERO, ZERO))
|
||||
assertEquals(NEGATIVE_INFINITY, $minOf(NEGATIVE_INFINITY, POSITIVE_INFINITY))
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_minOf_3 = test("minOf_3()") {
|
||||
include(Generic)
|
||||
include(Primitives, PrimitiveType.numericPrimitives)
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
sourceFile(f.sourceFileComparisons)
|
||||
|
||||
val p = primitive ?: PrimitiveType.Int
|
||||
val minOf = if (f == Generic) "minOf<${p.name}>" else "minOf"
|
||||
body {
|
||||
"""
|
||||
expect(${toP(1)}) { $minOf(${toP(2)}, ${toP(1)}, ${toP(3)}) }
|
||||
expect(${toP(55)}) { $minOf(${toP(58)}, ${toP(126)}, ${toP(55)}) }
|
||||
expect(${toP(23)}) { $minOf(${p.randomNextFrom(23)}, ${toP(23)}, ${p.randomNextFrom(23)}) }
|
||||
expect(MAX_VALUE) { $minOf(MAX_VALUE, MAX_VALUE, MAX_VALUE) }
|
||||
"""
|
||||
}
|
||||
if (p.isFloatingPoint()) {
|
||||
bodyAppend {
|
||||
"""
|
||||
expect(ZERO) { $minOf(MIN_VALUE, MAX_VALUE, ZERO) }
|
||||
assertEquals(-ZERO, $minOf(ZERO, -ZERO, -ZERO))
|
||||
assertEquals(-ZERO, $minOf(-ZERO, ZERO, ZERO))
|
||||
assertEquals(MIN_VALUE, $minOf(POSITIVE_INFINITY, MAX_VALUE, MIN_VALUE))
|
||||
"""
|
||||
}
|
||||
} else {
|
||||
bodyAppend {
|
||||
"""
|
||||
expect(MIN_VALUE) { $minOf(MIN_VALUE, MAX_VALUE, ZERO) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_minOf_vararg = test("minOf_vararg()") {
|
||||
include(Generic)
|
||||
include(Primitives, PrimitiveType.numericPrimitives)
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
sourceFile(f.sourceFileComparisons)
|
||||
|
||||
val p = primitive ?: PrimitiveType.Int
|
||||
val minOf = if (f == Generic) "minOf<${p.name}>" else "minOf"
|
||||
body {
|
||||
"""
|
||||
expect(${toP(1)}) { $minOf(${toP(2)}, ${toP(1)}, ${toP(3)}, ${toP(10)}) }
|
||||
expect(${toP(55)}) { $minOf(${toP(58)}, ${toP(126)}, ${toP(55)}, ${toP(87)}) }
|
||||
expect(${toP(21)}) { $minOf(${p.randomNextFrom(23)}, ${toP(23)}, ${p.randomNextFrom(23)}, ${toP(21)}) }
|
||||
expect(MAX_VALUE) { $minOf(MAX_VALUE, MAX_VALUE, MAX_VALUE, MAX_VALUE) }
|
||||
"""
|
||||
}
|
||||
if (p.isFloatingPoint()) {
|
||||
bodyAppend {
|
||||
"""
|
||||
assertEquals(ZERO, $minOf(MIN_VALUE, MAX_VALUE, ${toP(0)}, ${toP(1)}))
|
||||
assertEquals(-ZERO, $minOf(ZERO, -ZERO, -ZERO, ZERO))
|
||||
assertEquals(-ZERO, $minOf(-ZERO, ZERO, ZERO, -ZERO))
|
||||
assertEquals(NEGATIVE_INFINITY, $minOf(POSITIVE_INFINITY, NEGATIVE_INFINITY, MAX_VALUE, MIN_VALUE))
|
||||
"""
|
||||
}
|
||||
} else {
|
||||
bodyAppend {
|
||||
"""
|
||||
assertEquals(MIN_VALUE, $minOf(MIN_VALUE, MAX_VALUE, ${toP(0)}, ${toP(1)}))
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_maxOf_2 = test("maxOf_2()") {
|
||||
include(Generic)
|
||||
include(Primitives, PrimitiveType.numericPrimitives)
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
sourceFile(f.sourceFileComparisons)
|
||||
|
||||
val p = primitive ?: PrimitiveType.Int
|
||||
val maxOf = if (f == Generic) "maxOf<${p.name}>" else "maxOf"
|
||||
body {
|
||||
"""
|
||||
expect(${toP(2)}) { $maxOf(${toP(2)}, ${toP(1)}) }
|
||||
expect(${toP(126)}) { $maxOf(${toP(58)}, ${toP(126)}) }
|
||||
expect(${toP(23)}) { $maxOf(${p.randomNextUntil(23)}, ${toP(23)}) }
|
||||
expect(MAX_VALUE) { $maxOf(MIN_VALUE, MAX_VALUE) }
|
||||
expect(MIN_VALUE) { $maxOf(MIN_VALUE, MIN_VALUE) }
|
||||
"""
|
||||
}
|
||||
if (p.isFloatingPoint()) {
|
||||
bodyAppend {
|
||||
"""
|
||||
assertEquals(ZERO, $maxOf(ZERO, -ZERO))
|
||||
assertEquals(ZERO, $maxOf(-ZERO, ZERO))
|
||||
assertEquals(POSITIVE_INFINITY, $maxOf(NEGATIVE_INFINITY, POSITIVE_INFINITY))
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_maxOf_3 = test("maxOf_3()") {
|
||||
include(Generic)
|
||||
include(Primitives, PrimitiveType.numericPrimitives)
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
sourceFile(f.sourceFileComparisons)
|
||||
|
||||
val p = primitive ?: PrimitiveType.Int
|
||||
val maxOf = if (f == Generic) "maxOf<${p.name}>" else "maxOf"
|
||||
body {
|
||||
"""
|
||||
expect(${toP(3)}) { $maxOf(${toP(2)}, ${toP(1)}, ${toP(3)}) }
|
||||
expect(${toP(126)}) { $maxOf(${toP(58)}, ${toP(126)}, ${toP(55)}) }
|
||||
expect(${toP(23)}) { $maxOf(${p.randomNextUntil(23)}, ${toP(23)}, ${p.randomNextUntil(23)}) }
|
||||
expect(MIN_VALUE) { $maxOf(MIN_VALUE, MIN_VALUE, MIN_VALUE) }
|
||||
"""
|
||||
}
|
||||
if (p.isFloatingPoint()) {
|
||||
bodyAppend {
|
||||
"""
|
||||
expect(MAX_VALUE) { $maxOf(MIN_VALUE, MAX_VALUE, ${toP(0)}) }
|
||||
assertEquals(ZERO, $maxOf(ZERO, -ZERO, -ZERO))
|
||||
assertEquals(ZERO, $maxOf(-ZERO, ZERO, ZERO))
|
||||
assertEquals(POSITIVE_INFINITY, $maxOf(POSITIVE_INFINITY, MAX_VALUE, MIN_VALUE))
|
||||
"""
|
||||
}
|
||||
} else {
|
||||
bodyAppend {
|
||||
"""
|
||||
expect(MAX_VALUE) { $maxOf(MIN_VALUE, MAX_VALUE, ${toP(0)}) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_maxOf_vararg = test("maxOf_vararg()") {
|
||||
include(Generic)
|
||||
include(Primitives, PrimitiveType.numericPrimitives)
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
sourceFile(f.sourceFileComparisons)
|
||||
|
||||
val p = primitive ?: PrimitiveType.Int
|
||||
val maxOf = if (f == Generic) "maxOf<${p.name}>" else "maxOf"
|
||||
body {
|
||||
"""
|
||||
expect(${toP(10)}) { $maxOf(${toP(2)}, ${toP(1)}, ${toP(3)}, ${toP(10)}) }
|
||||
expect(${toP(126)}) { $maxOf(${toP(58)}, ${toP(126)}, ${toP(55)}, ${toP(87)}) }
|
||||
expect(${toP(23)}) { $maxOf(${p.randomNextUntil(23)}, ${toP(23)}, ${p.randomNextUntil(23)}, ${toP(21)}) }
|
||||
expect(MIN_VALUE) { $maxOf(MIN_VALUE, MIN_VALUE, MIN_VALUE, MIN_VALUE) }
|
||||
"""
|
||||
}
|
||||
if (p.isFloatingPoint()) {
|
||||
bodyAppend {
|
||||
"""
|
||||
expect(MAX_VALUE) { $maxOf(MIN_VALUE, MAX_VALUE, ${toP(0)}, ${toP(1)}) }
|
||||
assertEquals(ZERO, $maxOf(ZERO, -ZERO, -ZERO, ZERO))
|
||||
assertEquals(ZERO, $maxOf(-ZERO, ZERO, ZERO, -ZERO))
|
||||
assertEquals(POSITIVE_INFINITY, $maxOf(POSITIVE_INFINITY, NEGATIVE_INFINITY, MAX_VALUE, MIN_VALUE))
|
||||
"""
|
||||
}
|
||||
} else {
|
||||
bodyAppend {
|
||||
"""
|
||||
expect(MAX_VALUE) { $maxOf(MIN_VALUE, MAX_VALUE, ${toP(0)}, ${toP(1)}) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates.test
|
||||
|
||||
import templates.*
|
||||
import templates.Family.*
|
||||
|
||||
object ElementsTest : TestTemplateGroupBase() {
|
||||
|
||||
val f_indexOf = test("indexOf()") {
|
||||
include(Lists, Sequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
specialFor(ArraysOfPrimitives) {
|
||||
if (primitive!!.isFloatingPoint()) annotation("""@Suppress("DEPRECATION")""")
|
||||
}
|
||||
body {
|
||||
"""
|
||||
expect(-1) { ${of(1, 2, 3)}.indexOf(ZERO) }
|
||||
expect(0) { ${of(1, 2, 3)}.indexOf(ONE) }
|
||||
expect(1) { ${of(1, 2, 3)}.indexOf(TWO) }
|
||||
expect(2) { ${of(1, 2, 3)}.indexOf(THREE) }
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects, Lists) {
|
||||
"""
|
||||
expect(-1) { $of("cat", "dog", "bird").indexOf("mouse") }
|
||||
expect(0) { $of("cat", "dog", "bird").indexOf("cat") }
|
||||
expect(1) { $of("cat", "dog", "bird").indexOf("dog") }
|
||||
expect(2) { $of("cat", "dog", "bird").indexOf("bird") }
|
||||
expect(0) { $of(null, "dog", null).indexOf(null as String?)}
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"""
|
||||
expect(-1) { charArrayOf('a', 'b', 'c').indexOf('z') }
|
||||
expect(0) { charArrayOf('a', 'b', 'c').indexOf('a') }
|
||||
expect(1) { charArrayOf('a', 'b', 'c').indexOf('b') }
|
||||
expect(2) { charArrayOf('a', 'b', 'c').indexOf('c') }
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Boolean) {
|
||||
"""
|
||||
expect(0) { booleanArrayOf(true, false).indexOf(true) }
|
||||
expect(1) { booleanArrayOf(true, false).indexOf(false) }
|
||||
expect(-1) { booleanArrayOf(true).indexOf(false) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_indexOfFirst = test("indexOfFirst()") {
|
||||
include(Lists, Sequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
body {
|
||||
"""
|
||||
expect(-1) { ${of(1, 2, 3)}.indexOfFirst { it == ${toP(0)} } }
|
||||
expect(0) { ${of(1, 2, 3)}.indexOfFirst { it % TWO == ONE } }
|
||||
expect(1) { ${of(1, 2, 3)}.indexOfFirst { it % TWO == ZERO } }
|
||||
expect(2) { ${of(1, 2, 3)}.indexOfFirst { it == ${toP(3)} } }
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects, Lists) {
|
||||
"""
|
||||
expect(-1) { $of("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
|
||||
expect(0) { $of("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
|
||||
expect(1) { $of("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
|
||||
expect(2) { $of("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"""
|
||||
expect(-1) { charArrayOf('a', 'b', 'c').indexOfFirst { it == 'z' } }
|
||||
expect(0) { charArrayOf('a', 'b', 'c').indexOfFirst { it < 'c' } }
|
||||
expect(1) { charArrayOf('a', 'b', 'c').indexOfFirst { it > 'a' } }
|
||||
expect(2) { charArrayOf('a', 'b', 'c').indexOfFirst { it != 'a' && it != 'b' } }
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Boolean) {
|
||||
"""
|
||||
expect(0) { booleanArrayOf(true, false, false, true).indexOfFirst { it } }
|
||||
expect(1) { booleanArrayOf(true, false, false, true).indexOfFirst { !it } }
|
||||
expect(-1) { booleanArrayOf(true, true).indexOfFirst { !it } }
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates.test
|
||||
|
||||
import templates.*
|
||||
import templates.Family.*
|
||||
|
||||
object NumbersTest : TestTemplateGroupBase() {
|
||||
val f_rotate = test("rotate()") {
|
||||
include(Primitives, setOf(PrimitiveType.Byte, PrimitiveType.Short, PrimitiveType.Int, PrimitiveType.Long))
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
body {
|
||||
"""
|
||||
fun test(value: $P, n: Int, expected: $P) {
|
||||
assertEquals(expected, value.rotateLeft(n))
|
||||
assertEquals(expected, value.rotateRight(-n))
|
||||
}
|
||||
|
||||
fun testCyclic(value: $P) {
|
||||
for (n in -2 * $P.SIZE_BITS..2 * $P.SIZE_BITS) {
|
||||
val rl = value.rotateLeft(n)
|
||||
val rr = value.rotateRight(-n)
|
||||
assertEquals(rl, rr)
|
||||
assertEquals(rl, value.rotateLeft(n % $P.SIZE_BITS))
|
||||
assertEquals(rr, value.rotateRight((-n) % $P.SIZE_BITS))
|
||||
assertEquals(value, value.rotateLeft(n).rotateLeft(-n))
|
||||
assertEquals(value, value.rotateRight(n).rotateRight(-n))
|
||||
}
|
||||
}
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Byte) {
|
||||
"""
|
||||
test(0x73, 4, 0x37)
|
||||
test(0x73, -3, 0x6E)
|
||||
test(0x73, 1, 0xE6.toByte())
|
||||
test(0xE6.toByte(), 1, 0xCD.toByte())
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Short) {
|
||||
"""
|
||||
test(0x7361, 4, 0x3617)
|
||||
test(0x7361, -3, 0b001_0111_0011_0110_0)
|
||||
test(0x7361, 1, 0b111_0011_0110_0001_0.toShort())
|
||||
test(0xE6C2.toShort(), 1, 0b11_0011_0110_0001_01.toShort())
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Int) {
|
||||
"""
|
||||
test(0x7_3422345, 4, 0x3422345_7)
|
||||
test(0x7342234_5, -4, 0x5_7342234)
|
||||
test(0x73422345, 1, 0xE684468A.toInt())
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Long) {
|
||||
"""
|
||||
test(0x7372ABAC_DEEF0123, 4, 0x372ABAC_DEEF01237)
|
||||
test(0x88888888_44444444U.toLong(), -3, 0x91111111_08888888u.toLong())
|
||||
test(0x88888888_44444444U.toLong(), 1, 0x11111110_88888889)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.UByte) {
|
||||
"""
|
||||
test(0x73u, 4, 0x37u)
|
||||
test(0x73u, -3, 0x6Eu)
|
||||
test(0x73u, 1, 0xE6u)
|
||||
test(0xE6u, 1, 0xCDu)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.UShort) {
|
||||
"""
|
||||
test(0x7361u, 4, 0x3617u)
|
||||
test(0x7361u, -3, 0b001_0111_0011_0110_0u)
|
||||
test(0x7361u, 1, 0b111_0011_0110_0001_0u)
|
||||
test(0xE6C2u, 1, 0b11_0011_0110_0001_01u)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.UInt) {
|
||||
"""
|
||||
test(0x7_3422345u, 4, 0x3422345_7u)
|
||||
test(0x7342234_5u, -4, 0x5_7342234u)
|
||||
test(0x73422345u, 1, 0xE684468Au)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.ULong) {
|
||||
"""
|
||||
test(0x7372ABAC_DEEF0123uL, 4, 0x372ABAC_DEEF01237uL)
|
||||
test(0x88888888_44444444uL, -3, 0x91111111_08888888uL)
|
||||
test(0x88888888_44444444uL, 1, 0x11111110_88888889uL)
|
||||
"""
|
||||
}
|
||||
bodyAppend {
|
||||
"""
|
||||
repeat(100) {
|
||||
testCyclic(${primitive!!.randomNext()})
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_bits = test("bits()") {
|
||||
include(Primitives, setOf(PrimitiveType.Byte, PrimitiveType.Short, PrimitiveType.Int, PrimitiveType.Long))
|
||||
include(Unsigned)
|
||||
} builder {
|
||||
body {
|
||||
val sizeBits = primitive!!.SIZE_BITS
|
||||
"""
|
||||
fun test(value: $P, oneBits: Int, leadingZeroes: Int, trailingZeroes: Int) {
|
||||
assertEquals(oneBits, value.countOneBits())
|
||||
assertEquals(leadingZeroes, value.countLeadingZeroBits())
|
||||
assertEquals(trailingZeroes, value.countTrailingZeroBits())
|
||||
val highestBit = if (leadingZeroes < $P.SIZE_BITS) ONE.shl($P.SIZE_BITS - leadingZeroes - 1).to$P() else ZERO
|
||||
val lowestBit = if (trailingZeroes < $P.SIZE_BITS) ONE.shl(trailingZeroes).to$P() else ZERO
|
||||
assertEquals(highestBit, value.takeHighestOneBit())
|
||||
assertEquals(lowestBit, value.takeLowestOneBit())
|
||||
}
|
||||
|
||||
test(ZERO, 0, $sizeBits, $sizeBits)
|
||||
test(ONE, 1, ${sizeBits - 1}, 0)
|
||||
test(TWO, 1, ${sizeBits - 2}, 1)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Byte) {
|
||||
"""
|
||||
test(0x44, 2, 1, 2)
|
||||
test(0x80.toByte(), 1, 0, 7)
|
||||
test(0xF0.toByte(), 4, 0, 4)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Short) {
|
||||
"""
|
||||
test(0xF2, 5, 8, 1)
|
||||
test(0x8000.toShort(), 1, 0, 15)
|
||||
test(0xF200.toShort(), 5, 0, 9)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Int) {
|
||||
"""
|
||||
test(0xF002, 5, 16, 1)
|
||||
test(0xF00F0000.toInt(), 8, 0, 16)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Long) {
|
||||
"""
|
||||
test(0xF002, 5, 48, 1)
|
||||
test(0xF00F0000L, 8, 32, 16)
|
||||
test(0x1111_3333_EEEE_0000L, 4 + 8 + 12, 3, 17)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.UByte) {
|
||||
"""
|
||||
test(0x44u, 2, 1, 2)
|
||||
test(0x80u, 1, 0, 7)
|
||||
test(0xF0u, 4, 0, 4)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.UShort) {
|
||||
"""
|
||||
test(0xF2u, 5, 8, 1)
|
||||
test(0x8000u, 1, 0, 15)
|
||||
test(0xF200u, 5, 0, 9)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.UInt) {
|
||||
"""
|
||||
test(0xF002u, 5, 16, 1)
|
||||
test(0xF00F0000u, 8, 0, 16)
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.ULong) {
|
||||
"""
|
||||
test(0xF002uL, 5, 48, 1)
|
||||
test(0xF00F0000uL, 8, 32, 16)
|
||||
test(0x1111_3333_EEEE_0000uL, 4 + 8 + 12, 3, 17)
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package templates.test
|
||||
|
||||
import templates.*
|
||||
import templates.Family.*
|
||||
|
||||
object OrderingTest : TestTemplateGroupBase() {
|
||||
|
||||
private val TestBuilder.elementConversion: String
|
||||
get() = when (primitive) {
|
||||
null -> "it.toString()"
|
||||
PrimitiveType.Char -> "'a' + it"
|
||||
PrimitiveType.Boolean -> "it % 2 == 0"
|
||||
else -> "it.to${test.P}()"
|
||||
}
|
||||
|
||||
private val TestBuilder.arrayConversion: String
|
||||
get() = when (family) {
|
||||
ArraysOfPrimitives, ArraysOfUnsigned -> "to${test.P}Array()"
|
||||
else -> "toTypedArray()"
|
||||
}
|
||||
|
||||
val f_reverse = test("reverse()") {
|
||||
include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
body {
|
||||
"""
|
||||
val arrays = (0..4).map { n -> (1..n).map { $elementConversion }.$arrayConversion }
|
||||
for (array in arrays) {
|
||||
val original = array.toList()
|
||||
array.reverse()
|
||||
val reversed = array.toList()
|
||||
assertEquals(original.asReversed(), reversed)
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_reverse_range = test("reverseRange()") {
|
||||
include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
body {
|
||||
"""
|
||||
val arrays = (0..7).map { n -> n to (0 until n).map { $elementConversion }.$arrayConversion }
|
||||
for ((size, array) in arrays) {
|
||||
for (fromIndex in 0 until size) {
|
||||
for (toIndex in fromIndex..size) {
|
||||
val original = array.toMutableList()
|
||||
array.reverse(fromIndex, toIndex)
|
||||
val reversed = array.toMutableList()
|
||||
assertEquals(original.apply { subList(fromIndex, toIndex).reverse() }, reversed)
|
||||
}
|
||||
}
|
||||
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(-1, size) }
|
||||
assertFailsWith<IndexOutOfBoundsException> { array.reverse(0, size + 1) }
|
||||
assertFailsWith<IllegalArgumentException> { array.reverse(0, -1) }
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_reversed = listOf("reversed", "reversedArray").map { op ->
|
||||
val isReversedArray = op == "reversedArray"
|
||||
test("$op()") {
|
||||
include(Iterables, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
filter { family, _ -> (isReversedArray && !family.isArray()).not() }
|
||||
} builder {
|
||||
val resultF = if (isReversedArray) f else Lists
|
||||
body {
|
||||
"${resultF.assertEquals}(${resultF.of(3, 2, 1)}, ${of(1, 2, 3)}.$op())"
|
||||
}
|
||||
bodyAppend(Iterables, ArraysOfObjects) {
|
||||
"""${resultF.assertEquals}(${resultF.of}("3", "2", "1"), $of("1", "2", "3").$op())"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"${resultF.assertEquals}(${resultF.of}('3', '2', '1'), charArrayOf('1', '2', '3').$op())"
|
||||
}
|
||||
body(PrimitiveType.Boolean) {
|
||||
"${resultF.assertEquals}(${resultF.of}(false, false, true), booleanArrayOf(true, false, false).$op())"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_sorted = listOf("sorted", "sortedArray", "sortedDescending", "sortedArrayDescending").map { op ->
|
||||
val isSortedArray = op.startsWith("sortedArray")
|
||||
test("$op()") {
|
||||
includeDefault()
|
||||
include(ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
filter { family, _ -> (isSortedArray && !family.isArray()).not() }
|
||||
} builder {
|
||||
val cmp = if (op.endsWith("Descending")) ">=" else "<="
|
||||
val sortAndCheck = "$op().iterator().assertSorted { a, b -> a.compareTo(b) $cmp 0 }"
|
||||
body {
|
||||
"""
|
||||
${of(3, 7, 1)}.$sortAndCheck
|
||||
$of(ONE, MAX_VALUE, MIN_VALUE).$sortAndCheck
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables, Sequences, ArraysOfObjects) {
|
||||
"""
|
||||
$of("ac", "aD", "aba").$sortAndCheck
|
||||
"""
|
||||
}
|
||||
bodyAppend(PrimitiveType.Float, PrimitiveType.Double) {
|
||||
"""
|
||||
$of(POSITIVE_INFINITY, NEGATIVE_INFINITY, MAX_VALUE, -MAX_VALUE, ONE, -ONE,
|
||||
MIN_VALUE, -MIN_VALUE, ZERO, -ZERO, $P.NaN).$sortAndCheck
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"""
|
||||
$of('a', 'D', 'c').$sortAndCheck
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_sort = listOf("sort", "sortDescending").map { op ->
|
||||
val isDescending = op.endsWith("Descending")
|
||||
test("$op()") {
|
||||
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
} builder {
|
||||
val reversed = (if (f.isArray()) ".reversedArray()" else ".reversed()").takeIf { isDescending } ?: ""
|
||||
body {
|
||||
val (five, nine, eighty) = listOf(5, 9, 80).map { literal(it) }
|
||||
"""
|
||||
val data = $mutableOf($five, TWO, ONE, $nine, $eighty, MIN_VALUE, MAX_VALUE)
|
||||
data.$op()
|
||||
$assertEquals($of(MIN_VALUE, ONE, TWO, $five, $nine, $eighty, MAX_VALUE)$reversed, data)
|
||||
"""
|
||||
}
|
||||
bodyAppend(Lists, ArraysOfObjects) {
|
||||
"""
|
||||
val strings = $mutableOf("9", "80", "all", "Foo")
|
||||
strings.$op()
|
||||
$assertEquals($of("80", "9", "Foo", "all")$reversed, strings)
|
||||
"""
|
||||
}
|
||||
body(PrimitiveType.Char) {
|
||||
"""
|
||||
val data = charArrayOf('d', 'c', 'E', 'a', '\u0000', '\uFFFF')
|
||||
data.$op()
|
||||
$assertEquals($of('\u0000', 'E', 'a', 'c', 'd', '\uFFFF')$reversed, data)
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_sortedWith = listOf("sortedWith", "sortedArrayWith").map { op ->
|
||||
val isSortedArray = op.contains("Array")
|
||||
test("$op()") {
|
||||
includeDefault()
|
||||
filter { family, _ -> !isSortedArray || family == ArraysOfObjects }
|
||||
exclude(PrimitiveType.Char, PrimitiveType.Boolean)
|
||||
} builder {
|
||||
body {
|
||||
val sortAndCheck = "$op(comparator).iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }"
|
||||
"""
|
||||
val comparator = compareBy { it: $P -> it % THREE }.thenByDescending { it }
|
||||
${of(0, 1, 2, 3, 4, 5)}.$sortAndCheck
|
||||
"""
|
||||
}
|
||||
bodyAppend(Iterables/*, Sequences*/, ArraysOfObjects) { // `.asSequence()` and `object : Sequence { }` are not equal
|
||||
val resultF = if (isSortedArray || f == Sequences) f else Lists
|
||||
"""
|
||||
val comparator1 = compareBy<String> { it.toUpperCase().reversed() }
|
||||
val data = $of("cat", "dad", "BAD")
|
||||
|
||||
${resultF.assertEquals}(${resultF.of}("BAD", "dad", "cat"), data.$op(comparator1))
|
||||
${resultF.assertEquals}(${resultF.of}("cat", "dad", "BAD"), data.$op(comparator1.reversed()))
|
||||
${resultF.assertEquals}(${resultF.of}("BAD", "dad", "cat"), data.$op(comparator1.reversed().reversed()))
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_sortBy = listOf("sortBy", "sortByDescending").map { op ->
|
||||
val isDescending = op.contains("Descending")
|
||||
test("$op()") {
|
||||
include(Lists, ArraysOfObjects)
|
||||
} builder {
|
||||
body {
|
||||
val (bySecond, byFirst, byLength) = if (!isDescending) {
|
||||
listOf(
|
||||
""""ab" to 3, "aa" to 3, "aa" to 20""",
|
||||
""""aa" to 3, "aa" to 20, "ab" to 3""",
|
||||
""""aa" to 3, "ab" to 3, "aa" to 20"""
|
||||
)
|
||||
} else {
|
||||
listOf(
|
||||
""""aa" to 20, "ab" to 3, "aa" to 3""",
|
||||
""""ab" to 3, "aa" to 20, "aa" to 3""",
|
||||
""""aa" to 20, "ab" to 3, "aa" to 3"""
|
||||
)
|
||||
}
|
||||
|
||||
"""
|
||||
val data = $mutableOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.$op { it.second }
|
||||
$assertEquals($of($bySecond), data)
|
||||
|
||||
data.$op { it.first }
|
||||
$assertEquals($of($byFirst), data)
|
||||
|
||||
data.$op { (it.first + it.second).length }
|
||||
$assertEquals($of($byLength), data)
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val f_sortedBy = listOf("sortedBy", "sortedByDescending").map { op ->
|
||||
val isDescending = op.contains("Descending")
|
||||
test("$op()") {
|
||||
include(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
exclude(PrimitiveType.Boolean, PrimitiveType.Char)
|
||||
} builder {
|
||||
val resultF = if (f == Sequences) f else Lists
|
||||
val reversed = if (isDescending) ".reversed()" else ""
|
||||
body {
|
||||
"""
|
||||
val values = arrayOf("ac", "aD", "aba")
|
||||
val indices = ${of(0, 1, 2)}
|
||||
|
||||
assertEquals(${resultF.of(1, 2, 0)}$reversed, indices.$op { values[it.toInt()] })
|
||||
"""
|
||||
}
|
||||
bodyAppend(ArraysOfUnsigned) {
|
||||
"""
|
||||
val array = $of(5, 2, 1, 9, 80, 0, MAX_VALUE, MAX_VALUE - 100)
|
||||
assertEquals(listOf<$P>(MAX_VALUE - 100, MAX_VALUE, 0, 1, 2, 5, 9, 80)$reversed, array.$op { it.to${P.drop(1)}() })
|
||||
"""
|
||||
}
|
||||
bodyAppend(Lists, ArraysOfObjects) {
|
||||
"""
|
||||
assertEquals(${resultF.of}("two" to 3, "three" to 20)$reversed, $of("three" to 20, "two" to 3).$op { it.second })
|
||||
assertEquals(${resultF.of}("three" to 20, "two" to 3)$reversed, $of("three" to 20, "two" to 3).$op { it.first })
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val f_shuffle = test("shuffle()") {
|
||||
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
val type = if (f == Lists) "MutableList" else (primitive?.name ?: "") + "Array"
|
||||
val typeParam = if (primitive == null) "<*>" else ""
|
||||
val conversion = if (primitive == PrimitiveType.Boolean) "it % 2 == 0" else "it.to${test.P}()"
|
||||
body {
|
||||
"""
|
||||
fun test(data: $type$typeParam) {
|
||||
val original = data.toMutableList()
|
||||
data.shuffle()
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(original.groupBy { it }, shuffled.groupBy { it })
|
||||
}
|
||||
test($type(100) { $conversion })
|
||||
"""
|
||||
}
|
||||
bodyAppend(Lists, ArraysOfObjects) {
|
||||
"""
|
||||
test($mutableOf(1, "x", null, Any(), 'a', 2u, 5.0))
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_shuffleRandom = test("shuffleRandom()") {
|
||||
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
val type = if (f == Lists) "MutableList" else (primitive?.name ?: "") + "Array"
|
||||
val typeParam = if (primitive == null) "<*>" else ""
|
||||
val conversion = if (primitive == PrimitiveType.Boolean) "it % 2 == 0" else "it.to${test.P}()"
|
||||
body {
|
||||
"""
|
||||
fun test(data: $type$typeParam) {
|
||||
val seed = Random.nextInt()
|
||||
val original = data.toMutableList()
|
||||
val originalShuffled = original.shuffled(Random(seed))
|
||||
data.shuffle(Random(seed))
|
||||
val shuffled = data.toMutableList()
|
||||
assertNotEquals(original, shuffled)
|
||||
assertEquals(originalShuffled, shuffled)
|
||||
}
|
||||
test($type(16) { $conversion })
|
||||
"""
|
||||
}
|
||||
bodyAppend(Lists, ArraysOfObjects) {
|
||||
"""
|
||||
test($mutableOf(1, "x", null, Any(), 'a', 2u, 5.0))
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user