From f13e127b0f4ed3c16c0543be958178dc7b1f70cc Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 6 Apr 2023 08:56:32 +0200 Subject: [PATCH] KT-57607 Add test for MutableList.addAll at index Also improve other operations test coverage --- .../collections/MutableCollectionsTest.kt | 65 ++++++++++++++----- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/libraries/stdlib/test/collections/MutableCollectionsTest.kt b/libraries/stdlib/test/collections/MutableCollectionsTest.kt index b85600c0546..1a62d3772f3 100644 --- a/libraries/stdlib/test/collections/MutableCollectionsTest.kt +++ b/libraries/stdlib/test/collections/MutableCollectionsTest.kt @@ -6,6 +6,7 @@ package test.collections import kotlin.random.Random +import kotlin.random.nextInt import kotlin.test.* @@ -39,24 +40,57 @@ class MutableCollectionTest { } } + private fun forAllStandardMutableLists(data: List, block: (MutableList) -> 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.. + assertEquals("first", list.removeFirst()) + assertEquals("second", list.removeFirstOrNull()) - assertEquals("first", list.removeFirst()) - assertEquals("second", list.removeFirstOrNull()) - - assertNull(list.removeFirstOrNull()) - assertFailsWith { list.removeFirst() } + assertNull(list.removeFirstOrNull()) + assertFailsWith { list.removeFirst() } + } } @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()) - assertEquals("first", list.removeLastOrNull()) - - assertNull(list.removeLastOrNull()) - assertFailsWith { list.removeLast() } + assertNull(list.removeLastOrNull()) + assertFailsWith { list.removeLast() } + } } @Test fun removeAll() { @@ -121,9 +155,10 @@ class MutableCollectionTest { } @Test fun listFill() { - val list = MutableList(3) { it } - list.fill(42) - assertEquals(listOf(42, 42, 42), list) + forAllStandardMutableLists(List(3) { it }) { list -> + list.fill(42) + assertEquals(listOf(42, 42, 42), list) + } } @Test fun shuffled() {