New stdlib generators
This commit is contained in:
committed by
Andrey Breslav
parent
0980f5e40a
commit
e37d8174c3
@@ -0,0 +1,192 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
|
||||
class ArraysJVMTest {
|
||||
|
||||
test fun copyOf() {
|
||||
checkContent(booleanArray(true, false, true, false, true, false).copyOf().iterator(), 6) { it % 2 == 0 }
|
||||
checkContent(byteArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it.toByte() }
|
||||
checkContent(shortArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it.toShort() }
|
||||
checkContent(intArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it }
|
||||
checkContent(longArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it.toLong() }
|
||||
checkContent(floatArray(0.toFloat(), 1.toFloat(), 2.toFloat(), 3.toFloat()).copyOf().iterator(), 4) { it.toFloat() }
|
||||
checkContent(doubleArray(0.0, 1.0, 2.0, 3.0, 4.0, 5.0).copyOf().iterator(), 6) { it.toDouble() }
|
||||
checkContent(charArray('0', '1', '2', '3', '4', '5').copyOf().iterator(), 6) { (it + '0').toChar() }
|
||||
}
|
||||
|
||||
test fun copyOfRange() {
|
||||
checkContent(booleanArray(true, false, true, false, true, false).copyOfRange(0, 3).iterator(), 3) { it % 2 == 0 }
|
||||
checkContent(byteArray(0, 1, 2, 3, 4, 5).copyOfRange(0, 3).iterator(), 3) { it.toByte() }
|
||||
checkContent(shortArray(0, 1, 2, 3, 4, 5).copyOfRange(0, 3).iterator(), 3) { it.toShort() }
|
||||
checkContent(intArray(0, 1, 2, 3, 4, 5).copyOfRange(0, 3).iterator(), 3) { it }
|
||||
checkContent(longArray(0, 1, 2, 3, 4, 5).copyOfRange(0, 3).iterator(), 3) { it.toLong() }
|
||||
checkContent(floatArray(0.toFloat(), 1.toFloat(), 2.toFloat(), 3.toFloat()).copyOfRange(0, 3).iterator(), 3) { it.toFloat() }
|
||||
checkContent(doubleArray(0.0, 1.0, 2.0, 3.0, 4.0, 5.0).copyOfRange(0, 3).iterator(), 3) { it.toDouble() }
|
||||
checkContent(charArray('0', '1', '2', '3', '4', '5').copyOfRange(0, 3).iterator(), 3) { (it + '0').toChar() }
|
||||
}
|
||||
|
||||
test fun reduce() {
|
||||
expect(-4) { intArray(1, 2, 3) reduce { a, b -> a - b } }
|
||||
expect(-4.toLong()) { longArray(1, 2, 3) reduce { a, b -> a - b } }
|
||||
expect(-4.toFloat()) { floatArray(1.toFloat(), 2.toFloat(), 3.toFloat()) reduce { a, b -> a - b } }
|
||||
expect(-4.0) { doubleArray(1.0, 2.0, 3.0) reduce { a, b -> a - b } }
|
||||
expect('3') { charArray('1', '3', '2') reduce { a, b -> if(a > b) a else b } }
|
||||
expect(false) { booleanArray(true, true, false) reduce { a, b -> a && b } }
|
||||
expect(true) { booleanArray(true, true) reduce { a, b -> a && b } }
|
||||
expect(0.toByte()) { byteArray(3, 2, 1) reduce { a, b -> (a - b).toByte() } }
|
||||
expect(0.toShort()) { shortArray(3, 2, 1) reduce { a, b -> (a - b).toShort() } }
|
||||
|
||||
failsWith (javaClass<UnsupportedOperationException>()) {
|
||||
intArray().reduce { a, b -> a + b}
|
||||
}
|
||||
}
|
||||
|
||||
test fun reduceRight() {
|
||||
expect(2) { intArray(1, 2, 3) reduceRight { a, b -> a - b } }
|
||||
expect(2.toLong()) { longArray(1, 2, 3) reduceRight { a, b -> a - b } }
|
||||
expect(2.toFloat()) { floatArray(1.toFloat(), 2.toFloat(), 3.toFloat()) reduceRight { a, b -> a - b } }
|
||||
expect(2.0) { doubleArray(1.0, 2.0, 3.0) reduceRight { a, b -> a - b } }
|
||||
expect('3') { charArray('1', '3', '2') reduceRight { a, b -> if(a > b) a else b } }
|
||||
expect(false) { booleanArray(true, true, false) reduceRight { a, b -> a && b } }
|
||||
expect(true) { booleanArray(true, true) reduceRight { a, b -> a && b } }
|
||||
expect(2.toByte()) { byteArray(1, 2, 3) reduceRight { a, b -> (a - b).toByte() } }
|
||||
expect(2.toShort()) { shortArray(1, 2, 3) reduceRight { a, b -> (a - b).toShort() } }
|
||||
|
||||
failsWith (javaClass<UnsupportedOperationException>()) {
|
||||
intArray().reduceRight { a, b -> a + b}
|
||||
}
|
||||
}
|
||||
|
||||
test fun indexOf() {
|
||||
expect(-1) { byteArray(1, 2, 3) indexOf 0 }
|
||||
expect(0) { byteArray(1, 2, 3) indexOf 1 }
|
||||
expect(1) { byteArray(1, 2, 3) indexOf 2 }
|
||||
expect(2) { byteArray(1, 2, 3) indexOf 3 }
|
||||
|
||||
expect(-1) { shortArray(1, 2, 3) indexOf 0 }
|
||||
expect(0) { shortArray(1, 2, 3) indexOf 1 }
|
||||
expect(1) { shortArray(1, 2, 3) indexOf 2 }
|
||||
expect(2) { shortArray(1, 2, 3) indexOf 3 }
|
||||
|
||||
expect(-1) { intArray(1, 2, 3) indexOf 0 }
|
||||
expect(0) { intArray(1, 2, 3) indexOf 1 }
|
||||
expect(1) { intArray(1, 2, 3) indexOf 2 }
|
||||
expect(2) { intArray(1, 2, 3) indexOf 3 }
|
||||
|
||||
expect(-1) { longArray(1, 2, 3) indexOf 0 }
|
||||
expect(0) { longArray(1, 2, 3) indexOf 1 }
|
||||
expect(1) { longArray(1, 2, 3) indexOf 2 }
|
||||
expect(2) { longArray(1, 2, 3) indexOf 3 }
|
||||
|
||||
expect(-1) { floatArray(1.0f, 2.0f, 3.0f) indexOf 0f }
|
||||
expect(0) { floatArray(1.0f, 2.0f, 3.0f) indexOf 1.0f }
|
||||
expect(1) { floatArray(1.0f, 2.0f, 3.0f) indexOf 2.0f }
|
||||
expect(2) { floatArray(1.0f, 2.0f, 3.0f) indexOf 3.0f }
|
||||
|
||||
expect(-1) { doubleArray(1.0, 2.0, 3.0) indexOf 0.0 }
|
||||
expect(0) { doubleArray(1.0, 2.0, 3.0) indexOf 1.0 }
|
||||
expect(1) { doubleArray(1.0, 2.0, 3.0) indexOf 2.0 }
|
||||
expect(2) { doubleArray(1.0, 2.0, 3.0) indexOf 3.0 }
|
||||
|
||||
expect(-1) { charArray('a', 'b', 'c') indexOf 'z' }
|
||||
expect(0) { charArray('a', 'b', 'c') indexOf 'a' }
|
||||
expect(1) { charArray('a', 'b', 'c') indexOf 'b' }
|
||||
expect(2) { charArray('a', 'b', 'c') indexOf 'c' }
|
||||
|
||||
expect(0) { booleanArray(true, false) indexOf true }
|
||||
expect(1) { booleanArray(true, false) indexOf false }
|
||||
expect(-1) { booleanArray(true) indexOf false }
|
||||
}
|
||||
|
||||
test fun isEmpty() {
|
||||
assertTrue(intArray().isEmpty())
|
||||
assertFalse(intArray(1).isEmpty())
|
||||
assertTrue(byteArray().isEmpty())
|
||||
assertFalse(byteArray(1).isEmpty())
|
||||
assertTrue(shortArray().isEmpty())
|
||||
assertFalse(shortArray(1).isEmpty())
|
||||
assertTrue(longArray().isEmpty())
|
||||
assertFalse(longArray(1).isEmpty())
|
||||
assertTrue(charArray().isEmpty())
|
||||
assertFalse(charArray('a').isEmpty())
|
||||
assertTrue(floatArray().isEmpty())
|
||||
assertFalse(floatArray(0.1.toFloat()).isEmpty())
|
||||
assertTrue(doubleArray().isEmpty())
|
||||
assertFalse(doubleArray(0.1).isEmpty())
|
||||
assertTrue(booleanArray().isEmpty())
|
||||
assertFalse(booleanArray(false).isEmpty())
|
||||
}
|
||||
|
||||
test fun isNotEmpty() {
|
||||
assertFalse(intArray().isNotEmpty())
|
||||
assertTrue(intArray(1).isNotEmpty())
|
||||
}
|
||||
|
||||
test fun min() {
|
||||
expect(null, { intArray().min() })
|
||||
expect(1, { intArray(1).min() })
|
||||
expect(2, { intArray(2, 3).min() })
|
||||
expect(2000000000000, { longArray(3000000000000, 2000000000000).min() })
|
||||
expect(1, { byteArray(1, 3, 2).min() })
|
||||
expect(2, { shortArray(3, 2).min() })
|
||||
expect(2.0.toFloat(), { floatArray(3.0.toFloat(), 2.0.toFloat()).min() })
|
||||
expect(2.0, { doubleArray(2.0, 3.0).min() })
|
||||
expect('a', { charArray('a', 'b').min() })
|
||||
}
|
||||
|
||||
test fun max() {
|
||||
expect(null, { intArray().max() })
|
||||
expect(1, { intArray(1).max() })
|
||||
expect(3, { intArray(2, 3).max() })
|
||||
expect(3000000000000, { longArray(3000000000000, 2000000000000).max() })
|
||||
expect(3, { byteArray(1, 3, 2).max() })
|
||||
expect(3, { shortArray(3, 2).max() })
|
||||
expect(3.0.toFloat(), { floatArray(3.0.toFloat(), 2.0.toFloat()).max() })
|
||||
expect(3.0, { doubleArray(2.0, 3.0).max() })
|
||||
expect('b', { charArray('a', 'b').max() })
|
||||
}
|
||||
|
||||
test fun minBy() {
|
||||
expect(null, { intArray().minBy { it } })
|
||||
expect(1, { intArray(1).minBy { it } })
|
||||
expect(3, { intArray(2, 3).minBy { -it } })
|
||||
expect(2000000000000, { longArray(3000000000000, 2000000000000).minBy { it + 1 } })
|
||||
expect(1, { byteArray(1, 3, 2).minBy { it * it } })
|
||||
expect(3, { shortArray(3, 2).minBy { "a" } })
|
||||
expect(2.0.toFloat(), { floatArray(3.0.toFloat(), 2.0.toFloat()).minBy { it.toString() } })
|
||||
expect(2.0, { doubleArray(2.0, 3.0).minBy { Math.sqrt(it) } })
|
||||
}
|
||||
|
||||
test fun minIndex() {
|
||||
val a = intArray(1, 7, 9, -42, 54, 93)
|
||||
expect(3, { a.indices.minBy { a[it] } })
|
||||
}
|
||||
|
||||
test fun maxBy() {
|
||||
expect(null, { intArray().maxBy { it } })
|
||||
expect(1, { intArray(1).maxBy { it } })
|
||||
expect(2, { intArray(2, 3).maxBy { -it } })
|
||||
expect(3000000000000, { longArray(3000000000000, 2000000000000).maxBy { it + 1 } })
|
||||
expect(3, { byteArray(1, 3, 2).maxBy { it * it } })
|
||||
expect(3, { shortArray(3, 2).maxBy { "a" } })
|
||||
expect(3.0.toFloat(), { floatArray(3.0.toFloat(), 2.0.toFloat()).maxBy { it.toString() } })
|
||||
expect(3.0, { doubleArray(2.0, 3.0).maxBy { Math.sqrt(it) } })
|
||||
}
|
||||
|
||||
test fun maxIndex() {
|
||||
val a = intArray(1, 7, 9, 239, 54, 93)
|
||||
expect(3, { a.indices.maxBy { a[it] } })
|
||||
}
|
||||
|
||||
test fun sum() {
|
||||
expect(0) { intArray().sum() }
|
||||
expect(14) { intArray(2, 3, 9).sum() }
|
||||
expect(3.0) { doubleArray(1.0, 2.0).sum() }
|
||||
expect(200) { byteArray(100, 100).sum() }
|
||||
expect(50000) { shortArray(20000, 30000).sum() }
|
||||
expect(3000000000000) { longArray(1000000000000, 2000000000000).sum() }
|
||||
expect(3.0.toFloat()) { floatArray(1.0.toFloat(), 2.0.toFloat()).sum() }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
|
||||
fun <T> checkContent(iter : Iterator<T>, length : Int, value : (Int) -> T) {
|
||||
var idx = 0
|
||||
while (idx != length && iter.hasNext()) {
|
||||
assertEquals(value(idx++), iter.next(), "Invalid element")
|
||||
}
|
||||
|
||||
assertEquals(length, idx, "Invalid length")
|
||||
assertFalse(iter.hasNext(), "Invalid length (hasNext())")
|
||||
}
|
||||
|
||||
class ArraysTest {
|
||||
|
||||
test fun emptyArrayLastIndex() {
|
||||
val arr1 = IntArray(0)
|
||||
assertEquals(-1, arr1.lastIndex)
|
||||
|
||||
val arr2 = Array<String>(0, {"$it"})
|
||||
assertEquals(-1, arr2.lastIndex)
|
||||
}
|
||||
|
||||
test fun arrayLastIndex() {
|
||||
val arr1 = intArray(0, 1, 2, 3, 4)
|
||||
assertEquals(4, arr1.lastIndex)
|
||||
assertEquals(4, arr1[arr1.lastIndex])
|
||||
|
||||
val arr2 = Array<String>(5, {"$it"})
|
||||
assertEquals(4, arr2.lastIndex)
|
||||
assertEquals("4", arr2[arr2.lastIndex])
|
||||
}
|
||||
|
||||
test fun byteArray() {
|
||||
val arr = ByteArray(2)
|
||||
|
||||
val expected: Byte = 0
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(expected, arr[0])
|
||||
assertEquals(expected, arr[1])
|
||||
}
|
||||
|
||||
test fun shortArray() {
|
||||
val arr = ShortArray(2)
|
||||
|
||||
val expected: Short = 0
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(expected, arr[0])
|
||||
assertEquals(expected, arr[1])
|
||||
}
|
||||
|
||||
test fun intArray() {
|
||||
val arr = IntArray(2)
|
||||
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(0, arr[0])
|
||||
assertEquals(0, arr[1])
|
||||
}
|
||||
|
||||
test fun longArray() {
|
||||
val arr = LongArray(2)
|
||||
|
||||
val expected: Long = 0
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(expected, arr[0])
|
||||
assertEquals(expected, arr[1])
|
||||
}
|
||||
|
||||
test fun floatArray() {
|
||||
val arr = FloatArray(2)
|
||||
|
||||
val expected: Float = 0.0.toFloat()
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(expected, arr[0])
|
||||
assertEquals(expected, arr[1])
|
||||
}
|
||||
|
||||
test fun doubleArray() {
|
||||
val arr = DoubleArray(2)
|
||||
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(0.0, arr[0])
|
||||
assertEquals(0.0, arr[1])
|
||||
}
|
||||
|
||||
test fun charArray() {
|
||||
val arr = CharArray(2)
|
||||
|
||||
val expected: Char = '\u0000'
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(expected, arr[0])
|
||||
assertEquals(expected, arr[1])
|
||||
}
|
||||
|
||||
test fun booleanArray() {
|
||||
val arr = BooleanArray(2)
|
||||
assertEquals(arr.size, 2)
|
||||
assertEquals(false, arr[0])
|
||||
assertEquals(false, arr[1])
|
||||
}
|
||||
|
||||
test fun min() {
|
||||
expect(null, { array<Int>().min() })
|
||||
expect(1, { array(1).min() })
|
||||
expect(2, { array(2, 3).min() })
|
||||
expect(2000000000000, { array(3000000000000, 2000000000000).min() })
|
||||
expect('a', { array('a', 'b').min() })
|
||||
expect("a", { array("a", "b").min() })
|
||||
}
|
||||
|
||||
test fun max() {
|
||||
expect(null, { array<Int>().max() })
|
||||
expect(1, { array(1).max() })
|
||||
expect(3, { array(2, 3).max() })
|
||||
expect(3000000000000, { array(3000000000000, 2000000000000).max() })
|
||||
expect('b', { array('a', 'b').max() })
|
||||
expect("b", { array("a", "b").max() })
|
||||
}
|
||||
|
||||
test fun minBy() {
|
||||
expect(null, { array<Int>().minBy { it } })
|
||||
expect(1, { array(1).minBy { it } })
|
||||
expect(3, { array(2, 3).minBy { -it } })
|
||||
expect('a', { array('a', 'b').minBy { "x$it" } })
|
||||
expect("b", { array("b", "abc").minBy { it.length } })
|
||||
}
|
||||
|
||||
test fun maxBy() {
|
||||
expect(null, { array<Int>().maxBy { it } })
|
||||
expect(1, { array(1).maxBy { it } })
|
||||
expect(2, { array(2, 3).maxBy { -it } })
|
||||
expect('b', { array('a', 'b').maxBy { "x$it" } })
|
||||
expect("abc", { array("b", "abc").maxBy { it.length } })
|
||||
}
|
||||
|
||||
test fun minByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(1, { array(5, 4, 3, 2, 1).minBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
test fun maxByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(5, { array(5, 4, 3, 2, 1).maxBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
test fun sum() {
|
||||
expect(0) { array<Int>().sum() }
|
||||
expect(14) { array(2, 3, 9).sum() }
|
||||
expect(3.0) { array(1.0, 2.0).sum() }
|
||||
expect(200) { array<Byte>(100, 100).sum() }
|
||||
expect(50000) { array<Short>(20000, 30000).sum() }
|
||||
expect(3000000000000) { array<Long>(1000000000000, 2000000000000).sum() }
|
||||
expect(3.0.toFloat()) { array<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
|
||||
}
|
||||
|
||||
test fun indexOf() {
|
||||
expect(-1) { array("cat", "dog", "bird").indexOf("mouse") }
|
||||
expect(0) { array("cat", "dog", "bird").indexOf("cat") }
|
||||
expect(1) { array("cat", "dog", "bird").indexOf("dog") }
|
||||
expect(2) { array("cat", "dog", "bird").indexOf("bird") }
|
||||
expect(0) { array(null, "dog", null).indexOf(null)}
|
||||
}
|
||||
|
||||
test fun plus() {
|
||||
assertEquals(listOf("1","2","3","4"), array("1", "2") + array("3", "4"))
|
||||
assertEquals(listOf("1","2","3","4"), listOf("1", "2") + array("3", "4"))
|
||||
}
|
||||
|
||||
test fun first() {
|
||||
expect(1) { array(1,2,3).first() }
|
||||
expect(2) { array(1,2,3).first { it % 2 == 0 } }
|
||||
}
|
||||
|
||||
test fun last() {
|
||||
expect(3) { array(1,2,3).last() }
|
||||
expect(2) { array(1,2,3).last { it % 2 == 0 } }
|
||||
}
|
||||
|
||||
test fun contains() {
|
||||
assertTrue(array("1","2","3","4").contains("2"))
|
||||
assertTrue("3" in array("1","2","3","4"))
|
||||
assertTrue("0" !in array("1","2","3","4"))
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
TODO FIXME ASAP: These currently fail on JS due to missing upto() method on numbers
|
||||
|
||||
test fun reduce() {
|
||||
expect(-4) { intArray(1, 2, 3) reduce { a, b -> a - b } }
|
||||
// Fails in JS: expect(-4.toLong()) { longArray(1, 2, 3) reduce { a, b -> a - b } }
|
||||
expect(-4.toFloat()) { floatArray(1.toFloat(), 2.toFloat(), 3.toFloat()) reduce { a, b -> a - b } }
|
||||
expect(-4.0) { doubleArray(1.0, 2.0, 3.0) reduce { a, b -> a - b } }
|
||||
expect('3') { charArray('1', '3', '2') reduce { a, b -> if(a > b) a else b } }
|
||||
expect(false) { booleanArray(true, true, false) reduce { a, b -> a && b } }
|
||||
expect(true) { booleanArray(true, true) reduce { a, b -> a && b } }
|
||||
// Fails in JS: expect(0.toByte()) { byteArray(3, 2, 1) reduce { a, b -> (a - b).toByte() } }
|
||||
// Fails in JS: expect(0.toShort()) { shortArray(3, 2, 1) reduce { a, b -> (a - b).toShort() } }
|
||||
|
||||
failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
intArray().reduce { a, b -> a + b}
|
||||
}
|
||||
}
|
||||
|
||||
test fun reduceRight() {
|
||||
expect(2) { intArray(1, 2, 3) reduceRight { a, b -> a - b } }
|
||||
// Fails in JS: expect(2.toLong()) { longArray(1, 2, 3) reduceRight { a, b -> a - b } }
|
||||
expect(2.toFloat()) { floatArray(1.toFloat(), 2.toFloat(), 3.toFloat()) reduceRight { a, b -> a - b } }
|
||||
expect(2.0) { doubleArray(1.0, 2.0, 3.0) reduceRight { a, b -> a - b } }
|
||||
expect('3') { charArray('1', '3', '2') reduceRight { a, b -> if(a > b) a else b } }
|
||||
expect(false) { booleanArray(true, true, false) reduceRight { a, b -> a && b } }
|
||||
expect(true) { booleanArray(true, true) reduceRight { a, b -> a && b } }
|
||||
// Fails in JS: expect(2.toByte()) { byteArray(1, 2, 3) reduceRight { a, b -> (a - b).toByte() } }
|
||||
// Fails in JS: expect(2.toShort()) { shortArray(1, 2, 3) reduceRight { a, b -> (a - b).toShort() } }
|
||||
|
||||
failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
intArray().reduceRight { a, b -> a + b}
|
||||
}
|
||||
}
|
||||
|
||||
test fun reverse() {
|
||||
expect(arrayList(3, 2, 1)) { intArray(1, 2, 3).reverse() }
|
||||
// Fails in JS: expect(arrayList<Byte>(3, 2, 1)) { byteArray(1, 2, 3).reverse() }
|
||||
// Fails in JS: expect(arrayList<Short>(3, 2, 1)) { shortArray(1, 2, 3).reverse() }
|
||||
// Fails in JS: expect(arrayList<Long>(3, 2, 1)) { longArray(1, 2, 3).reverse() }
|
||||
expect(arrayList(3.toFloat(), 2.toFloat(), 1.toFloat())) { floatArray(1.toFloat(), 2.toFloat(), 3.toFloat()).reverse() }
|
||||
expect(arrayList(3.0, 2.0, 1.0)) { doubleArray(1.0, 2.0, 3.0).reverse() }
|
||||
expect(arrayList('3', '2', '1')) { charArray('1', '2', '3').reverse() }
|
||||
expect(arrayList(false, false, true)) { booleanArray(true, false, false).reverse() }
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
import org.junit.Test as test
|
||||
|
||||
class CollectionJVMTest {
|
||||
|
||||
test fun flatMap() {
|
||||
val data = arrayListOf("", "foo", "bar", "x", "")
|
||||
val characters = data.flatMap { it.toCharList() }
|
||||
println("Got list of characters ${characters}")
|
||||
assertEquals(7, characters.size())
|
||||
val text = characters.makeString("")
|
||||
assertEquals("foobarx", text)
|
||||
}
|
||||
|
||||
|
||||
test fun filterIntolinkedListOf() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val foo = data.filterTo(linkedListOf<String>()) { it.startsWith("f") }
|
||||
|
||||
assertTrue {
|
||||
foo.all { it.startsWith("f") }
|
||||
}
|
||||
assertEquals(1, foo.size)
|
||||
assertEquals(linkedListOf("foo"), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is LinkedList<String>
|
||||
}
|
||||
}
|
||||
|
||||
test fun filterNotIntolinkedListOf() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val foo = data.filterNotTo(linkedListOf<String>()) { it.startsWith("f") }
|
||||
|
||||
assertTrue {
|
||||
foo.all { !it.startsWith("f") }
|
||||
}
|
||||
assertEquals(1, foo.size)
|
||||
assertEquals(linkedListOf("bar"), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is LinkedList<String>
|
||||
}
|
||||
}
|
||||
|
||||
// TODO would be nice to avoid the <String>
|
||||
test fun filterNotNullIntolinkedListOf() {
|
||||
val data = arrayListOf(null, "foo", null, "bar")
|
||||
val foo = data.filterNotNullTo(linkedListOf<String>())
|
||||
|
||||
assertEquals(2, foo.size)
|
||||
assertEquals(linkedListOf("foo", "bar"), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is LinkedList<String>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO would be nice to avoid the <String>
|
||||
test fun filterIntoSortedSet() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val sorted = data.filterTo(sortedSetOf<String>()) { it.length == 3 }
|
||||
assertEquals(2, sorted.size)
|
||||
assertEquals(sortedSetOf("bar", "foo"), sorted)
|
||||
assertTrue {
|
||||
sorted is TreeSet<String>
|
||||
}
|
||||
}
|
||||
|
||||
test fun last() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
assertEquals("bar", data.last())
|
||||
assertEquals(25, arrayListOf(15, 19, 20, 25).last())
|
||||
assertEquals('a', linkedListOf('a').last())
|
||||
}
|
||||
|
||||
test fun lastException() {
|
||||
fails { linkedListOf<String>().last() }
|
||||
}
|
||||
|
||||
test fun contains() {
|
||||
assertTrue(linkedListOf(15, 19, 20).contains(15))
|
||||
}
|
||||
|
||||
test fun sortBy() {
|
||||
expect(arrayListOf("two" to 2, "three" to 3)) {
|
||||
arrayListOf("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(arrayListOf("two" to 2, "three" to 3)) {
|
||||
arrayListOf("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 }
|
||||
}
|
||||
|
||||
test fun sortFunctionShouldReturnSortedCopyForIterable() {
|
||||
val list: Iterable<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 2, 3)) { list.sort() }
|
||||
expect(arrayListOf(2, 3, 1)) { list }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,432 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
import org.junit.Test as test
|
||||
|
||||
class CollectionTest {
|
||||
|
||||
test fun appendString() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val buffer = StringBuilder()
|
||||
val text = data.appendString(buffer, "-", "{", "}")
|
||||
assertEquals("{foo-bar}", buffer.toString())
|
||||
}
|
||||
|
||||
test fun makeString() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val text = data.makeString("-", "<", ">")
|
||||
assertEquals("<foo-bar>", text)
|
||||
|
||||
val big = arrayListOf("a", "b", "c", "d", "e", "f")
|
||||
val text2 = big.makeString(limit = 3, truncated = "*")
|
||||
assertEquals("a, b, c, *", text2)
|
||||
}
|
||||
|
||||
test fun filterNotNull() {
|
||||
val data = arrayListOf(null, "foo", null, "bar")
|
||||
val foo = data.filterNotNull()
|
||||
|
||||
assertEquals(2, foo.size)
|
||||
assertEquals(arrayListOf("foo", "bar"), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is List<String>
|
||||
}
|
||||
}
|
||||
|
||||
test fun mapNotNull() {
|
||||
val data = arrayListOf(null, "foo", null, "bar")
|
||||
val foo = data.mapNotNull { it.length }
|
||||
assertEquals(2, foo.size)
|
||||
assertEquals(arrayListOf(3, 3), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is List<Int>
|
||||
}
|
||||
}
|
||||
|
||||
// TODO would be nice to avoid the <String>
|
||||
test fun filterIntoSet() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val foo = data.filterTo(hashSetOf<String>()) { it.startsWith("f") }
|
||||
|
||||
assertTrue {
|
||||
foo.all { it.startsWith("f") }
|
||||
}
|
||||
assertEquals(1, foo.size)
|
||||
assertEquals(hashSetOf("foo"), foo)
|
||||
|
||||
assertTrue {
|
||||
foo is HashSet<String>
|
||||
}
|
||||
}
|
||||
|
||||
test fun fold() {
|
||||
// lets calculate the sum of some numbers
|
||||
expect(10) {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
numbers.fold(0) { a, b -> a + b }
|
||||
}
|
||||
|
||||
expect(0) {
|
||||
val numbers = arrayListOf<Int>()
|
||||
numbers.fold(0) { a, b -> a + b }
|
||||
}
|
||||
|
||||
// lets concatenate some strings
|
||||
expect("1234") {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
numbers.map { it.toString() }.fold("") { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldWithDifferentTypes() {
|
||||
expect(7) {
|
||||
val numbers = arrayListOf("a", "ab", "abc")
|
||||
numbers.fold(1) { a, b -> a + b.size }
|
||||
}
|
||||
|
||||
expect("1234") {
|
||||
val numbers = arrayListOf(1, 2, 3, 4)
|
||||
numbers.fold("") { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldWithNonCommutativeOperation() {
|
||||
expect(1) {
|
||||
val numbers = arrayListOf(1, 2, 3)
|
||||
numbers.fold(7) { a, b -> a - b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldRight() {
|
||||
expect("1234") {
|
||||
val numbers = arrayListOf(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)
|
||||
numbers.foldRight("") { a, b -> "" + a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun foldRightWithNonCommutativeOperation() {
|
||||
expect(-5) {
|
||||
val numbers = arrayListOf(1, 2, 3)
|
||||
numbers.foldRight(7) { a, b -> a - b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun partition() {
|
||||
val data = arrayListOf("foo", "bar", "something", "xyz")
|
||||
val pair = data.partition { it.size == 3 }
|
||||
|
||||
assertEquals(arrayListOf("foo", "bar", "xyz"), pair.first, "pair.first")
|
||||
assertEquals(arrayListOf("something"), pair.second, "pair.second")
|
||||
}
|
||||
|
||||
test fun reduce() {
|
||||
expect("1234") {
|
||||
val list = arrayListOf("1", "2", "3", "4")
|
||||
list.reduce { a, b -> a + b }
|
||||
}
|
||||
|
||||
failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
arrayListOf<Int>().reduce { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun reduceRight() {
|
||||
expect("1234") {
|
||||
val list = arrayListOf("1", "2", "3", "4")
|
||||
list.reduceRight { a, b -> a + b }
|
||||
}
|
||||
|
||||
failsWith(javaClass<UnsupportedOperationException>()) {
|
||||
arrayListOf<Int>().reduceRight { a, b -> a + b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun groupBy() {
|
||||
val words = arrayListOf("a", "ab", "abc", "def", "abcd")
|
||||
val byLength = words.groupBy { it.length }
|
||||
assertEquals(4, byLength.size())
|
||||
|
||||
val l3 = byLength.getOrElse(3, { ArrayList<String>() })
|
||||
assertEquals(2, l3.size)
|
||||
}
|
||||
|
||||
test fun plusRanges() {
|
||||
val range1 = 1..3
|
||||
val range2 = 4..7
|
||||
val combined = range1 + range2
|
||||
assertEquals((1..7).toList(), combined)
|
||||
}
|
||||
|
||||
test fun mapRanges() {
|
||||
val range = 1..3 map { it * 2}
|
||||
assertEquals(listOf(2,4,6), range)
|
||||
}
|
||||
|
||||
test fun plus() {
|
||||
val list = arrayListOf("foo", "bar")
|
||||
val list2 = list + "cheese"
|
||||
assertEquals(arrayListOf("foo", "bar"), list)
|
||||
assertEquals(arrayListOf("foo", "bar", "cheese"), list2)
|
||||
|
||||
// lets use a mutable variable
|
||||
var list3 = arrayListOf("a", "b")
|
||||
list3 += "c"
|
||||
assertEquals(arrayListOf("a", "b", "c"), list3)
|
||||
}
|
||||
|
||||
test fun plusCollectionBug() {
|
||||
val list = arrayListOf("foo", "bar") + arrayListOf("cheese", "wine")
|
||||
assertEquals(arrayListOf("foo", "bar", "cheese", "wine"), list)
|
||||
}
|
||||
|
||||
test fun plusCollection() {
|
||||
val a = arrayListOf("foo", "bar")
|
||||
val b = arrayListOf("cheese", "wine")
|
||||
val list = a + b
|
||||
assertEquals(arrayListOf("foo", "bar", "cheese", "wine"), list)
|
||||
|
||||
// lets use a mutable variable
|
||||
var ml = a
|
||||
ml += "beer"
|
||||
ml += b
|
||||
ml += "z"
|
||||
assertEquals(arrayListOf("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)
|
||||
|
||||
val hasNulls = arrayListOf("foo", null, "bar")
|
||||
failsWith(javaClass<IllegalArgumentException>()) {
|
||||
// should throw an exception as we have a null
|
||||
hasNulls.requireNoNulls()
|
||||
}
|
||||
}
|
||||
|
||||
test fun reverse() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val rev = data.reverse()
|
||||
assertEquals(arrayListOf("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 }
|
||||
}
|
||||
|
||||
test fun reverseFunctionShouldReturnReversedCopyForIterable() {
|
||||
val iterable: Iterable<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 3, 2)) { iterable.reverse() }
|
||||
expect(arrayListOf(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))
|
||||
}
|
||||
|
||||
test fun dropWhile() {
|
||||
val coll = arrayListOf("foo", "bar", "abc")
|
||||
assertEquals(arrayListOf("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))
|
||||
}
|
||||
|
||||
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.size == 3 })
|
||||
}
|
||||
|
||||
test fun toArray() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val arr = data.toArray()
|
||||
println("Got array ${arr}")
|
||||
assertEquals(2, arr.size)
|
||||
todo {
|
||||
assertTrue {
|
||||
arr is Array<String>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test fun simpleCount() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
assertEquals(2, data.count())
|
||||
assertEquals(3, hashSetOf(12, 14, 15).count())
|
||||
assertEquals(0, ArrayList<Double>().count())
|
||||
}
|
||||
|
||||
//todo after KT-1873 the name might be returned to 'last'
|
||||
test fun lastElement() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
assertEquals("bar", data.last())
|
||||
assertEquals(25, arrayListOf(15, 19, 20, 25).last())
|
||||
assertEquals('a', arrayListOf('a').last())
|
||||
}
|
||||
// TODO
|
||||
// assertEquals(19, TreeSet(arrayListOf(90, 47, 19)).first())
|
||||
|
||||
|
||||
test fun lastException() {
|
||||
fails { arrayListOf<Int>().last() }
|
||||
}
|
||||
|
||||
test fun subscript() {
|
||||
val list = arrayListOf("foo", "bar")
|
||||
assertEquals("foo", list[0])
|
||||
assertEquals("bar", list[1])
|
||||
|
||||
// lists throw an exception if out of range
|
||||
fails {
|
||||
assertEquals(null, list[2])
|
||||
}
|
||||
|
||||
// lets try update the list
|
||||
list[0] = "new"
|
||||
list[1] = "thing"
|
||||
|
||||
// lists don't allow you to set past the end of the list
|
||||
fails {
|
||||
list[2] = "works"
|
||||
}
|
||||
|
||||
list.add("works")
|
||||
assertEquals(arrayListOf("new", "thing", "works"), list)
|
||||
}
|
||||
|
||||
test fun indices() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
val indices = data.indices
|
||||
assertEquals(0, indices.start)
|
||||
assertEquals(1, indices.end)
|
||||
|
||||
assertEquals(indices, data.size. indices)
|
||||
}
|
||||
|
||||
test fun contains() {
|
||||
val data = arrayListOf("foo", "bar")
|
||||
assertTrue(data.contains("foo"))
|
||||
assertTrue(data.contains("bar"))
|
||||
assertFalse(data.contains("some"))
|
||||
|
||||
// TODO: Problems with generation
|
||||
// assertTrue(IterableWrapper(data).contains("bar"))
|
||||
// assertFalse(IterableWrapper(data).contains("some"))
|
||||
|
||||
assertFalse(hashSetOf<Int>().contains(12))
|
||||
assertTrue(arrayListOf(15, 19, 20).contains(15))
|
||||
|
||||
// assertTrue(IterableWrapper(hashSet(45, 14, 13)).contains(14))
|
||||
// assertFalse(IterableWrapper(linkedList<Int>()).contains(15))
|
||||
}
|
||||
|
||||
test fun sortForMutableIterable() {
|
||||
val list: MutableIterable<Int> = arrayListOf(2, 3, 1)
|
||||
expect(arrayListOf(1, 2, 3)) { list.sort() }
|
||||
expect(arrayListOf(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 }
|
||||
}
|
||||
|
||||
test fun min() {
|
||||
expect(null, { listOf<Int>().min() })
|
||||
expect(1, { listOf(1).min() })
|
||||
expect(2, { listOf(2, 3).min() })
|
||||
expect(2000000000000, { listOf(3000000000000, 2000000000000).min() })
|
||||
expect('a', { listOf('a', 'b').min() })
|
||||
expect("a", { listOf("a", "b").min() })
|
||||
expect(null, { listOf<Int>().stream().min() })
|
||||
expect(2, { listOf(2, 3).stream().min() })
|
||||
}
|
||||
|
||||
test fun max() {
|
||||
expect(null, { listOf<Int>().max() })
|
||||
expect(1, { listOf(1).max() })
|
||||
expect(3, { listOf(2, 3).max() })
|
||||
expect(3000000000000, { listOf(3000000000000, 2000000000000).max() })
|
||||
expect('b', { listOf('a', 'b').max() })
|
||||
expect("b", { listOf("a", "b").max() })
|
||||
expect(null, { listOf<Int>().stream().max() })
|
||||
expect(3, { listOf(2, 3).stream().max() })
|
||||
}
|
||||
|
||||
test fun minBy() {
|
||||
expect(null, { listOf<Int>().minBy { it } })
|
||||
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(null, { listOf<Int>().stream().minBy { it } })
|
||||
expect(3, { listOf(2, 3).stream().minBy { -it } })
|
||||
}
|
||||
|
||||
test fun maxBy() {
|
||||
expect(null, { listOf<Int>().maxBy { it } })
|
||||
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(null, { listOf<Int>().stream().maxBy { it } })
|
||||
expect(2, { listOf(2, 3).stream().maxBy { -it } })
|
||||
}
|
||||
|
||||
test fun minByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
c = 0
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).stream().minBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
test fun maxByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
c = 0
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).stream().maxBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
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(3000000000000) { arrayListOf<Long>(1000000000000, 2000000000000).sum() }
|
||||
expect(3.0.toFloat()) { arrayListOf<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
|
||||
}
|
||||
|
||||
class IterableWrapper<T>(collection: Iterable<T>) : Iterable<T> {
|
||||
private val collection = collection
|
||||
|
||||
override fun iterator(): Iterator<T> {
|
||||
return collection.iterator()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
import junit.framework.TestCase
|
||||
import java.util.Random
|
||||
|
||||
class ImmutableArrayListTest() : TestCase() {
|
||||
|
||||
fun testSimple() {
|
||||
val builder = ImmutableArrayListBuilder<Int>()
|
||||
builder.add(17)
|
||||
val list = builder.build()
|
||||
assertEquals(1, list.size())
|
||||
assertEquals(17, list[0])
|
||||
}
|
||||
|
||||
|
||||
fun testGet() {
|
||||
for (length in 0 .. 55) {
|
||||
val list = buildIntArray(length, 19)
|
||||
assertEquals(length, list.size)
|
||||
checkList(list, length, 19)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildIntArray(length: Int, firstValue: Int): List<Int> {
|
||||
val builder = ImmutableArrayListBuilder<Int>()
|
||||
for (j in 0 .. length - 1) {
|
||||
builder.add(firstValue + j)
|
||||
}
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
|
||||
private fun checkList(list: List<Int>, expectedLength: Int, expectedFirstValue: Int) {
|
||||
assertEquals(expectedLength, list.size)
|
||||
for (i in 0 .. expectedLength - 1) {
|
||||
assertEquals(expectedFirstValue + i, list[i])
|
||||
}
|
||||
try {
|
||||
list[expectedLength]
|
||||
fail()
|
||||
} catch (e: IndexOutOfBoundsException) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun testSublist() {
|
||||
val r = Random(1)
|
||||
for (i in 0 .. 200) {
|
||||
val length = r.nextInt(55)
|
||||
val list = buildIntArray(length, 23)
|
||||
val fromIndex = r.nextInt(length + 1)
|
||||
val toIndex = fromIndex + r.nextInt(length - fromIndex + 1)
|
||||
val sublist = list.subList(fromIndex, toIndex)
|
||||
checkList(sublist, toIndex - fromIndex, 23 + fromIndex)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package test.collections
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
class LinkedSetTest : OrderedIterableTests<Set<String>>(setOf("foo", "bar"), setOf<String>())
|
||||
class LinkedListTest : OrderedIterableTests<LinkedList<String>>(linkedListOf("foo", "bar"), linkedListOf<String>())
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
package test.collections
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
class SetTest : IterableTests<Set<String>>(hashSetOf("foo", "bar"), hashSetOf<String>())
|
||||
class ListTest : OrderedIterableTests<List<String>>(listOf("foo", "bar"), listOf<String>())
|
||||
class ArrayListTest : OrderedIterableTests<ArrayList<String>>(arrayListOf("foo", "bar"), arrayListOf<String>())
|
||||
|
||||
abstract class OrderedIterableTests<T : Iterable<String>>(data: T, empty: T) : IterableTests<T>(data, empty) {
|
||||
Test fun indexOf() {
|
||||
expect(0) { data.indexOf("foo") }
|
||||
expect(-1) { empty.indexOf("foo") }
|
||||
expect(1) { data.indexOf("bar") }
|
||||
expect(-1) { data.indexOf("zap") }
|
||||
}
|
||||
|
||||
Test fun lastIndexOf() {
|
||||
expect(0) { data.lastIndexOf("foo") }
|
||||
expect(-1) { empty.lastIndexOf("foo") }
|
||||
expect(1) { data.lastIndexOf("bar") }
|
||||
expect(-1) { data.lastIndexOf("zap") }
|
||||
}
|
||||
|
||||
Test fun elementAt() {
|
||||
expect("foo") { data.elementAt(0) }
|
||||
expect("bar") { data.elementAt(1) }
|
||||
fails { data.elementAt(2) }
|
||||
fails { data.elementAt(-1) }
|
||||
fails { empty.elementAt(0) }
|
||||
}
|
||||
|
||||
Test fun first() {
|
||||
expect("foo") { data.first() }
|
||||
fails {
|
||||
data.first { it.startsWith("x") }
|
||||
}
|
||||
fails {
|
||||
empty.first()
|
||||
}
|
||||
expect("foo") { data.first { it.startsWith("f") } }
|
||||
}
|
||||
|
||||
Test fun firstOrNull() {
|
||||
expect(null) { data.firstOrNull { it.startsWith("x") } }
|
||||
expect(null) { empty.firstOrNull() }
|
||||
|
||||
val f = data.firstOrNull { it.startsWith("f") }
|
||||
assertEquals("foo", f)
|
||||
}
|
||||
|
||||
Test fun last() {
|
||||
assertEquals("bar", data.last())
|
||||
fails {
|
||||
data.last { it.startsWith("x") }
|
||||
}
|
||||
fails {
|
||||
empty.last()
|
||||
}
|
||||
expect("foo") { data.last { it.startsWith("f") } }
|
||||
}
|
||||
|
||||
Test fun lastOrNull() {
|
||||
expect(null) { data.lastOrNull { it.startsWith("x") } }
|
||||
expect(null) { empty.lastOrNull() }
|
||||
expect("foo") { data.lastOrNull { it.startsWith("f") } }
|
||||
}
|
||||
}
|
||||
|
||||
abstract class IterableTests<T : Iterable<String>>(val data: T, val empty: T) {
|
||||
Test fun any() {
|
||||
expect(true) { data.any() }
|
||||
expect(false) { empty.any() }
|
||||
expect(true) { data.any { it.startsWith("f") } }
|
||||
expect(false) { data.any { it.startsWith("x") } }
|
||||
expect(false) { empty.any { it.startsWith("x") } }
|
||||
}
|
||||
|
||||
Test fun all() {
|
||||
expect(true) { data.all { it.length == 3 } }
|
||||
expect(false) { data.all { it.startsWith("b") } }
|
||||
expect(true) { empty.all { it.startsWith("b") } }
|
||||
}
|
||||
|
||||
Test fun none() {
|
||||
expect(false) { data.none() }
|
||||
expect(true) { empty.none() }
|
||||
expect(false) { data.none { it.length == 3 } }
|
||||
expect(false) { data.none { it.startsWith("b") } }
|
||||
expect(true) { data.none { it.startsWith("x") } }
|
||||
expect(true) { empty.none { it.startsWith("b") } }
|
||||
}
|
||||
|
||||
Test fun filter() {
|
||||
val foo = data.filter { it.startsWith("f") }
|
||||
// TODO uncomment this when KT-4651 will be fixed
|
||||
//expect(true) { foo is List<String> }
|
||||
expect(true) { foo.all { it.startsWith("f") } }
|
||||
expect(1) { foo.size }
|
||||
assertEquals(listOf("foo"), foo)
|
||||
}
|
||||
|
||||
Test fun filterNot() {
|
||||
val notFoo = data.filterNot { it.startsWith("f") }
|
||||
// TODO uncomment this when KT-4651 will be fixed
|
||||
//expect(true) { notFoo is List<String> }
|
||||
expect(true) { notFoo.none { it.startsWith("f") } }
|
||||
expect(1) { notFoo.size }
|
||||
assertEquals(listOf("bar"), notFoo)
|
||||
}
|
||||
|
||||
Test fun forEach() {
|
||||
var count = 0
|
||||
data.forEach { count += it.length }
|
||||
assertEquals(6, count)
|
||||
}
|
||||
|
||||
Test fun contains() {
|
||||
assertTrue(data.contains("foo"))
|
||||
assertTrue("bar" in data)
|
||||
assertTrue("baz" !in data)
|
||||
assertFalse("baz" in empty)
|
||||
}
|
||||
|
||||
Test fun single() {
|
||||
fails { data.single() }
|
||||
fails { empty.single() }
|
||||
expect("foo") { data.single { it.startsWith("f") } }
|
||||
expect("bar") { data.single { it.startsWith("b") } }
|
||||
fails {
|
||||
data.single { it.length == 3 }
|
||||
}
|
||||
}
|
||||
|
||||
Test
|
||||
fun singleOrNull() {
|
||||
fails { data.singleOrNull() }
|
||||
fails { empty.singleOrNull() }
|
||||
expect("foo") { data.singleOrNull { it.startsWith("f") } }
|
||||
expect("bar") { data.singleOrNull { it.startsWith("b") } }
|
||||
fails {
|
||||
data.singleOrNull { it.length == 3 }
|
||||
}
|
||||
}
|
||||
|
||||
Test
|
||||
fun map() {
|
||||
val lengths = data.map { it.length }
|
||||
assertTrue {
|
||||
lengths.all { it == 3 }
|
||||
}
|
||||
assertEquals(2, lengths.size)
|
||||
assertEquals(arrayListOf(3, 3), lengths)
|
||||
}
|
||||
|
||||
Test
|
||||
fun max() {
|
||||
expect("foo") { data.max() }
|
||||
expect("bar") { data.maxBy { it.last() } }
|
||||
}
|
||||
|
||||
Test
|
||||
fun min() {
|
||||
expect("bar") { data.min() }
|
||||
expect("foo") { data.minBy { it.last() } }
|
||||
}
|
||||
|
||||
Test
|
||||
fun count() {
|
||||
expect(2) { data.count() }
|
||||
expect(0) { empty.count() }
|
||||
|
||||
expect(1) { data.count { it.startsWith("f") } }
|
||||
expect(0) { empty.count { it.startsWith("f") } }
|
||||
|
||||
expect(0) { data.count { it.startsWith("x") } }
|
||||
expect(0) { empty.count { it.startsWith("x") } }
|
||||
}
|
||||
|
||||
Test
|
||||
fun withIndices() {
|
||||
var index = 0
|
||||
for ((i, d) in data.withIndices()) {
|
||||
assertEquals(i, index)
|
||||
assertEquals(d, data.elementAt(index))
|
||||
index++
|
||||
}
|
||||
assertEquals(data.count(), index)
|
||||
}
|
||||
|
||||
Test
|
||||
fun fold() {
|
||||
|
||||
}
|
||||
|
||||
Test
|
||||
fun reduce() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package test.collections
|
||||
|
||||
import org.junit.Test as test
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
class IteratorsJVMTest {
|
||||
|
||||
test fun testEnumeration() {
|
||||
val v = Vector<Int>()
|
||||
for(i in 1..5)
|
||||
v.add(i)
|
||||
|
||||
var sum = 0
|
||||
for(k in v.elements())
|
||||
sum += k
|
||||
|
||||
assertEquals(15, sum)
|
||||
}
|
||||
|
||||
test fun flatMapAndTakeExtractTheTransformedElements() {
|
||||
fun intToBinaryDigits() = { (i: Int) ->
|
||||
val binary = Integer.toBinaryString(i)!!
|
||||
var index = 0
|
||||
stream<Char> { if (index < binary.length()) binary.get(index++) else null }
|
||||
}
|
||||
|
||||
val expected = arrayListOf(
|
||||
'0', // fibonacci(0) = 0
|
||||
'1', // fibonacci(1) = 1
|
||||
'1', // fibonacci(2) = 1
|
||||
'1', '0', // fibonacci(3) = 2
|
||||
'1', '1', // fibonacci(4) = 3
|
||||
'1', '0', '1' // fibonacci(5) = 5
|
||||
)
|
||||
|
||||
assertEquals(expected, fibonacci().flatMap<Int, Char>(intToBinaryDigits()).take(10).toList())
|
||||
}
|
||||
|
||||
test fun flatMapOnStream() {
|
||||
val result = listOf(1, 2).stream().flatMap<Int, Int> { (0..it).stream() }
|
||||
assertEquals(listOf(0, 1, 0, 1, 2), result.toList())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
|
||||
class IteratorsTest {
|
||||
test fun iterationOverIterator() {
|
||||
val c = arrayListOf(0, 1, 2, 3, 4, 5)
|
||||
var s = ""
|
||||
for (i in c.iterator()) {
|
||||
s = s + i.toString()
|
||||
}
|
||||
assertEquals("012345", s)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package test.collections
|
||||
|
||||
import java.util.ArrayList
|
||||
import kotlin.test.*
|
||||
import org.junit.Test
|
||||
|
||||
class ListSpecificTest {
|
||||
val data = listOf("foo", "bar")
|
||||
val empty = listOf<String>()
|
||||
|
||||
Test fun _toString() {
|
||||
assertEquals("[foo, bar]", data.toString())
|
||||
}
|
||||
|
||||
Test fun tail() {
|
||||
val data = arrayListOf("foo", "bar", "whatnot")
|
||||
val actual = data.tail
|
||||
val expected = arrayListOf("bar", "whatnot")
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
Test fun utils() {
|
||||
assertNull(empty.head)
|
||||
assertNull(empty.first)
|
||||
assertNull(empty.last)
|
||||
assertEquals(-1, empty.lastIndex)
|
||||
|
||||
assertEquals("foo", data.head)
|
||||
assertEquals("foo", data.first)
|
||||
assertEquals("bar", data.last)
|
||||
assertEquals(1, data.lastIndex)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
import java.util.*
|
||||
import org.junit.Test as test
|
||||
|
||||
class MapTest {
|
||||
|
||||
test fun getOrElse() {
|
||||
val data = hashMapOf<String, Int>()
|
||||
val a = data.getOrElse("foo"){2}
|
||||
assertEquals(2, a)
|
||||
|
||||
val b = data.getOrElse("foo"){3}
|
||||
assertEquals(3, b)
|
||||
assertEquals(0, data.size())
|
||||
|
||||
val empty = hashMapOf<String, Int?>()
|
||||
val c = empty.getOrElse("") {null}
|
||||
assertEquals(null, c)
|
||||
}
|
||||
|
||||
test fun getOrPut() {
|
||||
val data = hashMapOf<String, Int>()
|
||||
val a = data.getOrPut("foo"){2}
|
||||
assertEquals(2, a)
|
||||
|
||||
val b = data.getOrPut("foo"){3}
|
||||
assertEquals(2, b)
|
||||
|
||||
assertEquals(1, data.size())
|
||||
|
||||
val empty = hashMapOf<String, Int?>()
|
||||
val c = empty.getOrPut("") {null}
|
||||
assertEquals(null, c)
|
||||
}
|
||||
|
||||
test fun sizeAndEmpty() {
|
||||
val data = hashMapOf<String, Int>()
|
||||
assertTrue{ data.empty }
|
||||
assertEquals(data.size, 0)
|
||||
}
|
||||
|
||||
test fun setViaIndexOperators() {
|
||||
val map = hashMapOf<String, String>()
|
||||
assertTrue{ map.empty }
|
||||
assertEquals(map.size, 0)
|
||||
|
||||
map["name"] = "James"
|
||||
|
||||
assertTrue{ !map.empty }
|
||||
assertEquals(map.size(), 1)
|
||||
assertEquals("James", map["name"])
|
||||
}
|
||||
|
||||
test fun iterate() {
|
||||
val map = TreeMap<String, String>()
|
||||
map["beverage"] = "beer"
|
||||
map["location"] = "Mells"
|
||||
map["name"] = "James"
|
||||
|
||||
val list = arrayListOf<String>()
|
||||
for (e in map) {
|
||||
println("key = ${e.getKey()}, value = ${e.getValue()}")
|
||||
list.add(e.getKey())
|
||||
list.add(e.getValue())
|
||||
}
|
||||
|
||||
assertEquals(6, list.size())
|
||||
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
|
||||
}
|
||||
|
||||
test fun iterateWithProperties() {
|
||||
val map = TreeMap<String, String>()
|
||||
map["beverage"] = "beer"
|
||||
map["location"] = "Mells"
|
||||
map["name"] = "James"
|
||||
|
||||
val list = arrayListOf<String>()
|
||||
for (e in map) {
|
||||
println("key = ${e.key}, value = ${e.value}")
|
||||
list.add(e.key)
|
||||
list.add(e.value)
|
||||
}
|
||||
|
||||
assertEquals(6, list.size())
|
||||
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
|
||||
}
|
||||
|
||||
test fun iterateWithExtraction() {
|
||||
val map = TreeMap<String, String>()
|
||||
map["beverage"] = "beer"
|
||||
map["location"] = "Mells"
|
||||
map["name"] = "James"
|
||||
|
||||
val list = arrayListOf<String>()
|
||||
for ((key, value) in map) {
|
||||
println("key = ${key}, value = ${value}")
|
||||
list.add(key)
|
||||
list.add(value)
|
||||
}
|
||||
|
||||
assertEquals(6, list.size())
|
||||
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
|
||||
}
|
||||
|
||||
test fun map() {
|
||||
val m1 = TreeMap<String, String>()
|
||||
m1["beverage"] = "beer"
|
||||
m1["location"] = "Mells"
|
||||
|
||||
val list = m1.map{ it.value + " rocks" }
|
||||
|
||||
println("Got new list $list")
|
||||
assertEquals(arrayListOf("beer rocks", "Mells rocks"), list)
|
||||
}
|
||||
|
||||
test fun mapValues() {
|
||||
val m1 = TreeMap<String, String>()
|
||||
m1["beverage"] = "beer"
|
||||
m1["location"] = "Mells"
|
||||
|
||||
val m2 = m1.mapValues{ it.value + "2" }
|
||||
|
||||
assertEquals("beer2", m2["beverage"])
|
||||
assertEquals("Mells2", m2["location"])
|
||||
}
|
||||
|
||||
test fun createUsingPairs() {
|
||||
val map = hashMapOf(Pair("a", 1), Pair("b", 2))
|
||||
assertEquals(2, map.size)
|
||||
assertEquals(1, map.get("a"))
|
||||
assertEquals(2, map.get("b"))
|
||||
}
|
||||
|
||||
test fun createUsingTo() {
|
||||
val map = hashMapOf("a" to 1, "b" to 2)
|
||||
assertEquals(2, map.size)
|
||||
assertEquals(1, map.get("a"))
|
||||
assertEquals(2, map.get("b"))
|
||||
}
|
||||
|
||||
test fun createLinkedMap() {
|
||||
val map = linkedMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1))
|
||||
assertEquals(1, map.get("a"))
|
||||
assertEquals(2, map.get("b"))
|
||||
assertEquals(3, map.get("c"))
|
||||
assertEquals(arrayListOf("c", "b", "a"), map.keySet().toList())
|
||||
}
|
||||
|
||||
test fun createSortedMap() {
|
||||
val map = sortedMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1))
|
||||
assertEquals(1, map.get("a"))
|
||||
assertEquals(2, map.get("b"))
|
||||
assertEquals(3, map.get("c"))
|
||||
assertEquals(arrayListOf("a", "b", "c"), map.keySet().toList())
|
||||
}
|
||||
|
||||
test fun toSortedMap() {
|
||||
val map = hashMapOf<String,Int>(Pair("c", 3), Pair("b", 2), Pair("a", 1))
|
||||
val sorted = map.toSortedMap<String,Int>()
|
||||
assertEquals(1, sorted.get("a"))
|
||||
assertEquals(2, sorted.get("b"))
|
||||
assertEquals(3, sorted.get("c"))
|
||||
assertEquals(arrayListOf("a", "b", "c"), sorted.keySet().toList())
|
||||
}
|
||||
|
||||
test fun toSortedMapWithComparator() {
|
||||
val map = hashMapOf(Pair("c", 3), Pair("bc", 2), Pair("bd", 4), Pair("abc", 1))
|
||||
val c = comparator<String>{ a, b ->
|
||||
val answer = a.length() - b.length()
|
||||
if (answer == 0) a.compareTo(b) else answer
|
||||
}
|
||||
val sorted = map.toSortedMap(c)
|
||||
assertEquals(arrayListOf("c", "bc", "bd", "abc"), sorted.keySet().toList())
|
||||
assertEquals(1, sorted.get("abc"))
|
||||
assertEquals(2, sorted.get("bc"))
|
||||
assertEquals(3, sorted.get("c"))
|
||||
}
|
||||
|
||||
/**
|
||||
TODO
|
||||
test case for http://youtrack.jetbrains.com/issue/KT-1773
|
||||
|
||||
test fun compilerBug() {
|
||||
val map = TreeMap<String, String>()
|
||||
map["beverage"] = "beer"
|
||||
map["location"] = "Mells"
|
||||
map["name"] = "James"
|
||||
|
||||
var list = arrayListOf<String>()
|
||||
for (e in map) {
|
||||
println("key = ${e.getKey()}, value = ${e.getValue()}")
|
||||
list += e.getKey()
|
||||
list += e.getValue()
|
||||
}
|
||||
|
||||
assertEquals(6, list.size())
|
||||
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
|
||||
println("==== worked! $list")
|
||||
}
|
||||
*/
|
||||
|
||||
test fun toProperties() {
|
||||
val map = hashMapOf("a" to "A", "b" to "B")
|
||||
val prop = map.toProperties()
|
||||
assertEquals(2, prop.size)
|
||||
assertEquals("A", prop.getProperty("a", "fail"))
|
||||
assertEquals("B", prop.getProperty("b", "fail"))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
import org.junit.Test as test
|
||||
|
||||
class MutableCollectionTest {
|
||||
|
||||
test fun fromIterable() {
|
||||
val data: Iterable<String> = arrayListOf("foo", "bar")
|
||||
|
||||
val collection = ArrayList<String>()
|
||||
collection.addAll(data)
|
||||
|
||||
assertEquals(data, collection)
|
||||
}
|
||||
|
||||
test fun fromStream() {
|
||||
val list = arrayListOf("foo", "bar")
|
||||
val collection = ArrayList<String>()
|
||||
|
||||
collection.addAll(list.stream())
|
||||
|
||||
assertEquals(list, collection)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package test.collections
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
fun fibonacci(): Stream<Int> {
|
||||
// fibonacci terms
|
||||
var index = 0;
|
||||
var a = 0;
|
||||
var b = 1
|
||||
return stream<Int> {
|
||||
when (index++) { 0 -> a; 1 -> b; else -> {
|
||||
val result = a + b; a = b; b = result; result
|
||||
} }
|
||||
}
|
||||
}
|
||||
|
||||
public class StreamTest {
|
||||
|
||||
Test fun requireNoNulls() {
|
||||
val stream = arrayListOf<String?>("foo", "bar").stream()
|
||||
val notNull = stream.requireNoNulls()
|
||||
assertEquals(arrayListOf("foo", "bar"), notNull.toList())
|
||||
|
||||
val streamWithNulls = arrayListOf("foo", null, "bar").stream()
|
||||
val notNull2 = streamWithNulls.requireNoNulls() // shouldn't fail yet
|
||||
fails {
|
||||
// should throw an exception as we have a null
|
||||
notNull2.toList()
|
||||
}
|
||||
}
|
||||
|
||||
test fun mapNotNull() {
|
||||
val data = arrayListOf(null, "foo", null, "bar").stream()
|
||||
val foo = data.mapNotNull { it.length }
|
||||
assertEquals(arrayListOf(3, 3), foo.toList())
|
||||
|
||||
assertTrue {
|
||||
foo is Stream<Int>
|
||||
}
|
||||
}
|
||||
|
||||
Test fun filterAndTakeWhileExtractTheElementsWithinRange() {
|
||||
assertEquals(arrayListOf(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
|
||||
}
|
||||
|
||||
Test fun foldReducesTheFirstNElements() {
|
||||
val sum = {(a: Int, b: Int) -> a + b }
|
||||
assertEquals(arrayListOf(13, 21, 34, 55, 89).fold(0, sum), fibonacci().filter { it > 10 }.take(5).fold(0, sum))
|
||||
}
|
||||
|
||||
Test fun takeExtractsTheFirstNElements() {
|
||||
assertEquals(arrayListOf(0, 1, 1, 2, 3, 5, 8, 13, 21, 34), fibonacci().take(10).toList())
|
||||
}
|
||||
|
||||
Test fun mapAndTakeWhileExtractTheTransformedElements() {
|
||||
assertEquals(arrayListOf(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile {(i: Int) -> i < 20 }.toList())
|
||||
}
|
||||
|
||||
Test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
|
||||
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5))
|
||||
}
|
||||
|
||||
Test fun skippingIterator() {
|
||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().drop(7).makeString(limit = 10))
|
||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().drop(3).drop(4).makeString(limit = 10))
|
||||
}
|
||||
|
||||
Test fun toStringJoinsNoMoreThanTheFirstTenElements() {
|
||||
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().makeString(limit = 10))
|
||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.makeString(limit = 10))
|
||||
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.makeString())
|
||||
}
|
||||
|
||||
Test fun plus() {
|
||||
val stream = listOf("foo", "bar").stream()
|
||||
val streamChease = stream + "cheese"
|
||||
assertEquals(listOf("foo", "bar", "cheese"), streamChease.toList())
|
||||
|
||||
// lets use a mutable variable
|
||||
var mi = listOf("a", "b").stream()
|
||||
mi += "c"
|
||||
assertEquals(listOf("a", "b", "c"), mi.toList())
|
||||
}
|
||||
|
||||
Test fun plusCollection() {
|
||||
val a = listOf("foo", "bar")
|
||||
val b = listOf("cheese", "wine")
|
||||
val stream = a.stream() + b
|
||||
assertEquals(listOf("foo", "bar", "cheese", "wine"), stream.toList())
|
||||
|
||||
// lets use a mutable variable
|
||||
var ml = listOf("a").stream()
|
||||
ml += a
|
||||
ml += "beer"
|
||||
ml += b
|
||||
ml += "z"
|
||||
assertEquals(listOf("a", "foo", "bar", "beer", "cheese", "wine", "z"), ml.toList())
|
||||
}
|
||||
|
||||
|
||||
Test fun iterationOverStream() {
|
||||
val c = arrayListOf(0, 1, 2, 3, 4, 5)
|
||||
var s = ""
|
||||
for (i in c.stream()) {
|
||||
s = s + i.toString()
|
||||
}
|
||||
assertEquals("012345", s)
|
||||
}
|
||||
|
||||
Test fun streamFromFunction() {
|
||||
var count = 3
|
||||
|
||||
val stream = stream<Int> {
|
||||
count--
|
||||
if (count >= 0) count else null
|
||||
}
|
||||
|
||||
val list = stream.toList()
|
||||
assertEquals(listOf(2, 1, 0), list)
|
||||
}
|
||||
|
||||
Test fun streamFromFunctionWithInitialValue() {
|
||||
val values = stream<Int>(3) { n -> if (n > 0) n - 1 else null }
|
||||
assertEquals(arrayListOf(3, 2, 1, 0), values.toList())
|
||||
}
|
||||
|
||||
private fun <T, C : MutableCollection<in T>> Stream<T>.takeWhileTo(result: C, predicate: (T) -> Boolean): C {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
Test fun streamExtensions() {
|
||||
val c = arrayListOf(0, 1, 2, 3, 4, 5)
|
||||
val d = ArrayList<Int>()
|
||||
c.stream().takeWhileTo(d, { i -> i < 4 })
|
||||
assertEquals(4, d.size())
|
||||
}
|
||||
|
||||
/*
|
||||
Test fun pairIterator() {
|
||||
val pairStr = (fibonacci() zip fibonacci().map { i -> i*2 }).makeString(limit = 10)
|
||||
assertEquals("(0, 0), (1, 2), (1, 2), (2, 4), (3, 6), (5, 10), (8, 16), (13, 26), (21, 42), (34, 68), ...", pairStr)
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user