@@ -14,7 +14,7 @@ import kotlin.test.assertTrue
14
14
15
15
class KT41278Test {
16
16
// 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 >) {
18
18
assertTrue(map.keys.contains(key))
19
19
assertEquals(value, map[key])
20
20
// This one requires special efforts to make it work this way.
@@ -28,14 +28,28 @@ class KT41278Test {
28
28
assertFalse(map.entries.contains(" not an entry" as Any? ))
29
29
}
30
30
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
+
31
42
@Test
32
43
fun persistentOrderedMap () {
33
44
val mapLetterToIndex = (' a' .. ' z' ).mapIndexed { i, c -> " $c " to i }.fold(persistentMapOf<String , Int >()) { map, pair ->
34
45
map.put(pair.first, pair.second)
35
46
}
36
47
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 )
39
53
}
40
54
41
55
@Test
@@ -44,24 +58,33 @@ class KT41278Test {
44
58
map.put(pair.first, pair.second)
45
59
}
46
60
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 )
49
66
}
50
67
51
68
@Test
52
69
fun persistentOrderedMapBuilder () {
53
70
val mapLetterToIndex = persistentMapOf<String , Int >().builder().apply { putAll((' a' .. ' z' ).mapIndexed { i, c -> " $c " to i }) }
54
71
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 )
57
77
}
58
78
59
79
@Test
60
80
fun persistentHashMapBuilder () {
61
81
val mapLetterToIndex = persistentHashMapOf<String , Int >().builder().apply { putAll((' a' .. ' z' ).mapIndexed { i, c -> " $c " to i }) }
62
82
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 )
65
88
}
66
89
}
67
90
0 commit comments