17
17
package kotlinx.collections.immutable.contractTests.immutableSet
18
18
19
19
import kotlinx.collections.immutable.*
20
+ import kotlinx.collections.immutable.contractTests.compare
21
+ import kotlinx.collections.immutable.contractTests.setBehavior
20
22
import org.junit.Test
21
23
import kotlin.test.*
22
24
23
- class ImmutableSetTest : ImmutableSetTestBase () {
24
- override fun <T > immutableSetOf (vararg elements : T ) = persistentSetOf(* elements)
25
- }
26
-
27
25
class ImmutableHashSetTest : ImmutableSetTestBase () {
28
26
override fun <T > immutableSetOf (vararg elements : T ) = persistentHashSetOf(* elements)
29
-
30
- override fun empty () {
31
- val empty1 = immutableSetOf<Int >()
32
- val empty2 = immutableSetOf<String >()
33
- assertEquals<ImmutableSet <Any >>(empty1, empty2)
34
- assertEquals<Set <Any >>(setOf (), empty1)
35
- // assertTrue(empty1 === empty2) // fails to implement this property
36
- }
37
-
38
- override fun noOperation () {
39
- // immutableSetOf<Int>().testNoOperation({ clear() }, { clear() }) // fails to implement this property
40
-
41
- val list = " abcxyz12" .toList().toPersistentSet()
42
- with (list) {
43
- testNoOperation({ add(' a' ) }, { add(' a' ) })
44
- testNoOperation({ addAll(listOf (' a' , ' b' )) }, { addAll(listOf (' a' , ' b' )) })
45
- testNoOperation({ remove(' d' ) }, { remove(' d' ) })
46
- testNoOperation({ removeAll(listOf (' d' , ' e' )) }, { removeAll(listOf (' d' , ' e' )) })
47
- testNoOperation({ removeAll { it.isUpperCase() } }, { removeAll { it.isUpperCase() } })
48
- }
49
- }
27
+ }
28
+ class ImmutableOrderedSetTest : ImmutableSetTestBase () {
29
+ override fun <T > immutableSetOf (vararg elements : T ) = persistentSetOf(* elements)
30
+ override fun <T > compareSets (expected : Set <T >, actual : Set <T >) = compare(expected, actual) { setBehavior(ordered = true ) }
50
31
}
51
32
52
33
abstract class ImmutableSetTestBase {
53
34
54
35
abstract fun <T > immutableSetOf (vararg elements : T ): PersistentSet <T >
36
+ fun <T > immutableSetOf (elements : Collection <T >) = immutableSetOf<T >() + elements
37
+
38
+ open fun <T > compareSets (expected : Set <T >, actual : Set <T >) = compareSetsUnordered(expected, actual)
39
+ fun <T > compareSetsUnordered (expected : Set <T >, actual : Set <T >) = compare(expected, actual) { setBehavior(ordered = false ) }
55
40
56
41
@Test open fun empty () {
57
42
val empty1 = immutableSetOf<Int >()
58
43
val empty2 = immutableSetOf<String >()
59
44
assertEquals<ImmutableSet <Any >>(empty1, empty2)
60
45
assertEquals<Set <Any >>(setOf (), empty1)
61
46
assertTrue(empty1 == = empty2)
47
+
48
+ compareSets(emptySet(), empty1)
62
49
}
63
50
64
51
@Test fun ofElements () {
65
52
val set0 = setOf (" a" , " d" , 1 , null )
66
53
val set1 = immutableSetOf(" a" , " d" , 1 , null )
67
54
val set2 = immutableSetOf(" a" , " d" , 1 , null )
68
55
69
- assertEquals (set0, set1)
70
- assertEquals (set1, set2)
56
+ compareSets (set0, set1)
57
+ compareSets (set1, set2)
71
58
}
72
59
73
60
@Test fun toImmutable () {
74
61
val original = setOf (" a" , " bar" , " cat" , null )
62
+ val immOriginal = immutableSetOf(original)
63
+ compareSets(original, immOriginal)
75
64
76
- val set = original.toMutableSet( ) // copy
77
- var immSet = set.toPersistentSet( )
65
+ val hashSet = HashSet (original ) // copy
66
+ var immSet = immutableSetOf(hashSet )
78
67
val immSet2 = immSet.toImmutableSet()
79
68
assertTrue(immSet2 == = immSet)
80
69
81
- assertEquals<Set <* >>(set, immSet) // problem
82
- assertEquals(set.toString(), immSet.toString())
83
- assertEquals(set.hashCode(), immSet.hashCode())
70
+ compareSetsUnordered(original, immSet)
71
+ compareSetsUnordered(hashSet, immSet)
84
72
85
- set .remove(" a" )
86
- assertNotEquals<Set <* >>(set , immSet)
73
+ hashSet .remove(" a" )
74
+ assertNotEquals<Set <* >>(hashSet , immSet)
87
75
88
76
immSet = immSet.remove(" a" )
89
- assertEquals< Set < * >>(set , immSet) // problem
77
+ compareSetsUnordered(hashSet , immSet)
90
78
}
91
79
92
80
@Test fun addElements () {
@@ -96,14 +84,14 @@ abstract class ImmutableSetTestBase {
96
84
set = set + " y"
97
85
set + = " z"
98
86
set + = arrayOf(" 1" , " 2" ).asIterable()
99
- assertEquals (" xyz12" .map { it.toString() }.toSet(), set)
87
+ compareSets (" xyz12" .map { it.toString() }.toSet(), set)
100
88
}
101
89
102
90
103
91
@Test fun removeElements () {
104
- val set = " abcxyz12" .toList().toPersistentSet( )
92
+ val set = immutableSetOf( " abcxyz12" .toList())
105
93
fun expectSet (content : String , set : ImmutableSet <Char >) {
106
- assertEquals (content.toSet(), set)
94
+ compareSets (content.toSet(), set)
107
95
}
108
96
109
97
expectSet(" abcyz12" , set.remove(' x' ))
@@ -112,19 +100,21 @@ abstract class ImmutableSetTestBase {
112
100
expectSet(" abcy12" , set - setOf (' x' , ' z' ))
113
101
expectSet(" abcxyz" , set.removeAll { it.isDigit() })
114
102
115
- assertEquals (emptySet< Char > (), set - set)
116
- assertEquals (emptySet< Char > (), set.clear())
103
+ compareSets (emptySet(), set - set)
104
+ compareSets (emptySet(), set.clear())
117
105
}
118
106
119
107
@Test fun builder () {
120
108
val builder = immutableSetOf<Char >().builder()
121
109
" abcxaxyz12" .toCollection(builder)
122
110
val set = builder.build()
123
- assertEquals< Set < * >> (set, builder)
111
+ compareSets (set, builder)
124
112
assertTrue(set == = builder.build(), " Building the same set without modifications" )
125
113
126
114
val set2 = builder.toImmutableSet()
127
115
assertTrue(set2 == = set, " toImmutable calls build()" )
116
+ val set3 = builder.toPersistentSet()
117
+ assertTrue(set3 == = set, " toPersistent calls build()" )
128
118
129
119
with (set) {
130
120
testMutation { add(' K' ) }
@@ -139,22 +129,20 @@ abstract class ImmutableSetTestBase {
139
129
}
140
130
141
131
fun <T > PersistentSet<T>.testMutation (operation : MutableSet <T >.() -> Unit ) {
142
- val mutable = this .toMutableSet()
132
+ val mutable = HashSet ( this ) as MutableSet < T >
143
133
val builder = this .builder()
144
134
145
135
operation(mutable)
146
136
operation(builder)
147
137
148
- assertEquals (mutable, builder)
149
- assertEquals< Set < * >> (mutable, builder.build())
138
+ compareSetsUnordered (mutable, builder)
139
+ compareSetsUnordered (mutable, builder.build())
150
140
}
151
141
152
-
153
-
154
142
@Test open fun noOperation () {
155
143
immutableSetOf<Int >().testNoOperation({ clear() }, { clear() })
156
144
157
- val set = " abcxyz12" .asIterable().toPersistentSet( )
145
+ val set = immutableSetOf( " abcxyz12" .toList() )
158
146
with (set) {
159
147
testNoOperation({ add(' a' ) }, { add(' a' ) })
160
148
testNoOperation({ addAll(listOf (' a' , ' b' )) }, { addAll(listOf (' a' , ' b' )) })
0 commit comments