Implement reduce and reduceRight functions
- move arrays reduce tests
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
package test.arrays
|
package test.arrays
|
||||||
|
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
import org.junit.Test as test
|
||||||
|
|
||||||
import junit.framework.TestCase
|
class ArraysTest {
|
||||||
|
|
||||||
class ArraysTest() : TestCase() {
|
test fun copyOf() {
|
||||||
|
|
||||||
fun testCopyOf() {
|
|
||||||
checkContent(booleanArray(true, false, true, false, true, false).copyOf().iterator(), 6) { it % 2 == 0 }
|
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(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(shortArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it.toShort() }
|
||||||
@@ -17,7 +16,7 @@ class ArraysTest() : TestCase() {
|
|||||||
checkContent(charArray('0', '1', '2', '3', '4', '5').copyOf().iterator(), 6) { (it + '0').toChar() }
|
checkContent(charArray('0', '1', '2', '3', '4', '5').copyOf().iterator(), 6) { (it + '0').toChar() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testCopyOfRange() {
|
test fun copyOfRange() {
|
||||||
checkContent(booleanArray(true, false, true, false, true, false).copyOfRange(0, 3).iterator(), 3) { it % 2 == 0 }
|
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(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(shortArray(0, 1, 2, 3, 4, 5).copyOfRange(0, 3).iterator(), 3) { it.toShort() }
|
||||||
@@ -39,7 +38,7 @@ class ArraysTest() : TestCase() {
|
|||||||
assertFalse(iter.hasNext, "Invalid length (hasNext)")
|
assertFalse(iter.hasNext, "Invalid length (hasNext)")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testEmptyArrayLastIndex() {
|
test fun emptyArrayLastIndex() {
|
||||||
val arr1 = IntArray(0)
|
val arr1 = IntArray(0)
|
||||||
assertEquals(-1, arr1.lastIndex)
|
assertEquals(-1, arr1.lastIndex)
|
||||||
|
|
||||||
@@ -47,7 +46,7 @@ class ArraysTest() : TestCase() {
|
|||||||
assertEquals(-1, arr2.lastIndex)
|
assertEquals(-1, arr2.lastIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testArrayLastIndex() {
|
test fun arrayLastIndex() {
|
||||||
val arr1 = intArray(0, 1, 2, 3, 4)
|
val arr1 = intArray(0, 1, 2, 3, 4)
|
||||||
assertEquals(4, arr1.lastIndex)
|
assertEquals(4, arr1.lastIndex)
|
||||||
assertEquals(4, arr1[arr1.lastIndex])
|
assertEquals(4, arr1[arr1.lastIndex])
|
||||||
@@ -57,4 +56,36 @@ class ArraysTest() : TestCase() {
|
|||||||
assertEquals("4", arr2[arr2.lastIndex])
|
assertEquals("4", arr2[arr2.lastIndex])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<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<UnsupportedOperationException> {
|
||||||
|
intArray().reduceRight { a, b -> a + b}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,11 +222,6 @@ class CollectionTest {
|
|||||||
list.reduce { a, b -> a + b }
|
list.reduce { a, b -> a + b }
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(-4) {
|
|
||||||
val array = array(1, 2, 3)
|
|
||||||
array.reduce { a, b -> a - b }
|
|
||||||
}
|
|
||||||
|
|
||||||
failsWith<UnsupportedOperationException> {
|
failsWith<UnsupportedOperationException> {
|
||||||
arrayList<Int>().reduce { a, b -> a + b}
|
arrayList<Int>().reduce { a, b -> a + b}
|
||||||
}
|
}
|
||||||
@@ -237,10 +232,6 @@ class CollectionTest {
|
|||||||
val list = arrayList("1", "2", "3", "4")
|
val list = arrayList("1", "2", "3", "4")
|
||||||
list.reduceRight { a, b -> a + b }
|
list.reduceRight { a, b -> a + b }
|
||||||
}
|
}
|
||||||
expect(2) {
|
|
||||||
val array = array(1, 2, 3)
|
|
||||||
array.reduceRight { a, b -> a - b }
|
|
||||||
}
|
|
||||||
|
|
||||||
failsWith<UnsupportedOperationException> {
|
failsWith<UnsupportedOperationException> {
|
||||||
arrayList<Int>().reduceRight { a, b -> a + b}
|
arrayList<Int>().reduceRight { a, b -> a + b}
|
||||||
|
|||||||
Reference in New Issue
Block a user