Collection tests: format and fix usages of deprecated symbols.
This commit is contained in:
@@ -7,28 +7,28 @@ import org.junit.Test as test
|
||||
class CollectionTest {
|
||||
|
||||
test fun joinTo() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
val buffer = StringBuilder()
|
||||
data.joinTo(buffer, "-", "{", "}")
|
||||
assertEquals("{foo-bar}", buffer.toString())
|
||||
}
|
||||
|
||||
test fun join() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
val text = data.join("-", "<", ">")
|
||||
assertEquals("<foo-bar>", text)
|
||||
|
||||
val big = arrayListOf("a", "b", "c", "d", "e", "f")
|
||||
val big = listOf("a", "b", "c", "d", "e", "f")
|
||||
val text2 = big.join(limit = 3, truncated = "*")
|
||||
assertEquals("a, b, c, *", text2)
|
||||
}
|
||||
|
||||
test fun filterNotNull() {
|
||||
val data = arrayListOf(null, "foo", null, "bar")
|
||||
val data = listOf(null, "foo", null, "bar")
|
||||
val foo = data.filterNotNull()
|
||||
|
||||
assertEquals(2, foo.size())
|
||||
assertEquals(arrayListOf("foo", "bar"), foo)
|
||||
assertEquals(listOf("foo", "bar"), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is List<String>
|
||||
@@ -36,10 +36,10 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun mapNotNull() {
|
||||
val data = arrayListOf(null, "foo", null, "bar")
|
||||
val foo = data.mapNotNull { it.length }
|
||||
val data = listOf(null, "foo", null, "bar")
|
||||
val foo = data.mapNotNull { it.length() }
|
||||
assertEquals(2, foo.size())
|
||||
assertEquals(arrayListOf(3, 3), foo)
|
||||
assertEquals(listOf(3, 3), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is List<Int>
|
||||
@@ -47,7 +47,7 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun filterIntoSet() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
val foo = data.filterTo(hashSetOf<String>()) { it.startsWith("f") }
|
||||
|
||||
assertTrue {
|
||||
@@ -64,7 +64,7 @@ class CollectionTest {
|
||||
test fun fold() {
|
||||
// lets calculate the sum of some numbers
|
||||
expect(10) {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.fold(0) { a, b -> a + b }
|
||||
}
|
||||
|
||||
@@ -75,47 +75,47 @@ class CollectionTest {
|
||||
|
||||
// lets concatenate some strings
|
||||
expect("1234") {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.fold("") { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldWithDifferentTypes() {
|
||||
expect(7) {
|
||||
val numbers = arrayListOf("a", "ab", "abc")
|
||||
val numbers = listOf("a", "ab", "abc")
|
||||
numbers.fold(1) { a, b -> a + b.length() }
|
||||
}
|
||||
|
||||
expect("1234") {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.fold("") { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldWithNonCommutativeOperation() {
|
||||
expect(1) {
|
||||
val numbers = arrayListOf(1, 2, 3)
|
||||
val numbers = listOf(1, 2, 3)
|
||||
numbers.fold(7) { a, b -> a - b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldRight() {
|
||||
expect("1234") {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.foldRight("") { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldRightWithDifferentTypes() {
|
||||
expect("1234") {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
val numbers = listOf(1, 2, 3, 4)
|
||||
numbers.foldRight("") { a, b -> "" + a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldRightWithNonCommutativeOperation() {
|
||||
expect(-5) {
|
||||
val numbers = arrayListOf(1, 2, 3)
|
||||
val numbers = listOf(1, 2, 3)
|
||||
numbers.foldRight(7) { a, b -> a - b }
|
||||
}
|
||||
}
|
||||
@@ -135,21 +135,21 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun partition() {
|
||||
val data = arrayListOf("foo", "bar", "something", "xyz")
|
||||
val data = listOf("foo", "bar", "something", "xyz")
|
||||
val pair = data.partition { it.length() == 3 }
|
||||
|
||||
assertEquals(arrayListOf("foo", "bar", "xyz"), pair.first, "pair.first")
|
||||
assertEquals(arrayListOf("something"), pair.second, "pair.second")
|
||||
assertEquals(listOf("foo", "bar", "xyz"), pair.first, "pair.first")
|
||||
assertEquals(listOf("something"), pair.second, "pair.second")
|
||||
}
|
||||
|
||||
test fun reduce() {
|
||||
expect("1234") {
|
||||
val list = arrayListOf("1", "2", "3", "4")
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
list.reduce { a, b -> a + b }
|
||||
}
|
||||
|
||||
// TODO replace with more accurate version when KT-5987 will be fixed
|
||||
// failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
// TODO replace with more accurate version when KT-5987 will be fixed
|
||||
// failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
fails {
|
||||
arrayListOf<Int>().reduce { a, b -> a + b }
|
||||
}
|
||||
@@ -157,20 +157,20 @@ class CollectionTest {
|
||||
|
||||
test fun reduceRight() {
|
||||
expect("1234") {
|
||||
val list = arrayListOf("1", "2", "3", "4")
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
list.reduceRight { a, b -> a + b }
|
||||
}
|
||||
|
||||
// TODO replace with more accurate version when KT-5987 will be fixed
|
||||
// failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
// TODO replace with more accurate version when KT-5987 will be fixed
|
||||
// failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
fails {
|
||||
arrayListOf<Int>().reduceRight { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun groupBy() {
|
||||
val words = arrayListOf("a", "abc", "ab", "def", "abcd")
|
||||
val byLength = words.groupBy { it.length }
|
||||
val words = listOf("a", "abc", "ab", "def", "abcd")
|
||||
val byLength = words.groupBy { it.length() }
|
||||
assertEquals(4, byLength.size())
|
||||
|
||||
// verify that order of keys is preserved
|
||||
@@ -197,45 +197,45 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun plus() {
|
||||
val list = arrayListOf("foo", "bar")
|
||||
val list = listOf("foo", "bar")
|
||||
val list2 = list + "cheese"
|
||||
assertEquals(arrayListOf("foo", "bar"), list)
|
||||
assertEquals(arrayListOf("foo", "bar", "cheese"), list2)
|
||||
assertEquals(listOf("foo", "bar"), list)
|
||||
assertEquals(listOf("foo", "bar", "cheese"), list2)
|
||||
|
||||
// lets use a mutable variable
|
||||
var list3 = listOf("a", "b")
|
||||
list3 += "c"
|
||||
assertEquals(arrayListOf("a", "b", "c"), list3)
|
||||
assertEquals(listOf("a", "b", "c"), list3)
|
||||
}
|
||||
|
||||
test fun plusCollectionBug() {
|
||||
val list = arrayListOf("foo", "bar") + arrayListOf("cheese", "wine")
|
||||
assertEquals(arrayListOf("foo", "bar", "cheese", "wine"), list)
|
||||
val list = listOf("foo", "bar") + listOf("cheese", "wine")
|
||||
assertEquals(listOf("foo", "bar", "cheese", "wine"), list)
|
||||
}
|
||||
|
||||
test fun plusCollection() {
|
||||
val a = arrayListOf("foo", "bar")
|
||||
val b = arrayListOf("cheese", "wine")
|
||||
val a = listOf("foo", "bar")
|
||||
val b = listOf("cheese", "wine")
|
||||
val list = a + b
|
||||
assertEquals(arrayListOf("foo", "bar", "cheese", "wine"), list)
|
||||
assertEquals(listOf("foo", "bar", "cheese", "wine"), list)
|
||||
|
||||
// lets use a mutable variable
|
||||
var ml: List<String> = a
|
||||
ml += "beer"
|
||||
ml += b
|
||||
ml += "z"
|
||||
assertEquals(arrayListOf("foo", "bar", "beer", "cheese", "wine", "z"), ml)
|
||||
assertEquals(listOf("foo", "bar", "beer", "cheese", "wine", "z"), ml)
|
||||
}
|
||||
|
||||
test fun requireNoNulls() {
|
||||
val data = arrayListOf<String?>("foo", "bar")
|
||||
val notNull = data.requireNoNulls()
|
||||
assertEquals(arrayListOf("foo", "bar"), notNull)
|
||||
assertEquals(listOf("foo", "bar"), notNull)
|
||||
|
||||
val hasNulls = arrayListOf("foo", null, "bar")
|
||||
val hasNulls = listOf("foo", null, "bar")
|
||||
|
||||
// TODO replace with more accurate version when KT-5987 will be fixed
|
||||
// failsWith(javaClass<IllegalArgumentException>()) {
|
||||
// TODO replace with more accurate version when KT-5987 will be fixed
|
||||
// failsWith(javaClass<IllegalArgumentException>()) {
|
||||
fails {
|
||||
// should throw an exception as we have a null
|
||||
hasNulls.requireNoNulls()
|
||||
@@ -243,49 +243,49 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun reverse() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
val rev = data.reverse()
|
||||
assertEquals(arrayListOf("bar", "foo"), rev)
|
||||
assertEquals(listOf("bar", "foo"), rev)
|
||||
}
|
||||
|
||||
test fun reverseFunctionShouldReturnReversedCopyForList() {
|
||||
val list: List<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 3, 2)) { list.reverse() }
|
||||
expect(arrayListOf(2, 3, 1)) { list }
|
||||
val list: List<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 3, 2)) { list.reverse() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun reverseFunctionShouldReturnReversedCopyForIterable() {
|
||||
val iterable: Iterable<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 3, 2)) { iterable.reverse() }
|
||||
expect(arrayListOf(2, 3, 1)) { iterable }
|
||||
val iterable: Iterable<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 3, 2)) { iterable.reverse() }
|
||||
expect(listOf(2, 3, 1)) { iterable }
|
||||
}
|
||||
|
||||
|
||||
test fun drop() {
|
||||
val coll = arrayListOf("foo", "bar", "abc")
|
||||
assertEquals(arrayListOf("bar", "abc"), coll.drop(1))
|
||||
assertEquals(arrayListOf("abc"), coll.drop(2))
|
||||
val coll = listOf("foo", "bar", "abc")
|
||||
assertEquals(listOf("bar", "abc"), coll.drop(1))
|
||||
assertEquals(listOf("abc"), coll.drop(2))
|
||||
}
|
||||
|
||||
test fun dropWhile() {
|
||||
val coll = arrayListOf("foo", "bar", "abc")
|
||||
assertEquals(arrayListOf("bar", "abc"), coll.dropWhile { it.startsWith("f") })
|
||||
val coll = listOf("foo", "bar", "abc")
|
||||
assertEquals(listOf("bar", "abc"), coll.dropWhile { it.startsWith("f") })
|
||||
}
|
||||
|
||||
test fun take() {
|
||||
val coll = arrayListOf("foo", "bar", "abc")
|
||||
assertEquals(arrayListOf("foo"), coll.take(1))
|
||||
assertEquals(arrayListOf("foo", "bar"), coll.take(2))
|
||||
val coll = listOf("foo", "bar", "abc")
|
||||
assertEquals(listOf("foo"), coll.take(1))
|
||||
assertEquals(listOf("foo", "bar"), coll.take(2))
|
||||
}
|
||||
|
||||
test fun takeWhile() {
|
||||
val coll = arrayListOf("foo", "bar", "abc")
|
||||
assertEquals(arrayListOf("foo"), coll.takeWhile { it.startsWith("f") })
|
||||
assertEquals(arrayListOf("foo", "bar", "abc"), coll.takeWhile { it.length() == 3 })
|
||||
val coll = listOf("foo", "bar", "abc")
|
||||
assertEquals(listOf("foo"), coll.takeWhile { it.startsWith("f") })
|
||||
assertEquals(listOf("foo", "bar", "abc"), coll.takeWhile { it.length() == 3 })
|
||||
}
|
||||
|
||||
test fun copyToArray() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
val arr = data.copyToArray()
|
||||
println("Got array ${arr}")
|
||||
assertEquals(2, arr.size())
|
||||
@@ -297,25 +297,25 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun count() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
assertEquals(2, data.count())
|
||||
assertEquals(3, hashSetOf(12, 14, 15).count())
|
||||
assertEquals(0, ArrayList<Double>().count())
|
||||
}
|
||||
|
||||
test fun first() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
assertEquals("foo", data.first())
|
||||
assertEquals(15, arrayListOf(15, 19, 20, 25).first())
|
||||
assertEquals('a', arrayListOf('a').first())
|
||||
assertEquals(15, listOf(15, 19, 20, 25).first())
|
||||
assertEquals('a', listOf('a').first())
|
||||
fails { arrayListOf<Int>().first() }
|
||||
}
|
||||
|
||||
test fun last() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
assertEquals("bar", data.last())
|
||||
assertEquals(25, arrayListOf(15, 19, 20, 25).last())
|
||||
assertEquals('a', arrayListOf('a').last())
|
||||
assertEquals(25, listOf(15, 19, 20, 25).last())
|
||||
assertEquals('a', listOf('a').last())
|
||||
fails { arrayListOf<Int>().last() }
|
||||
}
|
||||
|
||||
@@ -339,11 +339,11 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
list.add("works")
|
||||
assertEquals(arrayListOf("new", "thing", "works"), list)
|
||||
assertEquals(listOf("new", "thing", "works"), list)
|
||||
}
|
||||
|
||||
test fun indices() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val data = listOf("foo", "bar")
|
||||
val indices = data.indices
|
||||
assertEquals(0, indices.start)
|
||||
assertEquals(1, indices.end)
|
||||
@@ -352,21 +352,21 @@ class CollectionTest {
|
||||
|
||||
test fun contains() {
|
||||
assertFalse(hashSetOf<Int>().contains(12))
|
||||
assertTrue(arrayListOf(15, 19, 20).contains(15))
|
||||
assertTrue(listOf(15, 19, 20).contains(15))
|
||||
|
||||
assertTrue(IterableWrapper(hashSetOf(45, 14, 13)).contains(14))
|
||||
}
|
||||
|
||||
test fun sortForMutableIterable() {
|
||||
val list: MutableIterable<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 2, 3)) { list.sort() }
|
||||
expect(arrayListOf(2, 3, 1)) { list }
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun sortForIterable() {
|
||||
val list: Iterable<Int> = listOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 2, 3)) { list.sort() }
|
||||
expect(arrayListOf(2, 3, 1)) { list }
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun min() {
|
||||
@@ -396,7 +396,7 @@ class CollectionTest {
|
||||
expect(1, { listOf(1).minBy { it } })
|
||||
expect(3, { listOf(2, 3).minBy { -it } })
|
||||
expect('a', { listOf('a', 'b').minBy { "x$it" } })
|
||||
expect("b", { listOf("b", "abc").minBy { it.length } })
|
||||
expect("b", { listOf("b", "abc").minBy { it.length() } })
|
||||
expect(null, { listOf<Int>().stream().minBy { it } })
|
||||
expect(3, { listOf(2, 3).stream().minBy { -it } })
|
||||
}
|
||||
@@ -406,7 +406,7 @@ class CollectionTest {
|
||||
expect(1, { listOf(1).maxBy { it } })
|
||||
expect(2, { listOf(2, 3).maxBy { -it } })
|
||||
expect('b', { listOf('a', 'b').maxBy { "x$it" } })
|
||||
expect("abc", { listOf("b", "abc").maxBy { it.length } })
|
||||
expect("abc", { listOf("b", "abc").maxBy { it.length() } })
|
||||
expect(null, { listOf<Int>().stream().maxBy { it } })
|
||||
expect(2, { listOf(2, 3).stream().maxBy { -it } })
|
||||
}
|
||||
@@ -431,8 +431,8 @@ class CollectionTest {
|
||||
|
||||
test fun sum() {
|
||||
expect(0) { arrayListOf<Int>().sum() }
|
||||
expect(14) { arrayListOf(2, 3, 9).sum() }
|
||||
expect(3.0) { arrayListOf(1.0, 2.0).sum() }
|
||||
expect(14) { listOf(2, 3, 9).sum() }
|
||||
expect(3.0) { listOf(1.0, 2.0).sum() }
|
||||
expect(3000000000000) { arrayListOf<Long>(1000000000000, 2000000000000).sum() }
|
||||
expect(3.0.toFloat()) { arrayListOf<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
|
||||
expect(3.0.toFloat()) { streamOf<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
|
||||
@@ -450,27 +450,27 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun sortBy() {
|
||||
expect(arrayListOf("two" to 2, "three" to 3)) {
|
||||
arrayListOf("three" to 3, "two" to 2).sortBy { it.second }
|
||||
expect(listOf("two" to 2, "three" to 3)) {
|
||||
listOf("three" to 3, "two" to 2).sortBy { it.second }
|
||||
}
|
||||
expect(arrayListOf("three" to 3, "two" to 2)) {
|
||||
arrayListOf("three" to 3, "two" to 2).sortBy { it.first }
|
||||
expect(listOf("three" to 3, "two" to 2)) {
|
||||
listOf("three" to 3, "two" to 2).sortBy { it.first }
|
||||
}
|
||||
expect(arrayListOf("two" to 2, "three" to 3)) {
|
||||
arrayListOf("three" to 3, "two" to 2).sortBy { it.first.length }
|
||||
expect(listOf("two" to 2, "three" to 3)) {
|
||||
listOf("three" to 3, "two" to 2).sortBy { it.first.length() }
|
||||
}
|
||||
}
|
||||
|
||||
test fun sortFunctionShouldReturnSortedCopyForList() {
|
||||
val list: List<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 2, 3)) { list.sort() }
|
||||
expect(arrayListOf(2, 3, 1)) { list }
|
||||
val list: List<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun sortFunctionShouldReturnSortedCopyForIterable() {
|
||||
val list: Iterable<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 2, 3)) { list.sort() }
|
||||
expect(arrayListOf(2, 3, 1)) { list }
|
||||
val list: Iterable<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun decomposeFirst() {
|
||||
|
||||
Reference in New Issue
Block a user