1
1
package kotlinx.collections.immutable
2
2
3
3
import org.junit.Test
4
+ import test.collections.behaviors.listBehavior
5
+ import test.collections.compare
4
6
import kotlin.test.*
5
7
6
8
class ImmutableListTest {
7
9
10
+ private fun <T > compareLists (expected : List <T >, actual : List <T >) = compare(expected, actual) { listBehavior() }
11
+
12
+
8
13
@Test fun empty () {
9
14
val empty1 = immutableListOf<Int >()
10
15
val empty2 = immutableListOf<String >()
11
16
assertEquals<ImmutableList <Any >>(empty1, empty2)
12
17
assertEquals<List <Any >>(listOf (), empty1)
13
18
assertTrue(empty1 == = empty2)
19
+
20
+ assertFailsWith<NoSuchElementException > { empty1.iterator().next() }
21
+
22
+ compareLists(emptyList(), empty1)
23
+
14
24
}
15
25
16
26
@Test fun ofElements () {
17
27
val list0 = listOf (" a" , " d" , 1 , null )
18
28
val list1 = immutableListOf(" a" , " d" , 1 , null )
19
29
val list2 = immutableListOf(" a" , " d" , 1 , null )
20
30
21
- assertEquals (list0, list1)
31
+ compareLists (list0, list1)
22
32
assertEquals(list1, list2)
23
33
}
24
34
@@ -30,15 +40,13 @@ class ImmutableListTest {
30
40
val immList2 = immList.toImmutableList()
31
41
assertTrue(immList2 == = immList)
32
42
33
- assertEquals<List <* >>(list, immList) // problem
34
- assertEquals(list.toString(), immList.toString())
35
- assertEquals(list.hashCode(), immList.hashCode())
43
+ compareLists(original, immList)
36
44
37
45
list.removeAt(0 )
38
46
assertNotEquals<List <* >>(list, immList)
39
47
40
48
immList = immList.removeAt(0 )
41
- assertEquals< List < * >> (list, immList) // problem
49
+ compareLists (list, immList)
42
50
}
43
51
44
52
@Test fun addElements () {
@@ -50,7 +58,7 @@ class ImmutableListTest {
50
58
list = list + " y"
51
59
list + = " z"
52
60
list + = arrayOf(" 1" , " 2" ).asIterable()
53
- assertEquals (" abcxaxyz12" .map { it.toString() }, list)
61
+ compareLists (" abcxaxyz12" .map { it.toString() }, list)
54
62
}
55
63
56
64
@Test fun replaceElements () {
@@ -68,7 +76,7 @@ class ImmutableListTest {
68
76
@Test fun removeElements () {
69
77
val list = " abcxaxyz12" .toImmutableList()
70
78
fun expectList (content : String , list : ImmutableList <Char >) {
71
- assertEquals (content, list.joinToString( " " ) )
79
+ compareLists (content.toList(), list )
72
80
}
73
81
74
82
expectList(" bcxaxyz12" , list.removeAt(0 ))
@@ -86,7 +94,7 @@ class ImmutableListTest {
86
94
val list = " abcxaxyz12" .toImmutableList()
87
95
val subList = list.subList(2 , 5 ) // 2, 3, 4
88
96
assertTrue(subList is ImmutableList )
89
- assertEquals (listOf (' c' , ' x' , ' a' ), subList)
97
+ compareLists (listOf (' c' , ' x' , ' a' ), subList)
90
98
91
99
assertFailsWith<IndexOutOfBoundsException > { list.subList(- 1 , 2 ) }
92
100
assertFailsWith<IndexOutOfBoundsException > { list.subList(0 , list.size + 1 ) }
@@ -136,8 +144,8 @@ class ImmutableListTest {
136
144
operation(mutable)
137
145
operation(builder)
138
146
139
- assertEquals (mutable, builder)
140
- assertEquals< List < * >> (mutable, builder.build())
147
+ compareLists (mutable, builder)
148
+ compareLists (mutable, builder.build())
141
149
}
142
150
143
151
@Test fun noOperation () {
0 commit comments