Support covariant MutableMap.entries.remove in JS/IR
Workaround for KT-43321 Follow up to KT-41278
This commit is contained in:
committed by
TeamCityServer
parent
666ad1f9d5
commit
ed3542cdf5
@@ -420,7 +420,10 @@ class MapTest {
|
||||
// which [contains] method takes [MutableEntry], so the compiler may generate special bridge
|
||||
// returning false for values that aren't [MutableEntry] (including [SimpleEntry]).
|
||||
assertTrue(map.entries.contains(SimpleEntry(key, value)), mapDescription)
|
||||
assertTrue(map.entries.toSet().contains(SimpleEntry(key, value)), mapDescription)
|
||||
assertTrue(map.entries.toSet().contains(SimpleEntry(key, value)), "$mapDescription: reference")
|
||||
|
||||
assertFalse(map.entries.contains(null as Any?), "$mapDescription: contains null")
|
||||
assertFalse(map.entries.contains("not an entry" as Any?), "$mapDescription: contains not an entry")
|
||||
}
|
||||
|
||||
val mapLetterToIndex = ('a'..'z').mapIndexed { i, c -> "$c" to i }.toMap()
|
||||
@@ -436,6 +439,36 @@ class MapTest {
|
||||
doTest("built Map", builtMap, "y", 24)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun entriesCovariantRemove() {
|
||||
fun doTest(implName: String, map: MutableMap<String, Int>, key: String, value: Int) {
|
||||
class SimpleEntry<out K, out V>(override val key: K, override val value: V) : Map.Entry<K, V> {
|
||||
override fun toString(): String = "$key=$value"
|
||||
override fun hashCode(): Int = key.hashCode() xor value.hashCode()
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is Map.Entry<*, *> && key == other.key && value == other.value
|
||||
}
|
||||
|
||||
val mapDescription = "$implName: ${map::class}"
|
||||
|
||||
assertTrue(map.entries.toMutableSet().remove(SimpleEntry(key, value) as Map.Entry<*, *>), "$mapDescription: reference")
|
||||
assertTrue(map.entries.remove(SimpleEntry(key, value) as Map.Entry<*, *>), mapDescription)
|
||||
|
||||
assertFalse(map.entries.remove(null as Any?), "$mapDescription: remove null")
|
||||
assertFalse(map.entries.remove("not an entry" as Any?), "$mapDescription: remove not an entry")
|
||||
}
|
||||
|
||||
val mapLetterToIndex = ('a'..'z').mapIndexed { i, c -> "$c" to i }.toMap()
|
||||
doTest("default mutable", mapLetterToIndex.toMutableMap(), "b", 1)
|
||||
doTest("HashMap", mapLetterToIndex.toMap(HashMap()), "c", 2)
|
||||
doTest("LinkedHashMap", mapLetterToIndex.toMap(LinkedHashMap()), "d", 3)
|
||||
|
||||
val builtMap = buildMap {
|
||||
putAll(mapLetterToIndex)
|
||||
doTest("MapBuilder", this, "z", 25)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun firstNotNullOf() {
|
||||
val map = mapOf("Alice" to 20, "Tom" to 13, "Bob" to 18)
|
||||
|
||||
Reference in New Issue
Block a user