Files
kotlin/libraries/stdlib/test/js/JsCollectionsTest.kt
Ilya Gorbunov d8cd926a8c Do not leak primitiveness of an array wrapped with asList
A primitive array wrapped in a List with asList had incorrect implementation of toArray method:
while it declares that an object array is returned, it returned a primitive array.
Therefore the methods such as `Collection.toTypedArray()` and its dependents
`ArrayList(collection)`, `Collection + Iterable` might behave incorrectly
having relied on `toTypedArray` returned an object array.

#KT-21828 Fixed
2017-12-18 15:50:15 +03:00

88 lines
3.3 KiB
Kotlin

package test.collections.js
import kotlin.test.*
import kotlin.comparisons.*
class JsCollectionsTest {
val TEST_LIST = arrayOf(2, 0, 9, 7, 1).toList()
@Test fun collectionToArray() {
val array = TEST_LIST.toTypedArray()
assertEquals(array.toList(), TEST_LIST)
}
@Test fun toListDoesNotCreateArrayView() {
snapshotDoesNotCreateView(arrayOf("first", "last"), { it.toList() })
snapshotDoesNotCreateView(arrayOf<Any>("item", 1), { it.toList() })
}
@Test fun toMutableListDoesNotCreateArrayView() {
snapshotDoesNotCreateView(arrayOf("first", "last"), { it.toMutableList() })
snapshotDoesNotCreateView(arrayOf<Any>("item", 2), { it.toMutableList() })
}
@Test fun listOfDoesNotCreateView() {
snapshotDoesNotCreateView(arrayOf("first", "last"), { listOf(*it) })
snapshotDoesNotCreateView(arrayOf<Any>("item", 3), { listOf(*it) })
}
@Test fun mutableListOfDoesNotCreateView() {
snapshotDoesNotCreateView(arrayOf("first", "last"), { mutableListOf(*it) })
snapshotDoesNotCreateView(arrayOf<Any>("item", 4), { mutableListOf(*it) })
}
@Test fun arrayListDoesNotCreateArrayView() {
snapshotDoesNotCreateView(arrayOf(1, 2), { arrayListOf(*it) })
snapshotDoesNotCreateView(arrayOf<Any>("first", "last"), { arrayListOf(*it) })
}
@Suppress("USELESS_CAST")
@Test fun asListHidesPrimitivenessOfArray() {
assertTrue(intArrayOf(1).asList().toTypedArray() as Any is Array<*>, "IntArray primitiveness leaks")
assertTrue(longArrayOf(1).asList().toTypedArray() as Any is Array<*>, "LongArray primitiveness leaks")
assertTrue(charArrayOf(' ').asList().toTypedArray() as Any is Array<*>, "CharArray primitiveness leaks")
}
@Test fun arrayListCapacity() {
val list = ArrayList<Any>(20)
list.ensureCapacity(100)
list.trimToSize()
assertTrue(list.isEmpty())
}
@Test fun listEqualsOperatesOnAny() {
assertFalse(listOf(1, 2, 3).equals(object {}))
}
@Test fun arrayListValidatesIndexRange() {
val list = mutableListOf(1)
for (index in listOf(-1, 1, 3)) {
if (index != list.size) { // size is a valid position index
assertFailsWith<IndexOutOfBoundsException> { list.add(index, 2) }
assertFailsWith<IndexOutOfBoundsException> { list.addAll(index, listOf(3, 0)) }
assertFailsWith<IndexOutOfBoundsException> { list.listIterator(index) }
}
assertFailsWith<IndexOutOfBoundsException> { list.removeAt(index) }
assertFailsWith<IndexOutOfBoundsException> { list[index] }
assertFailsWith<IndexOutOfBoundsException> { list.subList(index, index + 2) } // tests ranges [-1, 1), [1, 3) and [3, 5)
}
assertEquals(listOf(1), list)
}
@Test fun mutableIteratorRemove() {
val a = mutableListOf(1, 2, 3)
val it = a.iterator()
assertFailsWith<IllegalStateException> { it.remove() }
}
private fun <T> snapshotDoesNotCreateView(array: Array<T>, snapshot: (Array<T>) -> List<T>) {
val first = array.first()
val last = array.last()
val list = snapshot(array)
assertEquals(first, list[0])
array[0] = last
assertEquals(first, list[0])
}
}