Merge pull request #24 from Frostman/arrays_kt_fix

Arrays.copyOf() and .copyOfRange() should return not nullable result
This commit is contained in:
James Strachan
2012-03-17 09:15:48 -07:00
2 changed files with 58 additions and 16 deletions
+16 -16
View File
@@ -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 <T> Array<T>.copyOf(newLength: Int = this.size) : Array<T> = 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 <T> Array<T>.copyOfRange(from: Int, to: Int) : Array<T> = Arrays.copyOfRange(this, from, to).sure()
+42
View File
@@ -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 <T> checkContent(val iter : Iterator<T>, 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)")
}
}