Minor: normalize '@Test' annotation casing in all tests.

This commit is contained in:
Ilya Gorbunov
2016-11-16 21:26:39 +03:00
parent 50cd620f92
commit 6a70761783
48 changed files with 749 additions and 751 deletions
+100 -100
View File
@@ -19,7 +19,7 @@ package test.collections
import test.collections.behaviors.listBehavior
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
import kotlin.test.*
import org.junit.Test as test
import org.junit.Test
import kotlin.comparisons.*
fun <T> assertArrayNotSameButEquals(expected: Array<out T>, actual: Array<out T>, message: String = "") { assertTrue(expected !== actual && expected contentEquals actual, message) }
@@ -39,7 +39,7 @@ class ArraysTest {
override fun hashCode(): Int = value
}
@test fun orEmptyNull() {
@Test fun orEmptyNull() {
val x: Array<String>? = null
val y: Array<out String>? = null
val xArray = x.orEmpty()
@@ -48,7 +48,7 @@ class ArraysTest {
expect(0) { yArray.size }
}
@test fun orEmptyNotNull() {
@Test fun orEmptyNotNull() {
val x: Array<String>? = arrayOf("1", "2")
val xArray = x.orEmpty()
expect(2) { xArray.size }
@@ -56,7 +56,7 @@ class ArraysTest {
expect("2") { xArray[1] }
}
@test fun emptyArrayLastIndex() {
@Test fun emptyArrayLastIndex() {
val arr1 = IntArray(0)
assertEquals(-1, arr1.lastIndex)
@@ -64,7 +64,7 @@ class ArraysTest {
assertEquals(-1, arr2.lastIndex)
}
@test fun arrayLastIndex() {
@Test fun arrayLastIndex() {
val arr1 = intArrayOf(0, 1, 2, 3, 4)
assertEquals(4, arr1.lastIndex)
assertEquals(4, arr1[arr1.lastIndex])
@@ -74,7 +74,7 @@ class ArraysTest {
assertEquals("4", arr2[arr2.lastIndex])
}
@test fun byteArray() {
@Test fun byteArray() {
val arr = ByteArray(2)
val expected: Byte = 0
@@ -83,7 +83,7 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun byteArrayInit() {
@Test fun byteArrayInit() {
val arr = ByteArray(2) { it.toByte() }
assertEquals(2, arr.size)
@@ -91,7 +91,7 @@ class ArraysTest {
assertEquals(1.toByte(), arr[1])
}
@test fun shortArray() {
@Test fun shortArray() {
val arr = ShortArray(2)
val expected: Short = 0
@@ -100,7 +100,7 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun shortArrayInit() {
@Test fun shortArrayInit() {
val arr = ShortArray(2) { it.toShort() }
assertEquals(2, arr.size)
@@ -108,7 +108,7 @@ class ArraysTest {
assertEquals(1.toShort(), arr[1])
}
@test fun intArray() {
@Test fun intArray() {
val arr = IntArray(2)
assertEquals(arr.size, 2)
@@ -116,7 +116,7 @@ class ArraysTest {
assertEquals(0, arr[1])
}
@test fun intArrayInit() {
@Test fun intArrayInit() {
val arr = IntArray(2) { it.toInt() }
assertEquals(2, arr.size)
@@ -124,7 +124,7 @@ class ArraysTest {
assertEquals(1.toInt(), arr[1])
}
@test fun longArray() {
@Test fun longArray() {
val arr = LongArray(2)
val expected: Long = 0
@@ -133,7 +133,7 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun longArrayInit() {
@Test fun longArrayInit() {
val arr = LongArray(2) { it.toLong() }
assertEquals(2, arr.size)
@@ -141,7 +141,7 @@ class ArraysTest {
assertEquals(1.toLong(), arr[1])
}
@test fun floatArray() {
@Test fun floatArray() {
val arr = FloatArray(2)
val expected: Float = 0.0F
@@ -150,7 +150,7 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun floatArrayInit() {
@Test fun floatArrayInit() {
val arr = FloatArray(2) { it.toFloat() }
assertEquals(2, arr.size)
@@ -158,7 +158,7 @@ class ArraysTest {
assertEquals(1.toFloat(), arr[1])
}
@test fun doubleArray() {
@Test fun doubleArray() {
val arr = DoubleArray(2)
assertEquals(arr.size, 2)
@@ -166,7 +166,7 @@ class ArraysTest {
assertEquals(0.0, arr[1])
}
@test fun doubleArrayInit() {
@Test fun doubleArrayInit() {
val arr = DoubleArray(2) { it.toDouble() }
assertEquals(2, arr.size)
@@ -174,7 +174,7 @@ class ArraysTest {
assertEquals(1.toDouble(), arr[1])
}
@test fun charArray() {
@Test fun charArray() {
val arr = CharArray(2)
val expected: Char = '\u0000'
@@ -183,7 +183,7 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun charArrayInit() {
@Test fun charArrayInit() {
val arr = CharArray(2) { 'a' + it }
assertEquals(2, arr.size)
@@ -191,14 +191,14 @@ class ArraysTest {
assertEquals('b', arr[1])
}
@test fun booleanArray() {
@Test fun booleanArray() {
val arr = BooleanArray(2)
assertEquals(arr.size, 2)
assertEquals(false, arr[0])
assertEquals(false, arr[1])
}
@test fun booleanArrayInit() {
@Test fun booleanArrayInit() {
val arr = BooleanArray(2) { it % 2 == 0 }
assertEquals(2, arr.size)
@@ -206,7 +206,7 @@ class ArraysTest {
assertEquals(false, arr[1])
}
@test fun contentEquals() {
@Test fun contentEquals() {
val arr1 = arrayOf("a", 1, null)
val arr2 = arrayOf(*arr1)
assertTrue(arr1 contentEquals arr2)
@@ -214,7 +214,7 @@ class ArraysTest {
assertFalse(arr1 contentEquals arr3)
}
@test fun contentDeepEquals() {
@Test fun contentDeepEquals() {
val arr1 = arrayOf("a", 1, intArrayOf(2))
val arr2 = arrayOf("a", 1, intArrayOf(2))
assertFalse(arr1 contentEquals arr2)
@@ -223,17 +223,17 @@ class ArraysTest {
assertFalse(arr1 contentDeepEquals arr2)
}
@test fun contentToString() {
@Test fun contentToString() {
val arr = arrayOf("a", 1, null)
assertEquals(arr.asList().toString(), arr.contentToString())
}
@test fun contentDeepToString() {
@Test fun contentDeepToString() {
val arr = arrayOf("aa", 1, null, charArrayOf('d'))
assertEquals("[aa, 1, null, [d]]", arr.contentDeepToString())
}
@test fun contentDeepToStringNoRecursion() {
@Test fun contentDeepToStringNoRecursion() {
// a[b[a, b]]
val b = arrayOfNulls<Any>(2)
val a = arrayOf(b)
@@ -247,19 +247,19 @@ class ArraysTest {
assertEquals("[[[...], [...]]]", result)
}
@test fun contentHashCode() {
@Test fun contentHashCode() {
val arr = arrayOf("a", 1, null, Value(5))
assertEquals(listOf(*arr).hashCode(), arr.contentHashCode())
assertEquals((1*31 + 2)*31 + 3, arrayOf(Value(2), Value(3)).contentHashCode())
}
@test fun contentDeepHashCode() {
@Test fun contentDeepHashCode() {
val arr = arrayOf(null, Value(2), arrayOf(Value(3)))
assertEquals(((1*31 + 0)*31 + 2) * 31 + (1 * 31 + 3), arr.contentDeepHashCode())
}
@test fun min() {
@Test fun min() {
expect(null, { arrayOf<Int>().min() })
expect(1, { arrayOf(1).min() })
expect(2, { arrayOf(2, 3).min() })
@@ -268,7 +268,7 @@ class ArraysTest {
expect("a", { arrayOf("a", "b").min() })
}
@test fun minInPrimitiveArrays() {
@Test fun minInPrimitiveArrays() {
expect(null, { intArrayOf().min() })
expect(1, { intArrayOf(1).min() })
expect(2, { intArrayOf(2, 3).min() })
@@ -280,7 +280,7 @@ class ArraysTest {
expect('a', { charArrayOf('a', 'b').min() })
}
@test fun max() {
@Test fun max() {
expect(null, { arrayOf<Int>().max() })
expect(1, { arrayOf(1).max() })
expect(3, { arrayOf(2, 3).max() })
@@ -289,7 +289,7 @@ class ArraysTest {
expect("b", { arrayOf("a", "b").max() })
}
@test fun maxInPrimitiveArrays() {
@Test fun maxInPrimitiveArrays() {
expect(null, { intArrayOf().max() })
expect(1, { intArrayOf(1).max() })
expect(3, { intArrayOf(2, 3).max() })
@@ -301,29 +301,29 @@ class ArraysTest {
expect('b', { charArrayOf('a', 'b').max() })
}
@test fun minWith() {
@Test fun minWith() {
assertEquals(null, arrayOf<Int>().minWith(naturalOrder()) )
assertEquals("a", arrayOf("a", "B").minWith(STRING_CASE_INSENSITIVE_ORDER))
}
@test fun minWithInPrimitiveArrays() {
@Test fun minWithInPrimitiveArrays() {
expect(null, { intArrayOf().minWith(naturalOrder()) })
expect(1, { intArrayOf(1).minWith(naturalOrder()) })
expect(4, { intArrayOf(2, 3, 4).minWith(compareBy { it % 4 }) })
}
@test fun maxWith() {
@Test fun maxWith() {
assertEquals(null, arrayOf<Int>().maxWith(naturalOrder()) )
assertEquals("B", arrayOf("a", "B").maxWith(STRING_CASE_INSENSITIVE_ORDER))
}
@test fun maxWithInPrimitiveArrays() {
@Test fun maxWithInPrimitiveArrays() {
expect(null, { intArrayOf().maxWith(naturalOrder()) })
expect(1, { intArrayOf(1).maxWith(naturalOrder()) })
expect(-4, { intArrayOf(2, 3, -4).maxWith(compareBy { it*it }) })
}
@test fun minBy() {
@Test fun minBy() {
expect(null, { arrayOf<Int>().minBy { it } })
expect(1, { arrayOf(1).minBy { it } })
expect(3, { arrayOf(2, 3).minBy { -it } })
@@ -331,7 +331,7 @@ class ArraysTest {
expect("b", { arrayOf("b", "abc").minBy { it.length } })
}
@test fun minByInPrimitiveArrays() {
@Test fun minByInPrimitiveArrays() {
expect(null, { intArrayOf().minBy { it } })
expect(1, { intArrayOf(1).minBy { it } })
expect(3, { intArrayOf(2, 3).minBy { -it } })
@@ -342,7 +342,7 @@ class ArraysTest {
expect(2.0, { doubleArrayOf(2.0, 3.0).minBy { Math.sqrt(it) } })
}
@test fun maxBy() {
@Test fun maxBy() {
expect(null, { arrayOf<Int>().maxBy { it } })
expect(1, { arrayOf(1).maxBy { it } })
expect(2, { arrayOf(2, 3).maxBy { -it } })
@@ -350,7 +350,7 @@ class ArraysTest {
expect("abc", { arrayOf("b", "abc").maxBy { it.length } })
}
@test fun maxByInPrimitiveArrays() {
@Test fun maxByInPrimitiveArrays() {
expect(null, { intArrayOf().maxBy { it } })
expect(1, { intArrayOf(1).maxBy { it } })
expect(2, { intArrayOf(2, 3).maxBy { -it } })
@@ -361,29 +361,29 @@ class ArraysTest {
expect(3.0, { doubleArrayOf(2.0, 3.0).maxBy { Math.sqrt(it) } })
}
@test fun minIndex() {
@Test fun minIndex() {
val a = intArrayOf(1, 7, 9, -42, 54, 93)
expect(3, { a.indices.minBy { a[it] } })
}
@test fun maxIndex() {
@Test fun maxIndex() {
val a = intArrayOf(1, 7, 9, 239, 54, 93)
expect(3, { a.indices.maxBy { a[it] } })
}
@test fun minByEvaluateOnce() {
@Test fun minByEvaluateOnce() {
var c = 0
expect(1, { arrayOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
assertEquals(5, c)
}
@test fun maxByEvaluateOnce() {
@Test fun maxByEvaluateOnce() {
var c = 0
expect(5, { arrayOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
assertEquals(5, c)
}
@test fun sum() {
@Test fun sum() {
expect(0) { arrayOf<Int>().sum() }
expect(14) { arrayOf(2, 3, 9).sum() }
expect(3.0) { arrayOf(1.0, 2.0).sum() }
@@ -393,7 +393,7 @@ class ArraysTest {
expect(3.0F) { arrayOf<Float>(1.0F, 2.0F).sum() }
}
@test fun sumInPrimitiveArrays() {
@Test fun sumInPrimitiveArrays() {
expect(0) { intArrayOf().sum() }
expect(14) { intArrayOf(2, 3, 9).sum() }
expect(3.0) { doubleArrayOf(1.0, 2.0).sum() }
@@ -403,7 +403,7 @@ class ArraysTest {
expect(3.0F) { floatArrayOf(1.0F, 2.0F).sum() }
}
@test fun average() {
@Test fun average() {
expect(0.0) { arrayOf<Int>().average() }
expect(3.8) { arrayOf(1, 2, 5, 8, 3).average() }
expect(2.1) { arrayOf(1.6, 2.6, 3.6, 0.6).average() }
@@ -414,7 +414,7 @@ class ArraysTest {
// for each arr with size > 0 arr.average() = arr.sum().toDouble() / arr.size()
}
@test fun indexOfInPrimitiveArrays() {
@Test fun indexOfInPrimitiveArrays() {
expect(-1) { byteArrayOf(1, 2, 3).indexOf(0) }
expect(0) { byteArrayOf(1, 2, 3).indexOf(1) }
expect(1) { byteArrayOf(1, 2, 3).indexOf(2) }
@@ -455,7 +455,7 @@ class ArraysTest {
expect(-1) { booleanArrayOf(true).indexOf(false) }
}
@test fun indexOf() {
@Test fun indexOf() {
expect(-1) { arrayOf("cat", "dog", "bird").indexOf("mouse") }
expect(0) { arrayOf("cat", "dog", "bird").indexOf("cat") }
expect(1) { arrayOf("cat", "dog", "bird").indexOf("dog") }
@@ -473,7 +473,7 @@ class ArraysTest {
expect(2) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
}
@test fun lastIndexOf() {
@Test fun lastIndexOf() {
expect(-1) { arrayOf("cat", "dog", "bird").lastIndexOf("mouse") }
expect(0) { arrayOf("cat", "dog", "bird").lastIndexOf("cat") }
expect(1) { arrayOf("cat", "dog", "bird").lastIndexOf("dog") }
@@ -493,7 +493,7 @@ class ArraysTest {
expect(3) { sequenceOf("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
}
@test fun isEmpty() {
@Test fun isEmpty() {
assertTrue(emptyArray<String>().isEmpty())
assertFalse(arrayOf("").isEmpty())
assertTrue(intArrayOf().isEmpty())
@@ -514,12 +514,12 @@ class ArraysTest {
assertFalse(booleanArrayOf(false).isEmpty())
}
@test fun isNotEmpty() {
@Test fun isNotEmpty() {
assertFalse(intArrayOf().isNotEmpty())
assertTrue(intArrayOf(1).isNotEmpty())
}
@test fun plusInference() {
@Test fun plusInference() {
val arrayOfArrays: Array<Array<out Any>> = arrayOf(arrayOf<Any>("s") as Array<out Any>)
val elementArray = arrayOf<Any>("a") as Array<out Any>
val arrayPlusElement: Array<Array<out Any>> = arrayOfArrays.plusElement(elementArray)
@@ -533,7 +533,7 @@ class ArraysTest {
}
@test fun plus() {
@Test fun plus() {
assertEquals(listOf("1", "2", "3", "4"), listOf("1", "2") + arrayOf("3", "4"))
assertArrayNotSameButEquals(arrayOf("1", "2", "3"), arrayOf("1", "2") + "3")
assertArrayNotSameButEquals(arrayOf("1", "2", "3", "4"), arrayOf("1", "2") + arrayOf("3", "4"))
@@ -543,7 +543,7 @@ class ArraysTest {
assertArrayNotSameButEquals(intArrayOf(1, 2, 3, 4), intArrayOf(1, 2) + intArrayOf(3, 4))
}
@test fun plusVararg() {
@Test fun plusVararg() {
fun stringOnePlus(vararg a: String) = arrayOf("1") + a
fun longOnePlus(vararg a: Long) = longArrayOf(1) + a
fun intOnePlus(vararg a: Int) = intArrayOf(1) + a
@@ -553,7 +553,7 @@ class ArraysTest {
assertArrayNotSameButEquals(longArrayOf(1, 2), longOnePlus(2), "LongArray.plus")
}
@test fun plusAssign() {
@Test fun plusAssign() {
// lets use a mutable variable
var result = arrayOf("a")
result += "foo"
@@ -562,23 +562,23 @@ class ArraysTest {
assertArrayNotSameButEquals(arrayOf("a", "foo", "beer", "cheese", "wine"), result)
}
@test fun first() {
@Test fun first() {
expect(1) { arrayOf(1, 2, 3).first() }
expect(2) { arrayOf(1, 2, 3).first { it % 2 == 0 } }
}
@test fun last() {
@Test fun last() {
expect(3) { arrayOf(1, 2, 3).last() }
expect(2) { arrayOf(1, 2, 3).last { it % 2 == 0 } }
}
@test fun contains() {
@Test fun contains() {
assertTrue(arrayOf("1", "2", "3", "4").contains("2"))
assertTrue("3" in arrayOf("1", "2", "3", "4"))
assertTrue("0" !in arrayOf("1", "2", "3", "4"))
}
@test fun slice() {
@Test fun slice() {
val iter: Iterable<Int> = listOf(3, 1, 2)
assertEquals(listOf("B"), arrayOf("A", "B", "C").slice(1..1))
@@ -595,7 +595,7 @@ class ArraysTest {
assertEquals(listOf(true, false, true), booleanArrayOf(true, false, true, true).slice(iter))
}
@test fun sliceArray() {
@Test fun sliceArray() {
val coll: Collection<Int> = listOf(3, 1, 2)
assertArrayNotSameButEquals(arrayOf("B"), arrayOf("A", "B", "C").sliceArray(1..1))
@@ -614,7 +614,7 @@ class ArraysTest {
assertArrayNotSameButEquals(booleanArrayOf(true, false, true), booleanArrayOf(true, false, true, true).sliceArray(coll))
}
@test fun asIterable() {
@Test fun asIterable() {
val arr1 = intArrayOf(1, 2, 3, 4, 5)
val iter1 = arr1.asIterable()
assertEquals(arr1.toList(), iter1.toList())
@@ -636,7 +636,7 @@ class ArraysTest {
assertEquals(iter4.toList(), emptyList<String>())
}
@test fun asList() {
@Test fun asList() {
compare(listOf(1, 2, 3), intArrayOf(1, 2, 3).asList()) { listBehavior() }
compare(listOf<Byte>(1, 2, 3), byteArrayOf(1, 2, 3).asList()) { listBehavior() }
compare(listOf(true, false), booleanArrayOf(true, false).asList()) { listBehavior() }
@@ -651,7 +651,7 @@ class ArraysTest {
assertEquals(10, intsAsList[1], "Should reflect changes in original array")
}
@test fun toPrimitiveArray() {
@Test fun toPrimitiveArray() {
val genericArray: Array<Int> = arrayOf(1, 2, 3)
val primitiveArray: IntArray = genericArray.toIntArray()
expect(3) { primitiveArray.size }
@@ -663,14 +663,14 @@ class ArraysTest {
assertEquals(charList, charArray.asList())
}
@test fun toTypedArray() {
@Test fun toTypedArray() {
val primitiveArray: LongArray = longArrayOf(1, 2, Long.MAX_VALUE)
val genericArray: Array<Long> = primitiveArray.toTypedArray()
expect(3) { genericArray.size }
assertEquals(primitiveArray.asList(), genericArray.asList())
}
@test fun copyOf() {
@Test fun copyOf() {
booleanArrayOf(true, false, true).let { assertArrayNotSameButEquals(it, it.copyOf()) }
byteArrayOf(0, 1, 2, 3, 4, 5).let { assertArrayNotSameButEquals(it, it.copyOf()) }
shortArrayOf(0, 1, 2, 3, 4, 5).let { assertArrayNotSameButEquals(it, it.copyOf()) }
@@ -681,7 +681,7 @@ class ArraysTest {
charArrayOf('0', '1', '2', '3', '4', '5').let { assertArrayNotSameButEquals(it, it.copyOf()) }
}
@test fun copyAndResize() {
@Test fun copyAndResize() {
assertArrayNotSameButEquals(arrayOf("1", "2"), arrayOf("1", "2", "3").copyOf(2))
assertArrayNotSameButEquals(arrayOf("1", "2", null), arrayOf("1", "2").copyOf(3))
@@ -695,7 +695,7 @@ class ArraysTest {
assertArrayNotSameButEquals(charArrayOf('A', 'B', '\u0000'), charArrayOf('A', 'B').copyOf(3))
}
@test fun copyOfRange() {
@Test fun copyOfRange() {
assertArrayNotSameButEquals(booleanArrayOf(true, false, true), booleanArrayOf(true, false, true, true).copyOfRange(0, 3))
assertArrayNotSameButEquals(byteArrayOf(0, 1, 2), byteArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
assertArrayNotSameButEquals(shortArrayOf(0, 1, 2), shortArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
@@ -708,7 +708,7 @@ class ArraysTest {
@test fun reduceIndexed() {
@Test fun reduceIndexed() {
expect(-1) { intArrayOf(1, 2, 3).reduceIndexed { index, a, b -> index + a - b } }
expect(-1.toLong()) { longArrayOf(1, 2, 3).reduceIndexed { index, a, b -> index + a - b } }
expect(-1F) { floatArrayOf(1F, 2F, 3F).reduceIndexed { index, a, b -> index + a - b } }
@@ -724,7 +724,7 @@ class ArraysTest {
}
}
@test fun reduceRightIndexed() {
@Test fun reduceRightIndexed() {
expect(1) { intArrayOf(1, 2, 3).reduceRightIndexed { index, a, b -> index + a - b } }
expect(1.toLong()) { longArrayOf(1, 2, 3).reduceRightIndexed { index, a, b -> index + a - b } }
expect(1F) { floatArrayOf(1F, 2F, 3F).reduceRightIndexed { index, a, b -> index + a - b } }
@@ -740,7 +740,7 @@ class ArraysTest {
}
}
@test fun reduce() {
@Test fun reduce() {
expect(-4) { intArrayOf(1, 2, 3).reduce { a, b -> a - b } }
expect(-4.toLong()) { longArrayOf(1, 2, 3).reduce { a, b -> a - b } }
expect(-4F) { floatArrayOf(1F, 2F, 3F).reduce { a, b -> a - b } }
@@ -756,7 +756,7 @@ class ArraysTest {
}
}
@test fun reduceRight() {
@Test fun reduceRight() {
expect(2) { intArrayOf(1, 2, 3).reduceRight { a, b -> a - b } }
expect(2.toLong()) { longArrayOf(1, 2, 3).reduceRight { a, b -> a - b } }
expect(2F) { floatArrayOf(1F, 2F, 3F).reduceRight { a, b -> a - b } }
@@ -772,7 +772,7 @@ class ArraysTest {
}
}
@test fun reverseInPlace() {
@Test fun reverseInPlace() {
fun <TArray, T> doTest(build: Iterable<Int>.() -> TArray, reverse: TArray.() -> Unit, snapshot: TArray.() -> List<T>) {
val arrays = (0..4).map { n -> (1..n).build() }
@@ -797,7 +797,7 @@ class ArraysTest {
}
@test fun reversed() {
@Test fun reversed() {
expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reversed() }
expect(listOf<Byte>(3, 2, 1)) { byteArrayOf(1, 2, 3).reversed() }
expect(listOf<Short>(3, 2, 1)) { shortArrayOf(1, 2, 3).reversed() }
@@ -809,7 +809,7 @@ class ArraysTest {
expect(listOf("3", "2", "1")) { arrayOf("1", "2", "3").reversed() }
}
@test fun reversedArray() {
@Test fun reversedArray() {
assertArrayNotSameButEquals(intArrayOf(3, 2, 1), intArrayOf(1, 2, 3).reversedArray())
assertArrayNotSameButEquals(byteArrayOf(3, 2, 1), byteArrayOf(1, 2, 3).reversedArray())
assertArrayNotSameButEquals(shortArrayOf(3, 2, 1), shortArrayOf(1, 2, 3).reversedArray())
@@ -822,7 +822,7 @@ class ArraysTest {
assertArrayNotSameButEquals(arrayOf("3", "2", "1"), (arrayOf("1", "2", "3") as Array<out String>).reversedArray())
}
@test fun drop() {
@Test fun drop() {
expect(listOf(1), { intArrayOf(1).drop(0) })
expect(listOf(), { intArrayOf().drop(1) })
expect(listOf(), { intArrayOf(1).drop(1) })
@@ -840,7 +840,7 @@ class ArraysTest {
}
}
@test fun dropLast() {
@Test fun dropLast() {
expect(listOf(), { intArrayOf().dropLast(1) })
expect(listOf(), { intArrayOf(1).dropLast(1) })
expect(listOf(1), { intArrayOf(1).dropLast(0) })
@@ -858,7 +858,7 @@ class ArraysTest {
}
}
@test fun dropWhile() {
@Test fun dropWhile() {
expect(listOf(), { intArrayOf().dropWhile { it < 3 } })
expect(listOf(), { intArrayOf(1).dropWhile { it < 3 } })
expect(listOf(3, 1), { intArrayOf(2, 3, 1).dropWhile { it < 3 } })
@@ -872,7 +872,7 @@ class ArraysTest {
expect(listOf("b", "a"), { arrayOf("a", "b", "a").dropWhile { it < "b" } })
}
@test fun dropLastWhile() {
@Test fun dropLastWhile() {
expect(listOf(), { intArrayOf().dropLastWhile { it < 3 } })
expect(listOf(), { intArrayOf(1).dropLastWhile { it < 3 } })
expect(listOf(2, 3), { intArrayOf(2, 3, 1).dropLastWhile { it < 3 } })
@@ -886,7 +886,7 @@ class ArraysTest {
expect(listOf("a", "b"), { arrayOf("a", "b", "a").dropLastWhile { it < "b" } })
}
@test fun take() {
@Test fun take() {
expect(listOf(), { intArrayOf().take(1) })
expect(listOf(), { intArrayOf(1).take(0) })
expect(listOf(1), { intArrayOf(1).take(1) })
@@ -904,7 +904,7 @@ class ArraysTest {
}
}
@test fun takeLast() {
@Test fun takeLast() {
expect(listOf(), { intArrayOf().takeLast(1) })
expect(listOf(), { intArrayOf(1).takeLast(0) })
expect(listOf(1), { intArrayOf(1).takeLast(1) })
@@ -922,7 +922,7 @@ class ArraysTest {
}
}
@test fun takeWhile() {
@Test fun takeWhile() {
expect(listOf(), { intArrayOf().takeWhile { it < 3 } })
expect(listOf(1), { intArrayOf(1).takeWhile { it < 3 } })
expect(listOf(2), { intArrayOf(2, 3, 1).takeWhile { it < 3 } })
@@ -936,7 +936,7 @@ class ArraysTest {
expect(listOf("a"), { arrayOf("a", "c", "b").takeWhile { it < "c" } })
}
@test fun takeLastWhile() {
@Test fun takeLastWhile() {
expect(listOf(), { intArrayOf().takeLastWhile { it < 3 } })
expect(listOf(1), { intArrayOf(1).takeLastWhile { it < 3 } })
expect(listOf(1), { intArrayOf(2, 3, 1).takeLastWhile { it < 3 } })
@@ -950,7 +950,7 @@ class ArraysTest {
expect(listOf("b"), { arrayOf("a", "c", "b").takeLastWhile { it < "c" } })
}
@test fun filter() {
@Test fun filter() {
expect(listOf(), { intArrayOf().filter { it > 2 } })
expect(listOf(), { intArrayOf(1).filter { it > 2 } })
expect(listOf(3), { intArrayOf(2, 3).filter { it > 2 } })
@@ -964,7 +964,7 @@ class ArraysTest {
expect(listOf("b"), { arrayOf("a", "b").filter { it > "a" } })
}
@test fun filterIndexed() {
@Test fun filterIndexed() {
expect(listOf(), { intArrayOf().filterIndexed { i, v -> i > v } })
expect(listOf(2, 5, 8), { intArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == value % 2 } })
expect(listOf<Long>(2, 5, 8), { longArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } })
@@ -973,7 +973,7 @@ class ArraysTest {
expect(listOf("a", "c", "d"), { arrayOf("a", "b", "c", "d").filterIndexed { index, s -> s == "a" || index >= 2 } })
}
@test fun filterNot() {
@Test fun filterNot() {
expect(listOf(), { intArrayOf().filterNot { it > 2 } })
expect(listOf(1), { intArrayOf(1).filterNot { it > 2 } })
expect(listOf(2), { intArrayOf(2, 3).filterNot { it > 2 } })
@@ -987,32 +987,32 @@ class ArraysTest {
expect(listOf("a"), { arrayOf("a", "b").filterNot { it > "a" } })
}
@test fun filterNotNull() {
@Test fun filterNotNull() {
expect(listOf("a"), { arrayOf("a", null).filterNotNull() })
}
@test fun map() {
@Test fun map() {
assertEquals(listOf(1, 2, 4), arrayOf("a", "bc", "test").map { it.length })
assertEquals(listOf('a', 'b', 'c'), intArrayOf(1, 2, 3).map { 'a' + it - 1 })
assertEquals(listOf(1, 2, 3), longArrayOf(1000, 2000, 3000).map { (it / 1000).toInt() })
assertEquals(listOf(1.0, 0.5, 0.4, 0.2, 0.1), doubleArrayOf(1.0, 2.0, 2.5, 5.0, 10.0).map { 1 / it })
}
@test fun mapIndexed() {
@Test fun mapIndexed() {
assertEquals(listOf(1, 1, 2), arrayOf("a", "bc", "test").mapIndexed { index, s -> s.length - index })
assertEquals(listOf(0, 2, 2), intArrayOf(3, 2, 1).mapIndexed { index, i -> i * index })
assertEquals(listOf("0;20", "1;21", "2;22"), longArrayOf(20, 21, 22).mapIndexed { index, it -> "$index;$it" })
}
@test fun mapNotNull() {
@Test fun mapNotNull() {
assertEquals(listOf(2, 3), arrayOf("", "bc", "def").mapNotNull { if (it.isEmpty()) null else it.length })
}
@test fun mapIndexedNotNull() {
@Test fun mapIndexedNotNull() {
assertEquals(listOf(2), arrayOf("a", null, "test").mapIndexedNotNull { index, it -> it?.run { if (index != 0) length / index else null } })
}
@test fun flattenArray() {
@Test fun flattenArray() {
val arr1: Array<Array<Int>> = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6))
val arr2: Array<out Array<Int>> = arr1
val arr3: Array<out Array<out Int>> = arr1
@@ -1025,7 +1025,7 @@ class ArraysTest {
assertEquals(expected, arr4.flatten())
}
@test fun asListPrimitives() {
@Test fun asListPrimitives() {
// Array of primitives
val arr = intArrayOf(1, 2, 3, 4, 2, 5)
val list = arr.asList()
@@ -1057,7 +1057,7 @@ class ArraysTest {
assertEquals(IntArray(0).asList(), emptyList<Int>())
}
@test fun asListObjects() {
@Test fun asListObjects() {
val arr = arrayOf("a", "b", "c", "d", "b", "e")
val list = arr.asList()
@@ -1090,7 +1090,7 @@ class ArraysTest {
assertEquals(Array(0, { "" }).asList(), emptyList<String>())
}
@test fun sort() {
@Test fun sort() {
val intArr = intArrayOf(5, 2, 1, 9, 80, Int.MIN_VALUE, Int.MAX_VALUE)
intArr.sort()
assertArrayNotSameButEquals(intArrayOf(Int.MIN_VALUE, 1, 2, 5, 9, 80, Int.MAX_VALUE), intArr)
@@ -1117,7 +1117,7 @@ class ArraysTest {
assertArrayNotSameButEquals(arrayOf("all", "Foo", "9", "80"), strArr)
}
@test fun sortedTests() {
@Test fun sortedTests() {
assertTrue(arrayOf<Long>().sorted().none())
assertEquals(listOf(1), arrayOf(1).sorted())
@@ -1160,7 +1160,7 @@ class ArraysTest {
}
}
@test fun sortByInPlace() {
@Test fun sortByInPlace() {
val data = arrayOf("aa" to 20, "ab" to 3, "aa" to 3)
data.sortBy { it.second }
assertArrayNotSameButEquals(arrayOf("ab" to 3, "aa" to 3, "aa" to 20), data)
@@ -1172,14 +1172,14 @@ class ArraysTest {
assertArrayNotSameButEquals(arrayOf("aa" to 20, "aa" to 3, "ab" to 3), data)
}
@test fun sortedBy() {
@Test fun sortedBy() {
val values = arrayOf("ac", "aD", "aba")
val indices = values.indices.toList().toIntArray()
assertEquals(listOf(1, 2, 0), indices.sortedBy { values[it] })
}
@test fun sortedNullableBy() {
@Test fun sortedNullableBy() {
fun String.nullIfEmpty() = if (this.isEmpty()) null else this
arrayOf(null, "").let {
expect(listOf(null, "")) { it.sortedWith(nullsFirst(compareBy { it })) }
@@ -1188,7 +1188,7 @@ class ArraysTest {
}
}
@test fun sortedWith() {
@Test fun sortedWith() {
val comparator = compareBy { it: Int -> it % 3 }.thenByDescending { it }
fun <A, T> arrayData(array: A, comparator: Comparator<T>) = ArraySortedChecker<A, T>(array, comparator)
@@ -9,7 +9,7 @@ import kotlin.test.*
import kotlin.comparisons.*
import java.util.*
import org.junit.Test as test
import org.junit.Test
class CollectionJVMTest {
@@ -21,7 +21,7 @@ class CollectionJVMTest {
private data class IdentityData(public val value: Int)
@test fun removeAllWithDifferentEquality() {
@Test fun removeAllWithDifferentEquality() {
val data = listOf(IdentityData(1), IdentityData(1))
val list = data.toMutableList()
list -= identitySetOf(data[0]) as Iterable<IdentityData>
@@ -36,7 +36,7 @@ class CollectionJVMTest {
assertTrue(set3.isEmpty(), "Array doesn't have contains, equality contains is used instead")
}
@test fun flatMap() {
@Test fun flatMap() {
val data = listOf("", "foo", "bar", "x", "")
val characters = data.flatMap { it.toList() }
println("Got list of characters ${characters}")
@@ -46,7 +46,7 @@ class CollectionJVMTest {
}
@test fun filterIntoLinkedList() {
@Test fun filterIntoLinkedList() {
val data = listOf("foo", "bar")
val foo = data.filterTo(LinkedList<String>()) { it.startsWith("f") }
@@ -61,7 +61,7 @@ class CollectionJVMTest {
}
}
@test fun filterNotIntoLinkedListOf() {
@Test fun filterNotIntoLinkedListOf() {
val data = listOf("foo", "bar")
val foo = data.filterNotTo(LinkedList<String>()) { it.startsWith("f") }
@@ -76,7 +76,7 @@ class CollectionJVMTest {
}
}
@test fun filterNotNullIntoLinkedListOf() {
@Test fun filterNotNullIntoLinkedListOf() {
val data = listOf(null, "foo", null, "bar")
val foo = data.filterNotNullTo(LinkedList<String>())
@@ -88,7 +88,7 @@ class CollectionJVMTest {
}
}
@test fun filterIntoSortedSet() {
@Test fun filterIntoSortedSet() {
val data = listOf("foo", "bar")
val sorted = data.filterTo(sortedSetOf<String>()) { it.length == 3 }
assertEquals(2, sorted.size)
@@ -98,26 +98,26 @@ class CollectionJVMTest {
}
}
@test fun first() {
@Test fun first() {
assertEquals(19, TreeSet(listOf(90, 47, 19)).first())
}
@test fun last() {
@Test fun last() {
val data = listOf("foo", "bar")
assertEquals("bar", data.last())
assertEquals(25, listOf(15, 19, 20, 25).last())
assertEquals('a', LinkedList(listOf('a')).last())
}
@test fun lastException() {
@Test fun lastException() {
assertFails { LinkedList<String>().last() }
}
@test fun contains() {
@Test fun contains() {
assertTrue(LinkedList(listOf(15, 19, 20)).contains(15))
}
@test fun toArray() {
@Test fun toArray() {
val data = listOf("foo", "bar")
val arr = data.toTypedArray()
println("Got array ${arr}")
@@ -129,7 +129,7 @@ class CollectionJVMTest {
}
}
@test fun toSortedSet() {
@Test fun toSortedSet() {
val data = listOf("foo", "Foo", "bar")
val set1 = data.toSortedSet()
assertEquals(listOf("Foo", "bar", "foo"), set1.toList())
@@ -141,11 +141,11 @@ class CollectionJVMTest {
assertEquals(listOf("bar", "foo"), set3.toList())
}
@test fun takeReturnsFirstNElements() {
@Test fun takeReturnsFirstNElements() {
expect(setOf(1, 2)) { sortedSetOf(1, 2, 3, 4, 5).take(2).toSet() }
}
@test fun filterIsInstanceList() {
@Test fun filterIsInstanceList() {
val values: List<Any> = listOf(1, 2, 3.toDouble(), "abc", "cde")
val intValues: List<Int> = values.filterIsInstance<Int>()
@@ -164,7 +164,7 @@ class CollectionJVMTest {
assertEquals(0, charValues.size)
}
@test fun filterIsInstanceArray() {
@Test fun filterIsInstanceArray() {
val src: Array<Any> = arrayOf(1, 2, 3.toDouble(), "abc", "cde")
val intValues: List<Int> = src.filterIsInstance<Int>()
@@ -183,11 +183,11 @@ class CollectionJVMTest {
assertEquals(0, charValues.size)
}
@test fun emptyListIsSerializable() = testSingletonSerialization(emptyList<Any>())
@Test fun emptyListIsSerializable() = testSingletonSerialization(emptyList<Any>())
@test fun emptySetIsSerializable() = testSingletonSerialization(emptySet<Any>())
@Test fun emptySetIsSerializable() = testSingletonSerialization(emptySet<Any>())
@test fun emptyMapIsSerializable() = testSingletonSerialization(emptyMap<Any, Any>())
@Test fun emptyMapIsSerializable() = testSingletonSerialization(emptyMap<Any, Any>())
private fun testSingletonSerialization(value: Any) {
val result = serializeAndDeserialize(value)
@@ -209,15 +209,15 @@ class CollectionJVMTest {
return inputObjectStream.readObject() as T
}
@test fun deserializeEmptyList() = testPersistedDeserialization(
@Test fun deserializeEmptyList() = testPersistedDeserialization(
"ac ed 00 05 73 72 00 1c 6b 6f 74 6c 69 6e 2e 63 6f 6c 6c 65 63 74 69 6f 6e 73 2e 45 6d 70 74 79 4c 69 73 74 99 6f c7 d0 a7 e0 60 32 02 00 00 78 70",
emptyList<Any>())
@test fun deserializeEmptySet() = testPersistedDeserialization(
@Test fun deserializeEmptySet() = testPersistedDeserialization(
"ac ed 00 05 73 72 00 1b 6b 6f 74 6c 69 6e 2e 63 6f 6c 6c 65 63 74 69 6f 6e 73 2e 45 6d 70 74 79 53 65 74 2f 46 b0 15 76 d7 e2 f4 02 00 00 78 70",
emptySet<Any>())
@test fun deserializeEmptyMap() = testPersistedDeserialization(
@Test fun deserializeEmptyMap() = testPersistedDeserialization(
"ac ed 00 05 73 72 00 1b 6b 6f 74 6c 69 6e 2e 63 6f 6c 6c 65 63 74 69 6f 6e 73 2e 45 6d 70 74 79 4d 61 70 72 72 37 71 cb 04 4c d2 02 00 00 78 70",
emptyMap<Any, Any>())
@@ -17,21 +17,21 @@
package test.collections
import kotlin.test.*
import org.junit.Test as test
import org.junit.Test
import test.collections.behaviors.*
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
import kotlin.comparisons.*
class CollectionTest {
@test fun joinTo() {
@Test fun joinTo() {
val data = listOf("foo", "bar")
val buffer = StringBuilder()
data.joinTo(buffer, "-", "{", "}")
assertEquals("{foo-bar}", buffer.toString())
}
@test fun joinToString() {
@Test fun joinToString() {
val data = listOf("foo", "bar")
val text = data.joinToString("-", "<", ">")
assertEquals("<foo-bar>", text)
@@ -41,7 +41,7 @@ class CollectionTest {
assertEquals("a, b, c, *", text2)
}
@test fun filterNotNull() {
@Test fun filterNotNull() {
val data = listOf(null, "foo", null, "bar")
val foo = data.filterNotNull()
@@ -54,7 +54,7 @@ class CollectionTest {
}
/*
@test fun mapNotNull() {
@Test fun mapNotNull() {
val data = listOf(null, "foo", null, "bar")
val foo = data.mapNotNull { it.length() }
assertEquals(2, foo.size())
@@ -66,7 +66,7 @@ class CollectionTest {
}
*/
@test fun listOfNotNull() {
@Test fun listOfNotNull() {
val l1: List<Int> = listOfNotNull(null)
assertTrue(l1.isEmpty())
@@ -78,7 +78,7 @@ class CollectionTest {
assertEquals(listOf("value1", "value2"), l3)
}
@test fun filterIntoSet() {
@Test fun filterIntoSet() {
val data = listOf("foo", "bar")
val foo = data.filterTo(hashSetOf<String>()) { it.startsWith("f") }
@@ -93,7 +93,7 @@ class CollectionTest {
}
}
@test fun filterIsInstanceList() {
@Test fun filterIsInstanceList() {
val values: List<Any> = listOf(1, 2, 3.0, "abc", "cde")
val numberValues: List<Number> = values.filterIsInstance<Number>()
@@ -114,7 +114,7 @@ class CollectionTest {
assertEquals(0, charValues.size)
}
@test fun filterIsInstanceArray() {
@Test fun filterIsInstanceArray() {
val src: Array<Any> = arrayOf(1, 2, 3.0, "abc", "cde")
val numberValues: List<Number> = src.filterIsInstance<Number>()
@@ -135,7 +135,7 @@ class CollectionTest {
assertEquals(0, charValues.size)
}
@test fun foldIndexed() {
@Test fun foldIndexed() {
expect(42) {
val numbers = listOf(1, 2, 3, 4)
numbers.foldIndexed(0) { index, a, b -> index * (a + b) }
@@ -152,7 +152,7 @@ class CollectionTest {
}
}
@test fun foldIndexedWithDifferentTypes() {
@Test fun foldIndexedWithDifferentTypes() {
expect(10) {
val numbers = listOf("a", "ab", "abc")
numbers.foldIndexed(1) { index, a, b -> a + b.length + index }
@@ -164,35 +164,35 @@ class CollectionTest {
}
}
@test fun foldIndexedWithNonCommutativeOperation() {
@Test fun foldIndexedWithNonCommutativeOperation() {
expect(4) {
val numbers = listOf(1, 2, 3)
numbers.foldIndexed(7) { index, a, b -> index + a - b }
}
}
@test fun foldRightIndexed() {
@Test fun foldRightIndexed() {
expect("12343210") {
val numbers = listOf(1, 2, 3, 4)
numbers.map { it.toString() }.foldRightIndexed("") { index, a, b -> a + b + index }
}
}
@test fun foldRightIndexedWithDifferentTypes() {
@Test fun foldRightIndexedWithDifferentTypes() {
expect("12343210") {
val numbers = listOf(1, 2, 3, 4)
numbers.foldRightIndexed("") { index, a, b -> "" + a + b + index }
}
}
@test fun foldRightIndexedWithNonCommutativeOperation() {
@Test fun foldRightIndexedWithNonCommutativeOperation() {
expect(-4) {
val numbers = listOf(1, 2, 3)
numbers.foldRightIndexed(7) { index, a, b -> index + a - b }
}
}
@test fun fold() {
@Test fun fold() {
// lets calculate the sum of some numbers
expect(10) {
val numbers = listOf(1, 2, 3, 4)
@@ -211,7 +211,7 @@ class CollectionTest {
}
}
@test fun foldWithDifferentTypes() {
@Test fun foldWithDifferentTypes() {
expect(7) {
val numbers = listOf("a", "ab", "abc")
numbers.fold(1) { a, b -> a + b.length }
@@ -223,49 +223,49 @@ class CollectionTest {
}
}
@test fun foldWithNonCommutativeOperation() {
@Test fun foldWithNonCommutativeOperation() {
expect(1) {
val numbers = listOf(1, 2, 3)
numbers.fold(7) { a, b -> a - b }
}
}
@test fun foldRight() {
@Test fun foldRight() {
expect("1234") {
val numbers = listOf(1, 2, 3, 4)
numbers.map { it.toString() }.foldRight("") { a, b -> a + b }
}
}
@test fun foldRightWithDifferentTypes() {
@Test fun foldRightWithDifferentTypes() {
expect("1234") {
val numbers = listOf(1, 2, 3, 4)
numbers.foldRight("") { a, b -> "" + a + b }
}
}
@test fun foldRightWithNonCommutativeOperation() {
@Test fun foldRightWithNonCommutativeOperation() {
expect(-5) {
val numbers = listOf(1, 2, 3)
numbers.foldRight(7) { a, b -> a - b }
}
}
@test
@Test
fun zipTransform() {
expect(listOf("ab", "bc", "cd")) {
listOf("a", "b", "c").zip(listOf("b", "c", "d")) { a, b -> a + b }
}
}
@test
@Test
fun zip() {
expect(listOf("a" to "b", "b" to "c", "c" to "d")) {
listOf("a", "b", "c").zip(listOf("b", "c", "d"))
}
}
@test fun partition() {
@Test fun partition() {
val data = listOf("foo", "bar", "something", "xyz")
val pair = data.partition { it.length == 3 }
@@ -273,7 +273,7 @@ class CollectionTest {
assertEquals(listOf("something"), pair.second, "pair.second")
}
@test fun reduceIndexed() {
@Test fun reduceIndexed() {
expect("123") {
val list = listOf("1", "2", "3", "4")
list.reduceIndexed { index, a, b -> if (index == 3) a else a + b }
@@ -293,7 +293,7 @@ class CollectionTest {
}
}
@test fun reduceRightIndexed() {
@Test fun reduceRightIndexed() {
expect("234") {
val list = listOf("1", "2", "3", "4")
list.reduceRightIndexed { index, a, b -> if (index == 0) b else a + b }
@@ -313,7 +313,7 @@ class CollectionTest {
}
}
@test fun reduce() {
@Test fun reduce() {
expect("1234") {
val list = listOf("1", "2", "3", "4")
list.reduce { a, b -> a + b }
@@ -324,7 +324,7 @@ class CollectionTest {
}
}
@test fun reduceRight() {
@Test fun reduceRight() {
expect("1234") {
val list = listOf("1", "2", "3", "4")
list.reduceRight { a, b -> a + b }
@@ -335,7 +335,7 @@ class CollectionTest {
}
}
@test fun groupBy() {
@Test fun groupBy() {
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
assertEquals(4, byLength.size)
@@ -352,7 +352,7 @@ class CollectionTest {
assertEquals(listOf("abc", "def"), l3)
}
@test fun groupByKeysAndValues() {
@Test fun groupByKeysAndValues() {
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
assertEquals(
@@ -367,14 +367,14 @@ class CollectionTest {
assertEquals(namesByTeam, mutableNamesByTeam)
}
@test fun plusRanges() {
@Test fun plusRanges() {
val range1 = 1..3
val range2 = 4..7
val combined = range1 + range2
assertEquals((1..7).toList(), combined)
}
@test fun mapRanges() {
@Test fun mapRanges() {
val range = (1..3).map { it * 2 }
assertEquals(listOf(2, 4, 6), range)
}
@@ -386,17 +386,17 @@ class CollectionTest {
assertEquals(listOf("foo", "bar", "cheese", "wine"), list2)
}
@test fun plusElement() = testPlus { it + "cheese" + "wine" }
@test fun plusCollection() = testPlus { it + listOf("cheese", "wine") }
@test fun plusArray() = testPlus { it + arrayOf("cheese", "wine") }
@test fun plusSequence() = testPlus { it + sequenceOf("cheese", "wine") }
@Test fun plusElement() = testPlus { it + "cheese" + "wine" }
@Test fun plusCollection() = testPlus { it + listOf("cheese", "wine") }
@Test fun plusArray() = testPlus { it + arrayOf("cheese", "wine") }
@Test fun plusSequence() = testPlus { it + sequenceOf("cheese", "wine") }
@test fun plusCollectionBug() {
@Test fun plusCollectionBug() {
val list = listOf("foo", "bar") + listOf("cheese", "wine")
assertEquals(listOf("foo", "bar", "cheese", "wine"), list)
}
@test fun plusCollectionInference() {
@Test fun plusCollectionInference() {
val listOfLists = listOf(listOf("s"))
val elementList = listOf("a")
val result: List<List<String>> = listOfLists.plusElement(elementList)
@@ -409,7 +409,7 @@ class CollectionTest {
assertEquals(listOf("a", listOf("b")), listOfAnyAndList, "should be list + Any")
}
@test fun plusAssign() {
@Test fun plusAssign() {
// lets use a mutable variable of readonly list
var l: List<String> = listOf("cheese")
val lOriginal = l
@@ -436,12 +436,12 @@ class CollectionTest {
assertEquals(expected_, b.toList())
}
@test fun minusElement() = testMinus(expected = listOf("foo", "bar")) { it - "bar" - "zoo" }
@test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
@test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
@test fun minusSequence() = testMinus { it - sequenceOf("bar", "zoo") }
@Test fun minusElement() = testMinus(expected = listOf("foo", "bar")) { it - "bar" - "zoo" }
@Test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
@Test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
@Test fun minusSequence() = testMinus { it - sequenceOf("bar", "zoo") }
@test fun minusIsEager() {
@Test fun minusIsEager() {
val source = listOf("foo", "bar")
val list = arrayListOf<String>()
val result = source - list
@@ -452,7 +452,7 @@ class CollectionTest {
assertEquals(source, result)
}
@test fun minusAssign() {
@Test fun minusAssign() {
// lets use a mutable variable of readonly list
val data: List<String> = listOf("cheese", "foo", "beer", "cheese", "wine")
var l = data
@@ -475,7 +475,7 @@ class CollectionTest {
@test fun requireNoNulls() {
@Test fun requireNoNulls() {
val data = arrayListOf<String?>("foo", "bar")
val notNull = data.requireNoNulls()
assertEquals(listOf("foo", "bar"), notNull)
@@ -488,7 +488,7 @@ class CollectionTest {
}
}
@test fun reverseInPlace() {
@Test fun reverseInPlace() {
val data = arrayListOf<String>()
data.reverse()
assertTrue(data.isEmpty())
@@ -506,7 +506,7 @@ class CollectionTest {
assertEquals(listOf("zoo", "foo", "bar"), data)
}
@test fun reversed() {
@Test fun reversed() {
val data = listOf("foo", "bar")
val rev = data.reversed()
assertEquals(listOf("bar", "foo"), rev)
@@ -514,18 +514,18 @@ class CollectionTest {
}
@test fun drop() {
@Test fun drop() {
val coll = listOf("foo", "bar", "abc")
assertEquals(listOf("bar", "abc"), coll.drop(1))
assertEquals(listOf("abc"), coll.drop(2))
}
@test fun dropWhile() {
@Test fun dropWhile() {
val coll = listOf("foo", "bar", "abc")
assertEquals(listOf("bar", "abc"), coll.dropWhile { it.startsWith("f") })
}
@test fun dropLast() {
@Test fun dropLast() {
val coll = listOf("foo", "bar", "abc")
assertEquals(coll, coll.dropLast(0))
assertEquals(emptyList<String>(), coll.dropLast(coll.size))
@@ -536,7 +536,7 @@ class CollectionTest {
assertFails { coll.dropLast(-1) }
}
@test fun dropLastWhile() {
@Test fun dropLastWhile() {
val coll = listOf("Foo", "bare", "abc" )
assertEquals(coll, coll.dropLastWhile { false })
assertEquals(listOf<String>(), coll.dropLastWhile { true })
@@ -544,7 +544,7 @@ class CollectionTest {
assertEquals(listOf("Foo"), coll.dropLastWhile { it.all { it in 'a'..'z' } })
}
@test fun take() {
@Test fun take() {
val coll = listOf("foo", "bar", "abc")
assertEquals(emptyList<String>(), coll.take(0))
assertEquals(listOf("foo"), coll.take(1))
@@ -555,7 +555,7 @@ class CollectionTest {
assertFails { coll.take(-1) }
}
@test fun takeWhile() {
@Test fun takeWhile() {
val coll = listOf("foo", "bar", "abc")
assertEquals(emptyList<String>(), coll.takeWhile { false })
assertEquals(coll, coll.takeWhile { true })
@@ -563,7 +563,7 @@ class CollectionTest {
assertEquals(listOf("foo", "bar", "abc"), coll.takeWhile { it.length == 3 })
}
@test fun takeLast() {
@Test fun takeLast() {
val coll = listOf("foo", "bar", "abc")
assertEquals(emptyList<String>(), coll.takeLast(0))
@@ -575,7 +575,7 @@ class CollectionTest {
assertFails { coll.takeLast(-1) }
}
@test fun takeLastWhile() {
@Test fun takeLastWhile() {
val coll = listOf("foo", "bar", "abc")
assertEquals(emptyList<String>(), coll.takeLastWhile { false })
assertEquals(coll, coll.takeLastWhile { true })
@@ -583,21 +583,21 @@ class CollectionTest {
assertEquals(listOf("bar", "abc"), coll.takeLastWhile { it[0] < 'c' })
}
@test fun copyToArray() {
@Test fun copyToArray() {
val data = listOf("foo", "bar")
val arr = data.toTypedArray()
println("Got array ${arr}")
assertEquals(2, arr.size)
}
@test fun count() {
@Test fun count() {
val data = listOf("foo", "bar")
assertEquals(2, data.count())
assertEquals(3, hashSetOf(12, 14, 15).count())
assertEquals(0, ArrayList<Double>().count())
}
@test fun first() {
@Test fun first() {
val data = listOf("foo", "bar")
assertEquals("foo", data.first())
assertEquals(15, listOf(15, 19, 20, 25).first())
@@ -605,7 +605,7 @@ class CollectionTest {
assertFails { arrayListOf<Int>().first() }
}
@test fun last() {
@Test fun last() {
val data = listOf("foo", "bar")
assertEquals("bar", data.last())
assertEquals(25, listOf(15, 19, 20, 25).last())
@@ -613,7 +613,7 @@ class CollectionTest {
assertFails { arrayListOf<Int>().last() }
}
@test fun subscript() {
@Test fun subscript() {
val list = arrayListOf("foo", "bar")
assertEquals("foo", list[0])
assertEquals("bar", list[1])
@@ -636,7 +636,7 @@ class CollectionTest {
assertEquals(listOf("new", "thing", "works"), list)
}
@test fun indices() {
@Test fun indices() {
val data = listOf("foo", "bar")
val indices = data.indices
assertEquals(0, indices.start)
@@ -644,14 +644,14 @@ class CollectionTest {
assertEquals(0..data.size - 1, indices)
}
@test fun contains() {
@Test fun contains() {
assertFalse(hashSetOf<Int>().contains(12))
assertTrue(listOf(15, 19, 20).contains(15))
assertTrue(hashSetOf(45, 14, 13).toIterable().contains(14))
}
@test fun min() {
@Test fun min() {
expect(null, { listOf<Int>().min() })
expect(1, { listOf(1).min() })
expect(2, { listOf(2, 3).min() })
@@ -662,7 +662,7 @@ class CollectionTest {
expect(2, { listOf(2, 3).asSequence().min() })
}
@test fun max() {
@Test fun max() {
expect(null, { listOf<Int>().max() })
expect(1, { listOf(1).max() })
expect(3, { listOf(2, 3).max() })
@@ -673,21 +673,21 @@ class CollectionTest {
expect(3, { listOf(2, 3).asSequence().max() })
}
@test fun minWith() {
@Test fun minWith() {
expect(null, { listOf<Int>().minWith(naturalOrder()) })
expect(1, { listOf(1).minWith(naturalOrder()) })
expect("a", { listOf("a", "B").minWith(STRING_CASE_INSENSITIVE_ORDER) })
expect("a", { listOf("a", "B").asSequence().minWith(STRING_CASE_INSENSITIVE_ORDER) })
}
@test fun maxWith() {
@Test fun maxWith() {
expect(null, { listOf<Int>().maxWith(naturalOrder()) })
expect(1, { listOf(1).maxWith(naturalOrder()) })
expect("B", { listOf("a", "B").maxWith(STRING_CASE_INSENSITIVE_ORDER) })
expect("B", { listOf("a", "B").asSequence().maxWith(STRING_CASE_INSENSITIVE_ORDER) })
}
@test fun minBy() {
@Test fun minBy() {
expect(null, { listOf<Int>().minBy { it } })
expect(1, { listOf(1).minBy { it } })
expect(3, { listOf(2, 3).minBy { -it } })
@@ -697,7 +697,7 @@ class CollectionTest {
expect(3, { listOf(2, 3).asSequence().minBy { -it } })
}
@test fun maxBy() {
@Test fun maxBy() {
expect(null, { listOf<Int>().maxBy { it } })
expect(1, { listOf(1).maxBy { it } })
expect(2, { listOf(2, 3).maxBy { -it } })
@@ -707,7 +707,7 @@ class CollectionTest {
expect(2, { listOf(2, 3).asSequence().maxBy { -it } })
}
@test fun minByEvaluateOnce() {
@Test fun minByEvaluateOnce() {
var c = 0
expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
assertEquals(5, c)
@@ -716,7 +716,7 @@ class CollectionTest {
assertEquals(5, c)
}
@test fun maxByEvaluateOnce() {
@Test fun maxByEvaluateOnce() {
var c = 0
expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
assertEquals(5, c)
@@ -725,7 +725,7 @@ class CollectionTest {
assertEquals(5, c)
}
@test fun sum() {
@Test fun sum() {
expect(0) { arrayListOf<Int>().sum() }
expect(14) { listOf(2, 3, 9).sum() }
expect(3.0) { listOf(1.0, 2.0).sum() }
@@ -734,7 +734,7 @@ class CollectionTest {
expect(3.0.toFloat()) { sequenceOf<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
}
@test fun average() {
@Test fun average() {
expect(0.0) { arrayListOf<Int>().average() }
expect(3.8) { listOf(1, 2, 5, 8, 3).average() }
expect(2.1) { sequenceOf(1.6, 2.6, 3.6, 0.6).average() }
@@ -744,7 +744,7 @@ class CollectionTest {
expect(n.toDouble()/2) { range.average() }
}
@test fun takeReturnsFirstNElements() {
@Test fun takeReturnsFirstNElements() {
expect(listOf(1, 2, 3, 4, 5)) { (1..10).take(5) }
expect(listOf(1, 2, 3, 4, 5)) { (1..10).toList().take(5) }
expect(listOf(1, 2)) { (1..10).take(2) }
@@ -755,7 +755,7 @@ class CollectionTest {
expect(listOf(1)) { listOf(1).take(10) }
}
@test fun sortInPlace() {
@Test fun sortInPlace() {
val data = listOf(11, 3, 7)
val asc = data.toMutableList()
@@ -767,13 +767,13 @@ class CollectionTest {
assertEquals(listOf(11, 7, 3), desc)
}
@test fun sorted() {
@Test fun sorted() {
val data = listOf(11, 3, 7)
assertEquals(listOf(3, 7, 11), data.sorted())
assertEquals(listOf(11, 7, 3), data.sortedDescending())
}
@test fun sortByInPlace() {
@Test fun sortByInPlace() {
val data = arrayListOf("aa" to 20, "ab" to 3, "aa" to 3)
data.sortBy { it.second }
assertEquals(listOf("ab" to 3, "aa" to 3, "aa" to 20), data)
@@ -785,13 +785,13 @@ class CollectionTest {
assertEquals(listOf("aa" to 20, "aa" to 3, "ab" to 3), data)
}
@test fun sortedBy() {
@Test fun sortedBy() {
assertEquals(listOf("two" to 3, "three" to 20), listOf("three" to 20, "two" to 3).sortedBy { it.second })
assertEquals(listOf("three" to 20, "two" to 3), listOf("three" to 20, "two" to 3).sortedBy { it.first })
assertEquals(listOf("three", "two"), listOf("two", "three").sortedByDescending { it.length })
}
@test fun sortedNullableBy() {
@Test fun sortedNullableBy() {
fun String.nullIfEmpty() = if (isEmpty()) null else this
listOf(null, "", "a").let {
expect(listOf(null, "", "a")) { it.sortedWith(nullsFirst(compareBy { it })) }
@@ -800,7 +800,7 @@ class CollectionTest {
}
}
@test fun sortedByNullable() {
@Test fun sortedByNullable() {
fun String.nonEmptyLength() = if (isEmpty()) null else length
listOf("", "sort", "abc").let {
assertEquals(listOf("", "abc", "sort"), it.sortedBy { it.nonEmptyLength() })
@@ -809,7 +809,7 @@ class CollectionTest {
}
}
@test fun sortedWith() {
@Test fun sortedWith() {
val comparator = compareBy<String> { it.toUpperCase().reversed() }
val data = listOf("cat", "dad", "BAD")
@@ -818,18 +818,18 @@ class CollectionTest {
expect(listOf("BAD", "dad", "cat")) { data.sortedWith(comparator.reversed().reversed()) }
}
@test fun decomposeFirst() {
@Test fun decomposeFirst() {
val (first) = listOf(1, 2)
assertEquals(first, 1)
}
@test fun decomposeSplit() {
@Test fun decomposeSplit() {
val (key, value) = "key = value".split("=").map { it.trim() }
assertEquals(key, "key")
assertEquals(value, "value")
}
@test fun decomposeList() {
@Test fun decomposeList() {
val (a, b, c, d, e) = listOf(1, 2, 3, 4, 5)
assertEquals(a, 1)
assertEquals(b, 2)
@@ -838,7 +838,7 @@ class CollectionTest {
assertEquals(e, 5)
}
@test fun decomposeArray() {
@Test fun decomposeArray() {
val (a, b, c, d, e) = arrayOf(1, 2, 3, 4, 5)
assertEquals(a, 1)
assertEquals(b, 2)
@@ -847,7 +847,7 @@ class CollectionTest {
assertEquals(e, 5)
}
@test fun decomposeIntArray() {
@Test fun decomposeIntArray() {
val (a, b, c, d, e) = intArrayOf(1, 2, 3, 4, 5)
assertEquals(a, 1)
assertEquals(b, 2)
@@ -856,44 +856,44 @@ class CollectionTest {
assertEquals(e, 5)
}
@test fun unzipList() {
@Test fun unzipList() {
val list = listOf(1 to 'a', 2 to 'b', 3 to 'c')
val (ints, chars) = list.unzip()
assertEquals(listOf(1, 2, 3), ints)
assertEquals(listOf('a', 'b', 'c'), chars)
}
@test fun unzipArray() {
@Test fun unzipArray() {
val array = arrayOf(1 to 'a', 2 to 'b', 3 to 'c')
val (ints, chars) = array.unzip()
assertEquals(listOf(1, 2, 3), ints)
assertEquals(listOf('a', 'b', 'c'), chars)
}
@test fun specialLists() {
@Test fun specialLists() {
compare(arrayListOf<Int>(), listOf<Int>()) { listBehavior() }
compare(arrayListOf<Double>(), emptyList<Double>()) { listBehavior() }
compare(arrayListOf("value"), listOf("value")) { listBehavior() }
}
@test fun specialSets() {
@Test fun specialSets() {
compare(linkedSetOf<Int>(), setOf<Int>()) { setBehavior() }
compare(hashSetOf<Double>(), emptySet<Double>()) { setBehavior() }
compare(listOf("value").toMutableSet(), setOf("value")) { setBehavior() }
}
@test fun specialMaps() {
@Test fun specialMaps() {
compare(hashMapOf<String, Int>(), mapOf<String, Int>()) { mapBehavior() }
compare(linkedMapOf<Int, String>(), emptyMap<Int, String>()) { mapBehavior() }
compare(linkedMapOf(2 to 3), mapOf(2 to 3)) { mapBehavior() }
}
@test fun toStringTest() {
@Test fun toStringTest() {
// we need toString() inside pattern because of KT-8666
assertEquals("[1, a, null, ${Long.MAX_VALUE.toString()}]", listOf(1, "a", null, Long.MAX_VALUE).toString())
}
@test fun randomAccess() {
@Test fun randomAccess() {
assertTrue(arrayListOf(1) is RandomAccess, "ArrayList is RandomAccess")
assertTrue(listOf(1, 2) is RandomAccess, "Default read-only list implementation is RandomAccess")
assertTrue(listOf(1) is RandomAccess, "Default singleton list is RandomAccess")
@@ -1,13 +1,13 @@
@file:kotlin.jvm.JvmVersion
package test.collections
import org.junit.Test as test
import org.junit.Test
import kotlin.test.*
import java.util.*
class IteratorsJVMTest {
@test fun testEnumeration() {
@Test fun testEnumeration() {
val v = Vector<Int>()
for (i in 1..5)
v.add(i)
@@ -1,10 +1,10 @@
package test.collections
import kotlin.test.*
import org.junit.Test as test
import org.junit.Test
class IteratorsTest {
@test fun iterationOverIterator() {
@Test fun iterationOverIterator() {
val c = listOf(0, 1, 2, 3, 4, 5)
var s = ""
for (i in c.iterator()) {
@@ -6,11 +6,11 @@ import java.util.concurrent.ConcurrentMap
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.expect
import org.junit.Test as test
import org.junit.Test
import kotlin.comparisons.*
class MapJVMTest {
@test fun createSortedMap() {
@Test fun createSortedMap() {
val map = sortedMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1))
assertEquals(1, map["a"])
assertEquals(2, map["b"])
@@ -18,7 +18,7 @@ class MapJVMTest {
assertEquals(listOf("a", "b", "c"), map.keys.toList())
}
@test fun toSortedMap() {
@Test fun toSortedMap() {
val map = mapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1))
val sorted = map.toSortedMap()
assertEquals(1, sorted["a"])
@@ -27,7 +27,7 @@ class MapJVMTest {
assertEquals(listOf("a", "b", "c"), sorted.keys.toList())
}
@test fun toSortedMapWithComparator() {
@Test fun toSortedMapWithComparator() {
val map = mapOf(Pair("c", 3), Pair("bc", 2), Pair("bd", 4), Pair("abc", 1))
val sorted = map.toSortedMap(compareBy<String> { it.length }.thenBy { it })
assertEquals(listOf("c", "bc", "bd", "abc"), sorted.keys.toList())
@@ -36,7 +36,7 @@ class MapJVMTest {
assertEquals(3, sorted["c"])
}
@test fun toProperties() {
@Test fun toProperties() {
val map = mapOf("a" to "A", "b" to "B")
val prop = map.toProperties()
assertEquals(2, prop.size)
@@ -44,7 +44,7 @@ class MapJVMTest {
assertEquals("B", prop.getProperty("b", "fail"))
}
@test fun iterateAndRemove() {
@Test fun iterateAndRemove() {
val map = (1..5).associateByTo(linkedMapOf(), { it }, { 'a' + it })
val iterator = map.iterator()
while (iterator.hasNext()) {
@@ -55,7 +55,7 @@ class MapJVMTest {
assertEquals(listOf('b', 'd', 'f'), map.values.toList())
}
@test fun getOrPutFailsOnConcurrentMap() {
@Test fun getOrPutFailsOnConcurrentMap() {
val map = ConcurrentHashMap<String, Int>()
// not an error anymore
+45 -45
View File
@@ -1,14 +1,14 @@
package test.collections
import kotlin.test.*
import org.junit.Test as test
import org.junit.Test
class MapTest {
// just a static type check
fun <T> assertStaticTypeIs(value: T) {}
@test fun getOrElse() {
@Test fun getOrElse() {
val data = mapOf<String, Int>()
val a = data.getOrElse("foo") { 2 }
assertEquals(2, a)
@@ -29,7 +29,7 @@ class MapTest {
}
@Suppress("INVISIBLE_MEMBER")
@test fun getOrImplicitDefault() {
@Test fun getOrImplicitDefault() {
val data: MutableMap<String, Int> = hashMapOf("bar" to 1)
assertFailsWith<NoSuchElementException> { data.getOrImplicitDefault("foo") }
assertEquals(1, data.getOrImplicitDefault("bar"))
@@ -50,7 +50,7 @@ class MapTest {
assertEquals(42, withReplacedDefault.getOrImplicitDefault("loop"))
}
@test fun getOrPut() {
@Test fun getOrPut() {
val data = hashMapOf<String, Int>()
val a = data.getOrPut("foo") { 2 }
assertEquals(2, a)
@@ -68,13 +68,13 @@ class MapTest {
assertEquals(1, d)
}
@test fun sizeAndEmpty() {
@Test fun sizeAndEmpty() {
val data = hashMapOf<String, Int>()
assertTrue { data.none() }
assertEquals(data.size, 0)
}
@test fun setViaIndexOperators() {
@Test fun setViaIndexOperators() {
val map = hashMapOf<String, String>()
assertTrue { map.none() }
assertEquals(map.size, 0)
@@ -86,7 +86,7 @@ class MapTest {
assertEquals("James", map["name"])
}
@test fun iterate() {
@Test fun iterate() {
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
val list = arrayListOf<String>()
for (e in map) {
@@ -98,13 +98,13 @@ class MapTest {
assertEquals("beverage,beer,location,Mells,name,James", list.joinToString(","))
}
@test fun stream() {
@Test fun stream() {
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
val named = map.asSequence().filter { it.key == "name" }.single()
assertEquals("James", named.value)
}
@test fun iterateWithProperties() {
@Test fun iterateWithProperties() {
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
val list = arrayListOf<String>()
for (e in map) {
@@ -116,7 +116,7 @@ class MapTest {
assertEquals("beverage,beer,location,Mells,name,James", list.joinToString(","))
}
@test fun iterateWithExtraction() {
@Test fun iterateWithExtraction() {
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
val list = arrayListOf<String>()
for ((key, value) in map) {
@@ -128,13 +128,13 @@ class MapTest {
assertEquals("beverage,beer,location,Mells,name,James", list.joinToString(","))
}
@test fun contains() {
@Test fun contains() {
val map = mapOf("a" to 1, "b" to 2)
assertTrue("a" in map)
assertTrue("c" !in map)
}
@test fun map() {
@Test fun map() {
val m1 = mapOf("beverage" to "beer", "location" to "Mells")
val list = m1.map { it.value + " rocks" }
@@ -142,13 +142,13 @@ class MapTest {
}
@test fun mapNotNull() {
@Test fun mapNotNull() {
val m1 = mapOf("a" to 1, "b" to null)
val list = m1.mapNotNull { it.value?.let { v -> "${it.key}$v" } }
assertEquals(listOf("a1"), list)
}
@test fun mapValues() {
@Test fun mapValues() {
val m1 = mapOf("beverage" to "beer", "location" to "Mells")
val m2 = m1.mapValues { it.value + "2" }
@@ -160,7 +160,7 @@ class MapTest {
assertEquals(mapOf("beverage" to 4, "location" to 5), m3)
}
@test fun mapKeys() {
@Test fun mapKeys() {
val m1 = mapOf("beverage" to "beer", "location" to "Mells")
val m2 = m1.mapKeys { it.key + "2" }
@@ -172,7 +172,7 @@ class MapTest {
assertEquals(mapOf(8 to "Mells"), m3)
}
@test fun createFrom() {
@Test fun createFrom() {
val pairs = arrayOf("a" to 1, "b" to 2)
val expected = mapOf(*pairs)
@@ -189,7 +189,7 @@ class MapTest {
assertNotEquals(expected, mutableMap)
}
@test fun populateTo() {
@Test fun populateTo() {
val pairs = arrayOf("a" to 1, "b" to 2)
val expected = mapOf(*pairs)
@@ -209,7 +209,7 @@ class MapTest {
assertEquals<Map<*, *>>(expected, mutableMap3)
}
@test fun createWithSelector() {
@Test fun createWithSelector() {
val map = listOf("a", "bb", "ccc").associateBy { it.length }
assertEquals(3, map.size)
assertEquals("a", map.get(1))
@@ -217,14 +217,14 @@ class MapTest {
assertEquals("ccc", map.get(3))
}
@test fun createWithSelectorAndOverwrite() {
@Test fun createWithSelectorAndOverwrite() {
val map = listOf("aa", "bb", "ccc").associateBy { it.length }
assertEquals(2, map.size)
assertEquals("bb", map.get(2))
assertEquals("ccc", map.get(3))
}
@test fun createWithSelectorForKeyAndValue() {
@Test fun createWithSelectorForKeyAndValue() {
val map = listOf("a", "bb", "ccc").associateBy({ it.length }, { it.toUpperCase() })
assertEquals(3, map.size)
assertEquals("A", map[1])
@@ -232,7 +232,7 @@ class MapTest {
assertEquals("CCC", map[3])
}
@test fun createWithPairSelector() {
@Test fun createWithPairSelector() {
val map = listOf("a", "bb", "ccc").associate { it.length to it.toUpperCase() }
assertEquals(3, map.size)
assertEquals("A", map[1])
@@ -240,20 +240,20 @@ class MapTest {
assertEquals("CCC", map[3])
}
@test fun createUsingTo() {
@Test fun createUsingTo() {
val map = mapOf("a" to 1, "b" to 2)
assertEquals(2, map.size)
assertEquals(1, map["a"])
assertEquals(2, map["b"])
}
@test fun createMutableMap() {
@Test fun createMutableMap() {
val map = mutableMapOf("b" to 1, "c" to 2)
map.put("a", 3)
assertEquals(listOf("b" to 1, "c" to 2, "a" to 3), map.toList())
}
@test fun createLinkedMap() {
@Test fun createLinkedMap() {
val map = linkedMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1))
assertEquals(1, map["a"])
assertEquals(2, map["b"])
@@ -261,7 +261,7 @@ class MapTest {
assertEquals(listOf("c", "b", "a"), map.keys.toList())
}
@test fun filter() {
@Test fun filter() {
val map = mapOf(Pair("b", 3), Pair("c", 2), Pair("a", 2))
val filteredByKey = map.filter { it.key[0] == 'b' }
assertEquals(mapOf("b" to 3), filteredByKey)
@@ -276,7 +276,7 @@ class MapTest {
assertEquals(mapOf("a" to 2, "c" to 2), filteredByValue2)
}
@test fun filterOutProjectedTo() {
@Test fun filterOutProjectedTo() {
val map: Map<out String, Int> = mapOf(Pair("b", 3), Pair("c", 2), Pair("a", 2))
val filteredByKey = map.filterTo(mutableMapOf()) { it.key[0] == 'b' }
@@ -296,7 +296,7 @@ class MapTest {
assertEquals(mapOf("a" to 2, "c" to 2), filteredByValue2)
}
@test fun any() {
@Test fun any() {
val map = mapOf(Pair("b", 3), Pair("c", 2), Pair("a", 2))
assertTrue(map.any())
assertFalse(emptyMap<String, Int>().any())
@@ -308,7 +308,7 @@ class MapTest {
assertFalse(map.any { it.value == 5 })
}
@test fun all() {
@Test fun all() {
val map = mapOf(Pair("b", 3), Pair("c", 2), Pair("a", 2))
assertTrue(map.all { it.key != "d" })
assertTrue(emptyMap<String, Int>().all { it.key == "d" })
@@ -317,7 +317,7 @@ class MapTest {
assertFalse(map.all { it.value == 2 })
}
@test fun countBy() {
@Test fun countBy() {
val map = mapOf(Pair("b", 3), Pair("c", 2), Pair("a", 2))
assertEquals(3, map.count())
@@ -328,7 +328,7 @@ class MapTest {
assertEquals(2, filteredByValue)
}
@test fun filterNot() {
@Test fun filterNot() {
val map = mapOf(Pair("b", 3), Pair("c", 2), Pair("a", 2))
val filteredByKey = map.filterNot { it.key == "b" }
assertEquals(2, filteredByKey.size)
@@ -350,18 +350,18 @@ class MapTest {
assertEquals(3, map["c"])
}
@test fun plusAssign() = testPlusAssign {
@Test fun plusAssign() = testPlusAssign {
it += "b" to 4
it += "c" to 3
}
@test fun plusAssignList() = testPlusAssign { it += listOf("c" to 3, "b" to 4) }
@Test fun plusAssignList() = testPlusAssign { it += listOf("c" to 3, "b" to 4) }
@test fun plusAssignArray() = testPlusAssign { it += arrayOf("c" to 3, "b" to 4) }
@Test fun plusAssignArray() = testPlusAssign { it += arrayOf("c" to 3, "b" to 4) }
@test fun plusAssignSequence() = testPlusAssign { it += sequenceOf("c" to 3, "b" to 4) }
@Test fun plusAssignSequence() = testPlusAssign { it += sequenceOf("c" to 3, "b" to 4) }
@test fun plusAssignMap() = testPlusAssign { it += mapOf("c" to 3, "b" to 4) }
@Test fun plusAssignMap() = testPlusAssign { it += mapOf("c" to 3, "b" to 4) }
fun testPlus(doPlus: (Map<String, Int>) -> Map<String, Int>) {
val original = mapOf("A" to 1, "B" to 2)
@@ -372,17 +372,17 @@ class MapTest {
assertEquals(3, extended["C"])
}
@test fun plus() = testPlus { it + ("C" to 3) + ("B" to 4) }
@Test fun plus() = testPlus { it + ("C" to 3) + ("B" to 4) }
@test fun plusList() = testPlus { it + listOf("C" to 3, "B" to 4) }
@Test fun plusList() = testPlus { it + listOf("C" to 3, "B" to 4) }
@test fun plusArray() = testPlus { it + arrayOf("C" to 3, "B" to 4) }
@Test fun plusArray() = testPlus { it + arrayOf("C" to 3, "B" to 4) }
@test fun plusSequence() = testPlus { it + sequenceOf("C" to 3, "B" to 4) }
@Test fun plusSequence() = testPlus { it + sequenceOf("C" to 3, "B" to 4) }
@test fun plusMap() = testPlus { it + mapOf("C" to 3, "B" to 4) }
@Test fun plusMap() = testPlus { it + mapOf("C" to 3, "B" to 4) }
@test fun plusAny() {
@Test fun plusAny() {
testPlusAny(emptyMap<String, String>(), 1 to "A")
testPlusAny(mapOf("A" to null), "A" as CharSequence to 2)
}
@@ -413,13 +413,13 @@ class MapTest {
}
@test fun plusEmptyList() = testIdempotent { it + listOf() }
@Test fun plusEmptyList() = testIdempotent { it + listOf() }
@test fun plusEmptySet() = testIdempotent { it + setOf() }
@Test fun plusEmptySet() = testIdempotent { it + setOf() }
@test fun plusAssignEmptyList() = testIdempotentAssign { it += listOf() }
@Test fun plusAssignEmptyList() = testIdempotentAssign { it += listOf() }
@test fun plusAssignEmptySet() = testIdempotentAssign { it += setOf() }
@Test fun plusAssignEmptySet() = testIdempotentAssign { it += setOf() }
}
@@ -2,7 +2,7 @@ package test.collections
import kotlin.test.*
import org.junit.Test as test
import org.junit.Test
class MutableCollectionTest {
fun <T, C: MutableCollection<T>> testOperation(before: List<T>, after: List<T>, expectedModified: Boolean, toMutableCollection: (List<T>) -> C)
@@ -16,7 +16,7 @@ class MutableCollectionTest {
= testOperation(before, after, expectedModified, { it.toMutableList() })
@test fun addAll() {
@Test fun addAll() {
val data = listOf("foo", "bar")
testOperation(emptyList(), data, true).let { assertAdd ->
@@ -34,7 +34,7 @@ class MutableCollectionTest {
}
}
@test fun removeAll() {
@Test fun removeAll() {
val content = listOf("foo", "bar", "bar")
val data = listOf("bar")
val expected = listOf("foo")
@@ -59,7 +59,7 @@ class MutableCollectionTest {
}
}
@test fun retainAll() {
@Test fun retainAll() {
val content = listOf("foo", "bar", "bar")
val expected = listOf("bar", "bar")
@@ -21,15 +21,15 @@ import test.collections.compare
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.junit.Test as test
import org.junit.Test
class ReversedViewsTest {
@test fun testNullToString() {
@Test fun testNullToString() {
assertEquals("[null]", listOf<String?>(null).asReversed().toString())
}
@test fun testBehavior() {
@Test fun testBehavior() {
val original = listOf(2L, 3L, Long.MAX_VALUE)
val reversed = original.reversed()
compare(reversed, original.asReversed()) {
@@ -37,12 +37,12 @@ class ReversedViewsTest {
}
}
@test fun testSimple() {
@Test fun testSimple() {
assertEquals(listOf(3, 2, 1), listOf(1, 2, 3).asReversed())
assertEquals(listOf(3, 2, 1), listOf(1, 2, 3).asReversed().toList())
}
@test fun testRandomAccess() {
@Test fun testRandomAccess() {
val reversed = listOf(1, 2, 3).asReversed()
assertEquals(3, reversed[0])
@@ -50,21 +50,21 @@ class ReversedViewsTest {
assertEquals(1, reversed[2])
}
@test fun testDoubleReverse() {
@Test fun testDoubleReverse() {
assertEquals(listOf(1, 2, 3), listOf(1, 2, 3).asReversed().asReversed())
assertEquals(listOf(2, 3), listOf(1, 2, 3, 4).asReversed().subList(1, 3).asReversed())
}
@test fun testEmpty() {
@Test fun testEmpty() {
assertEquals(emptyList<Int>(), emptyList<Int>().asReversed())
}
@test fun testReversedSubList() {
@Test fun testReversedSubList() {
val reversed = (1..10).toList().asReversed()
assertEquals(listOf(9, 8, 7), reversed.subList(1, 4))
}
@test fun testMutableSubList() {
@Test fun testMutableSubList() {
val original = arrayListOf(1, 2, 3, 4)
val reversedSubList = original.asReversed().subList(1, 3)
@@ -78,26 +78,26 @@ class ReversedViewsTest {
assertEquals(listOf(1, 100, 4), original)
}
@test fun testMutableSimple() {
@Test fun testMutableSimple() {
assertEquals(listOf(3, 2, 1), mutableListOf(1, 2, 3).asReversed())
assertEquals(listOf(3, 2, 1), mutableListOf(1, 2, 3).asReversed().toList())
}
@test fun testMutableDoubleReverse() {
@Test fun testMutableDoubleReverse() {
assertEquals(listOf(1, 2, 3), mutableListOf(1, 2, 3).asReversed().asReversed())
assertEquals(listOf(2, 3), mutableListOf(1, 2, 3, 4).asReversed().subList(1, 3).asReversed())
}
@test fun testMutableEmpty() {
@Test fun testMutableEmpty() {
assertEquals(emptyList<Int>(), mutableListOf<Int>().asReversed())
}
@test fun testMutableReversedSubList() {
@Test fun testMutableReversedSubList() {
val reversed = (1..10).toMutableList().asReversed()
assertEquals(listOf(9, 8, 7), reversed.subList(1, 4))
}
@test fun testMutableAdd() {
@Test fun testMutableAdd() {
val original = mutableListOf(1, 2, 3)
val reversed = original.asReversed()
@@ -110,7 +110,7 @@ class ReversedViewsTest {
assertEquals(listOf(0, 1, 2, 3, 4), original)
}
@test fun testMutableSet() {
@Test fun testMutableSet() {
val original = mutableListOf(1, 2, 3)
val reversed = original.asReversed()
@@ -122,7 +122,7 @@ class ReversedViewsTest {
assertEquals(listOf(300, 200, 100), reversed)
}
@test fun testMutableRemove() {
@Test fun testMutableRemove() {
val original = mutableListOf("a", "b", "c")
val reversed = original.asReversed()
@@ -137,7 +137,7 @@ class ReversedViewsTest {
assertEquals(emptyList<String>(), original)
}
@test fun testMutableRemoveByObj() {
@Test fun testMutableRemoveByObj() {
val original = mutableListOf("a", "b", "c")
val reversed = original.asReversed()
@@ -146,7 +146,7 @@ class ReversedViewsTest {
assertEquals(listOf("b", "a"), reversed)
}
@test fun testMutableClear() {
@Test fun testMutableClear() {
val original = mutableListOf(1, 2, 3)
val reversed = original.asReversed()
@@ -156,17 +156,17 @@ class ReversedViewsTest {
assertEquals(emptyList<Int>(), original)
}
@test fun testContains() {
@Test fun testContains() {
assertTrue { 1 in listOf(1, 2, 3).asReversed() }
assertTrue { 1 in mutableListOf(1, 2, 3).asReversed() }
}
@test fun testIndexOf() {
@Test fun testIndexOf() {
assertEquals(2, listOf(1, 2, 3).asReversed().indexOf(1))
assertEquals(2, mutableListOf(1, 2, 3).asReversed().indexOf(1))
}
@test fun testBidirectionalModifications() {
@Test fun testBidirectionalModifications() {
val original = mutableListOf(1, 2, 3, 4)
val reversed = original.asReversed()
@@ -179,7 +179,7 @@ class ReversedViewsTest {
assertEquals(listOf(3, 2), reversed)
}
@test fun testGetIOOB() {
@Test fun testGetIOOB() {
val success = try {
listOf(1, 2, 3).asReversed().get(3)
true
@@ -190,7 +190,7 @@ class ReversedViewsTest {
assertFalse(success)
}
@test fun testSetIOOB() {
@Test fun testSetIOOB() {
val success = try {
mutableListOf(1, 2, 3).asReversed().set(3, 0)
true
@@ -201,7 +201,7 @@ class ReversedViewsTest {
assertFalse(success)
}
@test fun testAddIOOB() {
@Test fun testAddIOOB() {
val success = try {
mutableListOf(1, 2, 3).asReversed().add(4, 0)
true
@@ -212,7 +212,7 @@ class ReversedViewsTest {
assertFalse(success)
}
@test fun testRemoveIOOB() {
@Test fun testRemoveIOOB() {
val success = try {
mutableListOf(1, 2, 3).asReversed().removeAt(3)
true
@@ -1,12 +1,12 @@
@file:kotlin.jvm.JvmVersion
package test.collections
import org.junit.Test as test
import org.junit.Test
import kotlin.test.assertEquals
class SequenceJVMTest {
@test fun filterIsInstance() {
@Test fun filterIsInstance() {
val src: Sequence<Any> = listOf(1, 2, 3.toDouble(), "abc", "cde").asSequence()
val intValues: Sequence<Int> = src.filterIsInstance<Int>()
@@ -1,6 +1,5 @@
package test.collections
import org.junit.Test as test
import org.junit.Test
import kotlin.test.*
import kotlin.comparisons.*
@@ -13,20 +12,20 @@ fun fibonacci(): Sequence<Int> {
public class SequenceTest {
@test fun filterEmptySequence() {
@Test fun filterEmptySequence() {
for (sequence in listOf(emptySequence<String>(), sequenceOf<String>())) {
assertEquals(0, sequence.filter { false }.count())
assertEquals(0, sequence.filter { true }.count())
}
}
@test fun mapEmptySequence() {
@Test fun mapEmptySequence() {
for (sequence in listOf(emptySequence<String>(), sequenceOf<String>())) {
assertEquals(0, sequence.map { true }.count())
}
}
@test fun requireNoNulls() {
@Test fun requireNoNulls() {
val sequence = sequenceOf<String?>("foo", "bar")
val notNull = sequence.requireNoNulls()
assertEquals(listOf("foo", "bar"), notNull.toList())
@@ -39,37 +38,37 @@ public class SequenceTest {
}
}
@test fun filterIndexed() {
@Test fun filterIndexed() {
assertEquals(listOf(1, 2, 5, 13, 34), fibonacci().filterIndexed { index, value -> index % 2 == 1 }.take(5).toList())
}
@test fun filterNullable() {
@Test fun filterNullable() {
val data = sequenceOf(null, "foo", null, "bar")
val filtered = data.filter { it == null || it == "foo" }
assertEquals(listOf(null, "foo", null), filtered.toList())
}
@test fun filterNot() {
@Test fun filterNot() {
val data = sequenceOf(null, "foo", null, "bar")
val filtered = data.filterNot { it == null }
assertEquals(listOf("foo", "bar"), filtered.toList())
}
@test fun filterNotNull() {
@Test fun filterNotNull() {
val data = sequenceOf(null, "foo", null, "bar")
val filtered = data.filterNotNull()
assertEquals(listOf("foo", "bar"), filtered.toList())
}
@test fun mapIndexed() {
@Test fun mapIndexed() {
assertEquals(listOf(0, 1, 2, 6, 12), fibonacci().mapIndexed { index, value -> index * value }.takeWhile { i: Int -> i < 20 }.toList())
}
@test fun mapNotNull() {
@Test fun mapNotNull() {
assertEquals(listOf(0, 10, 110, 1220), fibonacci().mapNotNull { if (it % 5 == 0) it * 2 else null }.take(4).toList())
}
@test fun mapIndexedNotNull() {
@Test fun mapIndexedNotNull() {
// find which terms are divisible by their index
assertEquals(listOf("1/1", "5/5", "144/12", "46368/24", "75025/25"),
fibonacci().mapIndexedNotNull { index, value ->
@@ -78,38 +77,38 @@ public class SequenceTest {
}
@test fun mapAndJoinToString() {
@Test fun mapAndJoinToString() {
assertEquals("3, 5, 8", fibonacci().withIndex().filter { it.index > 3 }.take(3).joinToString { it.value.toString() })
}
@test fun withIndex() {
@Test fun withIndex() {
val data = sequenceOf("foo", "bar")
val indexed = data.withIndex().map { it.value.substring(0..it.index) }.toList()
assertEquals(listOf("f", "ba"), indexed)
}
@test fun filterAndTakeWhileExtractTheElementsWithinRange() {
@Test fun filterAndTakeWhileExtractTheElementsWithinRange() {
assertEquals(listOf(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
}
@test fun foldReducesTheFirstNElements() {
@Test fun foldReducesTheFirstNElements() {
val sum = { a: Int, b: Int -> a + b }
assertEquals(listOf(13, 21, 34, 55, 89).fold(0, sum), fibonacci().filter { it > 10 }.take(5).fold(0, sum))
}
@test fun takeExtractsTheFirstNElements() {
@Test fun takeExtractsTheFirstNElements() {
assertEquals(listOf(0, 1, 1, 2, 3, 5, 8, 13, 21, 34), fibonacci().take(10).toList())
}
@test fun mapAndTakeWhileExtractTheTransformedElements() {
@Test fun mapAndTakeWhileExtractTheTransformedElements() {
assertEquals(listOf(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { i: Int -> i < 20 }.toList())
}
@test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
@Test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.joinToString(separator = ", ", limit = 5))
}
@test fun drop() {
@Test fun drop() {
assertEquals(emptyList(), emptySequence<Int>().drop(1).toList())
listOf(2, 3, 4, 5).let { assertEquals(it, it.asSequence().drop(0).toList()) }
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().drop(7).joinToString(limit = 10))
@@ -117,7 +116,7 @@ public class SequenceTest {
assertFailsWith<IllegalArgumentException> { fibonacci().drop(-1) }
}
@test fun take() {
@Test fun take() {
assertEquals(emptyList(), emptySequence<Int>().take(1).toList())
assertEquals(emptyList(), fibonacci().take(0).toList())
@@ -131,7 +130,7 @@ public class SequenceTest {
assertFailsWith<IllegalArgumentException> { fibonacci().take(-1) }
}
@test fun subSequence() {
@Test fun subSequence() {
assertEquals(listOf(2, 3, 5, 8), fibonacci().drop(3).take(4).toList())
assertEquals(listOf(2, 3, 5, 8), fibonacci().take(7).drop(3).toList())
@@ -145,23 +144,23 @@ public class SequenceTest {
}
@test fun dropWhile() {
@Test fun dropWhile() {
assertEquals("233, 377, 610", fibonacci().dropWhile { it < 200 }.take(3).joinToString(limit = 10))
assertEquals("", sequenceOf(1).dropWhile { it < 200 }.joinToString(limit = 10))
}
@test fun zip() {
@Test fun zip() {
expect(listOf("ab", "bc", "cd")) {
sequenceOf("a", "b", "c").zip(sequenceOf("b", "c", "d")) { a, b -> a + b }.toList()
}
}
@test fun zipPairs() {
@Test fun zipPairs() {
val pairStr = (fibonacci() zip fibonacci().map { i -> i*2 }).joinToString(limit = 10)
assertEquals("(0, 0), (1, 2), (1, 2), (2, 4), (3, 6), (5, 10), (8, 16), (13, 26), (21, 42), (34, 68), ...", pairStr)
}
@test fun toStringJoinsNoMoreThanTheFirstTenElements() {
@Test fun toStringJoinsNoMoreThanTheFirstTenElements() {
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().joinToString(limit = 10))
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.joinToString(limit = 10))
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.joinToString())
@@ -176,12 +175,12 @@ public class SequenceTest {
}
@test fun plusElement() = testPlus { it + "cheese" + "wine" }
@test fun plusCollection() = testPlus { it + listOf("cheese", "wine") }
@test fun plusArray() = testPlus { it + arrayOf("cheese", "wine") }
@test fun plusSequence() = testPlus { it + sequenceOf("cheese", "wine") }
@Test fun plusElement() = testPlus { it + "cheese" + "wine" }
@Test fun plusCollection() = testPlus { it + listOf("cheese", "wine") }
@Test fun plusArray() = testPlus { it + arrayOf("cheese", "wine") }
@Test fun plusSequence() = testPlus { it + sequenceOf("cheese", "wine") }
@test fun plusAssign() {
@Test fun plusAssign() {
// lets use a mutable variable
var seq = sequenceOf("a")
seq += "foo"
@@ -198,12 +197,12 @@ public class SequenceTest {
assertEquals(expected_, b.toList())
}
@test fun minusElement() = testMinus(expected = listOf("foo", "bar")) { it - "bar" - "zoo" }
@test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
@test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
@test fun minusSequence() = testMinus { it - sequenceOf("bar", "zoo") }
@Test fun minusElement() = testMinus(expected = listOf("foo", "bar")) { it - "bar" - "zoo" }
@Test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
@Test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
@Test fun minusSequence() = testMinus { it - sequenceOf("bar", "zoo") }
@test fun minusIsLazyIterated() {
@Test fun minusIsLazyIterated() {
val seq = sequenceOf("foo", "bar")
val list = arrayListOf<String>()
val result = seq - list
@@ -214,7 +213,7 @@ public class SequenceTest {
assertEquals(emptyList<String>(), result.toList())
}
@test fun minusAssign() {
@Test fun minusAssign() {
// lets use a mutable variable of readonly list
val data = sequenceOf("cheese", "foo", "beer", "cheese", "wine")
var l = data
@@ -229,7 +228,7 @@ public class SequenceTest {
@test fun iterationOverSequence() {
@Test fun iterationOverSequence() {
var s = ""
for (i in sequenceOf(0, 1, 2, 3, 4, 5)) {
s += i.toString()
@@ -237,7 +236,7 @@ public class SequenceTest {
assertEquals("012345", s)
}
@test fun sequenceFromFunction() {
@Test fun sequenceFromFunction() {
var count = 3
val sequence = generateSequence {
@@ -253,7 +252,7 @@ public class SequenceTest {
}
}
@test fun sequenceFromFunctionWithInitialValue() {
@Test fun sequenceFromFunctionWithInitialValue() {
val values = generateSequence(3) { n -> if (n > 0) n - 1 else null }
val expected = listOf(3, 2, 1, 0)
assertEquals(expected, values.toList())
@@ -279,7 +278,7 @@ public class SequenceTest {
}
@test fun sequenceFromIterator() {
@Test fun sequenceFromIterator() {
val list = listOf(3, 2, 1, 0)
val iterator = list.iterator()
val sequence = iterator.asSequence()
@@ -289,7 +288,7 @@ public class SequenceTest {
}
}
@test fun makeSequenceOneTimeConstrained() {
@Test fun makeSequenceOneTimeConstrained() {
val sequence = sequenceOf(1, 2, 3, 4)
sequence.toList()
sequence.toList()
@@ -309,13 +308,13 @@ public class SequenceTest {
return result
}
@test fun sequenceExtensions() {
@Test fun sequenceExtensions() {
val d = ArrayList<Int>()
sequenceOf(0, 1, 2, 3, 4, 5).takeWhileTo(d, { i -> i < 4 })
assertEquals(4, d.size)
}
@test fun flatMapAndTakeExtractTheTransformedElements() {
@Test fun flatMapAndTakeExtractTheTransformedElements() {
val expected = listOf(
'3', // fibonacci(4) = 3
'5', // fibonacci(5) = 5
@@ -329,22 +328,22 @@ public class SequenceTest {
assertEquals(expected, fibonacci().drop(4).flatMap { it.toString().asSequence() }.take(10).toList())
}
@test fun flatMap() {
@Test fun flatMap() {
val result = sequenceOf(1, 2).flatMap { (0..it).asSequence() }
assertEquals(listOf(0, 1, 0, 1, 2), result.toList())
}
@test fun flatMapOnEmpty() {
@Test fun flatMapOnEmpty() {
val result = sequenceOf<Int>().flatMap { (0..it).asSequence() }
assertTrue(result.none())
}
@test fun flatMapWithEmptyItems() {
@Test fun flatMapWithEmptyItems() {
val result = sequenceOf(1, 2, 4).flatMap { if (it == 2) sequenceOf<Int>() else (it - 1..it).asSequence() }
assertEquals(listOf(0, 1, 3, 4), result.toList())
}
@test fun flatten() {
@Test fun flatten() {
val expected = listOf(0, 1, 0, 1, 2)
val seq = sequenceOf((0..1).asSequence(), (0..2).asSequence()).flatten()
@@ -360,31 +359,31 @@ public class SequenceTest {
assertEquals(expected, seqMappedIterable.toList())
}
@test fun distinct() {
@Test fun distinct() {
val sequence = fibonacci().dropWhile { it < 10 }.take(20)
assertEquals(listOf(1, 2, 3, 0), sequence.map { it % 4 }.distinct().toList())
}
@test fun distinctBy() {
@Test fun distinctBy() {
val sequence = fibonacci().dropWhile { it < 10 }.take(20)
assertEquals(listOf(13, 34, 55, 144), sequence.distinctBy { it % 4 }.toList())
}
@test fun unzip() {
@Test fun unzip() {
val seq = sequenceOf(1 to 'a', 2 to 'b', 3 to 'c')
val (ints, chars) = seq.unzip()
assertEquals(listOf(1, 2, 3), ints)
assertEquals(listOf('a', 'b', 'c'), chars)
}
@test fun sorted() {
@Test fun sorted() {
sequenceOf(3, 7, 5).let {
it.sorted().iterator().assertSorted { a, b -> a <= b }
it.sortedDescending().iterator().assertSorted { a, b -> a >= b }
}
}
@test fun sortedBy() {
@Test fun sortedBy() {
sequenceOf("it", "greater", "less").let {
it.sortedBy { it.length }.iterator().assertSorted { a, b -> compareValuesBy(a, b) { it.length } <= 0 }
it.sortedByDescending { it.length }.iterator().assertSorted { a, b -> compareValuesBy(a, b) { it.length } >= 0 }
@@ -396,7 +395,7 @@ public class SequenceTest {
}
}
@test fun sortedWith() {
@Test fun sortedWith() {
val comparator = compareBy { s: String -> s.reversed() }
assertEquals(listOf("act", "wast", "test"), sequenceOf("act", "test", "wast").sortedWith(comparator).toList())
}
@@ -1,32 +1,32 @@
package test.collections
import kotlin.test.*
import org.junit.Test as test
import org.junit.Test
class SetOperationsTest {
@test fun distinct() {
@Test fun distinct() {
assertEquals(listOf(1, 3, 5), listOf(1, 3, 3, 1, 5, 1, 3).distinct())
assertTrue(listOf<Int>().distinct().isEmpty())
}
@test fun distinctBy() {
@Test fun distinctBy() {
assertEquals(listOf("some", "cat", "do"), arrayOf("some", "case", "cat", "do", "dog", "it").distinctBy { it.length })
assertTrue(charArrayOf().distinctBy { it }.isEmpty())
}
@test fun union() {
@Test fun union() {
assertEquals(listOf(1, 3, 5), listOf(1, 3).union(listOf(5)).toList())
assertEquals(listOf(1), listOf<Int>().union(listOf(1)).toList())
}
@test fun subtract() {
@Test fun subtract() {
assertEquals(listOf(1, 3), listOf(1, 3).subtract(listOf(5)).toList())
assertEquals(listOf(1, 3), listOf(1, 3, 5).subtract(listOf(5)).toList())
assertTrue(listOf(1, 3, 5).subtract(listOf(1, 3, 5)).none())
assertTrue(listOf<Int>().subtract(listOf(1)).none())
}
@test fun intersect() {
@Test fun intersect() {
assertTrue(listOf(1, 3).intersect(listOf(5)).none())
assertEquals(listOf(5), listOf(1, 3, 5).intersect(listOf(5)).toList())
assertEquals(listOf(1, 3, 5), listOf(1, 3, 5).intersect(listOf(1, 3, 5)).toList())
@@ -40,12 +40,12 @@ class SetOperationsTest {
assertEquals(setOf("foo", "bar", "cheese", "wine"), set2)
}
@test fun plusElement() = testPlus { it + "bar" + "cheese" + "wine" }
@test fun plusCollection() = testPlus { it + listOf("bar", "cheese", "wine") }
@test fun plusArray() = testPlus { it + arrayOf("bar", "cheese", "wine") }
@test fun plusSequence() = testPlus { it + sequenceOf("bar", "cheese", "wine") }
@Test fun plusElement() = testPlus { it + "bar" + "cheese" + "wine" }
@Test fun plusCollection() = testPlus { it + listOf("bar", "cheese", "wine") }
@Test fun plusArray() = testPlus { it + arrayOf("bar", "cheese", "wine") }
@Test fun plusSequence() = testPlus { it + sequenceOf("bar", "cheese", "wine") }
@test fun plusAssign() {
@Test fun plusAssign() {
// lets use a mutable variable
var set = setOf("a")
val setOriginal = set
@@ -70,9 +70,9 @@ class SetOperationsTest {
assertEquals(setOf("foo"), b)
}
@test fun minusElement() = testMinus { it - "bar" - "zoo" }
@test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
@test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
@test fun minusSequence() = testMinus { it - sequenceOf("bar", "zoo") }
@Test fun minusElement() = testMinus { it - "bar" - "zoo" }
@Test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
@Test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
@Test fun minusSequence() = testMinus { it - sequenceOf("bar", "zoo") }
}