[K/N] Migrate runtime/collections tests to new testing infra ^KT-61259

This commit is contained in:
Alexander Shabalin
2023-09-04 11:54:24 +02:00
committed by Space Team
parent 10a6d8fd2c
commit b3e13fb2c2
56 changed files with 2375 additions and 3050 deletions
+1 -112
View File
@@ -1956,73 +1956,9 @@ standaloneTest('coroutines_suspendConversion') {
flags = ['-XXLanguage:+SuspendConversion']
}
tasks.register("AbstractMutableCollection", KonanLocalTest) {
expectedExitStatus = 0
source = "runtime/collections/AbstractMutableCollection.kt"
}
tasks.register("BitSet", KonanLocalTest) {
expectedExitStatus = 0
source = "runtime/collections/BitSet.kt"
}
tasks.register("stack_array", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/stack_array.kt"
}
tasks.register("array0", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/array0.kt"
}
tasks.register("array1", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/array1.kt"
}
tasks.register("array2", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/array2.kt"
}
tasks.register("array3", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/array3.kt"
}
tasks.register("array4", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/array4.kt"
}
tasks.register("array5", KonanLocalTest) {
source = "runtime/collections/array5.kt"
}
tasks.register("typed_array0", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/typed_array0.kt"
}
tasks.register("typed_array1", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/typed_array1.kt"
}
tasks.register("sort0", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/sort0.kt"
}
tasks.register("sort1", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/sort1.kt"
}
tasks.register("sortWith", KonanLocalTest) {
source = "runtime/collections/SortWith.kt"
source = "codegen/escapeAnalysis/stack_array.kt"
}
tasks.register("if_else", KonanLocalTest) {
@@ -2332,54 +2268,12 @@ standaloneTest("hypotenuse") {
source = "runtime/basic/hypot.kt"
}
tasks.register("array_list1", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/array_list1.kt"
}
tasks.register("array_list2", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/array_list2.kt"
}
tasks.register("hash_map0", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/hash_map0.kt"
}
tasks.register("hash_map1", KonanLocalTest) {
disabled = isAggressiveGC // TODO: Investigate why too slow
useGoldenData = true
source = "runtime/collections/hash_map1.kt"
}
tasks.register("hash_set0", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/hash_set0.kt"
}
tasks.register("listof0", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/listof0.kt"
}
tasks.register("listof1", KonanLocalTest) {
enabled = false
useGoldenData = true
source = "datagen/literals/listof1.kt"
}
tasks.register("moderately_large_array", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/moderately_large_array.kt"
}
tasks.register("moderately_large_array1", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/moderately_large_array1.kt"
}
tasks.register("string_builder0", KonanLocalTest) {
useGoldenData = true
source = "runtime/text/string_builder0.kt"
@@ -2719,11 +2613,6 @@ tasks.register("break1", KonanLocalTest) {
source = "codegen/controlflow/break1.kt"
}
tasks.register("range0", KonanLocalTest) {
useGoldenData = true
source = "runtime/collections/range0.kt"
}
standaloneTest("args0") {
arguments = ["AAA", "BB", "C"]
useGoldenData = true
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2023 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 codegen.escapeAnalysis.stack_array
import kotlin.test.*
@Test fun runTest() {
val array = IntArray(2)
array[0] = 1
array[1] = 2
val check = array is IntArray
println(check)
println(array[0] + array[1])
}
@@ -1,72 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.AbstractMutableCollection
import kotlin.test.*
class TestCollection(): AbstractMutableCollection<Int>() {
companion object {
const val SIZE = 7
}
private val array = IntArray(SIZE)
private var len = 0
override val size: Int
get() = len
override fun add(element: Int): Boolean {
if (len >= SIZE) return false
array[len++] = element
return true
}
override fun iterator(): MutableIterator<Int> = object: MutableIterator<Int> {
var nextIndex = 0
override fun hasNext() = nextIndex < len
override fun next() = array[nextIndex++]
override fun remove() {
if (nextIndex == 0) throw IllegalStateException()
for (i in nextIndex..len - 1) {
array[i - 1] = array[i]
}
len--
nextIndex--
}
}
override fun clear() {
len = 0
}
}
fun assertEquals(a: TestCollection, b: List<Int>) {
if (a.size != b.size) {
throw AssertionError()
}
val aIt = a.iterator()
val bIt = b.iterator()
while (aIt.hasNext()) {
if (aIt.next() != bIt.next()) throw AssertionError("TestCollection contains wrong elements. Expected: $b, actual: $a.")
}
}
@Test fun runTest() {
val c = TestCollection()
if (!c.addAll(listOf(1, 2, 3, 2, 4, 5, 4))) throw AssertionError("addAll is false when it must be true.")
if (c.addAll(listOf(1, 2)) != false) throw AssertionError("addAll is true when it must be false.")
c.removeAll(listOf(1, 2))
assertEquals(c, listOf(3, 4, 5, 4))
c.retainAll(listOf(4, 5))
assertEquals(c, listOf(4, 5, 4))
c.remove(4)
assertEquals(c, listOf(5, 4))
c.clear()
assertEquals(c, listOf())
}
@@ -1,458 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlin.native.ObsoleteNativeApi::class)
package runtime.collections.BitSet
import kotlin.test.*
fun assertContainsOnly(bitSet: BitSet, trueBits: Set<Int>, size: Int) {
for (i in 0 until size) {
val expectedBit = i in trueBits
if (bitSet[i] != expectedBit) {
throw AssertionError()
}
}
}
fun assertNotContainsOnly(bitSet: BitSet, falseBits: Set<Int>, size: Int) {
for (i in 0 until size) {
val expectedBit = i !in falseBits
if (bitSet[i] != expectedBit) {
throw AssertionError()
}
}
}
fun assertTrue(str: String, cond: Boolean) { if (!cond) throw AssertionError(str) }
fun assertFalse(str: String, cond: Boolean) { if (cond) throw AssertionError(str) }
fun <T> assertEquals(str: String, a: T, b: T) { if (a != b) throw AssertionError(str) }
fun assertTrue(cond: Boolean) = assertTrue("", cond)
fun assertFalse(cond: Boolean) = assertFalse("", cond)
fun <T> assertEquals(a: T, b: T) = assertEquals("", a, b)
fun fail(): Unit = throw AssertionError()
fun testConstructor() {
var b = BitSet(12)
assertContainsOnly(b, setOf(), 12)
b = BitSet(12) { it == 0 || it in 5..6 || it == 11 }
assertContainsOnly(b, setOf(0, 5, 6, 11), 12)
}
fun testSet() {
var b = BitSet(0)
assertEquals(b.lastTrueIndex, -1)
b = BitSet(2)
// Test set and clear operation for single index.
assertTrue(b.isEmpty)
b.set(0, true)
b.set(3, true)
assertEquals(b.lastTrueIndex, 3)
assertFalse(b.isEmpty)
assertContainsOnly(b, setOf(0, 3), 4)
b.clear()
assertContainsOnly(b, setOf(), 4)
b.set(1)
b.set(5)
assertEquals(b.lastTrueIndex, 5)
assertContainsOnly(b, setOf(1, 5), 6)
b.set(1, false)
b.set(7, false)
assertEquals(b.lastTrueIndex, 5)
assertContainsOnly(b, setOf(5), 8)
b.clear(5)
assertEquals(b.lastTrueIndex, -1)
assertContainsOnly(b, setOf(), 8)
b.set(70)
assertEquals(b.lastTrueIndex, 70)
assertContainsOnly(b, setOf(70), 71)
b.clear(70)
assertEquals(b.lastTrueIndex, -1)
assertContainsOnly(b, setOf(), 71)
// Test set and clear operations for ranges.
// Set false and clear.
b = BitSet(2)
assertContainsOnly(b, setOf(), 2)
b.set(0..70, true)
assertNotContainsOnly(b, setOf(), 71)
b.set(0..2, false)
assertNotContainsOnly(b, setOf(0, 1, 2), 71)
b.set(63..65, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 71)
b.set(68..70, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 71)
b.set(68..72, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.set(0..72, false)
assertContainsOnly(b, setOf(), 73)
b.set(0..72)
assertNotContainsOnly(b, setOf(), 73)
b.clear(0..2)
assertNotContainsOnly(b, setOf(0, 1, 2), 73)
b.clear(63..65)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.clear(68..70)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 73)
b.clear(68..72)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.clear(0..72)
assertContainsOnly(b, setOf(), 73)
// Set true.
b.set(0..2, true)
assertContainsOnly(b, setOf(0, 1, 2), 73)
b.set(63..65, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.set(70..72, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72), 73)
b.set(73..74, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72, 73, 74), 75)
b.set(0..74, true)
assertNotContainsOnly(b, setOf(), 75)
// Test set and clear for pair of indices.
b = BitSet(2)
assertContainsOnly(b, setOf(), 71)
b.set(0, 71, true)
assertNotContainsOnly(b, setOf(), 71)
b.set(0, 3, false)
assertNotContainsOnly(b, setOf(0, 1, 2), 71)
b.set(63, 66, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 71)
b.set(68, 71, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 71)
b.set(68, 73, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.set(0, 73, false)
assertContainsOnly(b, setOf(), 73)
b.set(0, 73)
assertNotContainsOnly(b, setOf(), 73)
b.clear(0, 3)
assertNotContainsOnly(b, setOf(0, 1, 2), 73)
b.clear(63, 66)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.clear(68, 71)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 73)
b.clear(68, 73)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.clear(0, 73)
assertContainsOnly(b, setOf(), 73)
// Set true.
b.set(0, 3, true)
assertContainsOnly(b, setOf(0, 1, 2), 73)
b.set(63, 66, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.set(70, 73, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72), 73)
b.set(73, 75, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72, 73, 74), 75)
b.set(0, 75, true)
assertNotContainsOnly(b, setOf(), 75)
// Access to negative elements must cause an exception
try {
b.set(-1)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b.clear(-1)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b.clear(-1..0)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b.set(-1..0)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b[-1]
fail()
} catch(e: IndexOutOfBoundsException) {}
}
fun testFlip() {
val b = BitSet(2)
b.set(0, true)
b.set(70, true)
b.set(63..65, true)
assertEquals(b.lastTrueIndex, 70)
// 0 element
assertContainsOnly(b, setOf(0, 63, 64, 65, 70), 71)
b.flip(0)
assertContainsOnly(b, setOf(63, 64, 65, 70), 71)
b.flip(1)
assertContainsOnly(b, setOf(1, 63, 64, 65, 70), 71)
b.flip(0)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 70), 71)
// last element
b.flip(70)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65), 71)
b.flip(69)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69), 71)
b.flip(70)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// element in the middle
b.flip(64)
assertContainsOnly(b, setOf(0, 1, 63, 65, 69, 70), 71)
b.flip(65)
assertContainsOnly(b, setOf(0, 1, 63, 69, 70), 71)
b.flip(65)
b.flip(64)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// range in the beginning
b.flip(0..2)
assertContainsOnly(b, setOf(2, 63, 64, 65, 69, 70), 71)
b.flip(0, 3)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// In the end
b.flip(68..70)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 68), 71)
b.flip(68, 71)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// In the middle
b.flip(64..66)
assertContainsOnly(b, setOf(0, 1, 63, 66, 69, 70), 71)
b.flip(64, 67)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// Access to a negative element must cause an exception.
try {
b.flip(-1)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b.flip(-1..0)
fail()
} catch(e: IndexOutOfBoundsException) {}
}
fun testNextBit() {
val b = BitSet(71)
b.set(0)
b.set(65)
b.set(70)
assertEquals(b.nextSetBit(), 0)
assertEquals(b.nextSetBit(0), 0)
assertEquals(b.nextSetBit(1), 65)
assertEquals(b.nextSetBit(65), 65)
assertEquals(b.nextSetBit(66), 70)
assertEquals(b.nextSetBit(70), 70)
assertEquals(b.nextSetBit(71), -1)
assertEquals(b.previousSetBit(0), 0)
assertEquals(b.previousSetBit(64), 0)
assertEquals(b.previousSetBit(65), 65)
assertEquals(b.previousSetBit(69), 65)
assertEquals(b.previousSetBit(70), 70)
assertEquals(b.previousSetBit(71), 70)
b.clear()
assertEquals(b.nextSetBit(), -1)
assertEquals(b.previousSetBit(70), -1)
b.set(0..70)
assertEquals(b.nextClearBit(), 71)
assertEquals(b.previousClearBit(70), -1)
b.clear(0)
b.clear(65)
b.clear(70)
assertEquals(b.nextClearBit(), 0)
assertEquals(b.nextClearBit(0), 0)
assertEquals(b.nextClearBit(1), 65)
assertEquals(b.nextClearBit(65), 65)
assertEquals(b.nextClearBit(66), 70)
assertEquals(b.nextClearBit(70), 70)
assertEquals(b.nextClearBit(71), 71) // assume that the bitset is extended here (virtually).
assertEquals(b.previousClearBit(0), 0)
assertEquals(b.previousClearBit(64), 0)
assertEquals(b.previousClearBit(65), 65)
assertEquals(b.previousClearBit(69), 65)
assertEquals(b.previousClearBit(70), 70)
assertEquals(b.previousClearBit(71), 71)
// See http://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html#previousClearBit-int-
assertEquals(b.previousClearBit(-1), -1)
assertEquals(b.previousSetBit(-1), -1)
// Test behaviour on the right border of the bit vector.
// We assume that the vector is infinite and have zeros after (size - 1)th bit.
var a = BitSet(64)
assertEquals(a.nextClearBit(63), 63)
assertEquals(a.nextClearBit(64), 64)
assertEquals(a.nextSetBit(63), -1)
assertEquals(a.nextSetBit(64), -1)
a.set(0, 64)
assertEquals(a.nextClearBit(63), 64)
assertEquals(a.nextClearBit(64), 64)
assertEquals(a.nextSetBit(63), 63)
assertEquals(a.nextSetBit(64), -1)
a.clear()
assertEquals(a.previousClearBit(63), 63)
assertEquals(a.previousClearBit(64), 64)
assertEquals(a.previousSetBit(63), -1)
assertEquals(a.previousSetBit(64), -1)
a.set(0, 64)
assertEquals(a.previousClearBit(63), -1)
assertEquals(a.previousClearBit(64), 64)
assertEquals(a.previousSetBit(63), 63)
assertEquals(a.previousSetBit(64), 63)
a = BitSet(0)
assertEquals(a.nextSetBit(0), -1)
assertEquals(a.nextClearBit(0), 0)
assertEquals(a.previousSetBit(0), -1)
assertEquals(a.previousClearBit(0), 0)
// Access to a negative element must cause an exception.
try {
b.previousSetBit(-2)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b.previousClearBit(-2)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b.nextSetBit(-1)
fail()
} catch(e: IndexOutOfBoundsException) {}
try {
b.nextClearBit(-1)
fail()
} catch(e: IndexOutOfBoundsException) {}
}
fun BitSet.setBits(vararg indices: Int, value: Boolean = true) {
indices.forEach {
set(it, value)
}
}
fun testLogic() {
var b2 = BitSet(76)
b2.setBits(1, 3,
61, 63,
65, 67,
70, 72)
// and
var b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.and(b2)
assertContainsOnly(b1, setOf(3, 63, 67, 72), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.and(b2)
assertContainsOnly(b1, setOf(3, 63, 67, 72), 129)
// or
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.or(b2)
assertContainsOnly(b1, setOf(1, 2, 3, 61, 62, 63, 65, 66, 67, 70, 71, 72), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.or(b2)
assertContainsOnly(b1, setOf(1, 2, 3, 61, 62, 63, 65, 66, 67, 70, 71, 72, 128), 129)
// xor
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.xor(b2)
assertContainsOnly(b1, setOf(1, 2, 61, 62, 65, 66, 70, 71), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.xor(b2)
assertContainsOnly(b1, setOf(1, 2, 61, 62, 65, 66, 70, 71, 128), 129)
// andNot
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.andNot(b2)
assertContainsOnly(b1, setOf(2, 62, 66, 71), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.andNot(b2)
assertContainsOnly(b1, setOf(2, 62, 66, 71, 128), 129)
// intersects
b1 = BitSet(73)
b1.set(0..1); b1.set(62..63); b1.set(64..65); b1.set(71..72)
b2.clear(); b2.set(0)
assertTrue(b1.intersects(b2))
b2.clear(); b2.set(62..65)
assertTrue(b1.intersects(b2))
b2.clear(); b2.set(72)
assertTrue(b1.intersects(b2))
b2.clear()
assertFalse(b1.intersects(b2))
b2.set(128)
assertFalse(b1.intersects(b2))
}
// Based on Harmony tests.
fun testEqualsHashCode() {
// HashCode.
val b = BitSet()
b.set(0..7)
b.clear(2)
b.clear(6)
assertEquals("BitSet returns wrong hash value", 1129, b.hashCode())
b.set(10)
b.clear(3)
assertEquals("BitSet returns wrong hash value", 97, b.hashCode())
// Equals.
val b1 = BitSet()
val b2 = BitSet()
b1.set(0..7)
b2.set(0..7)
assertTrue("Same BitSet returned false", b1 == b1)
assertTrue("Identical BitSet returned false", b1 == b2)
b2.clear(6)
assertFalse("Different BitSets returned true", b1 == b2)
val b3 = BitSet()
b3.set(0..7)
b3.set(128)
assertFalse("Different sized BitSet with higher bit set returned true", b1 == b3)
b3.clear(128)
assertTrue("Different sized BitSet with higher bits not set returned false", b1 == b3)
}
@Test fun runTest() {
testConstructor()
testSet()
testFlip()
testNextBit()
testLogic()
testEqualsHashCode()
}
@@ -1,112 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.SortWith
import kotlin.test.*
val correctIncreasing = Comparator<Int> { a, b -> // correct one.
when {
a > b -> 1
a < b -> -1
else -> 0
}
}
val correctDecreasing = Comparator<Int> { a, b -> // correct one.
when {
a < b -> 1
a > b -> -1
else -> 0
}
}
fun Array<Int>.assertSorted(cmp: Comparator<Int>, message: String = "") {
for (i in 1 until size) {
assertTrue(cmp.compare(this[i - 1], this[i]) <= 0, message)
}
}
fun Array<MyComparable>.assertSorted(message: String = "") {
for (i in 1 until size) {
assertTrue(this[i - 1] <= this[i], message)
}
}
data class ComparatorInfo(val name: String, val comparator: Comparator<Int>, val isCorrect: Boolean)
class MyComparable (val value: Int, val comparator: Comparator<Int>): Comparable<MyComparable> {
override fun compareTo(other: MyComparable): Int = comparator.compare(value, other.value)
}
// Assert that the array is sorted in terms of a comparator only for correct/partially correct cases
val comparators = listOf<ComparatorInfo>(
ComparatorInfo("Correct increasing", correctIncreasing , true),
ComparatorInfo("Correct decreasing", correctDecreasing , true),
ComparatorInfo("Incorrect increasing", Comparator { a ,b -> if (a > b) 1 else -1 }, false),
ComparatorInfo("Incorrect decreasing", Comparator { a ,b -> if (a < b) 1 else -1 }, false),
ComparatorInfo("Always 1", Comparator { a ,b -> 1 }, false),
ComparatorInfo("Always -1", Comparator { a ,b -> -1 }, false),
ComparatorInfo("Always 0", Comparator { a ,b -> 0 }, false)
)
val arrays = listOf<Array<Int>>(
arrayOf(),
arrayOf(1),
arrayOf(Int.MIN_VALUE, 0, Int.MAX_VALUE),
arrayOf(Int.MAX_VALUE, 0, Int.MIN_VALUE),
arrayOf(1, 2, 3),
arrayOf(-2, -1, 99, 1, 2),
arrayOf(90, 91, 0, 98, 99),
arrayOf(2, 1, 99, -1, 2),
arrayOf(99, 98, 0, 91, 90),
arrayOf(42, 42, 42),
arrayOf(99, 42, 0, 42, 50),
arrayOf(
100000, 190001, 200002, 200003, 200004, 210005, 220006, 250007, 300008, 310009, 360010, 365011,
380012, 390013, 390014, 399015, 400016, 400017, 400018, 400019, 400020, 400021, 400022, 400023,
400024, 400025, 400026, 450027, 450028, 480029, 480030, 500031, 500032, 500033, 500034, 500035,
500036, 500037, 500038, 500039, 500040, 500041, 500042, 500043, 500044, 500045, 500046, 500047,
500048, 500049, 500050, 500051, 500052, 500053, 505054, 510055, 510056, 510057, 510058, 510059,
510060, 510061, 510062, 510063, 410064, 410065, 511066, 511067, 520068, 520069, 420070, 520071,
530072, 530073, 530074, 430075, 430076, 530077, 540078, 540079, 540080, 540081, 540082, 540083,
540084, 490085, 540086, 540087, 542088, 544089, 546090, 550091, 550092, 550093, 550094, 590095,
590096, 595097, 600098, 600099, 600100, 600101, 600102, 600103, 600104, 550105, 600106, 600107,
600108, 600109, 600110, 610111, 610112, 610113, 620114, 620115, 620116, 620117, 620118, 620119,
640120, 640121, 645122, 645123, 645124, 645125, 645126, 645127, 650128, 700129, 700130
)
)
@Test fun runTest() {
arrays.forEach { array ->
comparators.forEach {
// Test with custom comparator
val arrayUnderTest = array.copyOf()
arrayUnderTest.sortWith(it.comparator)
if (it.isCorrect) {
arrayUnderTest.assertSorted(it.comparator, """
Assert sorted failed for comparator: "${it.name}"
Array: ${array.joinToString()}
Array after sorting: ${arrayUnderTest.joinToString()}
""".trimIndent())
}
// Test of a custom comparable
val comparableArrayUnderTest = Array(array.size) { i ->
MyComparable(array[i], it.comparator)
}
comparableArrayUnderTest.sort()
if (it.isCorrect) {
comparableArrayUnderTest.assertSorted("""
Assert sorted failed for Comparable: "${it.name}"
Array: ${array.joinToString()}
Array after sorting: ${comparableArrayUnderTest.joinToString()}
""".trimIndent())
}
}
}
}
@@ -1,38 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.array0
import kotlin.test.*
@Test fun runTest() {
// Create instances of all array types.
val byteArray = ByteArray(5)
println(byteArray.size.toString())
val charArray = CharArray(6)
println(charArray.size.toString())
val shortArray = ShortArray(7)
println(shortArray.size.toString())
val intArray = IntArray(8)
println(intArray.size.toString())
val longArray = LongArray(9)
println(longArray.size.toString())
val floatArray = FloatArray(10)
println(floatArray.size.toString())
val doubleArray = FloatArray(11)
println(doubleArray.size.toString())
val booleanArray = BooleanArray(12)
println(booleanArray.size.toString())
val stringArray = Array<String>(13, { i -> ""})
println(stringArray.size.toString())
}
@@ -1,9 +0,0 @@
5
6
7
8
9
10
11
12
13
@@ -1,36 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.array1
import kotlin.test.*
@Test fun runTest() {
val byteArray = ByteArray(5)
byteArray[1] = 2
byteArray[3] = 4
println(byteArray[3].toString() + " " + byteArray[1].toString())
val shortArray = ShortArray(2)
shortArray[0] = -1
shortArray[1] = 1
print(shortArray[1].toString())
print(shortArray[0].toString())
println()
val intArray = IntArray(7)
intArray[1] = 9
intArray[3] = 6
print(intArray[3].toString())
print(intArray[1].toString())
println()
val longArray = LongArray(9)
longArray[8] = 8
longArray[3] = 3
print(longArray[3].toString())
print(longArray[8].toString())
println()
}
@@ -1,4 +0,0 @@
4 2
1-1
69
38
@@ -1,16 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.array2
import kotlin.test.*
@Test fun runTest() {
val byteArray = Array<Byte>(5, { i -> (i * 2).toByte() })
byteArray.map { println(it) }
val intArray = Array<Int>(5, { i -> i * 4 })
println(intArray.sum())
}
@@ -1,6 +0,0 @@
0
2
4
6
8
40
@@ -1,22 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.array3
import kotlin.test.*
import kotlin.native.*
@Test fun runTest() {
val data = immutableBlobOf(0x1, 0x2, 0x3, 0x7, 0x8, 0x9, 0x80, 0xff)
for (b in data) {
print("$b ")
}
println()
val dataClone = data.toByteArray()
dataClone.map { print("$it ") }
println()
}
@@ -1,2 +0,0 @@
1 2 3 7 8 9 -128 -1
1 2 3 7 8 9 -128 -1
@@ -1,64 +0,0 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.array4
import kotlin.test.*
@Test fun runTest() {
assertFailsWith<IllegalArgumentException> {
val a = Array(-2) { "nope" }
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = ByteArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = UByteArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = ShortArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = UShortArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = IntArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = UIntArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = LongArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = ULongArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = FloatArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = DoubleArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = BooleanArray(-2)
println(a)
}
assertFailsWith<IllegalArgumentException> {
val a = CharArray(-2)
println(a)
}
println("OK")
}
@@ -1 +0,0 @@
OK
@@ -1,970 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlinx.cinterop.ExperimentalForeignApi::class)
package runtime.collections.array5
import kotlin.test.*
import kotlinx.cinterop.*
@Test fun arrayGet() {
val arr = Array(10) { 0 }
assertEquals(0, arr[0])
assertEquals(0, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun arraySet() {
val arr = Array(10) { 0 }
arr[0] = 1
arr[9] = 1
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun byteArrayGet() {
val arr = ByteArray(10) { 0 }
assertEquals(0, arr[0])
assertEquals(0, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun byteArraySet() {
val arr = ByteArray(10) { 0 }
arr[0] = 1
arr[9] = 1
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun uByteArrayGet() {
val arr = UByteArray(10) { 0U }
assertEquals(0U, arr[0])
assertEquals(0U, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun uByteArraySet() {
val arr = UByteArray(10) { 0U }
arr[0] = 1U
arr[9] = 1U
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1U
}
}
@Test fun shortArrayGet() {
val arr = ShortArray(10) { 0 }
assertEquals(0, arr[0])
assertEquals(0, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun shortArraySet() {
val arr = ShortArray(10) { 0 }
arr[0] = 1
arr[9] = 1
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun uShortArrayGet() {
val arr = UShortArray(10) { 0U }
assertEquals(0U, arr[0])
assertEquals(0U, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun uShortArraySet() {
val arr = UShortArray(10) { 0U }
arr[0] = 1U
arr[9] = 1U
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1U
}
}
@Test fun intArrayGet() {
val arr = IntArray(10) { 0 }
assertEquals(0, arr[0])
assertEquals(0, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun intArraySet() {
val arr = IntArray(10) { 0 }
arr[0] = 1
arr[9] = 1
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun uIntArrayGet() {
val arr = UIntArray(10) { 0U }
assertEquals(0U, arr[0])
assertEquals(0U, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun uIntArraySet() {
val arr = UIntArray(10) { 0U }
arr[0] = 1U
arr[9] = 1U
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1U
}
}
@Test fun longArrayGet() {
val arr = LongArray(10) { 0 }
assertEquals(0, arr[0])
assertEquals(0, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun longArraySet() {
val arr = LongArray(10) { 0 }
arr[0] = 1
arr[9] = 1
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun uLongArrayGet() {
val arr = ULongArray(10) { 0U }
assertEquals(0U, arr[0])
assertEquals(0U, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun uLongArraySet() {
val arr = ULongArray(10) { 0U }
arr[0] = 1U
arr[9] = 1U
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1U
}
}
@Test fun floatArrayGet() {
val arr = FloatArray(10) { 0.0f }
assertEquals(0.0f, arr[0])
assertEquals(0.0f, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun floatArraySet() {
val arr = FloatArray(10) { 0.0f }
arr[0] = 1.0f
arr[9] = 1.0f
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1.0f
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1.0f
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1.0f
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1.0f
}
}
@Test fun doubleArrayGet() {
val arr = DoubleArray(10) { 0.0 }
assertEquals(0.0, arr[0])
assertEquals(0.0, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun doubleArraySet() {
val arr = DoubleArray(10) { 0.0 }
arr[0] = 1.0
arr[9] = 1.0
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = 1.0
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1.0
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1.0
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1.0
}
}
@Test fun booleanArrayGet() {
val arr = BooleanArray(10) { false }
assertEquals(false, arr[0])
assertEquals(false, arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun booleanArraySet() {
val arr = BooleanArray(10) { false }
arr[0] = true
arr[9] = true
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = true
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = true
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = true
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = true
}
}
@Test fun charArrayGet() {
val arr = CharArray(10) { '0' }
assertEquals('0', arr[0])
assertEquals('0', arr[9])
assertFailsWith<IndexOutOfBoundsException> {
arr[10]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun charArraySet() {
val arr = CharArray(10) { '0' }
arr[0] = '1'
arr[9] = '1'
assertFailsWith<IndexOutOfBoundsException> {
arr[10] = '1'
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = '1'
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = '1'
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = '1'
}
}
@Test fun byteArrayGetUByte() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getUByteAt(0))
assertEquals(0U, arr.getUByteAt(9))
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetUByte() {
val arr = ByteArray(10) { 0 }
arr.setUByteAt(0, 1U)
arr.setUByteAt(9, 1U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(10, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(Int.MIN_VALUE, 1U)
}
}
@Test fun byteArrayGetChar() {
val arr = ByteArray(10) { 0 }
assertEquals(0.toChar(), arr.getCharAt(0))
assertEquals(0.toChar(), arr.getCharAt(8))
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(9)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetChar() {
val arr = ByteArray(10) { 0 }
arr.setCharAt(0, '1')
arr.setCharAt(8, '1')
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(9, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(10, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(-1, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(Int.MAX_VALUE, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(Int.MIN_VALUE, '1')
}
}
@Test fun byteArrayGetShort() {
val arr = ByteArray(10) { 0 }
assertEquals(0, arr.getShortAt(0))
assertEquals(0, arr.getShortAt(8))
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(9)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetShort() {
val arr = ByteArray(10) { 0 }
arr.setShortAt(0, 0)
arr.setShortAt(8, 0)
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(9, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(10, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(Int.MAX_VALUE, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(Int.MIN_VALUE, 1)
}
}
@Test fun byteArrayGetUShort() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getUShortAt(0))
assertEquals(0U, arr.getUShortAt(8))
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(9)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetUShort() {
val arr = ByteArray(10) { 0 }
arr.setUShortAt(0, 0U)
arr.setUShortAt(8, 0U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(9, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(10, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(Int.MIN_VALUE, 1U)
}
}
@Test fun byteArrayGetInt() {
val arr = ByteArray(10) { 0 }
assertEquals(0, arr.getIntAt(0))
assertEquals(0, arr.getIntAt(6))
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(7)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetInt() {
val arr = ByteArray(10) { 0 }
arr.setIntAt(0, 1)
arr.setIntAt(6, 1)
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(7, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(10, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(Int.MAX_VALUE, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(Int.MIN_VALUE, 1)
}
}
@Test fun byteArrayGetUInt() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getUIntAt(0))
assertEquals(0U, arr.getUIntAt(6))
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(7)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetUInt() {
val arr = ByteArray(10) { 0 }
arr.setUIntAt(0, 1U)
arr.setUIntAt(6, 1U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(7, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(10, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(Int.MIN_VALUE, 1U)
}
}
@Test fun byteArrayGetLong() {
val arr = ByteArray(10) { 0 }
assertEquals(0, arr.getLongAt(0))
assertEquals(0, arr.getLongAt(2))
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(3)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetLong() {
val arr = ByteArray(10) { 0 }
arr.setLongAt(0, 1)
arr.setLongAt(2, 1)
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(3, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(10, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(Int.MAX_VALUE, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(Int.MIN_VALUE, 1)
}
}
@Test fun byteArrayGetULong() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getULongAt(0))
assertEquals(0U, arr.getULongAt(2))
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(3)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetULong() {
val arr = ByteArray(10) { 0 }
arr.setULongAt(0, 1U)
arr.setULongAt(2, 1U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(3, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(10, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(Int.MIN_VALUE, 1U)
}
}
@Test fun byteArrayGetFloat() {
val arr = ByteArray(10) { 0 }
assertEquals(0.0f, arr.getFloatAt(0))
assertEquals(0.0f, arr.getFloatAt(6))
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(7)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetFloat() {
val arr = ByteArray(10) { 0 }
arr.setFloatAt(0, 1.0f)
arr.setFloatAt(6, 1.0f)
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(7, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(10, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(-1, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(Int.MAX_VALUE, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(Int.MIN_VALUE, 1.0f)
}
}
@Test fun byteArrayGetDouble() {
val arr = ByteArray(10) { 0 }
assertEquals(0.0, arr.getDoubleAt(0))
assertEquals(0.0, arr.getDoubleAt(2))
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(3)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(10)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(Int.MIN_VALUE)
}
}
@Test fun byteArraySetDouble() {
val arr = ByteArray(10) { 0 }
arr.setDoubleAt(0, 1.0)
arr.setDoubleAt(2, 1.0)
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(3, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(10, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(-1, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(Int.MAX_VALUE, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(Int.MIN_VALUE, 1.0)
}
}
@Test fun immutableBlobToByteArray() {
val blob = immutableBlobOf(0, 0)
val arr = blob.toByteArray(0, 1)
assertEquals(1, arr.size)
assertEquals(0, arr[0])
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(0, -1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(0, 10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(10, 11)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(10, 1)
}
}
@Test fun immutableBlobToUByteArray() {
val blob = immutableBlobOf(0, 0)
val arr = blob.toUByteArray(0, 1)
assertEquals(1, arr.size)
assertEquals(0U, arr[0])
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(0, -1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(0, 10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(10, 11)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(10, 1)
}
}
@Test fun immutableBlobAsCPointer() {
val blob = immutableBlobOf(0, 0)
assertEquals(0, blob.asCPointer(0).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(Int.MIN_VALUE)
}
}
@Test fun immutableBlobAsUCPointer() {
val blob = immutableBlobOf(0, 0)
assertEquals(0U, blob.asUCPointer(0).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(Int.MIN_VALUE)
}
}
@@ -1,385 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.array_list1
import kotlin.test.*
fun assertTrue(cond: Boolean) {
if (!cond)
println("FAIL")
}
fun assertFalse(cond: Boolean) {
if (cond)
println("FAIL")
}
fun assertEquals(value1: Any?, value2: Any?) {
if (value1 != value2)
throw Error("FAIL " + value1 + " " + value2)
}
fun assertEquals(value1: Int, value2: Int) {
if (value1 != value2)
throw Error("FAIL " + value1 + " " + value2)
}
fun testBasic() {
val a = ArrayList<String>()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
assertTrue(a.add("1"))
assertTrue(a.add("2"))
assertTrue(a.add("3"))
assertFalse(a.isEmpty())
assertEquals(3, a.size)
assertEquals("1", a[0])
assertEquals("2", a[1])
assertEquals("3", a[2])
a[0] = "11"
assertEquals("11", a[0])
assertEquals("11", a.removeAt(0))
assertEquals(2, a.size)
assertEquals("2", a[0])
assertEquals("3", a[1])
a.add(1, "22")
assertEquals(3, a.size)
assertEquals("2", a[0])
assertEquals("22", a[1])
assertEquals("3", a[2])
a.clear()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
}
fun testIterator() {
val a = ArrayList(listOf("1", "2", "3"))
val it = a.iterator()
assertTrue(it.hasNext())
assertEquals("1", it.next())
assertTrue(it.hasNext())
assertEquals("2", it.next())
assertTrue(it.hasNext())
assertEquals("3", it.next())
assertFalse(it.hasNext())
}
fun testContainsAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
assertFalse(a.containsAll(listOf("6", "7", "8")))
assertFalse(a.containsAll(listOf("5", "6", "7")))
assertFalse(a.containsAll(listOf("4", "5", "6")))
assertTrue(a.containsAll(listOf("3", "4", "5")))
assertTrue(a.containsAll(listOf("2", "3", "4")))
}
fun testRemove() {
val a = ArrayList(listOf("1", "2", "3"))
assertTrue(a.remove("2"))
assertEquals(2, a.size)
assertEquals("1", a[0])
assertEquals("3", a[1])
assertFalse(a.remove("2"))
assertEquals(2, a.size)
assertEquals("1", a[0])
assertEquals("3", a[1])
}
fun testRemoveAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "1"))
assertFalse(a.removeAll(listOf("6", "7", "8")))
assertEquals(listOf("1", "2", "3", "4", "5", "1"), a)
assertTrue(a.removeAll(listOf("5", "3", "1")))
assertEquals(listOf("2", "4"), a)
}
fun testRetainAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
assertFalse(a.retainAll(listOf("1", "2", "3", "4", "5")))
assertEquals(listOf("1", "2", "3", "4", "5"), a)
assertTrue(a.retainAll(listOf("5", "3", "1")))
assertEquals(listOf("1", "3", "5"), a)
}
fun testEquals() {
val a = ArrayList(listOf("1", "2", "3"))
assertTrue(a == listOf("1", "2", "3"))
assertFalse(a == listOf("2", "3", "1")) // order matters
assertFalse(a == listOf("1", "2", "4"))
assertFalse(a == listOf("1", "2"))
}
fun testHashCode() {
val a = ArrayList(listOf("1", "2", "3"))
assertTrue(a.hashCode() == listOf("1", "2", "3").hashCode())
}
fun testToString() {
val a = ArrayList(listOf("1", "2", "3"))
assertTrue(a.toString() == listOf("1", "2", "3").toString())
}
fun testSubList() {
val a0 = ArrayList(listOf("0", "1", "2", "3", "4"))
val a = a0.subList(1, 4)
assertEquals(3, a.size)
assertEquals("1", a[0])
assertEquals("2", a[1])
assertEquals("3", a[2])
assertTrue(a == listOf("1", "2", "3"))
assertTrue(a.hashCode() == listOf("1", "2", "3").hashCode())
assertTrue(a.toString() == listOf("1", "2", "3").toString())
}
fun testResize() {
val a = ArrayList<String>()
val n = 10000
for (i in 1..n)
assertTrue(a.add(i.toString()))
assertEquals(n, a.size)
for (i in 1..n)
assertEquals(i.toString(), a[i - 1])
a.trimToSize()
assertEquals(n, a.size)
for (i in 1..n)
assertEquals(i.toString(), a[i - 1])
}
fun testSubListContains() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertTrue(a.contains("1"))
assertFalse(s.contains("1"))
assertTrue(a.contains("2"))
assertTrue(s.contains("2"))
assertTrue(a.contains("3"))
assertTrue(s.contains("3"))
assertTrue(a.contains("4"))
assertFalse(s.contains("4"))
}
fun testSubListIndexOf() {
val a = ArrayList(listOf("1", "2", "3", "4", "1"))
val s = a.subList(1, 3)
assertEquals(0, a.indexOf("1"))
assertEquals(-1, s.indexOf("1"))
assertEquals(1, a.indexOf("2"))
assertEquals(0, s.indexOf("2"))
assertEquals(2, a.indexOf("3"))
assertEquals(1, s.indexOf("3"))
assertEquals(3, a.indexOf("4"))
assertEquals(-1, s.indexOf("4"))
}
fun testSubListLastIndexOf() {
val a = ArrayList(listOf("1", "2", "3", "4", "1"))
val s = a.subList(1, 3)
assertEquals(4, a.lastIndexOf("1"))
assertEquals(-1, s.lastIndexOf("1"))
assertEquals(1, a.lastIndexOf("2"))
assertEquals(0, s.lastIndexOf("2"))
assertEquals(2, a.lastIndexOf("3"))
assertEquals(1, s.lastIndexOf("3"))
assertEquals(3, a.lastIndexOf("4"))
assertEquals(-1, s.lastIndexOf("4"))
}
fun testSubListClear() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertEquals(listOf("2", "3"), s)
s.clear()
assertEquals(listOf<String>(), s)
assertEquals(listOf("1", "4"), a)
}
fun testSubListSubListClear() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6"))
val s = a.subList(1, 5)
val q = s.subList(1, 3)
assertEquals(listOf("2", "3", "4", "5"), s)
assertEquals(listOf("3", "4"), q)
q.clear()
assertEquals(listOf<String>(), q)
assertEquals(listOf("2", "5"), s)
assertEquals(listOf("1", "2", "5", "6"), a)
}
fun testSubListAdd() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertEquals(listOf("2", "3"), s)
assertTrue(s.add("5"))
assertEquals(listOf("2", "3", "5"), s)
assertEquals(listOf("1", "2", "3", "5", "4"), a)
}
fun testSubListSubListAdd() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6"))
val s = a.subList(1, 5)
val q = s.subList(1, 3)
assertEquals(listOf("2", "3", "4", "5"), s)
assertEquals(listOf("3", "4"), q)
assertTrue(q.add("7"))
assertEquals(listOf("3", "4", "7"), q)
assertEquals(listOf("2", "3", "4", "7", "5"), s)
assertEquals(listOf("1", "2", "3", "4", "7", "5", "6"), a)
}
fun testSubListAddAll() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertEquals(listOf("2", "3"), s)
assertTrue(s.addAll(listOf("5", "6")))
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "4"), a)
}
fun testSubListSubListAddAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6"))
val s = a.subList(1, 5)
val q = s.subList(1, 3)
assertEquals(listOf("2", "3", "4", "5"), s)
assertEquals(listOf("3", "4"), q)
assertTrue(q.addAll(listOf("7", "8")))
assertEquals(listOf("3", "4", "7", "8"), q)
assertEquals(listOf("2", "3", "4", "7", "8", "5"), s)
assertEquals(listOf("1", "2", "3", "4", "7", "8", "5", "6"), a)
}
fun testSubListRemoveAt() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val s = a.subList(1, 4)
assertEquals(listOf("2", "3", "4"), s)
assertEquals("3", s.removeAt(1))
assertEquals(listOf("2", "4"), s)
assertEquals(listOf("1", "2", "4", "5"), a)
}
fun testSubListSubListRemoveAt() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6", "7"))
val s = a.subList(1, 6)
val q = s.subList(1, 4)
assertEquals(listOf("2", "3", "4", "5", "6"), s)
assertEquals(listOf("3", "4", "5"), q)
assertEquals("4", q.removeAt(1))
assertEquals(listOf("3", "5"), q)
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "7"), a)
}
fun testSubListRemoveAll() {
val a = ArrayList(listOf("1", "2", "3", "3", "4", "5"))
val s = a.subList(1, 5)
assertEquals(listOf("2", "3", "3", "4"), s)
assertTrue(s.removeAll(listOf("3", "5")))
assertEquals(listOf("2", "4"), s)
assertEquals(listOf("1", "2", "4", "5"), a)
}
fun testSubListSubListRemoveAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6", "7"))
val s = a.subList(1, 6)
val q = s.subList(1, 4)
assertEquals(listOf("2", "3", "4", "5", "6"), s)
assertEquals(listOf("3", "4", "5"), q)
assertTrue(q.removeAll(listOf("4", "6")))
assertEquals(listOf("3", "5"), q)
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "7"), a)
}
fun testSubListRetainAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val s = a.subList(1, 4)
assertEquals(listOf("2", "3", "4"), s)
assertTrue(s.retainAll(listOf("5", "3")))
assertEquals(listOf("3"), s)
assertEquals(listOf("1", "3", "5"), a)
}
fun testSubListSubListRetainAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6", "7"))
val s = a.subList(1, 6)
val q = s.subList(1, 4)
assertEquals(listOf("2", "3", "4", "5", "6"), s)
assertEquals(listOf("3", "4", "5"), q)
assertTrue(q.retainAll(listOf("5", "3")))
assertEquals(listOf("3", "5"), q)
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "7"), a)
}
fun testIteratorRemove() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val it = a.iterator()
while (it.hasNext())
if (it.next()[0].toInt() % 2 == 0)
it.remove()
assertEquals(listOf("1", "3", "5"), a)
}
fun testIteratorAdd() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val it = a.listIterator()
while (it.hasNext()) {
val next = it.next()
if (next[0].toInt() % 2 == 0)
it.add("-" + next)
}
assertEquals(listOf("1", "2", "-2", "3", "4", "-4", "5"), a)
}
@Test fun runTest() {
testBasic()
testIterator()
testRemove()
testRemoveAll()
testRetainAll()
testEquals()
testHashCode()
testToString()
testSubList()
testResize()
testSubListContains()
testSubListIndexOf()
testSubListLastIndexOf()
testSubListClear()
testSubListSubListClear()
testSubListAdd()
testSubListSubListAdd()
testSubListAddAll()
testSubListSubListAddAll()
testSubListRemoveAt()
testSubListSubListRemoveAt()
testSubListRemoveAll()
testSubListSubListRemoveAll()
testSubListSubListRemoveAll()
testSubListRetainAll()
testSubListSubListRetainAll()
testIteratorRemove()
testIteratorAdd()
println("OK")
}
@@ -1,36 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.array_list2
import kotlin.test.*
fun testIteratorNext() {
val a = arrayListOf("1", "2", "3", "4", "5")
val it = a.listIterator()
assertFailsWith<NoSuchElementException> {
while (true) {
it.next()
}
}
}
fun testIteratorPrevious() {
val a = arrayListOf("1", "2", "3", "4", "5")
val it = a.listIterator()
it.next()
assertFailsWith<NoSuchElementException> {
while (true) {
it.previous()
}
}
}
@Test fun runTest() {
testIteratorNext()
testIteratorPrevious()
println("OK")
}
@@ -1,221 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.hash_map0
import kotlin.test.*
fun assertTrue(cond: Boolean) {
if (!cond)
println("FAIL")
}
fun assertFalse(cond: Boolean) {
if (cond)
println("FAIL")
}
fun assertEquals(value1: Any?, value2: Any?) {
if (value1 != value2)
println("FAIL")
}
fun assertNotEquals(value1: Any?, value2: Any?) {
if (value1 == value2)
println("FAIL")
}
fun assertEquals(value1: Int, value2: Int) {
if (value1 != value2)
println("FAIL")
}
fun testBasic() {
val m = HashMap<String, String>()
assertTrue(m.isEmpty())
assertEquals(0, m.size)
assertFalse(m.containsKey("1"))
assertFalse(m.containsValue("a"))
assertEquals(null, m.get("1"))
assertEquals(null, m.put("1", "a"))
assertTrue(m.containsKey("1"))
assertTrue(m.containsValue("a"))
assertEquals("a", m.get("1"))
assertFalse(m.isEmpty())
assertEquals(1, m.size)
assertFalse(m.containsKey("2"))
assertFalse(m.containsValue("b"))
assertEquals(null, m.get("2"))
assertEquals(null, m.put("2", "b"))
assertTrue(m.containsKey("1"))
assertTrue(m.containsValue("a"))
assertEquals("a", m.get("1"))
assertTrue(m.containsKey("2"))
assertTrue(m.containsValue("b"))
assertEquals("b", m.get("2"))
assertFalse(m.isEmpty())
assertEquals(2, m.size)
assertEquals("b", m.put("2", "bb"))
assertTrue(m.containsKey("1"))
assertTrue(m.containsValue("a"))
assertEquals("a", m.get("1"))
assertTrue(m.containsKey("2"))
assertTrue(m.containsValue("a"))
assertTrue(m.containsValue("bb"))
assertEquals("bb", m.get("2"))
assertFalse(m.isEmpty())
assertEquals(2, m.size)
assertEquals("a", m.remove("1"))
assertFalse(m.containsKey("1"))
assertFalse(m.containsValue("a"))
assertEquals(null, m.get("1"))
assertTrue(m.containsKey("2"))
assertTrue(m.containsValue("bb"))
assertEquals("bb", m.get("2"))
assertFalse(m.isEmpty())
assertEquals(1, m.size)
assertEquals("bb", m.remove("2"))
assertFalse(m.containsKey("1"))
assertFalse(m.containsValue("a"))
assertEquals(null, m.get("1"))
assertFalse(m.containsKey("2"))
assertFalse(m.containsValue("bb"))
assertEquals(null, m.get("2"))
assertTrue(m.isEmpty())
assertEquals(0, m.size)
}
fun testEquals() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertTrue(m == expected)
assertTrue(m == mapOf("b" to "2", "c" to "3", "a" to "1")) // order does not matter
assertFalse(m == mapOf("a" to "1", "b" to "2", "c" to "4"))
assertFalse(m == mapOf("a" to "1", "b" to "2", "c" to "5"))
assertFalse(m == mapOf("a" to "1", "b" to "2"))
assertEquals(m.keys, expected.keys)
assertEquals(m.values.toList(), expected.values.toList())
assertEquals(m.entries, expected.entries)
}
fun testHashCode() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertEquals(expected.hashCode(), m.hashCode())
assertEquals(expected.entries.hashCode(), m.entries.hashCode())
assertEquals(expected.keys.hashCode(), m.keys.hashCode())
}
fun testToString() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertEquals(expected.toString(), m.toString())
assertEquals(expected.entries.toString(), m.entries.toString())
assertEquals(expected.keys.toString(), m.keys.toString())
assertEquals(expected.values.toString(), m.values.toString())
}
fun testPutEntry() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
val e = expected.entries.iterator().next() as MutableMap.MutableEntry<String, String>
assertTrue(m.entries.contains(e))
assertTrue(m.entries.remove(e))
assertTrue(mapOf("b" to "2", "c" to "3") == m)
assertEquals(null, m.put(e.key, e.value))
assertTrue(expected == m)
assertEquals(e.value, m.put(e.key, e.value))
assertTrue(expected == m)
}
fun testRemoveAllEntries() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.entries.removeAll(mapOf("a" to "2", "b" to "3", "c" to "4").entries))
assertEquals(expected, m)
assertTrue(m.entries.removeAll(mapOf("b" to "22", "c" to "3", "d" to "4").entries))
assertNotEquals(expected, m)
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
fun testRetainAllEntries() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.entries.retainAll(expected.entries))
assertEquals(expected, m)
assertTrue(m.entries.retainAll(mapOf("b" to "22", "c" to "3", "d" to "4").entries))
assertEquals(mapOf("c" to "3"), m)
}
fun testContainsAllValues() {
val m = HashMap(mapOf("a" to "1", "b" to "2", "c" to "3"))
assertTrue(m.values.containsAll(listOf("1", "2")))
assertTrue(m.values.containsAll(listOf("1", "2", "3")))
assertFalse(m.values.containsAll(listOf("1", "2", "3", "4")))
assertFalse(m.values.containsAll(listOf("2", "3", "4")))
}
fun testRemoveValue() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.remove("b"))
assertEquals(expected, m)
assertTrue(m.values.remove("2"))
assertEquals(mapOf("a" to "1", "c" to "3"), m)
}
fun testRemoveAllValues() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.removeAll(listOf("b", "c")))
assertEquals(expected, m)
assertTrue(m.values.removeAll(listOf("b", "3")))
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
fun testRetainAllValues() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.retainAll(listOf("1", "2", "3")))
assertEquals(expected, m)
assertTrue(m.values.retainAll(listOf("1", "2", "c")))
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
fun testEntriesIteratorSet() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
val it = m.iterator()
while (it.hasNext()) {
val entry = it.next()
entry.setValue(entry.value + "!")
}
assertNotEquals(expected, m)
assertEquals(mapOf("a" to "1!", "b" to "2!", "c" to "3!"), m)
}
@Test fun runTest() {
testBasic()
testEquals()
testHashCode()
testToString()
testPutEntry()
testRemoveAllEntries()
testRetainAllEntries()
testContainsAllValues()
testRemoveValue()
testRemoveAllValues()
testRetainAllValues()
testEntriesIteratorSet()
//testDegenerateKeys()
println("OK")
}
@@ -1,92 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.hash_map1
import kotlin.test.*
fun assertTrue(cond: Boolean) {
if (!cond)
println("FAIL")
}
fun assertFalse(cond: Boolean) {
if (cond)
println("FAIL")
}
fun assertEquals(value1: Any?, value2: Any?) {
if (value1 != value2)
println("FAIL")
}
fun assertNotEquals(value1: Any?, value2: Any?) {
if (value1 == value2)
println("FAIL")
}
fun assertEquals(value1: Int, value2: Int) {
if (value1 != value2)
println("FAIL")
}
fun testRehashAndCompact() {
val m = HashMap<String, String>()
for (repeat in 1..10) {
val n = when (repeat) {
1 -> 1000
2 -> 10000
3 -> 10
else -> 100000
}
for (i in 1..n) {
assertFalse(m.containsKey(i.toString()))
assertEquals(null, m.put(i.toString(), "val$i"))
assertTrue(m.containsKey(i.toString()))
assertEquals(i, m.size)
}
for (i in 1..n) {
assertTrue(m.containsKey(i.toString()))
}
for (i in 1..n) {
assertEquals("val$i", m.remove(i.toString()))
assertFalse(m.containsKey(i.toString()))
assertEquals(n - i, m.size)
}
assertTrue(m.isEmpty())
}
}
fun testClear() {
val m = HashMap<String, String>()
for (repeat in 1..10) {
val n = when (repeat) {
1 -> 1000
2 -> 10000
3 -> 10
else -> 100000
}
for (i in 1..n) {
assertFalse(m.containsKey(i.toString()))
assertEquals(null, m.put(i.toString(), "val$i"))
assertTrue(m.containsKey(i.toString()))
assertEquals(i, m.size)
}
for (i in 1..n) {
assertTrue(m.containsKey(i.toString()))
}
m.clear()
assertEquals(0, m.size)
for (i in 1..n) {
assertFalse(m.containsKey(i.toString()))
}
}
}
@Test fun runTest() {
testRehashAndCompact()
testClear()
println("OK")
}
@@ -1,140 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.hash_set0
import kotlin.test.*
fun assertTrue(cond: Boolean) {
if (!cond)
println("FAIL")
}
fun assertFalse(cond: Boolean) {
if (cond)
println("FAIL")
}
fun assertEquals(value1: Any?, value2: Any?) {
if (value1 != value2)
println("FAIL")
}
fun assertEquals(value1: Int, value2: Int) {
if (value1 != value2)
println("FAIL")
}
fun testBasic() {
val a = HashSet<String>()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
assertTrue(a.add("1"))
assertTrue(a.add("2"))
assertTrue(a.add("3"))
assertFalse(a.isEmpty())
assertEquals(3, a.size)
assertTrue(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertFalse(a.contains("4"))
assertTrue(a.remove("1"))
assertEquals(2, a.size)
assertFalse(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertFalse(a.contains("4"))
assertTrue(a.add("4"))
assertEquals(3, a.size)
assertFalse(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertTrue(a.contains("4"))
assertFalse(a.add("4"))
assertEquals(3, a.size)
assertFalse(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertTrue(a.contains("4"))
a.clear()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
assertFalse(a.contains("1"))
assertFalse(a.contains("2"))
assertFalse(a.contains("3"))
assertFalse(a.contains("4"))
}
fun testIterator() {
val s = HashSet(listOf("1", "2", "3"))
val it = s.iterator()
assertTrue(it.hasNext())
assertEquals("1", it.next())
assertTrue(it.hasNext())
assertEquals("2", it.next())
assertTrue(it.hasNext())
assertEquals("3", it.next())
assertFalse(it.hasNext())
}
fun testEquals() {
val s = HashSet(listOf("1", "2", "3"))
assertTrue(s == setOf("1", "2", "3"))
assertTrue(s == setOf("2", "3", "1")) // order does not matter
assertFalse(s == setOf("1", "2", "4"))
assertFalse(s == setOf("1", "2"))
}
fun testHashCode() {
val s = HashSet(listOf("1", "2", "3"))
assertTrue(s.hashCode() == setOf("1", "2", "3").hashCode())
}
fun testToString() {
val s = HashSet(listOf("1", "2", "3"))
assertTrue(s.toString() == setOf("1", "2", "3").toString())
}
fun testContainsAll() {
val s = HashSet(listOf("1", "2", "3", "4", "5"))
assertFalse(s.containsAll(listOf("6", "7", "8")))
assertFalse(s.containsAll(listOf("5", "6", "7")))
assertFalse(s.containsAll(listOf("4", "5", "6")))
assertTrue(s.containsAll(listOf("3", "4", "5")))
assertTrue(s.containsAll(listOf("2", "3", "4")))
}
fun testRemoveAll() {
val s = HashSet(listOf("1", "2", "3", "4", "5", "1"))
assertFalse(s.removeAll(listOf("6", "7", "8")))
assertEquals(setOf("1", "2", "3", "4", "5", "1"), s)
assertTrue(s.removeAll(listOf("5", "3", "1")))
assertEquals(setOf("2", "4"), s)
}
fun testRetainAll() {
val s = HashSet(listOf("1", "2", "3", "4", "5"))
assertFalse(s.retainAll(listOf("1", "2", "3", "4", "5")))
assertEquals(setOf("1", "2", "3", "4", "5"), s)
assertTrue(s.retainAll(listOf("5", "3", "1")))
assertEquals(setOf("1", "3", "5"), s)
}
@Test fun runTest() {
testBasic()
testIterator()
testEquals()
testHashCode()
testToString()
testContainsAll()
testRemoveAll()
testRetainAll()
println("OK")
}
@@ -1,24 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.listof0
import kotlin.test.*
@Test fun runTest() {
main(arrayOf("a"))
}
fun main(args : Array<String>) {
val nonConstStr = args[0]
val list = arrayListOf(nonConstStr, "b", "c")
for (element in list) print(element)
println()
list.add("d")
println(list.toString())
val list2 = listOf("n", "s", nonConstStr)
println(list2.toString())
}
@@ -1,3 +0,0 @@
abc
[a, b, c, d]
[n, s, a]
@@ -1,20 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.moderately_large_array
import kotlin.test.*
@Test fun runTest() {
val a = ByteArray(1000000)
var sum = 0
for (b in a) {
sum += b
}
println(sum)
}
@@ -1,20 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.moderately_large_array1
import kotlin.test.*
@Test fun runTest() {
val a = Array<Byte>(100000, { i -> i.toByte()})
var sum = 0
for (b in a) {
sum += b
}
println(sum)
}
@@ -1,15 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.range0
import kotlin.test.*
@Test fun runTest() {
for (i in 1..3) print(i)
println()
for (i in 'a'..'d') print(i)
println()
}
@@ -1,2 +0,0 @@
123
abcd
@@ -1,13 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.sort0
import kotlin.test.*
@Test fun runTest() {
println(arrayOf("x", "a", "b").sorted().toString())
println(intArrayOf(239, 42, -1, 100500, 0).sorted().toString());
}
@@ -1,2 +0,0 @@
[a, b, x]
[-1, 0, 42, 239, 100500]
@@ -1,18 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.sort1
import kotlin.test.*
@Test fun runTest() {
val foo = mutableListOf("x", "a", "b")
foo.sort()
println(foo.toString())
var bar = mutableListOf(239, 42, -1, 100500, 0)
bar.sort()
println(bar.toString())
}
@@ -1,2 +0,0 @@
[a, b, x]
[-1, 0, 42, 239, 100500]
@@ -1,17 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.collections.stack_array
import kotlin.test.*
@Test fun runTest() {
val array = IntArray(2)
array[0] = 1
array[1] = 2
val check = array is IntArray
println(check)
println(array[0] + array[1])
}
@@ -1,31 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class)
package runtime.collections.typed_array0
import kotlin.test.*
@Test fun runTest() {
// Those tests assume little endian bit ordering.
val array = ByteArray(42)
array.setLongAt(5, 0x1234_5678_9abc_def0)
expect(0x1234_5678_9abc_def0) { array.getLongAt(5) }
expect(0xdef0.toInt()) { array.getCharAt(5).toInt() }
expect(0x9abc.toShort()) { array.getShortAt(7) }
expect(0x1234_5678) { array.getIntAt(9) }
expect(0xdef0_0000u) { array.getUIntAt(3) }
expect(0xf0_00u) { array.getUShortAt(4) }
expect(0xf0u) { array.getUByteAt(5) }
expect(0x1234_5678_9abcuL) { array.getULongAt(7) }
array.setIntAt(2, 0x40100000)
expect(2.25f) { array.getFloatAt(2) }
array.setLongAt(11, 0x400c_0000_0000_0000)
expect(3.5) { array.getDoubleAt(11) }
println("OK")
}
@@ -1,76 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, FreezingIsDeprecated::class)
package runtime.collections.typed_array1
import kotlin.test.*
import kotlin.native.concurrent.*
@Test fun runTest() {
val array = ByteArray(17)
val results = mutableSetOf<Any>()
var counter = 0
assertFailsWith<IndexOutOfBoundsException> {
results += array.getShortAt(16)
}
assertFailsWith<IndexOutOfBoundsException> {
results += array.getCharAt(22)
}
assertFailsWith<IndexOutOfBoundsException> {
results += array.getIntAt(15)
}
assertFailsWith<IndexOutOfBoundsException> {
results += array.getLongAt(14)
}
assertFailsWith<IndexOutOfBoundsException> {
results += array.getFloatAt(14)
}
assertFailsWith<IndexOutOfBoundsException> {
results += array.getDoubleAt(13)
}
assertFailsWith<IndexOutOfBoundsException> {
array.setShortAt(16, 2.toShort())
}
assertFailsWith<IndexOutOfBoundsException> {
array.setCharAt(22, 'a')
}
assertFailsWith<IndexOutOfBoundsException> {
array.setIntAt(15, 1234)
}
assertFailsWith<IndexOutOfBoundsException> {
array.setLongAt(14, 1.toLong())
}
assertFailsWith<IndexOutOfBoundsException> {
array.setFloatAt(14, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
array.setDoubleAt(13, 3.0)
}
expect(0) { results.size }
if (Platform.isFreezingEnabled) {
array.freeze()
assertFailsWith<InvalidMutabilityException> {
array.setShortAt(0, 2.toShort())
}
assertFailsWith<InvalidMutabilityException> {
array.setCharAt(0, 'a')
}
assertFailsWith<InvalidMutabilityException> {
array.setIntAt(0, 2)
}
assertFailsWith<InvalidMutabilityException> {
array.setLongAt(0, 2)
}
assertFailsWith<InvalidMutabilityException> {
array.setFloatAt(0, 1.0f)
}
assertFailsWith<InvalidMutabilityException> {
array.setDoubleAt(0, 1.0)
}
}
println("OK")
}
@@ -0,0 +1,97 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
import test.collections.behaviors.*
class AbstractMutableCollectionTest {
private class TestCollection(private val storage: IntArray): AbstractMutableCollection<Int>() {
private var len = 0
override val size: Int
get() = len
override fun add(element: Int): Boolean {
if (len >= storage.size) return false
storage[len++] = element
return true
}
override fun iterator(): MutableIterator<Int> = object: MutableIterator<Int> {
var nextIndex = 0
override fun hasNext() = nextIndex < len
override fun next() = storage[nextIndex++]
override fun remove() {
if (nextIndex == 0) throw IllegalStateException()
for (i in nextIndex..len - 1) {
storage[i - 1] = storage[i]
}
len--
nextIndex--
}
}
override fun clear() {
len = 0
}
}
@Test fun addAllSuccess() {
val collection = TestCollection(IntArray(3))
assertTrue(collection.addAll(listOf(1, 2, 3)))
compare(listOf(1, 2, 3), collection) {
collectionBehavior()
}
}
@Test fun addAllFailure() {
val collection = TestCollection(IntArray(3))
assertTrue(collection.addAll(listOf(1, 2, 3)))
assertFalse(collection.addAll(listOf(4, 5)))
compare(listOf(1, 2, 3), collection) {
collectionBehavior()
}
}
@Test fun removeAll() {
val collection = TestCollection(IntArray(7))
assertTrue(collection.addAll(listOf(1, 2, 3, 2, 4, 5, 4)))
assertTrue(collection.removeAll(listOf(1, 2)))
compare(listOf(3, 4, 5, 4), collection) {
collectionBehavior()
}
}
@Test fun retainAll() {
val collection = TestCollection(IntArray(7))
assertTrue(collection.addAll(listOf(1, 2, 4, 3, 5, 2, 4)))
assertTrue(collection.retainAll(listOf(4, 5)))
compare(listOf(4, 5, 4), collection) {
collectionBehavior()
}
}
@Test fun remove() {
val collection = TestCollection(IntArray(3))
assertTrue(collection.addAll(listOf(1, 2, 3)))
assertTrue(collection.remove(2))
compare(listOf(1, 3), collection) {
collectionBehavior()
}
}
@Test fun clear() {
val collection = TestCollection(IntArray(3))
assertTrue(collection.addAll(listOf(1, 2, 3)))
collection.clear()
compare(emptyList(), collection) {
collectionBehavior()
}
}
}
@@ -0,0 +1,372 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.random.Random
import kotlin.test.*
// TODO: Check which parts are already tested in libraries/stdlib/test/collections/IterableTests.kt
class ArrayListNativeTest {
@Test fun basic() {
val a = ArrayList<String>()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
assertTrue(a.add("1"))
assertTrue(a.add("2"))
assertTrue(a.add("3"))
assertFalse(a.isEmpty())
assertEquals(3, a.size)
assertEquals("1", a[0])
assertEquals("2", a[1])
assertEquals("3", a[2])
a[0] = "11"
assertEquals("11", a[0])
assertEquals("11", a.removeAt(0))
assertEquals(2, a.size)
assertEquals("2", a[0])
assertEquals("3", a[1])
a.add(1, "22")
assertEquals(3, a.size)
assertEquals("2", a[0])
assertEquals("22", a[1])
assertEquals("3", a[2])
a.clear()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
}
@Test fun iterator() {
val a = ArrayList(listOf("1", "2", "3"))
val it = a.iterator()
assertTrue(it.hasNext())
assertEquals("1", it.next())
assertTrue(it.hasNext())
assertEquals("2", it.next())
assertTrue(it.hasNext())
assertEquals("3", it.next())
assertFalse(it.hasNext())
}
@Test fun containsAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
assertFalse(a.containsAll(listOf("6", "7", "8")))
assertFalse(a.containsAll(listOf("5", "6", "7")))
assertFalse(a.containsAll(listOf("4", "5", "6")))
assertTrue(a.containsAll(listOf("3", "4", "5")))
assertTrue(a.containsAll(listOf("2", "3", "4")))
}
@Test fun remove() {
val a = ArrayList(listOf("1", "2", "3"))
assertTrue(a.remove("2"))
assertEquals(2, a.size)
assertEquals("1", a[0])
assertEquals("3", a[1])
assertFalse(a.remove("2"))
assertEquals(2, a.size)
assertEquals("1", a[0])
assertEquals("3", a[1])
}
@Test fun removeAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "1"))
assertFalse(a.removeAll(listOf("6", "7", "8")))
assertEquals(listOf("1", "2", "3", "4", "5", "1"), a)
assertTrue(a.removeAll(listOf("5", "3", "1")))
assertEquals(listOf("2", "4"), a)
}
@Test fun retainAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
assertFalse(a.retainAll(listOf("1", "2", "3", "4", "5")))
assertEquals(listOf("1", "2", "3", "4", "5"), a)
assertTrue(a.retainAll(listOf("5", "3", "1")))
assertEquals(listOf("1", "3", "5"), a)
}
@Test fun equals() {
val a = ArrayList(listOf("1", "2", "3"))
assertTrue(a == listOf("1", "2", "3"))
assertFalse(a == listOf("2", "3", "1")) // order matters
assertFalse(a == listOf("1", "2", "4"))
assertFalse(a == listOf("1", "2"))
}
@Test fun hashCodeIdentity() {
val a = ArrayList(listOf("1", "2", "3"))
assertEquals(listOf("1", "2", "3").hashCode(), a.hashCode())
}
@Test fun toStringIdentity() {
val a = ArrayList(listOf("1", "2", "3"))
assertEquals(listOf("1", "2", "3").toString(), a.toString())
}
@Test fun subList() {
val a0 = ArrayList(listOf("0", "1", "2", "3", "4"))
val a = a0.subList(1, 4)
assertEquals(3, a.size)
assertEquals("1", a[0])
assertEquals("2", a[1])
assertEquals("3", a[2])
assertTrue(a == listOf("1", "2", "3"))
assertTrue(a.hashCode() == listOf("1", "2", "3").hashCode())
assertTrue(a.toString() == listOf("1", "2", "3").toString())
}
@Test fun resize() {
val a = ArrayList<String>()
val n = 10000
for (i in 1..n)
assertTrue(a.add(i.toString()))
assertEquals(n, a.size)
for (i in 1..n)
assertEquals(i.toString(), a[i - 1])
a.trimToSize()
assertEquals(n, a.size)
for (i in 1..n)
assertEquals(i.toString(), a[i - 1])
}
@Test fun subListContains() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertTrue(a.contains("1"))
assertFalse(s.contains("1"))
assertTrue(a.contains("2"))
assertTrue(s.contains("2"))
assertTrue(a.contains("3"))
assertTrue(s.contains("3"))
assertTrue(a.contains("4"))
assertFalse(s.contains("4"))
}
@Test fun subListIndexOf() {
val a = ArrayList(listOf("1", "2", "3", "4", "1"))
val s = a.subList(1, 3)
assertEquals(0, a.indexOf("1"))
assertEquals(-1, s.indexOf("1"))
assertEquals(1, a.indexOf("2"))
assertEquals(0, s.indexOf("2"))
assertEquals(2, a.indexOf("3"))
assertEquals(1, s.indexOf("3"))
assertEquals(3, a.indexOf("4"))
assertEquals(-1, s.indexOf("4"))
}
@Test fun subListLastIndexOf() {
val a = ArrayList(listOf("1", "2", "3", "4", "1"))
val s = a.subList(1, 3)
assertEquals(4, a.lastIndexOf("1"))
assertEquals(-1, s.lastIndexOf("1"))
assertEquals(1, a.lastIndexOf("2"))
assertEquals(0, s.lastIndexOf("2"))
assertEquals(2, a.lastIndexOf("3"))
assertEquals(1, s.lastIndexOf("3"))
assertEquals(3, a.lastIndexOf("4"))
assertEquals(-1, s.lastIndexOf("4"))
}
@Test fun subListClear() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertEquals(listOf("2", "3"), s)
s.clear()
assertEquals(listOf<String>(), s)
assertEquals(listOf("1", "4"), a)
}
@Test fun subListSubListClear() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6"))
val s = a.subList(1, 5)
val q = s.subList(1, 3)
assertEquals(listOf("2", "3", "4", "5"), s)
assertEquals(listOf("3", "4"), q)
q.clear()
assertEquals(listOf<String>(), q)
assertEquals(listOf("2", "5"), s)
assertEquals(listOf("1", "2", "5", "6"), a)
}
@Test fun subListAdd() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertEquals(listOf("2", "3"), s)
assertTrue(s.add("5"))
assertEquals(listOf("2", "3", "5"), s)
assertEquals(listOf("1", "2", "3", "5", "4"), a)
}
@Test fun subListSubListAdd() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6"))
val s = a.subList(1, 5)
val q = s.subList(1, 3)
assertEquals(listOf("2", "3", "4", "5"), s)
assertEquals(listOf("3", "4"), q)
assertTrue(q.add("7"))
assertEquals(listOf("3", "4", "7"), q)
assertEquals(listOf("2", "3", "4", "7", "5"), s)
assertEquals(listOf("1", "2", "3", "4", "7", "5", "6"), a)
}
@Test fun subListAddAll() {
val a = ArrayList(listOf("1", "2", "3", "4"))
val s = a.subList(1, 3)
assertEquals(listOf("2", "3"), s)
assertTrue(s.addAll(listOf("5", "6")))
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "4"), a)
}
@Test fun subListSubListAddAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6"))
val s = a.subList(1, 5)
val q = s.subList(1, 3)
assertEquals(listOf("2", "3", "4", "5"), s)
assertEquals(listOf("3", "4"), q)
assertTrue(q.addAll(listOf("7", "8")))
assertEquals(listOf("3", "4", "7", "8"), q)
assertEquals(listOf("2", "3", "4", "7", "8", "5"), s)
assertEquals(listOf("1", "2", "3", "4", "7", "8", "5", "6"), a)
}
@Test fun subListRemoveAt() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val s = a.subList(1, 4)
assertEquals(listOf("2", "3", "4"), s)
assertEquals("3", s.removeAt(1))
assertEquals(listOf("2", "4"), s)
assertEquals(listOf("1", "2", "4", "5"), a)
}
@Test fun subListSubListRemoveAt() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6", "7"))
val s = a.subList(1, 6)
val q = s.subList(1, 4)
assertEquals(listOf("2", "3", "4", "5", "6"), s)
assertEquals(listOf("3", "4", "5"), q)
assertEquals("4", q.removeAt(1))
assertEquals(listOf("3", "5"), q)
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "7"), a)
}
@Test fun subListRemoveAll() {
val a = ArrayList(listOf("1", "2", "3", "3", "4", "5"))
val s = a.subList(1, 5)
assertEquals(listOf("2", "3", "3", "4"), s)
assertTrue(s.removeAll(listOf("3", "5")))
assertEquals(listOf("2", "4"), s)
assertEquals(listOf("1", "2", "4", "5"), a)
}
@Test fun subListSubListRemoveAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6", "7"))
val s = a.subList(1, 6)
val q = s.subList(1, 4)
assertEquals(listOf("2", "3", "4", "5", "6"), s)
assertEquals(listOf("3", "4", "5"), q)
assertTrue(q.removeAll(listOf("4", "6")))
assertEquals(listOf("3", "5"), q)
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "7"), a)
}
@Test fun subListRetainAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val s = a.subList(1, 4)
assertEquals(listOf("2", "3", "4"), s)
assertTrue(s.retainAll(listOf("5", "3")))
assertEquals(listOf("3"), s)
assertEquals(listOf("1", "3", "5"), a)
}
@Test fun subListSubListRetainAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5", "6", "7"))
val s = a.subList(1, 6)
val q = s.subList(1, 4)
assertEquals(listOf("2", "3", "4", "5", "6"), s)
assertEquals(listOf("3", "4", "5"), q)
assertTrue(q.retainAll(listOf("5", "3")))
assertEquals(listOf("3", "5"), q)
assertEquals(listOf("2", "3", "5", "6"), s)
assertEquals(listOf("1", "2", "3", "5", "6", "7"), a)
}
@Test fun iteratorRemove() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val it = a.iterator()
while (it.hasNext())
if (it.next()[0].toInt() % 2 == 0)
it.remove()
assertEquals(listOf("1", "3", "5"), a)
}
@Test fun iteratorAdd() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val it = a.listIterator()
while (it.hasNext()) {
val next = it.next()
if (next[0].toInt() % 2 == 0)
it.add("-" + next)
}
assertEquals(listOf("1", "2", "-2", "3", "4", "-4", "5"), a)
}
@Test fun iteratorNext() {
val a = arrayListOf("1", "2", "3", "4", "5")
val it = a.listIterator()
assertFailsWith<NoSuchElementException> {
while (true) {
it.next()
}
}
}
@Test fun iteratorPrevious() {
val a = arrayListOf("1", "2", "3", "4", "5")
val it = a.listIterator()
it.next()
assertFailsWith<NoSuchElementException> {
while (true) {
it.previous()
}
}
}
@Test fun factory() {
val nonConstStr = Random.nextInt().toString()
val list = arrayListOf(nonConstStr, "b", "c")
assertEquals(3, list.size)
assertEquals(nonConstStr, list[0])
assertEquals("b", list[1])
assertEquals("c", list[2])
list.add("d")
assertEquals(4, list.size)
assertEquals(nonConstStr, list[0])
assertEquals("b", list[1])
assertEquals("c", list[2])
assertEquals("d", list[3])
}
}
@@ -0,0 +1,384 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
// Native-specific part of stdlib/test/collections/ArraysTest.kt
class ArraysNativeTest {
@Test fun byteArraySet() {
val arr = ByteArray(5)
arr[1] = 1
arr[3] = -1
assertEquals(1, arr[1])
assertEquals(-1, arr[3])
}
@Test fun shortArraySet() {
val arr = ShortArray(5)
arr[1] = 1
arr[3] = -1
assertEquals(1, arr[1])
assertEquals(-1, arr[3])
}
@Test fun intArraySet() {
val arr = IntArray(5)
arr[1] = 1
arr[3] = -1
assertEquals(1, arr[1])
assertEquals(-1, arr[3])
}
@Test fun longArraySet() {
val arr = LongArray(5)
arr[1] = 1
arr[3] = -1
assertEquals(1, arr[1])
assertEquals(-1, arr[3])
}
@Test fun arrayGetOutOfBounds() {
val arr = Array<Any?>(5) { null }
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun byteArrayGetOutOfBounds() {
val arr = ByteArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun shortArrayGetOutOfBounds() {
val arr = ShortArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun intArrayGetOutOfBounds() {
val arr = IntArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun floatArrayGetOutOfBounds() {
val arr = FloatArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun doubleArrayGetOutOfBounds() {
val arr = DoubleArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun charArrayGetOutOfBounds() {
val arr = CharArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun arraySetOutOfBounds() {
val arr = Array<Any?>(5) { null }
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = Any()
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = Any()
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = Any()
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = Any()
}
}
@Test fun byteArraySetOutOfBounds() {
val arr = ByteArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun shortArraySetOutOfBounds() {
val arr = ShortArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun intArraySetOutOfBounds() {
val arr = IntArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1
}
}
@Test fun floatArraySetOutOfBounds() {
val arr = FloatArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1.0f
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1.0f
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1.0f
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1.0f
}
}
@Test fun doubleArraySetOutOfBounds() {
val arr = DoubleArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1.0
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1.0
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1.0
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1.0
}
}
@Test fun charArraySetOutOfBounds() {
val arr = CharArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = '1'
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = '1'
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = '1'
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = '1'
}
}
@Test fun largeByteArray() {
val arr = ByteArray(1000000) { it.toByte() }
var sum = 0
for (b in arr) {
sum += b
}
assertEquals(-497952, sum)
}
@Test fun largeArray() {
val arr = Array<Byte>(1000000) { it.toByte() }
var sum = 0
for (b in arr) {
sum += b
}
assertEquals(-497952, sum)
}
private fun Array<Int>.assertSorted(cmp: Comparator<Int>, message: String = "") {
for (i in 1 until size) {
assertTrue(cmp.compare(this[i - 1], this[i]) <= 0, message)
}
}
private fun Array<MyComparable>.assertSorted(message: String = "") {
for (i in 1 until size) {
assertTrue(this[i - 1] <= this[i], message)
}
}
private class MyComparable (val value: Int, val comparator: Comparator<Int>): Comparable<MyComparable> {
override fun compareTo(other: MyComparable): Int = comparator.compare(value, other.value)
}
@Test
fun sortWith() {
listOf<Array<Int>>(
arrayOf(),
arrayOf(1),
arrayOf(Int.MIN_VALUE, 0, Int.MAX_VALUE),
arrayOf(Int.MAX_VALUE, 0, Int.MIN_VALUE),
arrayOf(1, 2, 3),
arrayOf(-2, -1, 99, 1, 2),
arrayOf(90, 91, 0, 98, 99),
arrayOf(2, 1, 99, -1, 2),
arrayOf(99, 98, 0, 91, 90),
arrayOf(42, 42, 42),
arrayOf(99, 42, 0, 42, 50),
arrayOf(
100000, 190001, 200002, 200003, 200004, 210005, 220006, 250007, 300008, 310009, 360010, 365011,
380012, 390013, 390014, 399015, 400016, 400017, 400018, 400019, 400020, 400021, 400022, 400023,
400024, 400025, 400026, 450027, 450028, 480029, 480030, 500031, 500032, 500033, 500034, 500035,
500036, 500037, 500038, 500039, 500040, 500041, 500042, 500043, 500044, 500045, 500046, 500047,
500048, 500049, 500050, 500051, 500052, 500053, 505054, 510055, 510056, 510057, 510058, 510059,
510060, 510061, 510062, 510063, 410064, 410065, 511066, 511067, 520068, 520069, 420070, 520071,
530072, 530073, 530074, 430075, 430076, 530077, 540078, 540079, 540080, 540081, 540082, 540083,
540084, 490085, 540086, 540087, 542088, 544089, 546090, 550091, 550092, 550093, 550094, 590095,
590096, 595097, 600098, 600099, 600100, 600101, 600102, 600103, 600104, 550105, 600106, 600107,
600108, 600109, 600110, 610111, 610112, 610113, 620114, 620115, 620116, 620117, 620118, 620119,
640120, 640121, 645122, 645123, 645124, 645125, 645126, 645127, 650128, 700129, 700130
)
).forEach { array ->
data class ComparatorInfo(val name: String, val isCorrect: Boolean, val comparator: Comparator<Int>)
// Assert that the array is sorted in terms of a comparator only for correct/partially correct cases
listOf<ComparatorInfo>(
ComparatorInfo("Correct increasing", true) { a, b ->
when {
a > b -> 1
a < b -> -1
else -> 0
}
},
ComparatorInfo("Correct decreasing", true) { a, b ->
when {
a < b -> 1
a > b -> -1
else -> 0
}
},
ComparatorInfo("Incorrect increasing", false) { a, b -> if (a > b) 1 else -1 },
ComparatorInfo("Incorrect decreasing", false) { a, b -> if (a < b) 1 else -1 },
ComparatorInfo("Always 1", false) { a, b -> 1 },
ComparatorInfo("Always -1", false) { a, b -> -1 },
ComparatorInfo("Always 0", false) { a, b -> 0 },
).forEach { (name, isCorrect, comparator) ->
// Test with custom comparator
val arrayUnderTest = array.copyOf()
arrayUnderTest.sortWith(comparator)
if (isCorrect) {
arrayUnderTest.assertSorted(comparator, """
Assert sorted failed for comparator: "${name}"
Array: ${array.joinToString()}
Array after sorting: ${arrayUnderTest.joinToString()}
""".trimIndent())
}
// Test of a custom comparable
val comparableArrayUnderTest = Array(array.size) { i ->
MyComparable(array[i], comparator)
}
comparableArrayUnderTest.sort()
if (isCorrect) {
comparableArrayUnderTest.assertSorted("""
Assert sorted failed for Comparable: "${name}"
Array: ${array.joinToString()}
Array after sorting: ${comparableArrayUnderTest.joinToString()}
""".trimIndent())
}
}
}
}
}
@@ -0,0 +1,415 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
private fun assertContainsOnly(bitSet: BitSet, trueBits: Set<Int>, size: Int) {
for (i in 0 until size) {
assertEquals(i in trueBits, bitSet[i])
}
}
private fun assertNotContainsOnly(bitSet: BitSet, falseBits: Set<Int>, size: Int) {
for (i in 0 until size) {
assertNotEquals(i in falseBits, bitSet[i])
}
}
class BitSetTest {
@Test fun constructor() {
var b = BitSet(12)
assertContainsOnly(b, setOf(), 12)
b = BitSet(12) { it == 0 || it in 5..6 || it == 11 }
assertContainsOnly(b, setOf(0, 5, 6, 11), 12)
}
@Test fun set() {
var b = BitSet(0)
assertEquals(b.lastTrueIndex, -1)
b = BitSet(2)
// Test set and clear operation for single index.
assertTrue(b.isEmpty)
b.set(0, true)
b.set(3, true)
assertEquals(b.lastTrueIndex, 3)
assertFalse(b.isEmpty)
assertContainsOnly(b, setOf(0, 3), 4)
b.clear()
assertContainsOnly(b, setOf(), 4)
b.set(1)
b.set(5)
assertEquals(b.lastTrueIndex, 5)
assertContainsOnly(b, setOf(1, 5), 6)
b.set(1, false)
b.set(7, false)
assertEquals(b.lastTrueIndex, 5)
assertContainsOnly(b, setOf(5), 8)
b.clear(5)
assertEquals(b.lastTrueIndex, -1)
assertContainsOnly(b, setOf(), 8)
b.set(70)
assertEquals(b.lastTrueIndex, 70)
assertContainsOnly(b, setOf(70), 71)
b.clear(70)
assertEquals(b.lastTrueIndex, -1)
assertContainsOnly(b, setOf(), 71)
// Test set and clear operations for ranges.
// Set false and clear.
b = BitSet(2)
assertContainsOnly(b, setOf(), 2)
b.set(0..70, true)
assertNotContainsOnly(b, setOf(), 71)
b.set(0..2, false)
assertNotContainsOnly(b, setOf(0, 1, 2), 71)
b.set(63..65, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 71)
b.set(68..70, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 71)
b.set(68..72, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.set(0..72, false)
assertContainsOnly(b, setOf(), 73)
b.set(0..72)
assertNotContainsOnly(b, setOf(), 73)
b.clear(0..2)
assertNotContainsOnly(b, setOf(0, 1, 2), 73)
b.clear(63..65)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.clear(68..70)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 73)
b.clear(68..72)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.clear(0..72)
assertContainsOnly(b, setOf(), 73)
// Set true.
b.set(0..2, true)
assertContainsOnly(b, setOf(0, 1, 2), 73)
b.set(63..65, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.set(70..72, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72), 73)
b.set(73..74, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72, 73, 74), 75)
b.set(0..74, true)
assertNotContainsOnly(b, setOf(), 75)
// Test set and clear for pair of indices.
b = BitSet(2)
assertContainsOnly(b, setOf(), 71)
b.set(0, 71, true)
assertNotContainsOnly(b, setOf(), 71)
b.set(0, 3, false)
assertNotContainsOnly(b, setOf(0, 1, 2), 71)
b.set(63, 66, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 71)
b.set(68, 71, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 71)
b.set(68, 73, false)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.set(0, 73, false)
assertContainsOnly(b, setOf(), 73)
b.set(0, 73)
assertNotContainsOnly(b, setOf(), 73)
b.clear(0, 3)
assertNotContainsOnly(b, setOf(0, 1, 2), 73)
b.clear(63, 66)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.clear(68, 71)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70), 73)
b.clear(68, 73)
assertNotContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 68, 69, 70, 71, 72), 73)
b.clear(0, 73)
assertContainsOnly(b, setOf(), 73)
// Set true.
b.set(0, 3, true)
assertContainsOnly(b, setOf(0, 1, 2), 73)
b.set(63, 66, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65), 73)
b.set(70, 73, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72), 73)
b.set(73, 75, true)
assertContainsOnly(b, setOf(0, 1, 2, 63, 64, 65, 70, 71, 72, 73, 74), 75)
b.set(0, 75, true)
assertNotContainsOnly(b, setOf(), 75)
// Access to negative elements must cause an exception
assertFailsWith<IndexOutOfBoundsException> {
b.set(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
b.clear(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
b.clear(-1..0)
}
assertFailsWith<IndexOutOfBoundsException> {
b.set(-1..0)
}
assertFailsWith<IndexOutOfBoundsException> {
b[-1]
}
}
@Test fun flip() {
val b = BitSet(2)
b.set(0, true)
b.set(70, true)
b.set(63..65, true)
assertEquals(b.lastTrueIndex, 70)
// 0 element
assertContainsOnly(b, setOf(0, 63, 64, 65, 70), 71)
b.flip(0)
assertContainsOnly(b, setOf(63, 64, 65, 70), 71)
b.flip(1)
assertContainsOnly(b, setOf(1, 63, 64, 65, 70), 71)
b.flip(0)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 70), 71)
// last element
b.flip(70)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65), 71)
b.flip(69)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69), 71)
b.flip(70)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// element in the middle
b.flip(64)
assertContainsOnly(b, setOf(0, 1, 63, 65, 69, 70), 71)
b.flip(65)
assertContainsOnly(b, setOf(0, 1, 63, 69, 70), 71)
b.flip(65)
b.flip(64)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// range in the beginning
b.flip(0..2)
assertContainsOnly(b, setOf(2, 63, 64, 65, 69, 70), 71)
b.flip(0, 3)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// In the end
b.flip(68..70)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 68), 71)
b.flip(68, 71)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// In the middle
b.flip(64..66)
assertContainsOnly(b, setOf(0, 1, 63, 66, 69, 70), 71)
b.flip(64, 67)
assertContainsOnly(b, setOf(0, 1, 63, 64, 65, 69, 70), 71)
// Access to a negative element must cause an exception.
assertFailsWith<IndexOutOfBoundsException> {
b.flip(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
b.flip(-1..0)
}
}
@Test fun nextBit() {
val b = BitSet(71)
b.set(0)
b.set(65)
b.set(70)
assertEquals(b.nextSetBit(), 0)
assertEquals(b.nextSetBit(0), 0)
assertEquals(b.nextSetBit(1), 65)
assertEquals(b.nextSetBit(65), 65)
assertEquals(b.nextSetBit(66), 70)
assertEquals(b.nextSetBit(70), 70)
assertEquals(b.nextSetBit(71), -1)
assertEquals(b.previousSetBit(0), 0)
assertEquals(b.previousSetBit(64), 0)
assertEquals(b.previousSetBit(65), 65)
assertEquals(b.previousSetBit(69), 65)
assertEquals(b.previousSetBit(70), 70)
assertEquals(b.previousSetBit(71), 70)
b.clear()
assertEquals(b.nextSetBit(), -1)
assertEquals(b.previousSetBit(70), -1)
b.set(0..70)
assertEquals(b.nextClearBit(), 71)
assertEquals(b.previousClearBit(70), -1)
b.clear(0)
b.clear(65)
b.clear(70)
assertEquals(b.nextClearBit(), 0)
assertEquals(b.nextClearBit(0), 0)
assertEquals(b.nextClearBit(1), 65)
assertEquals(b.nextClearBit(65), 65)
assertEquals(b.nextClearBit(66), 70)
assertEquals(b.nextClearBit(70), 70)
assertEquals(b.nextClearBit(71), 71) // assume that the bitset is extended here (virtually).
assertEquals(b.previousClearBit(0), 0)
assertEquals(b.previousClearBit(64), 0)
assertEquals(b.previousClearBit(65), 65)
assertEquals(b.previousClearBit(69), 65)
assertEquals(b.previousClearBit(70), 70)
assertEquals(b.previousClearBit(71), 71)
// See http://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html#previousClearBit-int-
assertEquals(b.previousClearBit(-1), -1)
assertEquals(b.previousSetBit(-1), -1)
// Test behaviour on the right border of the bit vector.
// We assume that the vector is infinite and have zeros after (size - 1)th bit.
var a = BitSet(64)
assertEquals(a.nextClearBit(63), 63)
assertEquals(a.nextClearBit(64), 64)
assertEquals(a.nextSetBit(63), -1)
assertEquals(a.nextSetBit(64), -1)
a.set(0, 64)
assertEquals(a.nextClearBit(63), 64)
assertEquals(a.nextClearBit(64), 64)
assertEquals(a.nextSetBit(63), 63)
assertEquals(a.nextSetBit(64), -1)
a.clear()
assertEquals(a.previousClearBit(63), 63)
assertEquals(a.previousClearBit(64), 64)
assertEquals(a.previousSetBit(63), -1)
assertEquals(a.previousSetBit(64), -1)
a.set(0, 64)
assertEquals(a.previousClearBit(63), -1)
assertEquals(a.previousClearBit(64), 64)
assertEquals(a.previousSetBit(63), 63)
assertEquals(a.previousSetBit(64), 63)
a = BitSet(0)
assertEquals(a.nextSetBit(0), -1)
assertEquals(a.nextClearBit(0), 0)
assertEquals(a.previousSetBit(0), -1)
assertEquals(a.previousClearBit(0), 0)
// Access to a negative element must cause an exception.
assertFailsWith<IndexOutOfBoundsException> {
b.previousSetBit(-2)
}
assertFailsWith<IndexOutOfBoundsException> {
b.previousClearBit(-2)
}
assertFailsWith<IndexOutOfBoundsException> {
b.nextSetBit(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
b.nextClearBit(-1)
}
}
@Test fun logic() {
var b2 = BitSet(76)
intArrayOf(1, 3, 61, 63, 65, 67, 70, 72).forEach {
b2.set(it)
}
// and
var b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.and(b2)
assertContainsOnly(b1, setOf(3, 63, 67, 72), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.and(b2)
assertContainsOnly(b1, setOf(3, 63, 67, 72), 129)
// or
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.or(b2)
assertContainsOnly(b1, setOf(1, 2, 3, 61, 62, 63, 65, 66, 67, 70, 71, 72), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.or(b2)
assertContainsOnly(b1, setOf(1, 2, 3, 61, 62, 63, 65, 66, 67, 70, 71, 72, 128), 129)
// xor
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.xor(b2)
assertContainsOnly(b1, setOf(1, 2, 61, 62, 65, 66, 70, 71), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.xor(b2)
assertContainsOnly(b1, setOf(1, 2, 61, 62, 65, 66, 70, 71, 128), 129)
// andNot
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.andNot(b2)
assertContainsOnly(b1, setOf(2, 62, 66, 71), 76)
b1 = BitSet(73)
b1.set(2..3); b1.set(62..63); b1.set(66..67); b1.set(71..72)
b1.set(128)
b1.andNot(b2)
assertContainsOnly(b1, setOf(2, 62, 66, 71, 128), 129)
// intersects
b1 = BitSet(73)
b1.set(0..1); b1.set(62..63); b1.set(64..65); b1.set(71..72)
b2.clear(); b2.set(0)
assertTrue(b1.intersects(b2))
b2.clear(); b2.set(62..65)
assertTrue(b1.intersects(b2))
b2.clear(); b2.set(72)
assertTrue(b1.intersects(b2))
b2.clear()
assertFalse(b1.intersects(b2))
b2.set(128)
assertFalse(b1.intersects(b2))
}
// Based on Harmony tests.
@Test fun equalsHashCode() {
// HashCode.
val b = BitSet()
b.set(0..7)
b.clear(2)
b.clear(6)
assertEquals(1129, b.hashCode())
b.set(10)
b.clear(3)
assertEquals(97, b.hashCode())
// Equals.
val b1 = BitSet()
val b2 = BitSet()
b1.set(0..7)
b2.set(0..7)
assertTrue(b1 == b1)
assertTrue(b1 == b2)
b2.clear(6)
assertFalse(b1 == b2)
val b3 = BitSet()
b3.set(0..7)
b3.set(128)
assertFalse(b1 == b3)
b3.clear(128)
assertTrue(b1 == b3)
}
}
@@ -0,0 +1,444 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
class ByteArrayTest {
@Test fun getUByteOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getUByteAt(0))
assertEquals(0U, arr.getUByteAt(arr.size - 1))
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUByteAt(Int.MIN_VALUE)
}
}
@Test fun setUByteOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setUByteAt(0, 1U)
arr.setUByteAt(arr.size - 1, 1U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(arr.size, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUByteAt(Int.MIN_VALUE, 1U)
}
}
@Test fun getCharOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0.toChar(), arr.getCharAt(0))
assertEquals(0.toChar(), arr.getCharAt(8))
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(arr.size - 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getCharAt(Int.MIN_VALUE)
}
}
@Test fun setCharOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setCharAt(0, '1')
arr.setCharAt(8, '1')
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(arr.size - 1, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(arr.size, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(-1, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(Int.MAX_VALUE, '1')
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setCharAt(Int.MIN_VALUE, '1')
}
}
@Test fun getShortOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0, arr.getShortAt(0))
assertEquals(0, arr.getShortAt(8))
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(arr.size - 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getShortAt(Int.MIN_VALUE)
}
}
@Test fun setShortOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setShortAt(0, 0)
arr.setShortAt(8, 0)
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(arr.size - 1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(arr.size, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(Int.MAX_VALUE, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setShortAt(Int.MIN_VALUE, 1)
}
}
@Test fun getUShortOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getUShortAt(0))
assertEquals(0U, arr.getUShortAt(8))
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(arr.size - 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUShortAt(Int.MIN_VALUE)
}
}
@Test fun setUShortOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setUShortAt(0, 0U)
arr.setUShortAt(8, 0U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(arr.size - 1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(arr.size, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUShortAt(Int.MIN_VALUE, 1U)
}
}
@Test fun getIntOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0, arr.getIntAt(0))
assertEquals(0, arr.getIntAt(6))
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(7)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getIntAt(Int.MIN_VALUE)
}
}
@Test fun setIntOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setIntAt(0, 1)
arr.setIntAt(6, 1)
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(7, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(arr.size, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(Int.MAX_VALUE, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setIntAt(Int.MIN_VALUE, 1)
}
}
@Test fun getUIntOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getUIntAt(0))
assertEquals(0U, arr.getUIntAt(6))
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(7)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getUIntAt(Int.MIN_VALUE)
}
}
@Test fun setUIntOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setUIntAt(0, 1U)
arr.setUIntAt(6, 1U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(7, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(arr.size, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setUIntAt(Int.MIN_VALUE, 1U)
}
}
@Test fun getLongOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0, arr.getLongAt(0))
assertEquals(0, arr.getLongAt(2))
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(3)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getLongAt(Int.MIN_VALUE)
}
}
@Test fun setLongOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setLongAt(0, 1)
arr.setLongAt(2, 1)
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(3, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(arr.size, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(Int.MAX_VALUE, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setLongAt(Int.MIN_VALUE, 1)
}
}
@Test fun getULongOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0U, arr.getULongAt(0))
assertEquals(0U, arr.getULongAt(2))
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(3)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getULongAt(Int.MIN_VALUE)
}
}
@Test fun setULongOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setULongAt(0, 1U)
arr.setULongAt(2, 1U)
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(3, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(arr.size, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(-1, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(Int.MAX_VALUE, 1U)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setULongAt(Int.MIN_VALUE, 1U)
}
}
@Test fun getFloatOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0.0f, arr.getFloatAt(0))
assertEquals(0.0f, arr.getFloatAt(6))
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(7)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getFloatAt(Int.MIN_VALUE)
}
}
@Test fun setFloatOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setFloatAt(0, 1.0f)
arr.setFloatAt(6, 1.0f)
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(7, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(arr.size, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(-1, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(Int.MAX_VALUE, 1.0f)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setFloatAt(Int.MIN_VALUE, 1.0f)
}
}
@Test fun getDoubleOutOfBounds() {
val arr = ByteArray(10) { 0 }
assertEquals(0.0, arr.getDoubleAt(0))
assertEquals(0.0, arr.getDoubleAt(2))
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(3)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(arr.size)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.getDoubleAt(Int.MIN_VALUE)
}
}
@Test fun setDoubleOutOfBounds() {
val arr = ByteArray(10) { 0 }
arr.setDoubleAt(0, 1.0)
arr.setDoubleAt(2, 1.0)
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(3, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(arr.size, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(-1, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(Int.MAX_VALUE, 1.0)
}
assertFailsWith<IndexOutOfBoundsException> {
arr.setDoubleAt(Int.MIN_VALUE, 1.0)
}
}
@Test fun smoke() {
// These tests assume little endian bit ordering.
val array = ByteArray(42)
array.setLongAt(5, 0x1234_5678_9abc_def0)
expect(0x1234_5678_9abc_def0) { array.getLongAt(5) }
expect(0xdef0.toInt()) { array.getCharAt(5).toInt() }
expect(0x9abc.toShort()) { array.getShortAt(7) }
expect(0x1234_5678) { array.getIntAt(9) }
expect(0xdef0_0000u) { array.getUIntAt(3) }
expect(0xf0_00u) { array.getUShortAt(4) }
expect(0xf0u) { array.getUByteAt(5) }
expect(0x1234_5678_9abcuL) { array.getULongAt(7) }
array.setIntAt(2, 0x40100000)
expect(2.25f) { array.getFloatAt(2) }
array.setLongAt(11, 0x400c_0000_0000_0000)
expect(3.5) { array.getDoubleAt(11) }
}
}
@@ -0,0 +1,237 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
// TODO: Check which parts are already tested in libraries/stdlib/test/collections/MapTest.kt
class HashMapTest {
@Test fun basic() {
val m = HashMap<String, String>()
assertTrue(m.isEmpty())
assertEquals(0, m.size)
assertFalse(m.containsKey("1"))
assertFalse(m.containsValue("a"))
assertEquals(null, m.get("1"))
assertEquals(null, m.put("1", "a"))
assertTrue(m.containsKey("1"))
assertTrue(m.containsValue("a"))
assertEquals("a", m.get("1"))
assertFalse(m.isEmpty())
assertEquals(1, m.size)
assertFalse(m.containsKey("2"))
assertFalse(m.containsValue("b"))
assertEquals(null, m.get("2"))
assertEquals(null, m.put("2", "b"))
assertTrue(m.containsKey("1"))
assertTrue(m.containsValue("a"))
assertEquals("a", m.get("1"))
assertTrue(m.containsKey("2"))
assertTrue(m.containsValue("b"))
assertEquals("b", m.get("2"))
assertFalse(m.isEmpty())
assertEquals(2, m.size)
assertEquals("b", m.put("2", "bb"))
assertTrue(m.containsKey("1"))
assertTrue(m.containsValue("a"))
assertEquals("a", m.get("1"))
assertTrue(m.containsKey("2"))
assertTrue(m.containsValue("a"))
assertTrue(m.containsValue("bb"))
assertEquals("bb", m.get("2"))
assertFalse(m.isEmpty())
assertEquals(2, m.size)
assertEquals("a", m.remove("1"))
assertFalse(m.containsKey("1"))
assertFalse(m.containsValue("a"))
assertEquals(null, m.get("1"))
assertTrue(m.containsKey("2"))
assertTrue(m.containsValue("bb"))
assertEquals("bb", m.get("2"))
assertFalse(m.isEmpty())
assertEquals(1, m.size)
assertEquals("bb", m.remove("2"))
assertFalse(m.containsKey("1"))
assertFalse(m.containsValue("a"))
assertEquals(null, m.get("1"))
assertFalse(m.containsKey("2"))
assertFalse(m.containsValue("bb"))
assertEquals(null, m.get("2"))
assertTrue(m.isEmpty())
assertEquals(0, m.size)
}
@Test fun equals() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertTrue(m == expected)
assertTrue(m == mapOf("b" to "2", "c" to "3", "a" to "1")) // order does not matter
assertFalse(m == mapOf("a" to "1", "b" to "2", "c" to "4"))
assertFalse(m == mapOf("a" to "1", "b" to "2", "c" to "5"))
assertFalse(m == mapOf("a" to "1", "b" to "2"))
assertEquals(m.keys, expected.keys)
assertEquals(m.values.toList(), expected.values.toList())
assertEquals(m.entries, expected.entries)
}
@Test fun hashCodeTest() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertEquals(expected.hashCode(), m.hashCode())
assertEquals(expected.entries.hashCode(), m.entries.hashCode())
assertEquals(expected.keys.hashCode(), m.keys.hashCode())
}
@Test fun toStringTest() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertEquals(expected.toString(), m.toString())
assertEquals(expected.entries.toString(), m.entries.toString())
assertEquals(expected.keys.toString(), m.keys.toString())
assertEquals(expected.values.toString(), m.values.toString())
}
@Test fun put() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
val e = expected.entries.iterator().next() as MutableMap.MutableEntry<String, String>
assertTrue(m.entries.contains(e))
assertTrue(m.entries.remove(e))
assertTrue(mapOf("b" to "2", "c" to "3") == m)
assertEquals(null, m.put(e.key, e.value))
assertTrue(expected == m)
assertEquals(e.value, m.put(e.key, e.value))
assertTrue(expected == m)
}
@Test fun removeAll() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.entries.removeAll(mapOf("a" to "2", "b" to "3", "c" to "4").entries))
assertEquals(expected, m)
assertTrue(m.entries.removeAll(mapOf("b" to "22", "c" to "3", "d" to "4").entries))
assertNotEquals(expected, m)
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
@Test fun retainAll() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.entries.retainAll(expected.entries))
assertEquals(expected, m)
assertTrue(m.entries.retainAll(mapOf("b" to "22", "c" to "3", "d" to "4").entries))
assertEquals(mapOf("c" to "3"), m)
}
@Test fun containsAll() {
val m = HashMap(mapOf("a" to "1", "b" to "2", "c" to "3"))
assertTrue(m.values.containsAll(listOf("1", "2")))
assertTrue(m.values.containsAll(listOf("1", "2", "3")))
assertFalse(m.values.containsAll(listOf("1", "2", "3", "4")))
assertFalse(m.values.containsAll(listOf("2", "3", "4")))
}
@Test fun valuesRemove() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.remove("b"))
assertEquals(expected, m)
assertTrue(m.values.remove("2"))
assertEquals(mapOf("a" to "1", "c" to "3"), m)
}
@Test fun valuesRemoveAll() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.removeAll(listOf("b", "c")))
assertEquals(expected, m)
assertTrue(m.values.removeAll(listOf("b", "3")))
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
@Test fun valuesRetainAll() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.retainAll(listOf("1", "2", "3")))
assertEquals(expected, m)
assertTrue(m.values.retainAll(listOf("1", "2", "c")))
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
@Test fun iterator() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
val it = m.iterator()
while (it.hasNext()) {
val entry = it.next()
entry.setValue(entry.value + "!")
}
assertNotEquals(expected, m)
assertEquals(mapOf("a" to "1!", "b" to "2!", "c" to "3!"), m)
}
// TODO: Is it too slow with aggressive GC?
@Test fun rehashAndCompact() {
val m = HashMap<String, String>()
for (repeat in 1..10) {
val n = when (repeat) {
1 -> 1000
2 -> 10000
3 -> 10
else -> 100000
}
for (i in 1..n) {
assertFalse(m.containsKey(i.toString()))
assertEquals(null, m.put(i.toString(), "val$i"))
assertTrue(m.containsKey(i.toString()))
assertEquals(i, m.size)
}
for (i in 1..n) {
assertTrue(m.containsKey(i.toString()))
}
for (i in 1..n) {
assertEquals("val$i", m.remove(i.toString()))
assertFalse(m.containsKey(i.toString()))
assertEquals(n - i, m.size)
}
assertTrue(m.isEmpty())
}
}
// TODO: Is it too slow with aggressive GC?
@Test fun clear() {
val m = HashMap<String, String>()
for (repeat in 1..10) {
val n = when (repeat) {
1 -> 1000
2 -> 10000
3 -> 10
else -> 100000
}
for (i in 1..n) {
assertFalse(m.containsKey(i.toString()))
assertEquals(null, m.put(i.toString(), "val$i"))
assertTrue(m.containsKey(i.toString()))
assertEquals(i, m.size)
}
for (i in 1..n) {
assertTrue(m.containsKey(i.toString()))
}
m.clear()
assertEquals(0, m.size)
for (i in 1..n) {
assertFalse(m.containsKey(i.toString()))
}
}
}
}
@@ -0,0 +1,111 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
// TODO: Check which parts are already tested in libraries/stdlib/test/collections
class HashSetTest {
@Test fun basic() {
val a = HashSet<String>()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
assertTrue(a.add("1"))
assertTrue(a.add("2"))
assertTrue(a.add("3"))
assertFalse(a.isEmpty())
assertEquals(3, a.size)
assertTrue(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertFalse(a.contains("4"))
assertTrue(a.remove("1"))
assertEquals(2, a.size)
assertFalse(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertFalse(a.contains("4"))
assertTrue(a.add("4"))
assertEquals(3, a.size)
assertFalse(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertTrue(a.contains("4"))
assertFalse(a.add("4"))
assertEquals(3, a.size)
assertFalse(a.contains("1"))
assertTrue(a.contains("2"))
assertTrue(a.contains("3"))
assertTrue(a.contains("4"))
a.clear()
assertTrue(a.isEmpty())
assertEquals(0, a.size)
assertFalse(a.contains("1"))
assertFalse(a.contains("2"))
assertFalse(a.contains("3"))
assertFalse(a.contains("4"))
}
@Test fun iterator() {
val s = HashSet(listOf("1", "2", "3"))
val it = s.iterator()
assertTrue(it.hasNext())
assertEquals("1", it.next())
assertTrue(it.hasNext())
assertEquals("2", it.next())
assertTrue(it.hasNext())
assertEquals("3", it.next())
assertFalse(it.hasNext())
}
@Test fun equals() {
val s = HashSet(listOf("1", "2", "3"))
assertTrue(s == setOf("1", "2", "3"))
assertTrue(s == setOf("2", "3", "1")) // order does not matter
assertFalse(s == setOf("1", "2", "4"))
assertFalse(s == setOf("1", "2"))
}
@Test fun hashCodeTest() {
val s = HashSet(listOf("1", "2", "3"))
assertTrue(s.hashCode() == setOf("1", "2", "3").hashCode())
}
@Test fun toStringTest() {
val s = HashSet(listOf("1", "2", "3"))
assertTrue(s.toString() == setOf("1", "2", "3").toString())
}
@Test fun containsAll() {
val s = HashSet(listOf("1", "2", "3", "4", "5"))
assertFalse(s.containsAll(listOf("6", "7", "8")))
assertFalse(s.containsAll(listOf("5", "6", "7")))
assertFalse(s.containsAll(listOf("4", "5", "6")))
assertTrue(s.containsAll(listOf("3", "4", "5")))
assertTrue(s.containsAll(listOf("2", "3", "4")))
}
@Test fun removeAll() {
val s = HashSet(listOf("1", "2", "3", "4", "5", "1"))
assertFalse(s.removeAll(listOf("6", "7", "8")))
assertEquals(setOf("1", "2", "3", "4", "5", "1"), s)
assertTrue(s.removeAll(listOf("5", "3", "1")))
assertEquals(setOf("2", "4"), s)
}
@Test fun retainAll() {
val s = HashSet(listOf("1", "2", "3", "4", "5"))
assertFalse(s.retainAll(listOf("1", "2", "3", "4", "5")))
assertEquals(setOf("1", "2", "3", "4", "5"), s)
assertTrue(s.retainAll(listOf("5", "3", "1")))
assertEquals(setOf("1", "3", "5"), s)
}
}
@@ -0,0 +1,105 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
import kotlinx.cinterop.*
class ImmutableBlobTest {
@Test fun iterator() {
val blob = immutableBlobOf(1, 2, 3)
val actual = buildList {
for (b in blob) {
add(b)
}
}
assertContentEquals(listOf<Byte>(1, 2, 3), actual)
}
@Test fun toByteArray() {
val blob = immutableBlobOf(1, 2, 3)
val actual = blob.toByteArray()
assertContentEquals(byteArrayOf(1, 2, 3), actual)
}
@Test fun toByteArraySlice() {
val blob = immutableBlobOf(0, 0)
val arr = blob.toByteArray(0, 1)
assertEquals(1, arr.size)
assertEquals(0, arr[0])
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(0, -1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(0, 10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(10, 11)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toByteArray(10, 1)
}
}
@Test fun toUByteArraySlice() {
val blob = immutableBlobOf(0, 0)
val arr = blob.toUByteArray(0, 1)
assertEquals(1, arr.size)
assertEquals(0U, arr[0])
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(-1, 1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(0, -1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(0, 10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(10, 11)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.toUByteArray(10, 1)
}
}
@Test fun asCPointer() {
val blob = immutableBlobOf(0, 0)
assertEquals(0, blob.asCPointer(0).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asCPointer(Int.MIN_VALUE)
}
}
@Test fun asUCPointer() {
val blob = immutableBlobOf(0, 0)
assertEquals(0U, blob.asUCPointer(0).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(10)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
blob.asUCPointer(Int.MIN_VALUE)
}
}
}
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.random.Random
import kotlin.test.*
// Native-specific part of stdlib/test/collections/ListSpecificTest.kt
class ListSpecificNativeTest {
@Test fun factory() {
val nonConstStr = Random.nextInt().toString()
val list = listOf(nonConstStr, "b", "c")
assertEquals(3, list.size)
assertEquals(nonConstStr, list[0])
assertEquals("b", list[1])
assertEquals("c", list[2])
}
}
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2023 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.collection
import kotlin.test.*
// Native-specific part of stdlib/test/collections/MutableCollectionsTest.kt
class MutableCollectionsNativeTest {
@Test fun sortListString() {
val x = mutableListOf("x", "a", "b")
x.sort()
assertContentEquals(listOf("a", "b", "x"), x)
}
@Test fun sortListInt() {
val x = mutableListOf(239, 42, -1, 100500, 0)
x.sort()
assertContentEquals(listOf(-1, 0, 42, 239, 100500), x)
}
}
@@ -0,0 +1,139 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
// Native-specific part of stdlib/test/collections/UnsignedArraysTest.kt
class UnsignedArraysNativeTest {
@Test fun ubyteArray() {
assertFailsWith<IllegalArgumentException> { UByteArray(-1) }
}
@Test fun ubyteArrayInit() {
assertFailsWith<IllegalArgumentException> { UByteArray(-1) { it.toUByte() } }
}
@Test fun ushortArray() {
assertFailsWith<IllegalArgumentException> { UShortArray(-1) }
}
@Test fun ushortArrayInit() {
assertFailsWith<IllegalArgumentException> { UShortArray(-1) { it.toUShort() } }
}
@Test fun uintArray() {
assertFailsWith<IllegalArgumentException> { UIntArray(-1) }
}
@Test fun uintArrayInit() {
assertFailsWith<IllegalArgumentException> { UIntArray(-1) { it.toUInt() } }
}
@Test fun ulongArray() {
assertFailsWith<IllegalArgumentException> { ULongArray(-1) }
}
@Test fun ulongArrayInit() {
assertFailsWith<IllegalArgumentException> { ULongArray(-1) { it.toULong() } }
}
@Test fun ubyteArrayGetOutOfBounds() {
val arr = UByteArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun ushortArrayGetOutOfBounds() {
val arr = UShortArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun uintArrayGetOutOfBounds() {
val arr = UIntArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE]
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE]
}
}
@Test fun ubyteArraySetOutOfBounds() {
val arr = UByteArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1U
}
}
@Test fun ushortArraySetOutOfBounds() {
val arr = UShortArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1U
}
}
@Test fun uintArraySetOutOfBounds() {
val arr = UIntArray(5)
assertFailsWith<IndexOutOfBoundsException> {
arr[arr.size] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[-1] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MAX_VALUE] = 1U
}
assertFailsWith<IndexOutOfBoundsException> {
arr[Int.MIN_VALUE] = 1U
}
}
}
@@ -24,7 +24,9 @@ import org.jetbrains.kotlin.konan.test.blackbox.support.group.PredefinedTestCase
TC(
name = "default",
runnerType = TestRunnerType.DEFAULT,
freeCompilerArgs = [ENABLE_MPP, STDLIB_IS_A_FRIEND, ENABLE_X_STDLIB_API, ENABLE_X_ENCODING_API, ENABLE_X_FOREIGN_API, ENABLE_RANGE_UNTIL],
freeCompilerArgs = [
ENABLE_MPP, STDLIB_IS_A_FRIEND, ENABLE_X_STDLIB_API, ENABLE_X_ENCODING_API, ENABLE_RANGE_UNTIL,
ENABLE_X_FOREIGN_API, ENABLE_X_NATIVE_API, ENABLE_OBSOLETE_NATIVE_API],
sourceLocations = [
"libraries/stdlib/test/**.kt",
"libraries/stdlib/common/test/**.kt",
@@ -47,7 +49,9 @@ class StdlibTest : AbstractNativeBlackBoxTest() {
TC(
name = "default",
runnerType = TestRunnerType.DEFAULT,
freeCompilerArgs = [ENABLE_MPP, STDLIB_IS_A_FRIEND, ENABLE_X_STDLIB_API, ENABLE_X_ENCODING_API, ENABLE_X_FOREIGN_API, ENABLE_RANGE_UNTIL,
freeCompilerArgs = [
ENABLE_MPP, STDLIB_IS_A_FRIEND, ENABLE_X_STDLIB_API, ENABLE_X_ENCODING_API, ENABLE_RANGE_UNTIL,
ENABLE_X_FOREIGN_API, ENABLE_X_NATIVE_API, ENABLE_OBSOLETE_NATIVE_API,
"-Xcommon-sources=libraries/stdlib/common/test/jsCollectionFactories.kt",
"-Xcommon-sources=libraries/stdlib/common/test/testUtils.kt",
"-Xcommon-sources=libraries/stdlib/test/testUtils.kt",
@@ -75,5 +79,7 @@ internal const val STDLIB_IS_A_FRIEND = "-friend-modules=$KOTLIN_NATIVE_DISTRIBU
private const val ENABLE_X_STDLIB_API = "-opt-in=kotlin.ExperimentalStdlibApi"
private const val ENABLE_X_ENCODING_API = "-opt-in=kotlin.io.encoding.ExperimentalEncodingApi"
private const val ENABLE_X_FOREIGN_API = "-opt-in=kotlinx.cinterop.ExperimentalForeignApi"
private const val ENABLE_X_NATIVE_API = "-opt-in=kotlin.experimental.ExperimentalNativeApi"
private const val ENABLE_OBSOLETE_NATIVE_API = "-opt-in=kotlin.native.ObsoleteNativeApi"
private const val ENABLE_RANGE_UNTIL = "-XXLanguage:+RangeUntilOperator" // keep until 1.8
private const val DISABLED_STDLIB_TEST = "test.collections.CollectionTest.abstractCollectionToArray"