Add missing unsigned array constructors (from size)

#KT-25961 Fixed
This commit is contained in:
Ilya Gorbunov
2018-07-04 20:07:11 +03:00
parent 282cf73aff
commit f9479d12f9
7 changed files with 94 additions and 4 deletions
+7 -4
View File
@@ -292,6 +292,9 @@ class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn
out.println("internal constructor(private val storage: $storageArrayType) : Collection<$elementType> {")
out.println(
"""
/** Creates a new array of the specified [size], with all elements initialized to zero. */
public constructor(size: Int) : this($storageArrayType(size))
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): $elementType = storage[index].to$elementType()
@@ -322,10 +325,10 @@ class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn
out.println("}")
// TODO: Make inline constructor, like in ByteArray
out.println()
out.println("@SinceKotlin(\"1.3\")")
out.println("@ExperimentalUnsignedTypes")
out.println("""public inline fun $arrayType(size: Int, init: (Int) -> $elementType): $arrayType {
out.println("""
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public inline fun $arrayType(size: Int, init: (Int) -> $elementType): $arrayType {
return $arrayType($storageArrayType(size) { index -> init(index).to$storageElementType() })
}
@@ -0,0 +1,67 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package test.collections
import kotlin.test.*
class UnsignedArraysTest {
@Test
fun ubyteArrayInit() {
val zeroArray = UByteArray(42)
assertEquals(42, zeroArray.size)
for (index in zeroArray.indices) {
assertEquals(0u, zeroArray[index])
}
val initArray = UByteArray(42) { it.toUByte() }
for (index in initArray.indices) {
assertEquals(index.toUByte(), initArray[index])
}
}
@Test
fun ushortArrayInit() {
val zeroArray = UShortArray(42)
assertEquals(42, zeroArray.size)
for (index in zeroArray.indices) {
assertEquals(0u, zeroArray[index])
}
val initArray = UShortArray(42) { it.toUShort() }
for (index in initArray.indices) {
assertEquals(index.toUShort(), initArray[index])
}
}
@Test
fun uintArrayInit() {
val zeroArray = UIntArray(42)
assertEquals(42, zeroArray.size)
for (index in zeroArray.indices) {
assertEquals(0u, zeroArray[index])
}
val initArray = UIntArray(42) { it.toUInt() }
for (index in initArray.indices) {
assertEquals(index.toUInt(), initArray[index])
}
}
@Test
fun ulongArrayInit() {
val zeroArray = ULongArray(42)
assertEquals(42, zeroArray.size)
for (index in zeroArray.indices) {
assertEquals(0u, zeroArray[index])
}
val initArray = ULongArray(42) { it.toULong() }
for (index in initArray.indices) {
assertEquals(index.toULong(), initArray[index])
}
}
}
@@ -14,6 +14,9 @@ public inline class UByteArray
@PublishedApi
internal constructor(private val storage: ByteArray) : Collection<UByte> {
/** Creates a new array of the specified [size], with all elements initialized to zero. */
public constructor(size: Int) : this(ByteArray(size))
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): UByte = storage[index].toUByte()
@@ -14,6 +14,9 @@ public inline class UIntArray
@PublishedApi
internal constructor(private val storage: IntArray) : Collection<UInt> {
/** Creates a new array of the specified [size], with all elements initialized to zero. */
public constructor(size: Int) : this(IntArray(size))
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): UInt = storage[index].toUInt()
@@ -14,6 +14,9 @@ public inline class ULongArray
@PublishedApi
internal constructor(private val storage: LongArray) : Collection<ULong> {
/** Creates a new array of the specified [size], with all elements initialized to zero. */
public constructor(size: Int) : this(LongArray(size))
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): ULong = storage[index].toULong()
@@ -14,6 +14,9 @@ public inline class UShortArray
@PublishedApi
internal constructor(private val storage: ShortArray) : Collection<UShort> {
/** Creates a new array of the specified [size], with all elements initialized to zero. */
public constructor(size: Int) : this(ShortArray(size))
/** Returns the array element at the given [index]. This method can be called using the index operator. */
public operator fun get(index: Int): UShort = storage[index].toUShort()
@@ -273,6 +273,7 @@ public final class kotlin/UByte$Erased {
}
public final class kotlin/UByteArray : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> (I)V
public fun <init> ([B)V
public fun add (B)Z
public synthetic fun add (Ljava/lang/Object;)Z
@@ -299,6 +300,7 @@ public final class kotlin/UByteArray : java/util/Collection, kotlin/jvm/internal
public final class kotlin/UByteArray$Erased {
public static final fun box ([B)Lkotlin/UByteArray;
public static fun constructor (I)[B
public static fun constructor ([B)[B
public static fun contains ([BB)Z
public static fun containsAll ([BLjava/util/Collection;)Z
@@ -391,6 +393,7 @@ public final class kotlin/UInt$Erased {
}
public final class kotlin/UIntArray : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> (I)V
public fun <init> ([I)V
public fun add (I)Z
public synthetic fun add (Ljava/lang/Object;)Z
@@ -417,6 +420,7 @@ public final class kotlin/UIntArray : java/util/Collection, kotlin/jvm/internal/
public final class kotlin/UIntArray$Erased {
public static final fun box ([I)Lkotlin/UIntArray;
public static fun constructor (I)[I
public static fun constructor ([I)[I
public static fun contains ([II)Z
public static fun containsAll ([ILjava/util/Collection;)Z
@@ -509,6 +513,7 @@ public final class kotlin/ULong$Erased {
}
public final class kotlin/ULongArray : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> (I)V
public fun <init> ([J)V
public fun add (J)Z
public synthetic fun add (Ljava/lang/Object;)Z
@@ -535,6 +540,7 @@ public final class kotlin/ULongArray : java/util/Collection, kotlin/jvm/internal
public final class kotlin/ULongArray$Erased {
public static final fun box ([J)Lkotlin/ULongArray;
public static fun constructor (I)[J
public static fun constructor ([J)[J
public static fun contains ([JJ)Z
public static fun containsAll ([JLjava/util/Collection;)Z
@@ -625,6 +631,7 @@ public final class kotlin/UShort$Erased {
}
public final class kotlin/UShortArray : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> (I)V
public fun <init> ([S)V
public synthetic fun add (Ljava/lang/Object;)Z
public fun add (S)Z
@@ -651,6 +658,7 @@ public final class kotlin/UShortArray : java/util/Collection, kotlin/jvm/interna
public final class kotlin/UShortArray$Erased {
public static final fun box ([S)Lkotlin/UShortArray;
public static fun constructor (I)[S
public static fun constructor ([S)[S
public static fun contains ([SS)Z
public static fun containsAll ([SLjava/util/Collection;)Z