@@ -14,7 +14,7 @@ import kotlin.test.assertTrue
1414
1515class KT41278Test {
1616 // Based on https://youtrack.jetbrains.com/issue/KT-42428.
17- private fun doTest (map : Map <String , Int >, key : String , value : Int , createEntry : (String , Int ) -> Map .Entry <String , Int >) {
17+ private fun doContainsTest (map : Map <String , Int >, key : String , value : Int , createEntry : (String , Int ) -> Map .Entry <String , Int >) {
1818 assertTrue(map.keys.contains(key))
1919 assertEquals(value, map[key])
2020 // This one requires special efforts to make it work this way.
@@ -28,14 +28,28 @@ class KT41278Test {
2828 assertFalse(map.entries.contains(" not an entry" as Any? ))
2929 }
3030
31+ private fun doRemoveTest (map : MutableMap <String , Int >, key : String , value : Int , createEntry : (String , Int ) -> Map .Entry <String , Int >) {
32+ assertTrue(map.keys.contains(key))
33+ assertEquals(value, map[key])
34+ // This one requires special efforts to make it work this way.
35+ // map.entries can in fact be `MutableSet<MutableMap.MutableEntry>`,
36+ // which [remove] method takes [MutableEntry], so the compiler may generate special bridge
37+ // returning false for values that aren't [MutableEntry].
38+ assertTrue(map.entries.toMutableSet().remove(createEntry(key, value)))
39+ assertTrue(map.entries.remove(createEntry(key, value)))
40+ }
41+
3142 @Test
3243 fun persistentOrderedMap () {
3344 val mapLetterToIndex = (' a' .. ' z' ).mapIndexed { i, c -> " $c " to i }.fold(persistentMapOf<String , Int >()) { map, pair ->
3445 map.put(pair.first, pair.second)
3546 }
3647
37- doTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
38- doTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
48+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
49+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
50+
51+ doRemoveTest(mapLetterToIndex.builder(), " h" , 7 , ::TestMapEntry )
52+ doRemoveTest(mapLetterToIndex.builder(), " h" , 7 , ::TestMutableMapEntry )
3953 }
4054
4155 @Test
@@ -44,24 +58,33 @@ class KT41278Test {
4458 map.put(pair.first, pair.second)
4559 }
4660
47- doTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
48- doTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
61+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
62+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
63+
64+ doRemoveTest(mapLetterToIndex.builder(), " h" , 7 , ::TestMapEntry )
65+ doRemoveTest(mapLetterToIndex.builder(), " h" , 7 , ::TestMutableMapEntry )
4966 }
5067
5168 @Test
5269 fun persistentOrderedMapBuilder () {
5370 val mapLetterToIndex = persistentMapOf<String , Int >().builder().apply { putAll((' a' .. ' z' ).mapIndexed { i, c -> " $c " to i }) }
5471
55- doTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
56- doTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
72+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
73+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
74+
75+ doRemoveTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
76+ doRemoveTest(mapLetterToIndex, " b" , 1 , ::TestMutableMapEntry )
5777 }
5878
5979 @Test
6080 fun persistentHashMapBuilder () {
6181 val mapLetterToIndex = persistentHashMapOf<String , Int >().builder().apply { putAll((' a' .. ' z' ).mapIndexed { i, c -> " $c " to i }) }
6282
63- doTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
64- doTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
83+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
84+ doContainsTest(mapLetterToIndex, " h" , 7 , ::TestMutableMapEntry )
85+
86+ doRemoveTest(mapLetterToIndex, " h" , 7 , ::TestMapEntry )
87+ doRemoveTest(mapLetterToIndex, " b" , 1 , ::TestMutableMapEntry )
6588 }
6689}
6790
0 commit comments