KT-57607 Add test for MutableList.addAll at index
Also improve other operations test coverage
This commit is contained in:
committed by
Space Team
parent
99d47da014
commit
f13e127b0f
@@ -6,6 +6,7 @@
|
|||||||
package test.collections
|
package test.collections
|
||||||
|
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
import kotlin.random.nextInt
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
|
|
||||||
@@ -39,24 +40,57 @@ class MutableCollectionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <T> forAllStandardMutableLists(data: List<T>, block: (MutableList<T>) -> Unit) {
|
||||||
|
block(data.toMutableList())
|
||||||
|
block(ArrayDeque(data))
|
||||||
|
buildList {
|
||||||
|
addAll(data)
|
||||||
|
block(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun addAllAtIndex() {
|
||||||
|
val original = List(15000) { Random.nextInt() }
|
||||||
|
for (insertSize in listOf(0, 1, 10, 1000, 20000)) {
|
||||||
|
val insertion = List(insertSize) { Random.nextInt() }
|
||||||
|
for (index in listOf(0, original.size) + List(10) { Random.nextInt(original.indices) }) {
|
||||||
|
forAllStandardMutableLists(original) { mutable ->
|
||||||
|
mutable.addAll(index, insertion)
|
||||||
|
|
||||||
|
assertEquals(original.size + insertSize, mutable.size)
|
||||||
|
val tailIndex = index + insertSize
|
||||||
|
for (i in 0..<index) {
|
||||||
|
assertEquals(original[i], mutable[i])
|
||||||
|
}
|
||||||
|
for (i in index..<tailIndex) {
|
||||||
|
assertEquals(insertion[i - index], mutable[i])
|
||||||
|
}
|
||||||
|
for (i in tailIndex..<mutable.size) {
|
||||||
|
assertEquals(original[i - insertSize], mutable[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test fun removeFirst() {
|
@Test fun removeFirst() {
|
||||||
val list = mutableListOf("first", "second")
|
forAllStandardMutableLists(listOf("first", "second")) { list ->
|
||||||
|
assertEquals("first", list.removeFirst())
|
||||||
|
assertEquals("second", list.removeFirstOrNull())
|
||||||
|
|
||||||
assertEquals("first", list.removeFirst())
|
assertNull(list.removeFirstOrNull())
|
||||||
assertEquals("second", list.removeFirstOrNull())
|
assertFailsWith<NoSuchElementException> { list.removeFirst() }
|
||||||
|
}
|
||||||
assertNull(list.removeFirstOrNull())
|
|
||||||
assertFailsWith<NoSuchElementException> { list.removeFirst() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun removeLast() {
|
@Test fun removeLast() {
|
||||||
val list = mutableListOf("first", "second")
|
forAllStandardMutableLists(listOf("first", "second")) { list ->
|
||||||
|
assertEquals("second", list.removeLast())
|
||||||
|
assertEquals("first", list.removeLastOrNull())
|
||||||
|
|
||||||
assertEquals("second", list.removeLast())
|
assertNull(list.removeLastOrNull())
|
||||||
assertEquals("first", list.removeLastOrNull())
|
assertFailsWith<NoSuchElementException> { list.removeLast() }
|
||||||
|
}
|
||||||
assertNull(list.removeLastOrNull())
|
|
||||||
assertFailsWith<NoSuchElementException> { list.removeLast() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun removeAll() {
|
@Test fun removeAll() {
|
||||||
@@ -121,9 +155,10 @@ class MutableCollectionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun listFill() {
|
@Test fun listFill() {
|
||||||
val list = MutableList(3) { it }
|
forAllStandardMutableLists(List(3) { it }) { list ->
|
||||||
list.fill(42)
|
list.fill(42)
|
||||||
assertEquals(listOf(42, 42, 42), list)
|
assertEquals(listOf(42, 42, 42), list)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun shuffled() {
|
@Test fun shuffled() {
|
||||||
|
|||||||
Reference in New Issue
Block a user