From 527e379480ad370c9283bc2b782a130efd783b58 Mon Sep 17 00:00:00 2001 From: Sergey Lukjanov Date: Fri, 16 Mar 2012 13:07:26 +0400 Subject: [PATCH 1/2] Arrays.copyOf() and .copyOfRange() should return not nullable result --- libraries/stdlib/src/Arrays.kt | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libraries/stdlib/src/Arrays.kt b/libraries/stdlib/src/Arrays.kt index ba857153dcb..422b5bcce65 100644 --- a/libraries/stdlib/src/Arrays.kt +++ b/libraries/stdlib/src/Arrays.kt @@ -75,25 +75,25 @@ inline fun FloatArray.sort(fromIndex: Int, toIndex: Int) = Arrays.sort(this, fr inline fun DoubleArray.sort(fromIndex: Int, toIndex: Int) = Arrays.sort(this, fromIndex, toIndex) inline fun CharArray.sort(fromIndex: Int, toIndex: Int) = Arrays.sort(this, fromIndex, toIndex) -inline fun BooleanArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) -inline fun ByteArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) -inline fun ShortArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) -inline fun IntArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) -inline fun LongArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) -inline fun FloatArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) -inline fun DoubleArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) -inline fun CharArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength) +inline fun BooleanArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() +inline fun ByteArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() +inline fun ShortArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() +inline fun IntArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() +inline fun LongArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() +inline fun FloatArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() +inline fun DoubleArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() +inline fun CharArray.copyOf(newLength: Int = this.size) = Arrays.copyOf(this, newLength).sure() inline fun Array.copyOf(newLength: Int = this.size) : Array = Arrays.copyOf(this, newLength).sure() -inline fun BooleanArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) -inline fun ByteArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) -inline fun ShortArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) -inline fun IntArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) -inline fun LongArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) -inline fun FloatArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) -inline fun DoubleArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) -inline fun CharArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to) +inline fun BooleanArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() +inline fun ByteArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() +inline fun ShortArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() +inline fun IntArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() +inline fun LongArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() +inline fun FloatArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() +inline fun DoubleArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() +inline fun CharArray.copyOfRange(from: Int, to: Int) = Arrays.copyOfRange(this, from, to).sure() inline fun Array.copyOfRange(from: Int, to: Int) : Array = Arrays.copyOfRange(this, from, to).sure() From ec8cc3aa96a202d38ff2db1273e53bda7f16332f Mon Sep 17 00:00:00 2001 From: Sergey Lukjanov Date: Fri, 16 Mar 2012 15:46:42 +0400 Subject: [PATCH 2/2] Tests for Arrays#copyOf() and Arrays@copyOfRange() --- libraries/stdlib/test/ArraysTest.kt | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 libraries/stdlib/test/ArraysTest.kt diff --git a/libraries/stdlib/test/ArraysTest.kt b/libraries/stdlib/test/ArraysTest.kt new file mode 100644 index 00000000000..4fe0f3b133b --- /dev/null +++ b/libraries/stdlib/test/ArraysTest.kt @@ -0,0 +1,42 @@ +package test.arrays + +import kotlin.test.* + +import junit.framework.TestCase + +class ArraysTest() : TestCase() { + + fun testCopyOf() { + 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() } + } + + fun testCopyOfRange() { + 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() } + } + + + fun checkContent(val iter : Iterator, val length : Int, val 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)") + } + +}