Add min/max test generator and generated tests

Temporarily exclude these tests from kotlin-stdlib-wasm testing due to KT-51647
This commit is contained in:
Ilya Gorbunov
2022-03-17 21:28:04 +03:00
committed by Space
parent 60fbb0d171
commit 4c461d7864
20 changed files with 3902 additions and 0 deletions
@@ -7,6 +7,7 @@ package test.collections
import kotlin.test.*
import test.*
import kotlin.math.pow
class MapTest {
@@ -603,4 +604,166 @@ class MapTest {
@Test fun plusAssignEmptySet() = testIdempotentAssign { it += setOf() }
private fun <K, V> expectMinMaxWith(min: Pair<K, V>, max: Pair<K, V>, elements: Map<K, V>, comparator: Comparator<Map.Entry<K, V>>) {
assertEquals(min, elements.minWithOrNull(comparator)?.toPair())
assertEquals(max, elements.maxWithOrNull(comparator)?.toPair())
assertEquals(min, elements.minWith(comparator).toPair())
assertEquals(max, elements.maxWith(comparator).toPair())
}
@Test
fun minMaxWith() {
val map = listOf("a", "bcd", "Ef").associateWith { it.length }
expectMinMaxWith("Ef" to 2, "bcd" to 3, map, compareBy { it.key })
expectMinMaxWith("a" to 1, "Ef" to 2, map, compareBy(String.CASE_INSENSITIVE_ORDER) { it.key })
expectMinMaxWith("a" to 1, "bcd" to 3, map, compareBy { it.value })
}
@Test
fun minMaxWithEmpty() {
val empty = mapOf<Int, Int>()
val comparator = compareBy<Map.Entry<Int, Int>> { it.value }
assertNull(empty.minWithOrNull(comparator))
assertNull(empty.maxWithOrNull(comparator))
assertFailsWith<NoSuchElementException> { empty.minWith(comparator) }
assertFailsWith<NoSuchElementException> { empty.maxWith(comparator) }
}
private inline fun <K, V, R : Comparable<R>> expectMinMaxBy(min: Pair<K, V>, max: Pair<K, V>, elements: Map<K, V>, selector: (Map.Entry<K, V>) -> R) {
assertEquals(min, elements.minBy(selector).toPair())
assertEquals(min, elements.minByOrNull(selector)?.toPair())
assertEquals(max, elements.maxBy(selector).toPair())
assertEquals(max, elements.maxByOrNull(selector)?.toPair())
}
@Test
fun minMaxBy() {
val map = listOf("a", "bcd", "Ef").associateWith { it.length }
expectMinMaxBy("Ef" to 2, "bcd" to 3, map, { it.key })
expectMinMaxBy("a" to 1, "Ef" to 2, map, { it.key.lowercase() })
expectMinMaxBy("a" to 1, "bcd" to 3, map, { it.value })
}
@Test
fun minMaxByEmpty() {
val empty = mapOf<Int, Int>()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test fun minBySelectorEvaluateOnce() {
val source = listOf(1, 2, 3).associateWith { it }
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test fun maxBySelectorEvaluateOnce() {
val source = listOf(1, 2, 3).associateWith { it }
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <K, V, R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: Map<K, V>, selector: (Map.Entry<K, V>) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
val maps = (1..3).map { size -> listOf("a", "bcd", "Ef").take(size).associateWith { it.length } }
expectMinMaxOf("a=1", "a=1", maps[0], { it.toString() })
expectMinMaxOf("a=1", "bcd=3", maps[1], { it.toString() })
expectMinMaxOf("Ef=2", "bcd=3", maps[2], { it.toString() })
}
@Test
fun minMaxOfDouble() {
val items = mapOf("a" to 0.0, "b" to 1.0, "c" to -1.0)
assertTrue(items.minOf { it.value.pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.value.pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.value.pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.value.pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.value * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.value * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.value * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.value * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val items = mapOf("a" to 0.0F, "b" to 1.0F, "c" to -1.0F)
assertTrue(items.minOf { it.value.pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.value.pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.value.pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.value.pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.value * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.value * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.value * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.value * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = mapOf<Int, Int>()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <K, V, R> expectMinMaxOfWith(min: R, max: R, elements: Map<K, V>, comparator: Comparator<R>, selector: (Map.Entry<K, V>) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
val maps = (1..3).map { size -> listOf("a", "bcd", "Ef").take(size).associateWith { it.length } }
val comparator = String.CASE_INSENSITIVE_ORDER
expectMinMaxOfWith("a=1", "a=1", maps[0], comparator, { it.toString() })
expectMinMaxOfWith("a=1", "bcd=3", maps[1], comparator, { it.toString() })
expectMinMaxOfWith("a=1", "Ef=2", maps[2], comparator, { it.toString() })
}
@Test
fun minMaxOfWithEmpty() {
val empty = mapOf<Int, Int>()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,255 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxArrayTest {
private fun <T : Comparable<T>> expectMinMax(min: T, max: T, elements: Array<T>) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax("a", "a", arrayOf("a"))
expectMinMax("a", "bcd", arrayOf("a", "bcd"))
expectMinMax("a", "e", arrayOf("a", "bcd", "e"))
expectMinMax(1, Int.MAX_VALUE, arrayOf(1, 2, Int.MAX_VALUE))
expectMinMax(1, Long.MAX_VALUE, arrayOf(1, 2, Long.MAX_VALUE))
expectMinMax(1U, UInt.MAX_VALUE, arrayOf(1U, 2U, UInt.MAX_VALUE))
expectMinMax('a', Char.MAX_VALUE, arrayOf('a', 'b', Char.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = arrayOf<Int>()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
@Test
fun minMaxDouble() {
val zeroes = arrayOf(0.0, -0.0).apply { shuffle() }
val NaNs = arrayOf(0.0, Double.NaN).apply { shuffle() }
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
@Test
fun minMaxFloat() {
val zeroes = arrayOf(0.0F, -0.0F).apply { shuffle() }
val NaNs = arrayOf(0.0F, Float.NaN).apply { shuffle() }
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
private fun <T> expectMinMaxWith(min: T, max: T, elements: Array<T>, comparator: Comparator<T>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith("a", "a", arrayOf("a"), naturalOrder())
expectMinMaxWith("a", "bcd", arrayOf("a", "bcd"), naturalOrder())
expectMinMaxWith("a", "e", arrayOf("a", "bcd", "e"), naturalOrder())
expectMinMaxWith("a", "B", arrayOf("a", "B"), String.CASE_INSENSITIVE_ORDER)
}
@Test
fun minMaxWithEmpty() {
val empty = arrayOf<Int>()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <T, K : Comparable<K>> expectMinMaxBy(min: T, max: T, elements: Array<T>, selector: (T) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy("a", "a", arrayOf("a"), { it })
expectMinMaxBy("a", "bcd", arrayOf("a", "bcd"), { it })
expectMinMaxBy("a", "e", arrayOf("a", "bcd", "e"), { it })
expectMinMaxBy("De", "abc", arrayOf("abc", "De"), { it.length })
}
@Test
fun minMaxByEmpty() {
val empty = arrayOf<Int>()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = arrayOf("a", "bcd", "e")
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = arrayOf("a", "bcd", "e")
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <T, R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: Array<T>, selector: (T) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf("_" + "a", "_" + "a", arrayOf("a"), { "_$it" })
expectMinMaxOf("_" + "a", "_" + "bcd", arrayOf("a", "bcd"), { "_$it" })
expectMinMaxOf("_" + "a", "_" + "e", arrayOf("a", "bcd", "e"), { "_$it" })
}
@Test
fun minMaxOfDouble() {
val middle = "bcd"
val items = arrayOf("a", "bcd", "e").apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = "bcd"
val items = arrayOf("a", "bcd", "e").apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = arrayOf<Int>()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <T, R> expectMinMaxOfWith(min: R, max: R, elements: Array<T>, comparator: Comparator<R>, selector: (T) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith("_" + "a", "_" + "a", arrayOf("a"), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + "bcd", "_" + "a", arrayOf("a", "bcd"), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + "e", "_" + "a", arrayOf("a", "bcd", "e"), reverseOrder(), { "_$it" })
}
@Test
fun minMaxOfWithEmpty() {
val empty = arrayOf<Int>()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxByteArrayTest {
private fun expectMinMax(min: Byte, max: Byte, elements: ByteArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1, 1, byteArrayOf(1))
expectMinMax(1, 2, byteArrayOf(1, 2))
expectMinMax(1, Byte.MAX_VALUE, byteArrayOf(1, 2, Byte.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = byteArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: Byte, max: Byte, elements: ByteArray, comparator: Comparator<Byte>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1, 1, byteArrayOf(1), naturalOrder())
expectMinMaxWith(1, 2, byteArrayOf(1, 2), naturalOrder())
expectMinMaxWith(1, Byte.MAX_VALUE, byteArrayOf(1, 2, Byte.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = byteArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Byte, max: Byte, elements: ByteArray, selector: (Byte) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1, 1, byteArrayOf(1), { it })
expectMinMaxBy(1, 2, byteArrayOf(1, 2), { it })
expectMinMaxBy(1, Byte.MAX_VALUE, byteArrayOf(1, 2, Byte.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = byteArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = byteArrayOf(1, 2, Byte.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = byteArrayOf(1, 2, Byte.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: ByteArray, selector: (Byte) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf(-1, -1, byteArrayOf(1), { -it })
expectMinMaxOf(-2, -1, byteArrayOf(1, 2), { -it })
expectMinMaxOf(-Byte.MAX_VALUE, -1, byteArrayOf(1, 2, Byte.MAX_VALUE), { -it })
}
@Test
fun minMaxOfDouble() {
val middle = 2
val items = byteArrayOf(1, 2, Byte.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2
val items = byteArrayOf(1, 2, Byte.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = byteArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: ByteArray, comparator: Comparator<R>, selector: (Byte) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith(-1, -1, byteArrayOf(1), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -2, byteArrayOf(1, 2), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -Byte.MAX_VALUE, byteArrayOf(1, 2, Byte.MAX_VALUE), reverseOrder(), { -it })
}
@Test
fun minMaxOfWithEmpty() {
val empty = byteArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxCharArrayTest {
private fun expectMinMax(min: Char, max: Char, elements: CharArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax('a', 'a', charArrayOf('a'))
expectMinMax('a', 'b', charArrayOf('a', 'b'))
expectMinMax('a', Char.MAX_VALUE, charArrayOf('a', 'b', Char.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = charArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: Char, max: Char, elements: CharArray, comparator: Comparator<Char>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith('a', 'a', charArrayOf('a'), naturalOrder())
expectMinMaxWith('a', 'b', charArrayOf('a', 'b'), naturalOrder())
expectMinMaxWith('a', Char.MAX_VALUE, charArrayOf('a', 'b', Char.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = charArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Char, max: Char, elements: CharArray, selector: (Char) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy('a', 'a', charArrayOf('a'), { it })
expectMinMaxBy('a', 'b', charArrayOf('a', 'b'), { it })
expectMinMaxBy('a', Char.MAX_VALUE, charArrayOf('a', 'b', Char.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = charArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = charArrayOf('a', 'b', Char.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = charArrayOf('a', 'b', Char.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: CharArray, selector: (Char) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf("_" + 'a', "_" + 'a', charArrayOf('a'), { "_$it" })
expectMinMaxOf("_" + 'a', "_" + 'b', charArrayOf('a', 'b'), { "_$it" })
expectMinMaxOf("_" + 'a', "_" + Char.MAX_VALUE, charArrayOf('a', 'b', Char.MAX_VALUE), { "_$it" })
}
@Test
fun minMaxOfDouble() {
val middle = 'b'
val items = charArrayOf('a', 'b', Char.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 'b'
val items = charArrayOf('a', 'b', Char.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = charArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: CharArray, comparator: Comparator<R>, selector: (Char) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith("_" + 'a', "_" + 'a', charArrayOf('a'), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + 'b', "_" + 'a', charArrayOf('a', 'b'), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + Char.MAX_VALUE, "_" + 'a', charArrayOf('a', 'b', Char.MAX_VALUE), reverseOrder(), { "_$it" })
}
@Test
fun minMaxOfWithEmpty() {
val empty = charArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxCharSequenceTest {
private fun expectMinMax(min: Char, max: Char, elements: CharSequence) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax('a', 'a', StringBuilder(charArrayOf('a').concatToString()))
expectMinMax('a', 'b', StringBuilder(charArrayOf('a', 'b').concatToString()))
expectMinMax('a', Char.MAX_VALUE, StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).concatToString()))
}
@Test
fun minMaxEmpty() {
val empty = StringBuilder()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: Char, max: Char, elements: CharSequence, comparator: Comparator<Char>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith('a', 'a', StringBuilder(charArrayOf('a').concatToString()), naturalOrder())
expectMinMaxWith('a', 'b', StringBuilder(charArrayOf('a', 'b').concatToString()), naturalOrder())
expectMinMaxWith('a', Char.MAX_VALUE, StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).concatToString()), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = StringBuilder()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Char, max: Char, elements: CharSequence, selector: (Char) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy('a', 'a', StringBuilder(charArrayOf('a').concatToString()), { it })
expectMinMaxBy('a', 'b', StringBuilder(charArrayOf('a', 'b').concatToString()), { it })
expectMinMaxBy('a', Char.MAX_VALUE, StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).concatToString()), { it })
}
@Test
fun minMaxByEmpty() {
val empty = StringBuilder()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).concatToString())
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).concatToString())
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: CharSequence, selector: (Char) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf("_" + 'a', "_" + 'a', StringBuilder(charArrayOf('a').concatToString()), { "_$it" })
expectMinMaxOf("_" + 'a', "_" + 'b', StringBuilder(charArrayOf('a', 'b').concatToString()), { "_$it" })
expectMinMaxOf("_" + 'a', "_" + Char.MAX_VALUE, StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).concatToString()), { "_$it" })
}
@Test
fun minMaxOfDouble() {
val middle = 'b'
val items = StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).apply { shuffle() }.concatToString())
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 'b'
val items = StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).apply { shuffle() }.concatToString())
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = StringBuilder()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: CharSequence, comparator: Comparator<R>, selector: (Char) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith("_" + 'a', "_" + 'a', StringBuilder(charArrayOf('a').concatToString()), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + 'b', "_" + 'a', StringBuilder(charArrayOf('a', 'b').concatToString()), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + Char.MAX_VALUE, "_" + 'a', StringBuilder(charArrayOf('a', 'b', Char.MAX_VALUE).concatToString()), reverseOrder(), { "_$it" })
}
@Test
fun minMaxOfWithEmpty() {
val empty = StringBuilder()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,231 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxDoubleArrayTest {
private fun expectMinMax(min: Double, max: Double, elements: DoubleArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1.0, 1.0, doubleArrayOf(1.0))
expectMinMax(1.0, 2.0, doubleArrayOf(1.0, 2.0))
expectMinMax(1.0, Double.POSITIVE_INFINITY, doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY))
}
@Test
fun minMaxEmpty() {
val empty = doubleArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
@Test
fun minMaxDouble() {
val zeroes = doubleArrayOf(0.0, -0.0).apply { shuffle() }
val NaNs = doubleArrayOf(0.0, Double.NaN).apply { shuffle() }
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
private fun expectMinMaxWith(min: Double, max: Double, elements: DoubleArray, comparator: Comparator<Double>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1.0, 1.0, doubleArrayOf(1.0), naturalOrder())
expectMinMaxWith(1.0, 2.0, doubleArrayOf(1.0, 2.0), naturalOrder())
expectMinMaxWith(1.0, Double.POSITIVE_INFINITY, doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = doubleArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Double, max: Double, elements: DoubleArray, selector: (Double) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1.0, 1.0, doubleArrayOf(1.0), { it })
expectMinMaxBy(1.0, 2.0, doubleArrayOf(1.0, 2.0), { it })
expectMinMaxBy(1.0, Double.POSITIVE_INFINITY, doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY), { it })
}
@Test
fun minMaxByEmpty() {
val empty = doubleArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: DoubleArray, selector: (Double) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf(-1.0, -1.0, doubleArrayOf(1.0), { -it })
expectMinMaxOf(-2.0, -1.0, doubleArrayOf(1.0, 2.0), { -it })
expectMinMaxOf(-Double.POSITIVE_INFINITY, -1.0, doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY), { -it })
}
@Test
fun minMaxOfDouble() {
val middle = 2.0
val items = doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2.0
val items = doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = doubleArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: DoubleArray, comparator: Comparator<R>, selector: (Double) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith(-1.0, -1.0, doubleArrayOf(1.0), reverseOrder(), { -it })
expectMinMaxOfWith(-1.0, -2.0, doubleArrayOf(1.0, 2.0), reverseOrder(), { -it })
expectMinMaxOfWith(-1.0, -Double.POSITIVE_INFINITY, doubleArrayOf(1.0, 2.0, Double.POSITIVE_INFINITY), reverseOrder(), { -it })
}
@Test
fun minMaxOfWithEmpty() {
val empty = doubleArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,231 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxFloatArrayTest {
private fun expectMinMax(min: Float, max: Float, elements: FloatArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1.0F, 1.0F, floatArrayOf(1.0F))
expectMinMax(1.0F, 2.0F, floatArrayOf(1.0F, 2.0F))
expectMinMax(1.0F, Float.POSITIVE_INFINITY, floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY))
}
@Test
fun minMaxEmpty() {
val empty = floatArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
@Test
fun minMaxFloat() {
val zeroes = floatArrayOf(0.0F, -0.0F).apply { shuffle() }
val NaNs = floatArrayOf(0.0F, Float.NaN).apply { shuffle() }
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
private fun expectMinMaxWith(min: Float, max: Float, elements: FloatArray, comparator: Comparator<Float>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1.0F, 1.0F, floatArrayOf(1.0F), naturalOrder())
expectMinMaxWith(1.0F, 2.0F, floatArrayOf(1.0F, 2.0F), naturalOrder())
expectMinMaxWith(1.0F, Float.POSITIVE_INFINITY, floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = floatArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Float, max: Float, elements: FloatArray, selector: (Float) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1.0F, 1.0F, floatArrayOf(1.0F), { it })
expectMinMaxBy(1.0F, 2.0F, floatArrayOf(1.0F, 2.0F), { it })
expectMinMaxBy(1.0F, Float.POSITIVE_INFINITY, floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY), { it })
}
@Test
fun minMaxByEmpty() {
val empty = floatArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: FloatArray, selector: (Float) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf(-1.0F, -1.0F, floatArrayOf(1.0F), { -it })
expectMinMaxOf(-2.0F, -1.0F, floatArrayOf(1.0F, 2.0F), { -it })
expectMinMaxOf(-Float.POSITIVE_INFINITY, -1.0F, floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY), { -it })
}
@Test
fun minMaxOfDouble() {
val middle = 2.0F
val items = floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2.0F
val items = floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = floatArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: FloatArray, comparator: Comparator<R>, selector: (Float) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith(-1.0F, -1.0F, floatArrayOf(1.0F), reverseOrder(), { -it })
expectMinMaxOfWith(-1.0F, -2.0F, floatArrayOf(1.0F, 2.0F), reverseOrder(), { -it })
expectMinMaxOfWith(-1.0F, -Float.POSITIVE_INFINITY, floatArrayOf(1.0F, 2.0F, Float.POSITIVE_INFINITY), reverseOrder(), { -it })
}
@Test
fun minMaxOfWithEmpty() {
val empty = floatArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxIntArrayTest {
private fun expectMinMax(min: Int, max: Int, elements: IntArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1, 1, intArrayOf(1))
expectMinMax(1, 2, intArrayOf(1, 2))
expectMinMax(1, Int.MAX_VALUE, intArrayOf(1, 2, Int.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = intArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: Int, max: Int, elements: IntArray, comparator: Comparator<Int>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1, 1, intArrayOf(1), naturalOrder())
expectMinMaxWith(1, 2, intArrayOf(1, 2), naturalOrder())
expectMinMaxWith(1, Int.MAX_VALUE, intArrayOf(1, 2, Int.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = intArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Int, max: Int, elements: IntArray, selector: (Int) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1, 1, intArrayOf(1), { it })
expectMinMaxBy(1, 2, intArrayOf(1, 2), { it })
expectMinMaxBy(1, Int.MAX_VALUE, intArrayOf(1, 2, Int.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = intArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = intArrayOf(1, 2, Int.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = intArrayOf(1, 2, Int.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: IntArray, selector: (Int) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf(-1, -1, intArrayOf(1), { -it })
expectMinMaxOf(-2, -1, intArrayOf(1, 2), { -it })
expectMinMaxOf(-Int.MAX_VALUE, -1, intArrayOf(1, 2, Int.MAX_VALUE), { -it })
}
@Test
fun minMaxOfDouble() {
val middle = 2
val items = intArrayOf(1, 2, Int.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2
val items = intArrayOf(1, 2, Int.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = intArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: IntArray, comparator: Comparator<R>, selector: (Int) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith(-1, -1, intArrayOf(1), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -2, intArrayOf(1, 2), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -Int.MAX_VALUE, intArrayOf(1, 2, Int.MAX_VALUE), reverseOrder(), { -it })
}
@Test
fun minMaxOfWithEmpty() {
val empty = intArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,255 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxIterableTest {
private fun <T : Comparable<T>> expectMinMax(min: T, max: T, elements: Iterable<T>) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax("a", "a", listOf("a"))
expectMinMax("a", "bcd", listOf("a", "bcd"))
expectMinMax("a", "e", listOf("a", "bcd", "e"))
expectMinMax(1, Int.MAX_VALUE, listOf(1, 2, Int.MAX_VALUE))
expectMinMax(1, Long.MAX_VALUE, listOf(1, 2, Long.MAX_VALUE))
expectMinMax(1U, UInt.MAX_VALUE, listOf(1U, 2U, UInt.MAX_VALUE))
expectMinMax('a', Char.MAX_VALUE, listOf('a', 'b', Char.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = listOf<Int>()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
@Test
fun minMaxDouble() {
val zeroes = listOf(0.0, -0.0).shuffled()
val NaNs = listOf(0.0, Double.NaN).shuffled()
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
@Test
fun minMaxFloat() {
val zeroes = listOf(0.0F, -0.0F).shuffled()
val NaNs = listOf(0.0F, Float.NaN).shuffled()
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
private fun <T> expectMinMaxWith(min: T, max: T, elements: Iterable<T>, comparator: Comparator<T>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith("a", "a", listOf("a"), naturalOrder())
expectMinMaxWith("a", "bcd", listOf("a", "bcd"), naturalOrder())
expectMinMaxWith("a", "e", listOf("a", "bcd", "e"), naturalOrder())
expectMinMaxWith("a", "B", listOf("a", "B"), String.CASE_INSENSITIVE_ORDER)
}
@Test
fun minMaxWithEmpty() {
val empty = listOf<Int>()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <T, K : Comparable<K>> expectMinMaxBy(min: T, max: T, elements: Iterable<T>, selector: (T) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy("a", "a", listOf("a"), { it })
expectMinMaxBy("a", "bcd", listOf("a", "bcd"), { it })
expectMinMaxBy("a", "e", listOf("a", "bcd", "e"), { it })
expectMinMaxBy("De", "abc", listOf("abc", "De"), { it.length })
}
@Test
fun minMaxByEmpty() {
val empty = listOf<Int>()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = listOf("a", "bcd", "e")
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = listOf("a", "bcd", "e")
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <T, R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: Iterable<T>, selector: (T) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf("_" + "a", "_" + "a", listOf("a"), { "_$it" })
expectMinMaxOf("_" + "a", "_" + "bcd", listOf("a", "bcd"), { "_$it" })
expectMinMaxOf("_" + "a", "_" + "e", listOf("a", "bcd", "e"), { "_$it" })
}
@Test
fun minMaxOfDouble() {
val middle = "bcd"
val items = listOf("a", "bcd", "e").shuffled()
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = "bcd"
val items = listOf("a", "bcd", "e").shuffled()
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = listOf<Int>()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <T, R> expectMinMaxOfWith(min: R, max: R, elements: Iterable<T>, comparator: Comparator<R>, selector: (T) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith("_" + "a", "_" + "a", listOf("a"), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + "bcd", "_" + "a", listOf("a", "bcd"), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + "e", "_" + "a", listOf("a", "bcd", "e"), reverseOrder(), { "_$it" })
}
@Test
fun minMaxOfWithEmpty() {
val empty = listOf<Int>()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxLongArrayTest {
private fun expectMinMax(min: Long, max: Long, elements: LongArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1, 1, longArrayOf(1))
expectMinMax(1, 2, longArrayOf(1, 2))
expectMinMax(1, Long.MAX_VALUE, longArrayOf(1, 2, Long.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = longArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: Long, max: Long, elements: LongArray, comparator: Comparator<Long>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1, 1, longArrayOf(1), naturalOrder())
expectMinMaxWith(1, 2, longArrayOf(1, 2), naturalOrder())
expectMinMaxWith(1, Long.MAX_VALUE, longArrayOf(1, 2, Long.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = longArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Long, max: Long, elements: LongArray, selector: (Long) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1, 1, longArrayOf(1), { it })
expectMinMaxBy(1, 2, longArrayOf(1, 2), { it })
expectMinMaxBy(1, Long.MAX_VALUE, longArrayOf(1, 2, Long.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = longArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = longArrayOf(1, 2, Long.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = longArrayOf(1, 2, Long.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: LongArray, selector: (Long) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf(-1, -1, longArrayOf(1), { -it })
expectMinMaxOf(-2, -1, longArrayOf(1, 2), { -it })
expectMinMaxOf(-Long.MAX_VALUE, -1, longArrayOf(1, 2, Long.MAX_VALUE), { -it })
}
@Test
fun minMaxOfDouble() {
val middle = 2
val items = longArrayOf(1, 2, Long.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2
val items = longArrayOf(1, 2, Long.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = longArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: LongArray, comparator: Comparator<R>, selector: (Long) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith(-1, -1, longArrayOf(1), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -2, longArrayOf(1, 2), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -Long.MAX_VALUE, longArrayOf(1, 2, Long.MAX_VALUE), reverseOrder(), { -it })
}
@Test
fun minMaxOfWithEmpty() {
val empty = longArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,255 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxSequenceTest {
private fun <T : Comparable<T>> expectMinMax(min: T, max: T, elements: Sequence<T>) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax("a", "a", sequenceOf("a"))
expectMinMax("a", "bcd", sequenceOf("a", "bcd"))
expectMinMax("a", "e", sequenceOf("a", "bcd", "e"))
expectMinMax(1, Int.MAX_VALUE, sequenceOf(1, 2, Int.MAX_VALUE))
expectMinMax(1, Long.MAX_VALUE, sequenceOf(1, 2, Long.MAX_VALUE))
expectMinMax(1U, UInt.MAX_VALUE, sequenceOf(1U, 2U, UInt.MAX_VALUE))
expectMinMax('a', Char.MAX_VALUE, sequenceOf('a', 'b', Char.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = sequenceOf<Int>()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
@Test
fun minMaxDouble() {
val zeroes = sequenceOf(0.0, -0.0).shuffled()
val NaNs = sequenceOf(0.0, Double.NaN).shuffled()
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
@Test
fun minMaxFloat() {
val zeroes = sequenceOf(0.0F, -0.0F).shuffled()
val NaNs = sequenceOf(0.0F, Float.NaN).shuffled()
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
}
private fun <T> expectMinMaxWith(min: T, max: T, elements: Sequence<T>, comparator: Comparator<T>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith("a", "a", sequenceOf("a"), naturalOrder())
expectMinMaxWith("a", "bcd", sequenceOf("a", "bcd"), naturalOrder())
expectMinMaxWith("a", "e", sequenceOf("a", "bcd", "e"), naturalOrder())
expectMinMaxWith("a", "B", sequenceOf("a", "B"), String.CASE_INSENSITIVE_ORDER)
}
@Test
fun minMaxWithEmpty() {
val empty = sequenceOf<Int>()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <T, K : Comparable<K>> expectMinMaxBy(min: T, max: T, elements: Sequence<T>, selector: (T) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy("a", "a", sequenceOf("a"), { it })
expectMinMaxBy("a", "bcd", sequenceOf("a", "bcd"), { it })
expectMinMaxBy("a", "e", sequenceOf("a", "bcd", "e"), { it })
expectMinMaxBy("De", "abc", sequenceOf("abc", "De"), { it.length })
}
@Test
fun minMaxByEmpty() {
val empty = sequenceOf<Int>()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = sequenceOf("a", "bcd", "e")
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = sequenceOf("a", "bcd", "e")
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <T, R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: Sequence<T>, selector: (T) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf("_" + "a", "_" + "a", sequenceOf("a"), { "_$it" })
expectMinMaxOf("_" + "a", "_" + "bcd", sequenceOf("a", "bcd"), { "_$it" })
expectMinMaxOf("_" + "a", "_" + "e", sequenceOf("a", "bcd", "e"), { "_$it" })
}
@Test
fun minMaxOfDouble() {
val middle = "bcd"
val items = sequenceOf("a", "bcd", "e").shuffled()
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = "bcd"
val items = sequenceOf("a", "bcd", "e").shuffled()
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = sequenceOf<Int>()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <T, R> expectMinMaxOfWith(min: R, max: R, elements: Sequence<T>, comparator: Comparator<R>, selector: (T) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith("_" + "a", "_" + "a", sequenceOf("a"), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + "bcd", "_" + "a", sequenceOf("a", "bcd"), reverseOrder(), { "_$it" })
expectMinMaxOfWith("_" + "e", "_" + "a", sequenceOf("a", "bcd", "e"), reverseOrder(), { "_$it" })
}
@Test
fun minMaxOfWithEmpty() {
val empty = sequenceOf<Int>()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxShortArrayTest {
private fun expectMinMax(min: Short, max: Short, elements: ShortArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1, 1, shortArrayOf(1))
expectMinMax(1, 2, shortArrayOf(1, 2))
expectMinMax(1, Short.MAX_VALUE, shortArrayOf(1, 2, Short.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = shortArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: Short, max: Short, elements: ShortArray, comparator: Comparator<Short>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1, 1, shortArrayOf(1), naturalOrder())
expectMinMaxWith(1, 2, shortArrayOf(1, 2), naturalOrder())
expectMinMaxWith(1, Short.MAX_VALUE, shortArrayOf(1, 2, Short.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = shortArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: Short, max: Short, elements: ShortArray, selector: (Short) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1, 1, shortArrayOf(1), { it })
expectMinMaxBy(1, 2, shortArrayOf(1, 2), { it })
expectMinMaxBy(1, Short.MAX_VALUE, shortArrayOf(1, 2, Short.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = shortArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = shortArrayOf(1, 2, Short.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = shortArrayOf(1, 2, Short.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: ShortArray, selector: (Short) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf(-1, -1, shortArrayOf(1), { -it })
expectMinMaxOf(-2, -1, shortArrayOf(1, 2), { -it })
expectMinMaxOf(-Short.MAX_VALUE, -1, shortArrayOf(1, 2, Short.MAX_VALUE), { -it })
}
@Test
fun minMaxOfDouble() {
val middle = 2
val items = shortArrayOf(1, 2, Short.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2
val items = shortArrayOf(1, 2, Short.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = shortArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: ShortArray, comparator: Comparator<R>, selector: (Short) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith(-1, -1, shortArrayOf(1), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -2, shortArrayOf(1, 2), reverseOrder(), { -it })
expectMinMaxOfWith(-1, -Short.MAX_VALUE, shortArrayOf(1, 2, Short.MAX_VALUE), reverseOrder(), { -it })
}
@Test
fun minMaxOfWithEmpty() {
val empty = shortArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxUByteArrayTest {
private fun expectMinMax(min: UByte, max: UByte, elements: UByteArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1U, 1U, ubyteArrayOf(1U))
expectMinMax(1U, 2U, ubyteArrayOf(1U, 2U))
expectMinMax(1U, UByte.MAX_VALUE, ubyteArrayOf(1U, 2U, UByte.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = ubyteArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: UByte, max: UByte, elements: UByteArray, comparator: Comparator<UByte>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1U, 1U, ubyteArrayOf(1U), naturalOrder())
expectMinMaxWith(1U, 2U, ubyteArrayOf(1U, 2U), naturalOrder())
expectMinMaxWith(1U, UByte.MAX_VALUE, ubyteArrayOf(1U, 2U, UByte.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = ubyteArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: UByte, max: UByte, elements: UByteArray, selector: (UByte) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1U, 1U, ubyteArrayOf(1U), { it })
expectMinMaxBy(1U, 2U, ubyteArrayOf(1U, 2U), { it })
expectMinMaxBy(1U, UByte.MAX_VALUE, ubyteArrayOf(1U, 2U, UByte.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = ubyteArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = ubyteArrayOf(1U, 2U, UByte.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = ubyteArrayOf(1U, 2U, UByte.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: UByteArray, selector: (UByte) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf((0u - 1U).toUByte(), (0u - 1U).toUByte(), ubyteArrayOf(1U), { (0u - it).toUByte() })
expectMinMaxOf((0u - 2U).toUByte(), (0u - 1U).toUByte(), ubyteArrayOf(1U, 2U), { (0u - it).toUByte() })
expectMinMaxOf((0u - UByte.MAX_VALUE).toUByte(), (0u - 1U).toUByte(), ubyteArrayOf(1U, 2U, UByte.MAX_VALUE), { (0u - it).toUByte() })
}
@Test
fun minMaxOfDouble() {
val middle = 2U
val items = ubyteArrayOf(1U, 2U, UByte.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2U
val items = ubyteArrayOf(1U, 2U, UByte.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = ubyteArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: UByteArray, comparator: Comparator<R>, selector: (UByte) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith((0u - 1U).toUByte(), (0u - 1U).toUByte(), ubyteArrayOf(1U), reverseOrder(), { (0u - it).toUByte() })
expectMinMaxOfWith((0u - 1U).toUByte(), (0u - 2U).toUByte(), ubyteArrayOf(1U, 2U), reverseOrder(), { (0u - it).toUByte() })
expectMinMaxOfWith((0u - 1U).toUByte(), (0u - UByte.MAX_VALUE).toUByte(), ubyteArrayOf(1U, 2U, UByte.MAX_VALUE), reverseOrder(), { (0u - it).toUByte() })
}
@Test
fun minMaxOfWithEmpty() {
val empty = ubyteArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxUIntArrayTest {
private fun expectMinMax(min: UInt, max: UInt, elements: UIntArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1U, 1U, uintArrayOf(1U))
expectMinMax(1U, 2U, uintArrayOf(1U, 2U))
expectMinMax(1U, UInt.MAX_VALUE, uintArrayOf(1U, 2U, UInt.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = uintArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: UInt, max: UInt, elements: UIntArray, comparator: Comparator<UInt>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1U, 1U, uintArrayOf(1U), naturalOrder())
expectMinMaxWith(1U, 2U, uintArrayOf(1U, 2U), naturalOrder())
expectMinMaxWith(1U, UInt.MAX_VALUE, uintArrayOf(1U, 2U, UInt.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = uintArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: UInt, max: UInt, elements: UIntArray, selector: (UInt) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1U, 1U, uintArrayOf(1U), { it })
expectMinMaxBy(1U, 2U, uintArrayOf(1U, 2U), { it })
expectMinMaxBy(1U, UInt.MAX_VALUE, uintArrayOf(1U, 2U, UInt.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = uintArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = uintArrayOf(1U, 2U, UInt.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = uintArrayOf(1U, 2U, UInt.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: UIntArray, selector: (UInt) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf((0u - 1U).toUInt(), (0u - 1U).toUInt(), uintArrayOf(1U), { (0u - it).toUInt() })
expectMinMaxOf((0u - 2U).toUInt(), (0u - 1U).toUInt(), uintArrayOf(1U, 2U), { (0u - it).toUInt() })
expectMinMaxOf((0u - UInt.MAX_VALUE).toUInt(), (0u - 1U).toUInt(), uintArrayOf(1U, 2U, UInt.MAX_VALUE), { (0u - it).toUInt() })
}
@Test
fun minMaxOfDouble() {
val middle = 2U
val items = uintArrayOf(1U, 2U, UInt.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2U
val items = uintArrayOf(1U, 2U, UInt.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = uintArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: UIntArray, comparator: Comparator<R>, selector: (UInt) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith((0u - 1U).toUInt(), (0u - 1U).toUInt(), uintArrayOf(1U), reverseOrder(), { (0u - it).toUInt() })
expectMinMaxOfWith((0u - 1U).toUInt(), (0u - 2U).toUInt(), uintArrayOf(1U, 2U), reverseOrder(), { (0u - it).toUInt() })
expectMinMaxOfWith((0u - 1U).toUInt(), (0u - UInt.MAX_VALUE).toUInt(), uintArrayOf(1U, 2U, UInt.MAX_VALUE), reverseOrder(), { (0u - it).toUInt() })
}
@Test
fun minMaxOfWithEmpty() {
val empty = uintArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxULongArrayTest {
private fun expectMinMax(min: ULong, max: ULong, elements: ULongArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1UL, 1UL, ulongArrayOf(1UL))
expectMinMax(1UL, 2UL, ulongArrayOf(1UL, 2UL))
expectMinMax(1UL, ULong.MAX_VALUE, ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = ulongArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: ULong, max: ULong, elements: ULongArray, comparator: Comparator<ULong>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1UL, 1UL, ulongArrayOf(1UL), naturalOrder())
expectMinMaxWith(1UL, 2UL, ulongArrayOf(1UL, 2UL), naturalOrder())
expectMinMaxWith(1UL, ULong.MAX_VALUE, ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = ulongArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: ULong, max: ULong, elements: ULongArray, selector: (ULong) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1UL, 1UL, ulongArrayOf(1UL), { it })
expectMinMaxBy(1UL, 2UL, ulongArrayOf(1UL, 2UL), { it })
expectMinMaxBy(1UL, ULong.MAX_VALUE, ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = ulongArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: ULongArray, selector: (ULong) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf((0u - 1UL).toULong(), (0u - 1UL).toULong(), ulongArrayOf(1UL), { (0u - it).toULong() })
expectMinMaxOf((0u - 2UL).toULong(), (0u - 1UL).toULong(), ulongArrayOf(1UL, 2UL), { (0u - it).toULong() })
expectMinMaxOf((0u - ULong.MAX_VALUE).toULong(), (0u - 1UL).toULong(), ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE), { (0u - it).toULong() })
}
@Test
fun minMaxOfDouble() {
val middle = 2UL
val items = ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2UL
val items = ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = ulongArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: ULongArray, comparator: Comparator<R>, selector: (ULong) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith((0u - 1UL).toULong(), (0u - 1UL).toULong(), ulongArrayOf(1UL), reverseOrder(), { (0u - it).toULong() })
expectMinMaxOfWith((0u - 1UL).toULong(), (0u - 2UL).toULong(), ulongArrayOf(1UL, 2UL), reverseOrder(), { (0u - it).toULong() })
expectMinMaxOfWith((0u - 1UL).toULong(), (0u - ULong.MAX_VALUE).toULong(), ulongArrayOf(1UL, 2UL, ULong.MAX_VALUE), reverseOrder(), { (0u - it).toULong() })
}
@Test
fun minMaxOfWithEmpty() {
val empty = ulongArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2022 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.generated.minmax
//
// NOTE: THIS FILE IS AUTO-GENERATED by the MinMaxTestGenerator.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.math.pow
import kotlin.test.*
import test.*
class MinMaxUShortArrayTest {
private fun expectMinMax(min: UShort, max: UShort, elements: UShortArray) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {
expectMinMax(1U, 1U, ushortArrayOf(1U))
expectMinMax(1U, 2U, ushortArrayOf(1U, 2U))
expectMinMax(1U, UShort.MAX_VALUE, ushortArrayOf(1U, 2U, UShort.MAX_VALUE))
}
@Test
fun minMaxEmpty() {
val empty = ushortArrayOf()
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
private fun expectMinMaxWith(min: UShort, max: UShort, elements: UShortArray, comparator: Comparator<UShort>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {
expectMinMaxWith(1U, 1U, ushortArrayOf(1U), naturalOrder())
expectMinMaxWith(1U, 2U, ushortArrayOf(1U, 2U), naturalOrder())
expectMinMaxWith(1U, UShort.MAX_VALUE, ushortArrayOf(1U, 2U, UShort.MAX_VALUE), naturalOrder())
}
@Test
fun minMaxWithEmpty() {
val empty = ushortArrayOf()
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <K : Comparable<K>> expectMinMaxBy(min: UShort, max: UShort, elements: UShortArray, selector: (UShort) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {
expectMinMaxBy(1U, 1U, ushortArrayOf(1U), { it })
expectMinMaxBy(1U, 2U, ushortArrayOf(1U, 2U), { it })
expectMinMaxBy(1U, UShort.MAX_VALUE, ushortArrayOf(1U, 2U, UShort.MAX_VALUE), { it })
}
@Test
fun minMaxByEmpty() {
val empty = ushortArrayOf()
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = ushortArrayOf(1U, 2U, UShort.MAX_VALUE)
var c = 0
source.minBy { c++ }
assertEquals(3, c)
c = 0
source.minByOrNull { c++ }
assertEquals(3, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = ushortArrayOf(1U, 2U, UShort.MAX_VALUE)
var c = 0
source.maxBy { c++ }
assertEquals(3, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(3, c)
}
private inline fun <R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: UShortArray, selector: (UShort) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {
expectMinMaxOf((0u - 1U).toUShort(), (0u - 1U).toUShort(), ushortArrayOf(1U), { (0u - it).toUShort() })
expectMinMaxOf((0u - 2U).toUShort(), (0u - 1U).toUShort(), ushortArrayOf(1U, 2U), { (0u - it).toUShort() })
expectMinMaxOf((0u - UShort.MAX_VALUE).toUShort(), (0u - 1U).toUShort(), ushortArrayOf(1U, 2U, UShort.MAX_VALUE), { (0u - it).toUShort() })
}
@Test
fun minMaxOfDouble() {
val middle = 2U
val items = ushortArrayOf(1U, 2U, UShort.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = 2U
val items = ushortArrayOf(1U, 2U, UShort.MAX_VALUE).apply { shuffle() }
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = ushortArrayOf()
assertNull(empty.minOfOrNull { it.toString() })
assertNull(empty.maxOfOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOf { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOf { it.toString() } }
assertNull(empty.minOfOrNull { 0.0 })
assertNull(empty.maxOfOrNull { 0.0 })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0 } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0 } }
assertNull(empty.minOfOrNull { 0.0F })
assertNull(empty.maxOfOrNull { 0.0F })
assertFailsWith<NoSuchElementException> { empty.minOf { 0.0F } }
assertFailsWith<NoSuchElementException> { empty.maxOf { 0.0F } }
}
private inline fun <R> expectMinMaxOfWith(min: R, max: R, elements: UShortArray, comparator: Comparator<R>, selector: (UShort) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {
expectMinMaxOfWith((0u - 1U).toUShort(), (0u - 1U).toUShort(), ushortArrayOf(1U), reverseOrder(), { (0u - it).toUShort() })
expectMinMaxOfWith((0u - 1U).toUShort(), (0u - 2U).toUShort(), ushortArrayOf(1U, 2U), reverseOrder(), { (0u - it).toUShort() })
expectMinMaxOfWith((0u - 1U).toUShort(), (0u - UShort.MAX_VALUE).toUShort(), ushortArrayOf(1U, 2U, UShort.MAX_VALUE), reverseOrder(), { (0u - it).toUShort() })
}
@Test
fun minMaxOfWithEmpty() {
val empty = ushortArrayOf()
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
}
+3
View File
@@ -62,6 +62,9 @@ val commonTestSources by task<Sync> {
sources.forEach { path ->
from("$rootDir/$path") {
into(path.dropLastWhile { it != '/' })
// exclusions due to KT-51647
exclude("generated/minmax")
exclude("collections/MapTest.kt")
}
}
@@ -36,6 +36,14 @@ task run(type: JavaExec) {
systemProperty 'line.separator', '\n'
}
task generateStdlibTests(type: JavaExec) {
group 'application'
main 'generators.GenerateStandardLibTestsKt'
classpath sourceSets.main.runtimeClasspath
args = ["${rootDir}"]
systemProperty 'line.separator', '\n'
}
task generateUnicodeData(type: JavaExec) {
group 'application'
main 'generators.unicode.GenerateUnicodeDataKt'
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2022 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 generators.test.MinMaxTestGenerator
fun main() {
MinMaxTestGenerator.generate()
}
@@ -0,0 +1,358 @@
/*
* Copyright 2010-2022 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.test
import templates.*
import templates.Family.*
import templates.PrimitiveType
import java.io.File
object MinMaxTestGenerator {
fun generate() {
generate(Iterables)
generate(Sequences)
generate(CharSequences)
generate(ArraysOfObjects)
for (primitive in PrimitiveType.defaultPrimitives - PrimitiveType.Boolean) {
generate(ArraysOfPrimitives, primitive)
}
for (primitive in PrimitiveType.unsignedPrimitives) {
generate(ArraysOfUnsigned, primitive)
}
}
private fun elements(elementType: String) = when (elementType) {
"Int", "Byte", "Short", "Long" -> listOf("1", "2", "$elementType.MAX_VALUE")
"UInt", "UByte", "UShort" -> listOf("1U", "2U", "$elementType.MAX_VALUE")
"ULong" -> listOf("1UL", "2UL", "$elementType.MAX_VALUE")
"Double" -> listOf("1.0", "2.0", "$elementType.POSITIVE_INFINITY")
"Float" -> listOf("1.0F", "2.0F", "$elementType.POSITIVE_INFINITY")
"Char" -> listOf("'a'", "'b'", "$elementType.MAX_VALUE")
"T" -> listOf("a", "bcd", "e").map { "\"$it\"" }
else -> error(elementType)
}
private fun generate(family: Family, primitive: PrimitiveType? = null) {
val containerClass = when(family) {
Iterables, Sequences, CharSequences -> family.toString().dropLast(1)
ArraysOfObjects -> "Array"
ArraysOfPrimitives, ArraysOfUnsigned -> "${primitive!!}Array"
else -> error(family)
}
val isGeneric = family in listOf(Iterables, Sequences, ArraysOfObjects)
val elementType = when {
isGeneric -> "T"
family == CharSequences -> "Char"
else -> primitive!!.toString()
}
val defaultElements = elements(elementType)
val containerType = containerClass + "<T>".ifOrEmpty(isGeneric)
fun emptyContainer(elementType: String): String {
val type = if (elementType == "T") "Int" else elementType
return when (family) {
CharSequences -> "StringBuilder()"
Iterables -> "listOf<$type>()"
Sequences -> "sequenceOf<$type>()"
ArraysOfObjects -> "arrayOf<$type>()"
else -> "${containerClass.let { it.substring(0, 2).lowercase() + it.substring(2) }}Of()"
}
}
fun containerOf(elements: List<String>, shuffle: Boolean = false): String {
require(elements.isNotEmpty())
val args = elements.joinToString()
val shuffled = when (family) {
ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences -> ".apply { shuffle() }"
else -> ".shuffled()"
}
return when (family) {
CharSequences -> "StringBuilder(charArrayOf($args)${shuffled.ifOrEmpty(shuffle)}.concatToString())"
Iterables -> "listOf($args)${shuffled.ifOrEmpty(shuffle)}"
else -> "${containerClass.let { it.substring(0, 2).lowercase() + it.substring(2) }}Of($args)${shuffled.ifOrEmpty(shuffle)}"
}
}
val className = "MinMax${containerClass}Test"
val file = File("libraries/stdlib/test/generated/minmax/$className.kt")
file.bufferedWriter().use { writer ->
writer.appendLine(COPYRIGHT_NOTICE)
writer.appendLine()
writer.appendLine("package test.generated.minmax")
writer.appendLine()
writer.appendLine(autoGeneratedWarning("MinMaxTestGenerator.kt"))
writer.appendLine()
writer.appendLine("import kotlin.math.pow")
writer.appendLine("import kotlin.test.*")
writer.appendLine("import test.*")
writer.appendLine()
writer.appendLine("class $className {")
writer.appendLine("""
private fun ${"<T : Comparable<T>> ".ifOrEmpty(isGeneric)}expectMinMax(min: $elementType, max: $elementType, elements: $containerType) {
assertEquals(min, elements.minOrNull())
assertEquals(max, elements.maxOrNull())
assertEquals(min, elements.min())
assertEquals(max, elements.max())
}
@Test
fun minMax() {""")
for (size in 1..3) {
val elements = defaultElements.take(size)
writer.appendLine(" expectMinMax(${elements.first()}, ${elements.last()}, ${containerOf(elements)})")
}
if (isGeneric) {
for (type in listOf(PrimitiveType.Int, PrimitiveType.Long, PrimitiveType.UInt, PrimitiveType.Char)) {
val elements = elements(type.toString())
writer.appendLine(" expectMinMax(${elements.first()}, ${elements.last()}, ${containerOf(elements)})")
}
}
writer.appendLine(
"""
}
@Test
fun minMaxEmpty() {
val empty = ${emptyContainer(elementType)}
assertNull(empty.minOrNull())
assertNull(empty.maxOrNull())
assertFailsWith<NoSuchElementException> { empty.min() }
assertFailsWith<NoSuchElementException> { empty.max() }
}
""")
run {
val assertions = """
assertIsNegativeZero(zeroes.min().toDouble())
assertIsNegativeZero(zeroes.minOrNull()!!.toDouble())
assertTrue(NaNs.min().isNaN())
assertTrue(NaNs.minOrNull()!!.isNaN())
assertIsPositiveZero(zeroes.max().toDouble())
assertIsPositiveZero(zeroes.maxOrNull()!!.toDouble())
assertTrue(NaNs.max().isNaN())
assertTrue(NaNs.maxOrNull()!!.isNaN())
"""
if (isGeneric || primitive == PrimitiveType.Double) {
writer.appendLine("""
@Test
fun minMaxDouble() {
val zeroes = ${containerOf(listOf("0.0", "-0.0"), shuffle = true)}
val NaNs = ${containerOf(listOf("0.0", "Double.NaN"), shuffle = true)}
$assertions
}
""")
}
if (isGeneric || primitive == PrimitiveType.Float) {
writer.appendLine("""
@Test
fun minMaxFloat() {
val zeroes = ${containerOf(listOf("0.0F", "-0.0F"), shuffle = true)}
val NaNs = ${containerOf(listOf("0.0F", "Float.NaN"), shuffle = true)}
$assertions
}
""")
}
}
writer.appendLine("""
private fun ${"<T> ".ifOrEmpty(isGeneric)}expectMinMaxWith(min: $elementType, max: $elementType, elements: $containerType, comparator: Comparator<$elementType>) {
assertEquals(min, elements.minWithOrNull(comparator))
assertEquals(max, elements.maxWithOrNull(comparator))
assertEquals(min, elements.minWith(comparator))
assertEquals(max, elements.maxWith(comparator))
}
@Test
fun minMaxWith() {""")
for (size in 1..3) {
val elements = defaultElements.take(size)
writer.appendLine(" expectMinMaxWith(${elements.first()}, ${elements.last()}, ${containerOf(elements)}, naturalOrder())")
}
if (isGeneric) {
val elements = listOf("a", "B").map { "\"$it\"" }
writer.appendLine(" expectMinMaxWith(${elements.first()}, ${elements.last()}, ${containerOf(elements)}, String.CASE_INSENSITIVE_ORDER)")
}
writer.appendLine("""
}
@Test
fun minMaxWithEmpty() {
val empty = ${emptyContainer(elementType)}
assertNull(empty.minWithOrNull(naturalOrder()))
assertNull(empty.maxWithOrNull(naturalOrder()))
assertFailsWith<NoSuchElementException> { empty.minWith(naturalOrder()) }
assertFailsWith<NoSuchElementException> { empty.maxWith(naturalOrder()) }
}
private inline fun <${"T, ".ifOrEmpty(isGeneric)}K : Comparable<K>> expectMinMaxBy(min: $elementType, max: $elementType, elements: $containerType, selector: ($elementType) -> K) {
assertEquals(min, elements.minBy(selector))
assertEquals(min, elements.minByOrNull(selector))
assertEquals(max, elements.maxBy(selector))
assertEquals(max, elements.maxByOrNull(selector))
}
@Test
fun minMaxBy() {""")
for (size in 1..3) {
val elements = defaultElements.take(size)
writer.appendLine(" expectMinMaxBy(${elements.first()}, ${elements.last()}, ${containerOf(elements)}, { it })")
}
if (isGeneric) {
val elements = listOf("abc", "De").map { "\"$it\"" }
writer.appendLine(" expectMinMaxBy(${elements.last()}, ${elements.first()}, ${containerOf(elements)}, { it.length })")
}
writer.appendLine("""
}
@Test
fun minMaxByEmpty() {
val empty = ${emptyContainer(elementType)}
assertNull(empty.minByOrNull { it.toString() })
assertNull(empty.maxByOrNull { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minBy { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxBy { it.toString() } }
}
@Test
fun minBySelectorEvaluateOnce() {
val source = ${containerOf(defaultElements)}
var c = 0
source.minBy { c++ }
assertEquals(${defaultElements.size}, c)
c = 0
source.minByOrNull { c++ }
assertEquals(${defaultElements.size}, c)
}
@Test
fun maxBySelectorEvaluateOnce() {
val source = ${containerOf(defaultElements)}
var c = 0
source.maxBy { c++ }
assertEquals(${defaultElements.size}, c)
c = 0
source.maxByOrNull { c++ }
assertEquals(${defaultElements.size}, c)
}
private inline fun <${"T, ".ifOrEmpty(isGeneric)}R : Comparable<R>> expectMinMaxOf(min: R, max: R, elements: $containerType, selector: ($elementType) -> R) {
assertEquals(min, elements.minOf(selector))
assertEquals(min, elements.minOfOrNull(selector))
assertEquals(max, elements.maxOf(selector))
assertEquals(max, elements.maxOfOrNull(selector))
}
@Test
fun minMaxOf() {""")
for (size in 1..3) {
val elements = defaultElements.take(size)
if (primitive?.isNumeric() == true) {
writer.appendLine(" expectMinMaxOf(-${elements.last()}, -${elements.first()}, ${containerOf(elements)}, { -it })")
} else if (primitive?.isUnsigned() == true) {
writer.appendLine(" expectMinMaxOf((0u - ${elements.last()}).to$primitive(), (0u - ${elements.first()}).to$primitive(), ${containerOf(elements)}, { (0u - it).to$primitive() })")
} else {
writer.appendLine(" expectMinMaxOf(\"_\" + ${elements.first()}, \"_\" + ${elements.last()}, ${containerOf(elements)}, { \"_\$it\" })")
}
}
writer.appendLine("""
}
@Test
fun minMaxOfDouble() {
val middle = ${defaultElements[1]}
val items = ${containerOf(defaultElements, shuffle = true)}
assertTrue(items.minOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toDouble().pow(0.5) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toDouble().pow(0.5) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0 })
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0 }!!)
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0 })
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0 }!!)
}
@Test
fun minMaxOfFloat() {
val middle = ${defaultElements[1]}
val items = ${containerOf(defaultElements, shuffle = true)}
assertTrue(items.minOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.minOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertTrue(items.maxOf { it.compareTo(middle).toFloat().pow(0.5F) }.isNaN())
assertTrue(items.maxOfOrNull { it.compareTo(middle).toFloat().pow(0.5F) }!!.isNaN())
assertIsNegativeZero(items.minOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsNegativeZero(items.minOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
assertIsPositiveZero(items.maxOf { it.compareTo(middle) * 0.0F }.toDouble())
assertIsPositiveZero(items.maxOfOrNull { it.compareTo(middle) * 0.0F }!!.toDouble())
}
@Test
fun minMaxOfEmpty() {
val empty = ${emptyContainer(elementType)}""")
for (selector in listOf("it.toString()", "0.0", "0.0F")) {
writer.appendLine("""
assertNull(empty.minOfOrNull { $selector })
assertNull(empty.maxOfOrNull { $selector })
assertFailsWith<NoSuchElementException> { empty.minOf { $selector } }
assertFailsWith<NoSuchElementException> { empty.maxOf { $selector } }
""")
}
writer.appendLine("""
}
private inline fun <${"T, ".ifOrEmpty(isGeneric)}R> expectMinMaxOfWith(min: R, max: R, elements: $containerType, comparator: Comparator<R>, selector: ($elementType) -> R) {
assertEquals(min, elements.minOfWith(comparator, selector))
assertEquals(min, elements.minOfWithOrNull(comparator, selector))
assertEquals(max, elements.maxOfWith(comparator, selector))
assertEquals(max, elements.maxOfWithOrNull(comparator, selector))
}
@Test
fun minMaxOfWith() {""")
for (size in 1..3) {
val elements = defaultElements.take(size)
if (primitive?.isNumeric() == true) {
writer.appendLine(" expectMinMaxOfWith(-${elements.first()}, -${elements.last()}, ${containerOf(elements)}, reverseOrder(), { -it })")
} else if (primitive?.isUnsigned() == true) {
writer.appendLine(" expectMinMaxOfWith((0u - ${elements.first()}).to$primitive(), (0u - ${elements.last()}).to$primitive(), ${containerOf(elements)}, reverseOrder(), { (0u - it).to$primitive() })")
} else {
writer.appendLine(" expectMinMaxOfWith(\"_\" + ${elements.last()}, \"_\" + ${elements.first()}, ${containerOf(elements)}, reverseOrder(), { \"_\$it\" })")
}
}
writer.appendLine("""
}
@Test
fun minMaxOfWithEmpty() {
val empty = ${emptyContainer(elementType)}
assertNull(empty.minOfWithOrNull(naturalOrder()) { it.toString() })
assertNull(empty.maxOfWithOrNull(naturalOrder()) { it.toString() })
assertFailsWith<NoSuchElementException> { empty.minOfWith(naturalOrder()) { it.toString() } }
assertFailsWith<NoSuchElementException> { empty.maxOfWith(naturalOrder()) { it.toString() } }
}
""")
writer.appendLine("}")
}
}
}