tests: Copy stdlib tests from Kotlin/JVM

This patch copies Kotlin/JVM stdlib tests (libraries/stdlib/test)
excepting ones with explicit java dependencies. It also transforms
the tests to use them as box-tests.
This commit is contained in:
Ilya Matveev
2017-04-04 18:20:09 +07:00
committed by ilmat192
parent 5d50943ebd
commit 85827b4880
530 changed files with 11027 additions and 4 deletions
@@ -0,0 +1,26 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
val v3 = Item("apple", 20)
fun box() {
val byName = compareBy<Item> { it.name }
val byRating = compareBy<Item> { it.rating }
val v3 = Item(v1.name, v1.rating + 1)
val v4 = Item(v2.name + "_", v2.rating)
assertTrue((byName then byRating).compare(v1, v2) > 0)
assertTrue((byName then byRating).compare(v1, v3) < 0)
assertTrue((byName thenDescending byRating).compare(v1, v3) > 0)
assertTrue((byRating then byName).compare(v1, v2) < 0)
assertTrue((byRating then byName).compare(v4, v2) > 0)
assertTrue((byRating thenDescending byName).compare(v4, v2) < 0)
}
@@ -0,0 +1,16 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val diff = v1.compareTo(v2)
assertTrue(diff < 0)
}
@@ -0,0 +1,16 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val diff = compareValuesBy(v1, v2, { it.name }, { it.rating })
assertTrue(diff > 0)
}
@@ -0,0 +1,16 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val diff = compareValuesBy(v1, v2, { it.rating }, { it.name })
assertTrue(diff < 0)
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val v1: Item? = v1
val v2: Item? = null
val diff = compareValuesBy(v1, v2) { it?.rating }
assertTrue(diff > 0)
val diff2 = nullsLast(compareBy<Item> { it.rating }.thenBy { it.name }).compare(v1, v2)
assertTrue(diff2 < 0)
}
@@ -0,0 +1,15 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
fun box() {
val diff = compareValuesBy(v1, v1, { it.rating }, { it.name })
assertTrue(diff == 0)
}
@@ -0,0 +1,14 @@
import kotlin.test.*
import kotlin.comparisons.*
fun box() {
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE))
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE))
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
}
@@ -0,0 +1,19 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
val v3 = Item("apple", 20)
fun box() {
assertEquals(v1, maxOf(v1, v2, compareBy { it.name }))
assertEquals(v1, maxOf(v3, v2, v1, compareBy { it.name }))
assertEquals(v2, maxOf(v1, v2, compareBy { it.rating }))
assertEquals(v3, maxOf(v1, v2, v3, compareBy { it.rating }))
}
@@ -0,0 +1,14 @@
import kotlin.test.*
import kotlin.comparisons.*
fun box() {
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE))
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE))
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
assertEquals(Double.MIN_VALUE, minOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
}
@@ -0,0 +1,19 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
val v3 = Item("apple", 20)
fun box() {
assertEquals(v2, minOf(v1, v2, compareBy { it.name }))
assertEquals(v3, minOf(v3, v2, v1, compareBy { it.name }))
assertEquals(v1, minOf(v1, v2, compareBy { it.rating }))
assertEquals(v1, minOf(v1, v2, v3, compareBy { it.rating }))
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val v1 = "a"
val v2 = "beta"
assertTrue(naturalOrder<String>().compare(v1, v2) < 0)
assertTrue(reverseOrder<String>().compare(v1, v2) > 0)
assertTrue(reverseOrder<Int>() === naturalOrder<Int>().reversed())
assertTrue(naturalOrder<Int>() === reverseOrder<Int>().reversed())
}
@@ -0,0 +1,18 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item> { it.name }
val reversed = comparator.reversed()
assertEquals(comparator.compare(v2, v1), reversed.compare(v1, v2))
assertEquals(comparator, reversed.reversed())
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item> { it.rating }.thenBy { it.name }
val diff = comparator.compare(v1, v2)
assertTrue(diff < 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v1, items[0])
assertEquals(v2, items[1])
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item> { it.rating }.thenByDescending { it.name }
val diff = comparator.compare(v1, v2)
assertTrue(diff < 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v1, items[0])
assertEquals(v2, items[1])
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = Comparator<Item> { a, b -> a.name.compareTo(b.name) }.thenComparator { a, b -> a.rating.compareTo(b.rating) }
val diff = comparator.compare(v1, v2)
assertTrue(diff > 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v2, items[0])
assertEquals(v1, items[1])
}
@@ -0,0 +1,28 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = object : Comparator<Item> {
override fun compare(o1: Item, o2: Item): Int {
return compareValuesBy(o1, o2, { it.name }, { it.rating })
}
override fun equals(other: Any?): Boolean {
return this == other
}
}
val diff = comparator.compare(v1, v2)
assertTrue(diff > 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v2, items[0])
assertEquals(v1, items[1])
}
@@ -0,0 +1,20 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item>({ it.name }, { it.rating })
val diff = comparator.compare(v1, v2)
assertTrue(diff > 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v2, items[0])
assertEquals(v1, items[1])
}
@@ -0,0 +1,14 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(Pair(1, "a"), p)
assertNotEquals(Pair(2, "a"), p)
assertNotEquals(Pair(1, "b"), p)
assertTrue(!p.equals(null))
assertNotEquals("", (p as Any))
}
@@ -0,0 +1,11 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(1, p.first)
assertEquals("a", p.second)
}
@@ -0,0 +1,14 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(Pair(1, "a").hashCode(), p.hashCode())
assertNotEquals(Pair(2, "a").hashCode(), p.hashCode())
assertNotEquals(0, Pair(null, "b").hashCode())
assertNotEquals(0, Pair("b", null).hashCode())
assertEquals(0, Pair(null, null).hashCode())
}
@@ -0,0 +1,12 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
val s = hashSetOf(Pair(1, "a"), Pair(1, "b"), Pair(1, "a"))
assertEquals(2, s.size)
assertTrue(s.contains(p))
}
@@ -0,0 +1,12 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
val (a, b) = p
assertEquals(1, a)
assertEquals("a", b)
}
@@ -0,0 +1,11 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(listOf(1, 2), (1 to 2).toList())
assertEquals(listOf(1, null), (1 to null).toList())
assertEquals(listOf(1, "2"), (1 to "2").toList())
}
@@ -0,0 +1,10 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals("(1, a)", p.toString())
}
@@ -0,0 +1,15 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(Triple(1, "a", 0.07), t)
assertNotEquals(Triple(2, "a", 0.07), t)
assertNotEquals(Triple(1, "b", 0.07), t)
assertNotEquals(Triple(1, "a", 0.1), t)
assertTrue(!t.equals(null))
assertNotEquals("", (t as Any))
}
@@ -0,0 +1,11 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(1, t.first)
assertEquals("a", t.second)
assertEquals(0.07, t.third)
}
@@ -0,0 +1,15 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(Triple(1, "a", 0.07).hashCode(), t.hashCode())
assertNotEquals(Triple(2, "a", 0.07).hashCode(), t.hashCode())
assertNotEquals(0, Triple(null, "b", 0.07).hashCode())
assertNotEquals(0, Triple("b", null, 0.07).hashCode())
assertNotEquals(0, Triple("b", 1, null).hashCode())
assertEquals(0, Triple(null, null, null).hashCode())
}
@@ -0,0 +1,12 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
val s = hashSetOf(Triple(1, "a", 0.07), Triple(1, "b", 0.07), Triple(1, "a", 0.07))
assertEquals(2, s.size)
assertTrue(s.contains(t))
}
@@ -0,0 +1,12 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val t = Triple(1, "a", 0.07)
fun box() {
val (a, b, c) = t
assertEquals(1, a)
assertEquals("a", b)
assertEquals(0.07, c)
}
@@ -0,0 +1,12 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val p = Pair(1, "a")
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals(listOf(1, 2, 3), (Triple(1, 2, 3)).toList())
assertEquals(listOf(1, null, 3), (Triple(1, null, 3)).toList())
assertEquals(listOf(1, 2, "3"), (Triple(1, 2, "3")).toList())
}
@@ -0,0 +1,9 @@
import kotlin.test.*
import kotlin.test.assertTrue
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
val t = Triple(1, "a", 0.07)
fun box() {
assertEquals("(1, a, 0.07)", t.toString())
}
@@ -0,0 +1,11 @@
import kotlin.test.*
fun box() {
val arr1 = intArrayOf(0, 1, 2, 3, 4)
assertEquals(4, arr1.lastIndex)
assertEquals(4, arr1[arr1.lastIndex])
val arr2 = Array<String>(5, { "$it" })
assertEquals(4, arr2.lastIndex)
assertEquals("4", arr2[arr2.lastIndex])
}
@@ -0,0 +1,23 @@
import kotlin.test.*
fun box() {
val arr1 = intArrayOf(1, 2, 3, 4, 5)
val iter1 = arr1.asIterable()
assertEquals(arr1.toList(), iter1.toList())
arr1[0] = 0
assertEquals(arr1.toList(), iter1.toList())
val arr2 = arrayOf("one", "two", "three")
val iter2 = arr2.asIterable()
assertEquals(arr2.toList(), iter2.toList())
arr2[0] = ""
assertEquals(arr2.toList(), iter2.toList())
val arr3 = IntArray(0)
val iter3 = arr3.asIterable()
assertEquals(iter3.toList(), emptyList<Int>())
val arr4 = Array(0, { "$it" })
val iter4 = arr4.asIterable()
assertEquals(iter4.toList(), emptyList<String>())
}
@@ -0,0 +1,109 @@
import kotlin.test.*
fun <T> compare(expected: T, actual: T, block: CompareContext<T>.() -> Unit) {
CompareContext(expected, actual).block()
}
class CompareContext<out T>(public val expected: T, public val actual: T) {
public fun equals(message: String = "") {
assertEquals(expected, actual, message)
}
public fun <P> propertyEquals(message: String = "", getter: T.() -> P) {
assertEquals(expected.getter(), actual.getter(), message)
}
public fun propertyFails(getter: T.() -> Unit) {
assertFailEquals({ expected.getter() }, { actual.getter() })
}
public fun <P> compareProperty(getter: T.() -> P, block: CompareContext<P>.() -> Unit) {
compare(expected.getter(), actual.getter(), block)
}
private fun assertFailEquals(expected: () -> Unit, actual: () -> Unit) {
val expectedFail = assertFails(expected)
val actualFail = assertFails(actual)
//assertEquals(expectedFail != null, actualFail != null)
assertTypeEquals(expectedFail, actualFail)
}
}
fun <T> CompareContext<List<T>>.listBehavior() {
equalityBehavior()
collectionBehavior()
compareProperty({ listIterator() }, { listIteratorBehavior() })
compareProperty({ listIterator(0) }, { listIteratorBehavior() })
propertyFails { listIterator(-1) }
propertyFails { listIterator(size + 1) }
for (index in expected.indices)
propertyEquals { this[index] }
propertyFails { this[size] }
propertyEquals { indexOf(elementAtOrNull(0)) }
propertyEquals { lastIndexOf(elementAtOrNull(0)) }
propertyFails { subList(0, size + 1) }
propertyFails { subList(-1, 0) }
propertyEquals { subList(0, size) }
}
fun <T> CompareContext<T>.equalityBehavior(objectName: String = "") {
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
equals(objectName)
propertyEquals(prefix + "hashCode") { hashCode() }
propertyEquals(prefix + "toString") { toString() }
}
fun <T> CompareContext<Collection<T>>.collectionBehavior(objectName: String = "") {
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
propertyEquals(prefix + "size") { size }
propertyEquals(prefix + "isEmpty") { isEmpty() }
(object {}).let { propertyEquals { contains(it as Any?) } }
propertyEquals { contains(firstOrNull()) }
propertyEquals { containsAll(this) }
}
fun <T> CompareContext<ListIterator<T>>.listIteratorBehavior() {
listIteratorProperties()
while (expected.hasNext()) {
propertyEquals { next() }
listIteratorProperties()
}
propertyFails { next() }
while (expected.hasPrevious()) {
propertyEquals { previous() }
listIteratorProperties()
}
propertyFails { previous() }
}
public fun CompareContext<ListIterator<*>>.listIteratorProperties() {
propertyEquals { hasNext() }
propertyEquals { hasPrevious() }
propertyEquals { nextIndex() }
propertyEquals { previousIndex() }
}
fun box() {
compare(listOf(1, 2, 3), intArrayOf(1, 2, 3).asList()) { listBehavior() }
compare(listOf<Byte>(1, 2, 3), byteArrayOf(1, 2, 3).asList()) { listBehavior() }
compare(listOf(true, false), booleanArrayOf(true, false).asList()) { listBehavior() }
compare(listOf(1, 2, 3), arrayOf(1, 2, 3).asList()) { listBehavior() }
compare(listOf("abc", "def"), arrayOf("abc", "def").asList()) { listBehavior() }
val ints = intArrayOf(1, 5, 7)
val intsAsList = ints.asList()
assertEquals(5, intsAsList[1])
ints[1] = 10
assertEquals(10, intsAsList[1], "Should reflect changes in original array")
}
@@ -0,0 +1,34 @@
import kotlin.test.*
fun box() {
val arr = arrayOf("a", "b", "c", "d", "b", "e")
val list = arr.asList()
assertEquals(list, arr.toList())
assertTrue("b" in list)
assertFalse("z" in list)
expect(1) { list.indexOf("b") }
expect(4) { list.lastIndexOf("b") }
expect(-1) { list.indexOf("x") }
assertTrue(list.containsAll(listOf("e", "d", "c")))
assertFalse(list.containsAll(listOf("e", "x", "c")))
assertEquals(list.subList(3, 5), listOf("d", "b"))
val iter = list.listIterator(2)
expect(2) { iter.nextIndex() }
expect(1) { iter.previousIndex() }
expect("c") {
iter.next()
iter.previous()
iter.next()
}
arr[2] = "xx"
assertEquals(list, arr.toList())
assertEquals(Array(0, { "" }).asList(), emptyList<String>())
}
@@ -0,0 +1,33 @@
import kotlin.test.*
fun box() {
// Array of primitives
val arr = intArrayOf(1, 2, 3, 4, 2, 5)
val list = arr.asList()
assertEquals(list, arr.toList())
assertTrue(2 in list)
assertFalse(0 in list)
assertTrue(list.containsAll(listOf(5, 4, 3)))
assertFalse(list.containsAll(listOf(5, 6, 3)))
expect(1) { list.indexOf(2) }
expect(4) { list.lastIndexOf(2) }
expect(-1) { list.indexOf(6) }
assertEquals(list.subList(3, 5), listOf(4, 2))
val iter = list.listIterator(2)
expect(2) { iter.nextIndex() }
expect(1) { iter.previousIndex() }
expect(3) {
iter.next()
iter.previous()
iter.next()
}
arr[2] = 4
assertEquals(list, arr.toList())
assertEquals(IntArray(0).asList(), emptyList<Int>())
}
@@ -0,0 +1,12 @@
import kotlin.test.*
fun box() {
assertTrue() { arrayOf<Int>().average().isNaN() }
expect(3.8) { arrayOf(1, 2, 5, 8, 3).average() }
expect(2.1) { arrayOf(1.6, 2.6, 3.6, 0.6).average() }
expect(100.0) { arrayOf<Byte>(100, 100, 100, 100, 100, 100).average() }
expect(0) { arrayOf<Short>(1, -1, 2, -2, 3, -3).average().toShort() }
// TODO: Property based tests
// for each arr with size 1 arr.average() == arr[0]
// for each arr with size > 0 arr.average() = arr.sum().toDouble() / arr.size()
}
@@ -0,0 +1,8 @@
import kotlin.test.*
fun box() {
val arr = BooleanArray(2)
assertEquals(arr.size, 2)
assertEquals(false, arr[0])
assertEquals(false, arr[1])
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = BooleanArray(2) { it % 2 == 0 }
assertEquals(2, arr.size)
assertEquals(true, arr[0])
assertEquals(false, arr[1])
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
val arr = ByteArray(2)
val expected: Byte = 0
assertEquals(arr.size, 2)
assertEquals(expected, arr[0])
assertEquals(expected, arr[1])
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = ByteArray(2) { it.toByte() }
assertEquals(2, arr.size)
assertEquals(0.toByte(), arr[0])
assertEquals(1.toByte(), arr[1])
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
val arr = CharArray(2)
val expected: Char = '\u0000'
assertEquals(arr.size, 2)
assertEquals(expected, arr[0])
assertEquals(expected, arr[1])
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = CharArray(2) { 'a' + it }
assertEquals(2, arr.size)
assertEquals('a', arr[0])
assertEquals('b', arr[1])
}
@@ -0,0 +1,7 @@
import kotlin.test.*
fun box() {
assertTrue(arrayOf("1", "2", "3", "4").contains("2"))
assertTrue("3" in arrayOf("1", "2", "3", "4"))
assertTrue("0" !in arrayOf("1", "2", "3", "4"))
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
val arr1 = arrayOf("a", 1, intArrayOf(2))
val arr2 = arrayOf("a", 1, intArrayOf(2))
assertFalse(arr1 contentEquals arr2)
assertTrue(arr1 contentDeepEquals arr2)
arr2[2] = arr1
assertFalse(arr1 contentDeepEquals arr2)
}
@@ -0,0 +1,10 @@
import kotlin.test.*
data class Value(val value: Int) {
override fun hashCode(): Int = value
}
fun box() {
val arr = arrayOf(null, Value(2), arrayOf(Value(3)))
assertEquals(((1 * 31 + 0) * 31 + 2) * 31 + (1 * 31 + 3), arr.contentDeepHashCode())
}
@@ -0,0 +1,12 @@
import kotlin.test.*
fun box() {
// Don't run this test unless primitive array `is` checks are supported (KT-17137)
if ((intArrayOf() as Any) is Array<*>) {
assertTrue(true)
return
}
val arr = arrayOf("aa", 1, null, charArrayOf('d'))
assertEquals("[aa, 1, null, [d]]", arr.contentDeepToString())
}
@@ -0,0 +1,15 @@
import kotlin.test.*
fun box() {
// a[b[a, b]]
val b = arrayOfNulls<Any>(2)
val a = arrayOf(b)
b[0] = a
b[1] = b
a.toString()
assertTrue(true, "toString does not cycle")
a.contentToString()
assertTrue(true, "contentToString does not cycle")
val result = a.contentDeepToString()
assertEquals("[[[...], [...]]]", result)
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr1 = arrayOf("a", 1, null)
val arr2 = arrayOf(*arr1)
assertTrue(arr1 contentEquals arr2)
val arr3 = arr2.reversedArray()
assertFalse(arr1 contentEquals arr3)
}
@@ -0,0 +1,11 @@
import kotlin.test.*
data class Value(val value: Int) {
override fun hashCode(): Int = value
}
fun box() {
val arr = arrayOf("a", 1, null, Value(5))
assertEquals(listOf(*arr).hashCode(), arr.contentHashCode())
assertEquals((1 * 31 + 2) * 31 + 3, arrayOf(Value(2), Value(3)).contentHashCode())
}
@@ -0,0 +1,6 @@
import kotlin.test.*
fun box() {
val arr = arrayOf("a", 1, null)
assertEquals(arr.asList().toString(), arr.contentToString())
}
@@ -0,0 +1,51 @@
import kotlin.test.*
fun <T> assertArrayNotSameButEquals(expected: Array<out T>, actual: Array<out T>, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: IntArray, actual: IntArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: LongArray, actual: LongArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: ShortArray, actual: ShortArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: ByteArray, actual: ByteArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: DoubleArray, actual: DoubleArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: FloatArray, actual: FloatArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: CharArray, actual: CharArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: BooleanArray, actual: BooleanArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun box() {
assertArrayNotSameButEquals(arrayOf("1", "2"), arrayOf("1", "2", "3").copyOf(2))
assertArrayNotSameButEquals(arrayOf("1", "2", null), arrayOf("1", "2").copyOf(3))
assertArrayNotSameButEquals(longArrayOf(1, 2), longArrayOf(1, 2, 3).copyOf(2))
assertArrayNotSameButEquals(longArrayOf(1, 2, 0), longArrayOf(1, 2).copyOf(3))
assertArrayNotSameButEquals(intArrayOf(1, 2), intArrayOf(1, 2, 3).copyOf(2))
assertArrayNotSameButEquals(intArrayOf(1, 2, 0), intArrayOf(1, 2).copyOf(3))
assertArrayNotSameButEquals(charArrayOf('A', 'B'), charArrayOf('A', 'B', 'C').copyOf(2))
assertArrayNotSameButEquals(charArrayOf('A', 'B', '\u0000'), charArrayOf('A', 'B').copyOf(3))
}
@@ -0,0 +1,48 @@
import kotlin.test.*
fun <T> assertArrayNotSameButEquals(expected: Array<out T>, actual: Array<out T>, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: IntArray, actual: IntArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: LongArray, actual: LongArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: ShortArray, actual: ShortArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: ByteArray, actual: ByteArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: DoubleArray, actual: DoubleArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: FloatArray, actual: FloatArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: CharArray, actual: CharArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: BooleanArray, actual: BooleanArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun box() {
booleanArrayOf(true, false, true).let { assertArrayNotSameButEquals(it, it.copyOf()) }
byteArrayOf(0, 1, 2, 3, 4, 5).let { assertArrayNotSameButEquals(it, it.copyOf()) }
shortArrayOf(0, 1, 2, 3, 4, 5).let { assertArrayNotSameButEquals(it, it.copyOf()) }
intArrayOf(0, 1, 2, 3, 4, 5).let { assertArrayNotSameButEquals(it, it.copyOf()) }
longArrayOf(0, 1, 2, 3, 4, 5).let { assertArrayNotSameButEquals(it, it.copyOf()) }
floatArrayOf(0F, 1F, 2F, 3F).let { assertArrayNotSameButEquals(it, it.copyOf()) }
doubleArrayOf(0.0, 1.0, 2.0, 3.0, 4.0, 5.0).let { assertArrayNotSameButEquals(it, it.copyOf()) }
charArrayOf('0', '1', '2', '3', '4', '5').let { assertArrayNotSameButEquals(it, it.copyOf()) }
}
@@ -0,0 +1,48 @@
import kotlin.test.*
fun <T> assertArrayNotSameButEquals(expected: Array<out T>, actual: Array<out T>, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: IntArray, actual: IntArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: LongArray, actual: LongArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: ShortArray, actual: ShortArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: ByteArray, actual: ByteArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: DoubleArray, actual: DoubleArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: FloatArray, actual: FloatArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: CharArray, actual: CharArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun assertArrayNotSameButEquals(expected: BooleanArray, actual: BooleanArray, message: String = "") {
assertTrue(expected !== actual && expected contentEquals actual, message)
}
fun box() {
assertArrayNotSameButEquals(booleanArrayOf(true, false, true), booleanArrayOf(true, false, true, true).copyOfRange(0, 3))
assertArrayNotSameButEquals(byteArrayOf(0, 1, 2), byteArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
assertArrayNotSameButEquals(shortArrayOf(0, 1, 2), shortArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
assertArrayNotSameButEquals(intArrayOf(0, 1, 2), intArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
assertArrayNotSameButEquals(longArrayOf(0, 1, 2), longArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
assertArrayNotSameButEquals(floatArrayOf(0F, 1F, 2F), floatArrayOf(0F, 1F, 2F, 3F, 4F, 5F).copyOfRange(0, 3))
assertArrayNotSameButEquals(doubleArrayOf(0.0, 1.0, 2.0), doubleArrayOf(0.0, 1.0, 2.0, 3.0, 4.0, 5.0).copyOfRange(0, 3))
assertArrayNotSameButEquals(charArrayOf('0', '1', '2'), charArrayOf('0', '1', '2', '3', '4', '5').copyOfRange(0, 3))
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = DoubleArray(2)
assertEquals(arr.size, 2)
assertEquals(0.0, arr[0])
assertEquals(0.0, arr[1])
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = DoubleArray(2) { it.toDouble() }
assertEquals(2, arr.size)
assertEquals(0.toDouble(), arr[0])
assertEquals(1.toDouble(), arr[1])
}
@@ -0,0 +1,19 @@
import kotlin.test.*
fun box() {
expect(listOf(1), { intArrayOf(1).drop(0) })
expect(listOf(), { intArrayOf().drop(1) })
expect(listOf(), { intArrayOf(1).drop(1) })
expect(listOf(3), { intArrayOf(2, 3).drop(1) })
expect(listOf(2000000000000), { longArrayOf(3000000000000, 2000000000000).drop(1) })
expect(listOf(3.toByte()), { byteArrayOf(2, 3).drop(1) })
expect(listOf(3.toShort()), { shortArrayOf(2, 3).drop(1) })
expect(listOf(3.0f), { floatArrayOf(2f, 3f).drop(1) })
expect(listOf(3.0), { doubleArrayOf(2.0, 3.0).drop(1) })
expect(listOf(false), { booleanArrayOf(true, false).drop(1) })
expect(listOf('b'), { charArrayOf('a', 'b').drop(1) })
expect(listOf("b"), { arrayOf("a", "b").drop(1) })
assertFails {
listOf(2).drop(-1)
}
}
@@ -0,0 +1,19 @@
import kotlin.test.*
fun box() {
expect(listOf(), { intArrayOf().dropLast(1) })
expect(listOf(), { intArrayOf(1).dropLast(1) })
expect(listOf(1), { intArrayOf(1).dropLast(0) })
expect(listOf(2), { intArrayOf(2, 3).dropLast(1) })
expect(listOf(3000000000000), { longArrayOf(3000000000000, 2000000000000).dropLast(1) })
expect(listOf(2.toByte()), { byteArrayOf(2, 3).dropLast(1) })
expect(listOf(2.toShort()), { shortArrayOf(2, 3).dropLast(1) })
expect(listOf(2.0f), { floatArrayOf(2f, 3f).dropLast(1) })
expect(listOf(2.0), { doubleArrayOf(2.0, 3.0).dropLast(1) })
expect(listOf(true), { booleanArrayOf(true, false).dropLast(1) })
expect(listOf('a'), { charArrayOf('a', 'b').dropLast(1) })
expect(listOf("a"), { arrayOf("a", "b").dropLast(1) })
assertFails {
listOf(1).dropLast(-1)
}
}
@@ -0,0 +1,15 @@
import kotlin.test.*
fun box() {
expect(listOf(), { intArrayOf().dropLastWhile { it < 3 } })
expect(listOf(), { intArrayOf(1).dropLastWhile { it < 3 } })
expect(listOf(2, 3), { intArrayOf(2, 3, 1).dropLastWhile { it < 3 } })
expect(listOf(3000000000000), { longArrayOf(3000000000000, 2000000000000).dropLastWhile { it < 3000000000000 } })
expect(listOf(2.toByte(), 3.toByte()), { byteArrayOf(2, 3, 1).dropLastWhile { it < 3 } })
expect(listOf(2.toShort(), 3.toShort()), { shortArrayOf(2, 3, 1).dropLastWhile { it < 3 } })
expect(listOf(2f, 3f), { floatArrayOf(2f, 3f, 1f).dropLastWhile { it < 3 } })
expect(listOf(2.0, 3.0), { doubleArrayOf(2.0, 3.0, 1.0).dropLastWhile { it < 3 } })
expect(listOf(true, false), { booleanArrayOf(true, false, true).dropLastWhile { it } })
expect(listOf('a', 'b'), { charArrayOf('a', 'b', 'a').dropLastWhile { it < 'b' } })
expect(listOf("a", "b"), { arrayOf("a", "b", "a").dropLastWhile { it < "b" } })
}
@@ -0,0 +1,15 @@
import kotlin.test.*
fun box() {
expect(listOf(), { intArrayOf().dropWhile { it < 3 } })
expect(listOf(), { intArrayOf(1).dropWhile { it < 3 } })
expect(listOf(3, 1), { intArrayOf(2, 3, 1).dropWhile { it < 3 } })
expect(listOf(2000000000000), { longArrayOf(3000000000000, 2000000000000).dropWhile { it > 2000000000000 } })
expect(listOf(3.toByte(), 1.toByte()), { byteArrayOf(2, 3, 1).dropWhile { it < 3 } })
expect(listOf(3.toShort(), 1.toShort()), { shortArrayOf(2, 3, 1).dropWhile { it < 3 } })
expect(listOf(3f, 1f), { floatArrayOf(2f, 3f, 1f).dropWhile { it < 3 } })
expect(listOf(3.0, 1.0), { doubleArrayOf(2.0, 3.0, 1.0).dropWhile { it < 3 } })
expect(listOf(false, true), { booleanArrayOf(true, false, true).dropWhile { it } })
expect(listOf('b', 'a'), { charArrayOf('a', 'b', 'a').dropWhile { it < 'b' } })
expect(listOf("b", "a"), { arrayOf("a", "b", "a").dropWhile { it < "b" } })
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr1 = IntArray(0)
assertEquals(-1, arr1.lastIndex)
val arr2 = emptyArray<String>()
assertEquals(-1, arr2.lastIndex)
}
@@ -0,0 +1,15 @@
import kotlin.test.*
fun box() {
expect(listOf(), { intArrayOf().filter { it > 2 } })
expect(listOf(), { intArrayOf(1).filter { it > 2 } })
expect(listOf(3), { intArrayOf(2, 3).filter { it > 2 } })
expect(listOf(3000000000000), { longArrayOf(3000000000000, 2000000000000).filter { it > 2000000000000 } })
expect(listOf(3.toByte()), { byteArrayOf(2, 3).filter { it > 2 } })
expect(listOf(3.toShort()), { shortArrayOf(2, 3).filter { it > 2 } })
expect(listOf(3.0f), { floatArrayOf(2f, 3f).filter { it > 2 } })
expect(listOf(3.0), { doubleArrayOf(2.0, 3.0).filter { it > 2 } })
expect(listOf(true), { booleanArrayOf(true, false).filter { it } })
expect(listOf('b'), { charArrayOf('a', 'b').filter { it > 'a' } })
expect(listOf("b"), { arrayOf("a", "b").filter { it > "a" } })
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
expect(listOf(), { intArrayOf().filterIndexed { i, v -> i > v } })
expect(listOf(2, 5, 8), { intArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == value % 2 } })
expect(listOf<Long>(2, 5, 8), { longArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } })
expect(listOf<Byte>(2, 5, 8), { byteArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } })
expect(listOf('9', 'e', 'a'), { charArrayOf('9', 'e', 'd', 'a').filterIndexed { index, c -> c == 'a' || index < 2 } })
expect(listOf("a", "c", "d"), { arrayOf("a", "b", "c", "d").filterIndexed { index, s -> s == "a" || index >= 2 } })
}
@@ -0,0 +1,15 @@
import kotlin.test.*
fun box() {
expect(listOf(), { intArrayOf().filterNot { it > 2 } })
expect(listOf(1), { intArrayOf(1).filterNot { it > 2 } })
expect(listOf(2), { intArrayOf(2, 3).filterNot { it > 2 } })
expect(listOf(2000000000000), { longArrayOf(3000000000000, 2000000000000).filterNot { it > 2000000000000 } })
expect(listOf(2.toByte()), { byteArrayOf(2, 3).filterNot { it > 2 } })
expect(listOf(2.toShort()), { shortArrayOf(2, 3).filterNot { it > 2 } })
expect(listOf(2.0f), { floatArrayOf(2f, 3f).filterNot { it > 2 } })
expect(listOf(2.0), { doubleArrayOf(2.0, 3.0).filterNot { it > 2 } })
expect(listOf(false), { booleanArrayOf(true, false).filterNot { it } })
expect(listOf('a'), { charArrayOf('a', 'b').filterNot { it > 'a' } })
expect(listOf("a"), { arrayOf("a", "b").filterNot { it > "a" } })
}
@@ -0,0 +1,5 @@
import kotlin.test.*
fun box() {
expect(listOf("a"), { arrayOf("a", null).filterNotNull() })
}
@@ -0,0 +1,6 @@
import kotlin.test.*
fun box() {
expect(1) { arrayOf(1, 2, 3).first() }
expect(2) { arrayOf(1, 2, 3).first { it % 2 == 0 } }
}
@@ -0,0 +1,14 @@
import kotlin.test.*
fun box() {
val arr1: Array<Array<Int>> = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6))
val arr2: Array<out Array<Int>> = arr1
val arr3: Array<out Array<out Int>> = arr1
val arr4: Array<Array<out Int>> = arr1 as Array<Array<out Int>>
val expected = listOf(1, 2, 3, 4, 5, 6)
assertEquals(expected, arr1.flatten())
assertEquals(expected, arr2.flatten())
assertEquals(expected, arr3.flatten())
assertEquals(expected, arr4.flatten())
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
val arr = FloatArray(2)
val expected: Float = 0.0F
assertEquals(arr.size, 2)
assertEquals(expected, arr[0])
assertEquals(expected, arr[1])
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = FloatArray(2) { it.toFloat() }
assertEquals(2, arr.size)
assertEquals(0.toFloat(), arr[0])
assertEquals(1.toFloat(), arr[1])
}
@@ -0,0 +1,19 @@
import kotlin.test.*
fun box() {
expect(-1) { arrayOf("cat", "dog", "bird").indexOf("mouse") }
expect(0) { arrayOf("cat", "dog", "bird").indexOf("cat") }
expect(1) { arrayOf("cat", "dog", "bird").indexOf("dog") }
expect(2) { arrayOf("cat", "dog", "bird").indexOf("bird") }
expect(0) { arrayOf(null, "dog", null).indexOf(null as String?) }
expect(-1) { arrayOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
expect(0) { arrayOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
expect(1) { arrayOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
expect(2) { arrayOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
expect(-1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
expect(0) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
expect(1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
expect(2) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
}
@@ -0,0 +1,42 @@
import kotlin.test.*
fun box() {
expect(-1) { byteArrayOf(1, 2, 3).indexOf(0) }
expect(0) { byteArrayOf(1, 2, 3).indexOf(1) }
expect(1) { byteArrayOf(1, 2, 3).indexOf(2) }
expect(2) { byteArrayOf(1, 2, 3).indexOf(3) }
expect(-1) { shortArrayOf(1, 2, 3).indexOf(0) }
expect(0) { shortArrayOf(1, 2, 3).indexOf(1) }
expect(1) { shortArrayOf(1, 2, 3).indexOf(2) }
expect(2) { shortArrayOf(1, 2, 3).indexOf(3) }
expect(-1) { intArrayOf(1, 2, 3).indexOf(0) }
expect(0) { intArrayOf(1, 2, 3).indexOf(1) }
expect(1) { intArrayOf(1, 2, 3).indexOf(2) }
expect(2) { intArrayOf(1, 2, 3).indexOf(3) }
expect(-1) { longArrayOf(1, 2, 3).indexOf(0) }
expect(0) { longArrayOf(1, 2, 3).indexOf(1) }
expect(1) { longArrayOf(1, 2, 3).indexOf(2) }
expect(2) { longArrayOf(1, 2, 3).indexOf(3) }
expect(-1) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(0f) }
expect(0) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(1.0f) }
expect(1) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(2.0f) }
expect(2) { floatArrayOf(1.0f, 2.0f, 3.0f).indexOf(3.0f) }
expect(-1) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(0.0) }
expect(0) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(1.0) }
expect(1) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(2.0) }
expect(2) { doubleArrayOf(1.0, 2.0, 3.0).indexOf(3.0) }
expect(-1) { charArrayOf('a', 'b', 'c').indexOf('z') }
expect(0) { charArrayOf('a', 'b', 'c').indexOf('a') }
expect(1) { charArrayOf('a', 'b', 'c').indexOf('b') }
expect(2) { charArrayOf('a', 'b', 'c').indexOf('c') }
expect(0) { booleanArrayOf(true, false).indexOf(true) }
expect(1) { booleanArrayOf(true, false).indexOf(false) }
expect(-1) { booleanArrayOf(true).indexOf(false) }
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = IntArray(2)
assertEquals(arr.size, 2)
assertEquals(0, arr[0])
assertEquals(0, arr[1])
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = IntArray(2) { it.toInt() }
assertEquals(2, arr.size)
assertEquals(0.toInt(), arr[0])
assertEquals(1.toInt(), arr[1])
}
@@ -0,0 +1,22 @@
import kotlin.test.*
fun box() {
assertTrue(emptyArray<String>().isEmpty())
assertFalse(arrayOf("").isEmpty())
assertTrue(intArrayOf().isEmpty())
assertFalse(intArrayOf(1).isEmpty())
assertTrue(byteArrayOf().isEmpty())
assertFalse(byteArrayOf(1).isEmpty())
assertTrue(shortArrayOf().isEmpty())
assertFalse(shortArrayOf(1).isEmpty())
assertTrue(longArrayOf().isEmpty())
assertFalse(longArrayOf(1).isEmpty())
assertTrue(charArrayOf().isEmpty())
assertFalse(charArrayOf('a').isEmpty())
assertTrue(floatArrayOf().isEmpty())
assertFalse(floatArrayOf(0.1F).isEmpty())
assertTrue(doubleArrayOf().isEmpty())
assertFalse(doubleArrayOf(0.1).isEmpty())
assertTrue(booleanArrayOf().isEmpty())
assertFalse(booleanArrayOf(false).isEmpty())
}
@@ -0,0 +1,6 @@
import kotlin.test.*
fun box() {
assertFalse(intArrayOf().isNotEmpty())
assertTrue(intArrayOf(1).isNotEmpty())
}
@@ -0,0 +1,15 @@
import kotlin.test.*
fun box() {
val text = arrayOf("foo", "bar").joinToString("-", "<", ">")
assertEquals("<foo-bar>", text)
val text2 = arrayOf('a', "b", StringBuilder("c"), null, "d", 'e', 'f').joinToString(limit = 4, truncated = "*")
assertEquals("a, b, c, null, *", text2)
val text3 = intArrayOf(1, 2, 5, 8).joinToString("+", "[", "]")
assertEquals("[1+2+5+8]", text3)
val text4 = charArrayOf('f', 'o', 'o').joinToString()
assertEquals("f, o, o", text4)
}
@@ -0,0 +1,6 @@
import kotlin.test.*
fun box() {
expect(3) { arrayOf(1, 2, 3).last() }
expect(2) { arrayOf(1, 2, 3).last { it % 2 == 0 } }
}
@@ -0,0 +1,21 @@
import kotlin.test.*
fun box() {
expect(-1) { arrayOf("cat", "dog", "bird").lastIndexOf("mouse") }
expect(0) { arrayOf("cat", "dog", "bird").lastIndexOf("cat") }
expect(1) { arrayOf("cat", "dog", "bird").lastIndexOf("dog") }
expect(2) { arrayOf(null, "dog", null).lastIndexOf(null as String?) }
expect(3) { arrayOf("cat", "dog", "bird", "dog").lastIndexOf("dog") }
expect(-1) { arrayOf("cat", "dog", "bird").indexOfLast { it.contains("p") } }
expect(0) { arrayOf("cat", "dog", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { arrayOf("cat", "dog", "cap", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { arrayOf("cat", "dog", "bird").indexOfLast { it.endsWith('d') } }
expect(3) { arrayOf("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
expect(-1) { sequenceOf("cat", "dog", "bird").indexOfLast { it.contains("p") } }
expect(0) { sequenceOf("cat", "dog", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { sequenceOf("cat", "dog", "cap", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { sequenceOf("cat", "dog", "bird").indexOfLast { it.endsWith('d') } }
expect(3) { sequenceOf("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
val arr = LongArray(2)
val expected: Long = 0
assertEquals(arr.size, 2)
assertEquals(expected, arr[0])
assertEquals(expected, arr[1])
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
val arr = LongArray(2) { it.toLong() }
assertEquals(2, arr.size)
assertEquals(0.toLong(), arr[0])
assertEquals(1.toLong(), arr[1])
}
@@ -0,0 +1,8 @@
import kotlin.test.*
fun box() {
assertEquals(listOf(1, 2, 4), arrayOf("a", "bc", "test").map { it.length })
assertEquals(listOf('a', 'b', 'c'), intArrayOf(1, 2, 3).map { 'a' + it - 1 })
assertEquals(listOf(1, 2, 3), longArrayOf(1000, 2000, 3000).map { (it / 1000).toInt() })
assertEquals(listOf(1.0, 0.5, 0.4, 0.2, 0.1), doubleArrayOf(1.0, 2.0, 2.5, 5.0, 10.0).map { 1 / it })
}
@@ -0,0 +1,7 @@
import kotlin.test.*
fun box() {
assertEquals(listOf(1, 1, 2), arrayOf("a", "bc", "test").mapIndexed { index, s -> s.length - index })
assertEquals(listOf(0, 2, 2), intArrayOf(3, 2, 1).mapIndexed { index, i -> i * index })
assertEquals(listOf("0;20", "1;21", "2;22"), longArrayOf(20, 21, 22).mapIndexed { index, it -> "$index;$it" })
}
@@ -0,0 +1,5 @@
import kotlin.test.*
fun box() {
assertEquals(listOf(2), arrayOf("a", null, "test").mapIndexedNotNull { index, it -> it?.run { if (index != 0) length / index else null } })
}
@@ -0,0 +1,5 @@
import kotlin.test.*
fun box() {
assertEquals(listOf(2, 3), arrayOf("", "bc", "def").mapNotNull { if (it.isEmpty()) null else it.length })
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
expect(null, { arrayOf<Int>().max() })
expect(1, { arrayOf(1).max() })
expect(3, { arrayOf(2, 3).max() })
expect(3000000000000, { arrayOf(3000000000000, 2000000000000).max() })
expect('b', { arrayOf('a', 'b').max() })
expect("b", { arrayOf("a", "b").max() })
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
expect(null, { arrayOf<Int>().maxBy { it } })
expect(1, { arrayOf(1).maxBy { it } })
expect(2, { arrayOf(2, 3).maxBy { -it } })
expect('b', { arrayOf('a', 'b').maxBy { "x$it" } })
expect("abc", { arrayOf("b", "abc").maxBy { it.length } })
}
@@ -0,0 +1,7 @@
import kotlin.test.*
fun box() {
var c = 0
expect(5, { arrayOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
assertEquals(5, c)
}
@@ -0,0 +1,12 @@
import kotlin.test.*
fun box() {
expect(null, { intArrayOf().maxBy { it } })
expect(1, { intArrayOf(1).maxBy { it } })
expect(2, { intArrayOf(2, 3).maxBy { -it } })
expect(3000000000000, { longArrayOf(3000000000000, 2000000000000).maxBy { it + 1 } })
expect(3, { byteArrayOf(1, 3, 2).maxBy { it * it } })
expect(3, { shortArrayOf(3, 2).maxBy { "a" } })
expect(3.0F, { floatArrayOf(3.0F, 2.0F).maxBy { it.toString() } })
expect(3.0, { doubleArrayOf(2.0, 3.0).maxBy { it * it } })
}
@@ -0,0 +1,13 @@
import kotlin.test.*
fun box() {
expect(null, { intArrayOf().max() })
expect(1, { intArrayOf(1).max() })
expect(3, { intArrayOf(2, 3).max() })
expect(3000000000000, { longArrayOf(3000000000000, 2000000000000).max() })
expect(3, { byteArrayOf(1, 3, 2).max() })
expect(3, { shortArrayOf(3, 2).max() })
expect(3.0F, { floatArrayOf(3.0F, 2.0F).max() })
expect(3.0, { doubleArrayOf(2.0, 3.0).max() })
expect('b', { charArrayOf('a', 'b').max() })
}
@@ -0,0 +1,6 @@
import kotlin.test.*
fun box() {
val a = intArrayOf(1, 7, 9, 239, 54, 93)
expect(3, { a.indices.maxBy { a[it] } })
}
@@ -0,0 +1,7 @@
import kotlin.test.*
val STRING_CASE_INSENSITIVE_ORDER: Comparator<String> = compareBy { it: String -> it.toUpperCase() }.thenBy { it.toLowerCase() }.thenBy { it }
fun box() {
assertEquals(null, arrayOf<Int>().maxWith(naturalOrder()))
assertEquals("B", arrayOf("a", "B").maxWith(STRING_CASE_INSENSITIVE_ORDER))
}
@@ -0,0 +1,7 @@
import kotlin.test.*
fun box() {
expect(null, { intArrayOf().maxWith(naturalOrder()) })
expect(1, { intArrayOf(1).maxWith(naturalOrder()) })
expect(-4, { intArrayOf(2, 3, -4).maxWith(compareBy { it * it }) })
}
@@ -0,0 +1,10 @@
import kotlin.test.*
fun box() {
expect(null, { arrayOf<Int>().min() })
expect(1, { arrayOf(1).min() })
expect(2, { arrayOf(2, 3).min() })
expect(2000000000000, { arrayOf(3000000000000, 2000000000000).min() })
expect('a', { arrayOf('a', 'b').min() })
expect("a", { arrayOf("a", "b").min() })
}
@@ -0,0 +1,9 @@
import kotlin.test.*
fun box() {
expect(null, { arrayOf<Int>().minBy { it } })
expect(1, { arrayOf(1).minBy { it } })
expect(3, { arrayOf(2, 3).minBy { -it } })
expect('a', { arrayOf('a', 'b').minBy { "x$it" } })
expect("b", { arrayOf("b", "abc").minBy { it.length } })
}
@@ -0,0 +1,7 @@
import kotlin.test.*
fun box() {
var c = 0
expect(1, { arrayOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
assertEquals(5, c)
}
@@ -0,0 +1,12 @@
import kotlin.test.*
fun box() {
expect(null, { intArrayOf().minBy { it } })
expect(1, { intArrayOf(1).minBy { it } })
expect(3, { intArrayOf(2, 3).minBy { -it } })
expect(2000000000000, { longArrayOf(3000000000000, 2000000000000).minBy { it + 1 } })
expect(1, { byteArrayOf(1, 3, 2).minBy { it * it } })
expect(3, { shortArrayOf(3, 2).minBy { "a" } })
expect(2.0F, { floatArrayOf(3.0F, 2.0F).minBy { it.toString() } })
expect(2.0, { doubleArrayOf(2.0, 3.0).minBy { it * it } })
}
@@ -0,0 +1,13 @@
import kotlin.test.*
fun box() {
expect(null, { intArrayOf().min() })
expect(1, { intArrayOf(1).min() })
expect(2, { intArrayOf(2, 3).min() })
expect(2000000000000, { longArrayOf(3000000000000, 2000000000000).min() })
expect(1, { byteArrayOf(1, 3, 2).min() })
expect(2, { shortArrayOf(3, 2).min() })
expect(2.0F, { floatArrayOf(3.0F, 2.0F).min() })
expect(2.0, { doubleArrayOf(2.0, 3.0).min() })
expect('a', { charArrayOf('a', 'b').min() })
}
@@ -0,0 +1,6 @@
import kotlin.test.*
fun box() {
val a = intArrayOf(1, 7, 9, -42, 54, 93)
expect(3, { a.indices.minBy { a[it] } })
}
@@ -0,0 +1,7 @@
import kotlin.test.*
val STRING_CASE_INSENSITIVE_ORDER: Comparator<String> = compareBy { it: String -> it.toUpperCase() }.thenBy { it.toLowerCase() }.thenBy { it }
fun box() {
assertEquals(null, arrayOf<Int>().minWith(naturalOrder()))
assertEquals("a", arrayOf("a", "B").minWith(STRING_CASE_INSENSITIVE_ORDER))
}
@@ -0,0 +1,7 @@
import kotlin.test.*
fun box() {
expect(null, { intArrayOf().minWith(naturalOrder()) })
expect(1, { intArrayOf(1).minWith(naturalOrder()) })
expect(4, { intArrayOf(2, 3, 4).minWith(compareBy { it % 4 }) })
}

Some files were not shown because too many files have changed in this diff Show More