stdlib: Introduced filterIsInstance version
without class-literal as a parameter
This commit is contained in:
committed by
Andrey Breslav
parent
da5164acd8
commit
e768b4e285
@@ -104,4 +104,42 @@ class CollectionJVMTest {
|
||||
test fun takeReturnsFirstNElements() {
|
||||
expect(setOf(1, 2)) { sortedSetOf(1, 2, 3, 4, 5).take(2).toSet() }
|
||||
}
|
||||
|
||||
test fun filterIsInstanceList() {
|
||||
val src: List<Any> = listOf(1,2,3.toDouble(), "abc", "cde")
|
||||
|
||||
val ints: List<Int> = src.filterIsInstance<Int>()
|
||||
assertEquals(arrayListOf(1,2), ints)
|
||||
|
||||
val doubles: List<Double> = src.filterIsInstance<Double>()
|
||||
assertEquals(arrayListOf(3.0), doubles)
|
||||
|
||||
val strings: List<String> = src.filterIsInstance<String>()
|
||||
assertEquals(arrayListOf("abc", "cde"), strings)
|
||||
|
||||
val anys: List<Any> = src.filterIsInstance<Any>()
|
||||
assertEquals(src.toList(), anys)
|
||||
|
||||
val chars: List<Char> = src.filterIsInstance<Char>()
|
||||
assertEquals(0, chars.size())
|
||||
}
|
||||
|
||||
test fun filterIsInstanceArray() {
|
||||
val src: Array<Any> = array(1,2,3.toDouble(), "abc", "cde")
|
||||
|
||||
val ints: List<Int> = src.filterIsInstance<Int>()
|
||||
assertEquals(arrayListOf(1,2), ints)
|
||||
|
||||
val doubles: List<Double> = src.filterIsInstance<Double>()
|
||||
assertEquals(arrayListOf(3.0), doubles)
|
||||
|
||||
val strings: List<String> = src.filterIsInstance<String>()
|
||||
assertEquals(arrayListOf("abc", "cde"), strings)
|
||||
|
||||
val anys: List<Any> = src.filterIsInstance<Any>()
|
||||
assertEquals(src.toList(), anys)
|
||||
|
||||
val chars: List<Char> = src.filterIsInstance<Char>()
|
||||
assertEquals(0, chars.size())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package test.collections
|
||||
|
||||
import org.junit.Test as test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class StreamJVMTest {
|
||||
|
||||
test fun filterIsInstance() {
|
||||
val src: Stream<Any> = listOf(1,2,3.toDouble(), "abc", "cde").stream()
|
||||
|
||||
val ints: Stream<Int> = src.filterIsInstance<Int>()
|
||||
assertEquals(arrayListOf(1,2), ints.toArrayList())
|
||||
|
||||
val doubles: Stream<Double> = src.filterIsInstance<Double>()
|
||||
assertEquals(arrayListOf(3.0), doubles.toArrayList())
|
||||
|
||||
val strings: Stream<String> = src.filterIsInstance<String>()
|
||||
assertEquals(arrayListOf("abc", "cde"), strings.toArrayList())
|
||||
|
||||
val anys: Stream<Any> = src.filterIsInstance<Any>()
|
||||
assertEquals(src.toList(), anys.toArrayList())
|
||||
|
||||
val chars: Stream<Char> = src.filterIsInstance<Char>()
|
||||
assertEquals(0, chars.toArrayList().size())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user