Change removeAll extension implementation to match the contract of member removeAll.
Add retainAll for sequences.
This commit is contained in:
@@ -12,6 +12,29 @@ import org.junit.Test as test
|
||||
|
||||
class CollectionJVMTest {
|
||||
|
||||
private fun <T> identitySetOf(vararg values: T): MutableSet<T> {
|
||||
val map = IdentityHashMap<T, String>()
|
||||
values.forEach { map.put(it, "") }
|
||||
return map.keySet()
|
||||
}
|
||||
|
||||
private data class IdentityData(public val value: Int)
|
||||
|
||||
test fun removeAllWithDifferentEquality() {
|
||||
val data = listOf(IdentityData(1), IdentityData(1))
|
||||
val list = data.toArrayList()
|
||||
list -= identitySetOf(data[0]) as Iterable<IdentityData>
|
||||
assertTrue(list.single() === data[1], "Identity contains should be used")
|
||||
|
||||
val list2 = data.toArrayList()
|
||||
list2 -= hashSetOf(data[0]) as Iterable<IdentityData>
|
||||
assertTrue(list2.isEmpty(), "Equality contains should be used")
|
||||
|
||||
val set3: MutableSet<IdentityData> = identitySetOf(*data.toTypedArray())
|
||||
set3 -= arrayOf(data[1])
|
||||
assertTrue(set3.isEmpty(), "Array doesn't have contains, equality contains is used instead")
|
||||
}
|
||||
|
||||
test fun flatMap() {
|
||||
val data = listOf("", "foo", "bar", "x", "")
|
||||
val characters = data.flatMap { it.toCharList() }
|
||||
|
||||
@@ -8,22 +8,44 @@ import org.junit.Test as test
|
||||
|
||||
class MutableCollectionTest {
|
||||
|
||||
test fun fromIterable() {
|
||||
val data: Iterable<String> = listOf("foo", "bar")
|
||||
// TODO: Use apply scope function
|
||||
|
||||
val collection = ArrayList<String>()
|
||||
collection.addAll(data)
|
||||
test fun addAll() {
|
||||
val data = listOf("foo", "bar")
|
||||
|
||||
assertEquals(data, collection)
|
||||
fun assertAdd(f: MutableList<String>.() -> Unit) = assertEquals(data, arrayListOf<String>().let { it.f(); it })
|
||||
|
||||
assertAdd { addAll(data) }
|
||||
assertAdd { addAll(data.toTypedArray()) }
|
||||
assertAdd { addAll(data.toTypedArray().asIterable()) }
|
||||
assertAdd { addAll(data.asSequence()) }
|
||||
}
|
||||
|
||||
test fun fromSequence() {
|
||||
val list = listOf("foo", "bar")
|
||||
val collection = ArrayList<String>()
|
||||
test fun removeAll() {
|
||||
val content = arrayOf("foo", "bar", "bar")
|
||||
val data = listOf("bar")
|
||||
val expected = listOf("foo")
|
||||
|
||||
collection.addAll(list.asSequence())
|
||||
fun assertRemove(f: MutableList<String>.() -> Unit) = assertEquals(expected, arrayListOf(*content).let { it.f(); it })
|
||||
|
||||
assertEquals(list, collection)
|
||||
assertRemove { removeAll(data) }
|
||||
assertRemove { removeAll(data.toTypedArray()) }
|
||||
assertRemove { removeAll(data.toTypedArray().asIterable()) }
|
||||
assertRemove { removeAll(data.asSequence()) }
|
||||
}
|
||||
|
||||
|
||||
test fun retainAll() {
|
||||
val content = arrayOf("foo", "bar", "bar")
|
||||
val data = listOf("bar")
|
||||
val expected = listOf("bar", "bar")
|
||||
|
||||
fun assertRetain(f: MutableList<String>.() -> Unit) = assertEquals(expected, arrayListOf(*content).let { it.f(); it })
|
||||
|
||||
assertRetain { retainAll(data) }
|
||||
assertRetain { retainAll(data.toTypedArray()) }
|
||||
assertRetain { retainAll(data.toTypedArray().asIterable()) }
|
||||
assertRetain { retainAll(data.asSequence()) }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user