Primitive array constructor-like functions with init lambda.

#KT-8831

Update testData and resolve ambiguity in newArray test
This commit is contained in:
Ilya Gorbunov
2015-12-18 02:05:04 +03:00
parent 513c4a4562
commit 03816373b3
7 changed files with 524 additions and 16 deletions
+8
View File
@@ -1,6 +1,14 @@
package-fragment kotlin
public inline fun </*0*/ reified T> Array(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, T>): kotlin.Array<T>
public inline fun BooleanArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Boolean>): kotlin.BooleanArray
public inline fun ByteArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Byte>): kotlin.ByteArray
public inline fun CharArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Char>): kotlin.CharArray
public inline fun DoubleArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Double>): kotlin.DoubleArray
public inline fun FloatArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Float>): kotlin.FloatArray
public inline fun IntArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Int>): kotlin.IntArray
public inline fun LongArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Long>): kotlin.LongArray
public inline fun ShortArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, kotlin.Short>): kotlin.ShortArray
public inline fun </*0*/ reified T> arrayOf(/*0*/ vararg elements: T /*kotlin.Array<out T>*/): kotlin.Array<T>
public fun </*0*/ reified T> arrayOfNulls(/*0*/ size: kotlin.Int): kotlin.Array<T?>
public fun booleanArrayOf(/*0*/ vararg elements: kotlin.Boolean /*kotlin.BooleanArray*/): kotlin.BooleanArray
@@ -1,11 +1,13 @@
private fun <T> upcast(value: T): T = value
fun box(): String {
(::ByteArray)(10)
(::IntArray)(10)
(::ShortArray)(10)
(::LongArray)(10)
(::DoubleArray)(10)
(::FloatArray)(10)
(::BooleanArray)(10)
upcast<(Int)->ByteArray>(::ByteArray)(10)
upcast<(Int)->IntArray>(::IntArray)(10)
upcast<(Int)->ShortArray>(::ShortArray)(10)
upcast<(Int)->LongArray>(::LongArray)(10)
upcast<(Int)->DoubleArray>(::DoubleArray)(10)
upcast<(Int)->FloatArray>(::FloatArray)(10)
upcast<(Int)->BooleanArray>(::BooleanArray)(10)
return "OK"
}
+97 -4
View File
@@ -22,14 +22,107 @@ package kotlin
*/
public inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
val result = arrayOfNulls<T>(size)
for (i in 0..size - 1) {
for (i in 0..size - 1)
result[i] = init(i)
}
return result as Array<T>
}
/**
* Returns an array of [Double] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun DoubleArray(size: Int, init: (Int) -> Double): DoubleArray {
val result = DoubleArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Float] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun FloatArray(size: Int, init: (Int) -> Float): FloatArray {
val result = FloatArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Long] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun LongArray(size: Int, init: (Int) -> Long): LongArray {
val result = LongArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Int] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun IntArray(size: Int, init: (Int) -> Int): IntArray {
val result = IntArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Char] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun CharArray(size: Int, init: (Int) -> Char): CharArray {
val result = CharArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Short] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun ShortArray(size: Int, init: (Int) -> Short): ShortArray {
val result = ShortArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Byte] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun ByteArray(size: Int, init: (Int) -> Byte): ByteArray {
val result = ByteArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Boolean] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray {
val result = BooleanArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an empty array of the specified type [T].
*/
+232
View File
@@ -37,6 +37,238 @@ PsiJetFileStubImpl[package=kotlin]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=T]
FUN:[fqName=kotlin.BooleanArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=BooleanArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Boolean]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=BooleanArray]
FUN:[fqName=kotlin.ByteArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=ByteArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Byte]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=ByteArray]
FUN:[fqName=kotlin.CharArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=CharArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Char]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=CharArray]
FUN:[fqName=kotlin.DoubleArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=DoubleArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Double]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=DoubleArray]
FUN:[fqName=kotlin.FloatArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=FloatArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Float]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=FloatArray]
FUN:[fqName=kotlin.IntArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=IntArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=IntArray]
FUN:[fqName=kotlin.LongArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=LongArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Long]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=LongArray]
FUN:[fqName=kotlin.ShortArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=ShortArray]
MODIFIER_LIST:[public inline]
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init]
TYPE_REFERENCE:
FUNCTION_TYPE:
VALUE_PARAMETER_LIST:
VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Short]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=ShortArray]
FUN:[fqName=kotlin.arrayOf, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=true, isExtension=false, isTopLevel=true, name=arrayOf]
MODIFIER_LIST:[public inline]
TYPE_PARAMETER_LIST:
+16
View File
@@ -5,6 +5,22 @@ package kotlin
public inline fun <reified T> Array(size: kotlin.Int, init: (kotlin.Int) -> T): kotlin.Array<T> { /* compiled code */ }
public inline fun BooleanArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Boolean): kotlin.BooleanArray { /* compiled code */ }
public inline fun ByteArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Byte): kotlin.ByteArray { /* compiled code */ }
public inline fun CharArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Char): kotlin.CharArray { /* compiled code */ }
public inline fun DoubleArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Double): kotlin.DoubleArray { /* compiled code */ }
public inline fun FloatArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Float): kotlin.FloatArray { /* compiled code */ }
public inline fun IntArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Int): kotlin.IntArray { /* compiled code */ }
public inline fun LongArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Long): kotlin.LongArray { /* compiled code */ }
public inline fun ShortArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Short): kotlin.ShortArray { /* compiled code */ }
public inline fun <reified T> arrayOf(vararg elements: T): kotlin.Array<T> { /* compiled code */ }
public fun <reified T> arrayOfNulls(size: kotlin.Int): kotlin.Array<T?> { /* compiled code */ }
+98 -5
View File
@@ -2,21 +2,114 @@ package kotlin
import java.util.*
// TODO: @library("arrayFromFun")
/**
* Returns an array with the specified [size], where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
// TODO: @library("arrayFromFun")
public inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
val result = arrayOfNulls<T>(size)
for (i in 0..size - 1) {
for (i in 0..size - 1)
result[i] = init(i)
}
return result as Array<T>
}
/**
* Returns an array of [Double] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun DoubleArray(size: Int, init: (Int) -> Double): DoubleArray {
val result = DoubleArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Float] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun FloatArray(size: Int, init: (Int) -> Float): FloatArray {
val result = FloatArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Long] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun LongArray(size: Int, init: (Int) -> Long): LongArray {
val result = LongArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Int] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun IntArray(size: Int, init: (Int) -> Int): IntArray {
val result = IntArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Char] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun CharArray(size: Int, init: (Int) -> Char): CharArray {
val result = CharArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Short] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun ShortArray(size: Int, init: (Int) -> Short): ShortArray {
val result = ShortArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Byte] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun ByteArray(size: Int, init: (Int) -> Byte): ByteArray {
val result = ByteArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an array of [Boolean] numbers with the specified [size],
* where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray {
val result = BooleanArray(size)
for (i in 0..size - 1)
result[i] = init(i)
return result
}
/**
* Returns an empty array of the specified type [T].
*/
@@ -46,6 +46,14 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun byteArrayInit() {
val arr = ByteArray(2) { it.toByte() }
assertEquals(2, arr.size)
assertEquals(0.toByte(), arr[0])
assertEquals(1.toByte(), arr[1])
}
@test fun shortArray() {
val arr = ShortArray(2)
@@ -55,6 +63,14 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun shortArrayInit() {
val arr = ShortArray(2) { it.toShort() }
assertEquals(2, arr.size)
assertEquals(0.toShort(), arr[0])
assertEquals(1.toShort(), arr[1])
}
@test fun intArray() {
val arr = IntArray(2)
@@ -62,7 +78,15 @@ class ArraysTest {
assertEquals(0, arr[0])
assertEquals(0, arr[1])
}
@test fun intArrayInit() {
val arr = IntArray(2) { it.toInt() }
assertEquals(2, arr.size)
assertEquals(0.toInt(), arr[0])
assertEquals(1.toInt(), arr[1])
}
@test fun longArray() {
val arr = LongArray(2)
@@ -72,6 +96,14 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun longArrayInit() {
val arr = LongArray(2) { it.toLong() }
assertEquals(2, arr.size)
assertEquals(0.toLong(), arr[0])
assertEquals(1.toLong(), arr[1])
}
@test fun floatArray() {
val arr = FloatArray(2)
@@ -80,6 +112,14 @@ class ArraysTest {
assertEquals(expected, arr[0])
assertEquals(expected, arr[1])
}
@test fun floatArrayInit() {
val arr = FloatArray(2) { it.toFloat() }
assertEquals(2, arr.size)
assertEquals(0.toFloat(), arr[0])
assertEquals(1.toFloat(), arr[1])
}
@test fun doubleArray() {
val arr = DoubleArray(2)
@@ -89,6 +129,14 @@ class ArraysTest {
assertEquals(0.0, arr[1])
}
@test fun doubleArrayInit() {
val arr = DoubleArray(2) { it.toDouble() }
assertEquals(2, arr.size)
assertEquals(0.toDouble(), arr[0])
assertEquals(1.toDouble(), arr[1])
}
@test fun charArray() {
val arr = CharArray(2)
@@ -98,6 +146,14 @@ class ArraysTest {
assertEquals(expected, arr[1])
}
@test fun charArrayInit() {
val arr = CharArray(2) { 'a' + it }
assertEquals(2, arr.size)
assertEquals('a', arr[0])
assertEquals('b', arr[1])
}
@test fun booleanArray() {
val arr = BooleanArray(2)
assertEquals(arr.size, 2)
@@ -105,6 +161,14 @@ class ArraysTest {
assertEquals(false, arr[1])
}
@test fun booleanArrayInit() {
val arr = BooleanArray(2) { it % 2 == 0 }
assertEquals(2, arr.size)
assertEquals(true, arr[0])
assertEquals(false, arr[1])
}
@test fun min() {
expect(null, { arrayOf<Int>().min() })
expect(1, { arrayOf(1).min() })