translator: fix IntArray.copyOfRange, add tests for IntArray extensions

This commit is contained in:
Alexey Stepanov
2016-08-11 12:48:41 +03:00
parent a8e843a10c
commit 9452ae5a3b
3 changed files with 41 additions and 6 deletions
+6 -6
View File
@@ -38,7 +38,7 @@ class IntArray(var size: Int) {
}
public fun IntArray.copyOf(newSize: Int): IntArray {
fun IntArray.copyOf(newSize: Int): IntArray {
val newInstance = IntArray(newSize)
var index = 0
val end = if (newSize > this.size) this.size else newSize
@@ -56,10 +56,10 @@ public fun IntArray.copyOf(newSize: Int): IntArray {
return newInstance
}
public fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray {
val newInstance = IntArray(toIndex - fromIndex + 1)
fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray {
val newInstance = IntArray(toIndex - fromIndex)
var index = fromIndex
while (index <= toIndex) {
while (index < toIndex) {
val value = this.get(index)
newInstance.set(index - fromIndex, value)
index = index + 1
@@ -68,14 +68,14 @@ public fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray {
return newInstance
}
public operator fun IntArray.plus(element: Int): IntArray {
operator fun IntArray.plus(element: Int): IntArray {
val index = size
val result = this.copyOf(index + 1)
result[index] = element
return result
}
public operator fun IntArray.plus(elements: IntArray): IntArray {
operator fun IntArray.plus(elements: IntArray): IntArray {
val thisSize = size
val arraySize = elements.size
val resultSize = thisSize + arraySize
@@ -0,0 +1,2 @@
array_extensions_1_copyOf() == 22
array_extensions_1_copyOfRange() == 779
@@ -0,0 +1,33 @@
fun IntArray.print() {
var index = 0
while (index < size) {
println(get(index))
index++
}
println(1111111)
}
fun array_extensions_1_copyOf(): Int {
val array = IntArray(3)
array[0] = 1
array[1] = 20
array[2] = 333
val minimize = array.copyOf(2)
return minimize[1] + minimize.size
}
fun array_extensions_1_copyOfRange(): Int {
val array = IntArray(7)
array[0] = 1
array[1] = 20
array[2] = 333
array[3] = 444
array[4] = 555
array[5] = 666
array[6] = 777
val minimize = array.copyOfRange(2, 4)
val ans = minimize[0] + minimize[1] + minimize.size
return ans
}