1
1
package kotlinx.collections.immutable
2
2
3
3
import org.junit.Test
4
+ import test.collections.behaviors.*
5
+ import test.collections.compare
4
6
import java.util.*
5
7
import kotlin.test.*
6
8
@@ -9,33 +11,39 @@ class ImmutableHashMapTest : ImmutableMapTest() {
9
11
}
10
12
class ImmutableOrderedMapTest : ImmutableMapTest () {
11
13
override fun <K , V > immutableMapOf (vararg pairs : Pair <K , V >): ImmutableMap <K , V > = kotlinx.collections.immutable.immutableMapOf(* pairs)
14
+ override fun <K , V > compareMaps (expected : Map <K , V >, actual : Map <K , V >) = compare(expected, actual) { mapBehavior(ordered = true ) }
12
15
13
16
@Test fun iterationOrder () {
14
17
var map = immutableMapOf(" x" to null , " y" to 1 )
15
- assertEquals( listOf (" x" , " y" ), map.keys.toList())
18
+ compare( setOf (" x" , " y" ), map.keys) { setBehavior(ordered = true ) }
16
19
17
20
map + = " x" to 1
18
- assertEquals( listOf (" x" , " y" ), map.keys.toList())
21
+ compare( setOf (" x" , " y" ), map.keys) { setBehavior(ordered = true ) }
19
22
20
23
map = map.remove(" x" )
21
24
map + = " x" to 2
22
- assertEquals( listOf (" y" , " x" ), map.keys.toList())
23
- assertEquals (listOf (1 , 2 ), map.values.toList())
24
- assertEquals( listOf (" y" to 1 , " x" to 2 ), map.toList())
25
+ compare( setOf (" y" , " x" ), map.keys) { setBehavior(ordered = true ) }
26
+ compare (listOf (1 , 2 ), map.values) { collectionBehavior(ordered = true ) }
27
+ compare( mapOf (" y" to 1 , " x" to 2 ).entries , map.entries) { setBehavior(ordered = true ) }
25
28
}
26
29
}
27
30
28
31
abstract class ImmutableMapTest {
29
32
30
33
abstract fun <K , V > immutableMapOf (vararg pairs : Pair <K , V >): ImmutableMap <K , V >
31
34
35
+ open fun <K , V > compareMaps (expected : Map <K , V >, actual : Map <K , V >) = compareMapsUnordered(expected, actual)
36
+ fun <K , V > compareMapsUnordered (expected : Map <K , V >, actual : Map <K , V >) = compare(expected, actual) { mapBehavior(ordered = false ) }
37
+
32
38
33
39
@Test fun empty () {
34
40
val empty1 = immutableMapOf<Int , String >()
35
41
val empty2 = immutableMapOf<String , Int >()
36
42
assertEquals<ImmutableMap <* , * >>(empty1, empty2)
37
43
assertEquals(mapOf<Int , String >(), empty1)
38
44
assertTrue(empty1 == = empty2)
45
+
46
+ compareMaps(emptyMap(), empty1)
39
47
}
40
48
41
49
@@ -44,25 +52,24 @@ abstract class ImmutableMapTest {
44
52
val map1 = immutableMapOf(" x" to 1 , " y" to null , null to 2 )
45
53
val map2 = immutableMapOf(" x" to 1 , " y" to null , null to 2 )
46
54
47
- assertEquals (map0, map1)
48
- assertEquals (map1, map2)
55
+ compareMaps (map0, map1)
56
+ compareMaps (map1, map2)
49
57
}
50
58
51
59
52
60
@Test fun toImmutable () {
53
61
val original = mapOf (" x" to 1 , " y" to null , null to 2 )
62
+ val immOriginal = original.toImmutableMap()
63
+ compareMaps(original, immOriginal)
64
+
54
65
55
66
val map = HashMap (original) // copy
56
67
var immMap = map.toImmutableMap()
57
68
val immMap2 = immMap.toImmutableMap()
58
69
assertTrue(immMap2 == = immMap)
59
70
60
- assertEquals<Map <* , * >>(map, immMap) // problem
61
- // assertEquals(map.toString(), immMap.toString()) // order sensitive
62
- assertEquals(map.hashCode(), immMap.hashCode())
63
- assertEquals<Set <* >>(map.keys, immMap.keys)
64
- assertEquals<Set <* >>(map.entries, immMap.entries)
65
- assertEquals(map.values.toSet(), immMap.values.toSet())
71
+ compareMapsUnordered(original, immMap)
72
+ compareMapsUnordered(map, immMap)
66
73
67
74
map.remove(null )
68
75
assertNotEquals<Map <* , * >>(map, immMap)
@@ -79,8 +86,10 @@ abstract class ImmutableMapTest {
79
86
map = map.putAll(arrayOf(" x" to null ))
80
87
map = map + (" y" to null )
81
88
map + = " y" to 1
89
+ assertEquals(mapOf (" x" to null , " y" to 1 ), map)
90
+
82
91
map + = map
83
- map + = map.map { it.key + " !" to it.value }
92
+ map + = map.map { it.key + " !" to it.value }
84
93
85
94
assertEquals(map.size, map.entries.size)
86
95
@@ -128,8 +137,8 @@ abstract class ImmutableMapTest {
128
137
operation(mutable)
129
138
operation(builder)
130
139
131
- assertEquals (mutable, builder)
132
- assertEquals< Map < * , * >> (mutable, builder.build())
140
+ compareMapsUnordered (mutable, builder)
141
+ compareMapsUnordered (mutable, builder.build())
133
142
}
134
143
135
144
@Test fun noOperation () {
0 commit comments