Generate extensions for unsigned arrays and provide tests for them
- contentEquals, contentToString, contentHashCode - as[Signed]Array, as[Unsigned]Array - to[Signed]Array, to[Unsigned]Array - toTypedArray - copyOf(), copyOf(newSize), copyOfRange(...)
This commit is contained in:
@@ -0,0 +1,507 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@file:kotlin.jvm.JvmMultifileClass
|
||||
@file:kotlin.jvm.JvmName("UArraysKt")
|
||||
|
||||
package kotlin.collections
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import kotlin.*
|
||||
import kotlin.text.*
|
||||
import kotlin.comparisons.*
|
||||
|
||||
/**
|
||||
* Returns an array of type [ByteArray], which is a view of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.asByteArray(): ByteArray {
|
||||
return storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [IntArray], which is a view of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.asIntArray(): IntArray {
|
||||
return storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [LongArray], which is a view of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.asLongArray(): LongArray {
|
||||
return storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [ShortArray], which is a view of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.asShortArray(): ShortArray {
|
||||
return storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [UByteArray], which is a view of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.asUByteArray(): UByteArray {
|
||||
return UByteArray(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [UIntArray], which is a view of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.asUIntArray(): UIntArray {
|
||||
return UIntArray(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [ULongArray], which is a view of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.asULongArray(): ULongArray {
|
||||
return ULongArray(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [UShortArray], which is a view of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.asUShortArray(): UShortArray {
|
||||
return UShortArray(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the two specified arrays are *structurally* equal to one another,
|
||||
* i.e. contain the same number of the same elements in the same order.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public infix fun UIntArray.contentEquals(other: UIntArray): Boolean {
|
||||
return storage.contentEquals(other.storage)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the two specified arrays are *structurally* equal to one another,
|
||||
* i.e. contain the same number of the same elements in the same order.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public infix fun ULongArray.contentEquals(other: ULongArray): Boolean {
|
||||
return storage.contentEquals(other.storage)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the two specified arrays are *structurally* equal to one another,
|
||||
* i.e. contain the same number of the same elements in the same order.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public infix fun UByteArray.contentEquals(other: UByteArray): Boolean {
|
||||
return storage.contentEquals(other.storage)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the two specified arrays are *structurally* equal to one another,
|
||||
* i.e. contain the same number of the same elements in the same order.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public infix fun UShortArray.contentEquals(other: UShortArray): Boolean {
|
||||
return storage.contentEquals(other.storage)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hash code based on the contents of this array as if it is [List].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.contentHashCode(): Int {
|
||||
return storage.contentHashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hash code based on the contents of this array as if it is [List].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.contentHashCode(): Int {
|
||||
return storage.contentHashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hash code based on the contents of this array as if it is [List].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.contentHashCode(): Int {
|
||||
return storage.contentHashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hash code based on the contents of this array as if it is [List].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.contentHashCode(): Int {
|
||||
return storage.contentHashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
*
|
||||
* @sample samples.collections.Arrays.ContentOperations.contentToString
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.contentToString(): String {
|
||||
return joinToString(", ", "[", "]")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
*
|
||||
* @sample samples.collections.Arrays.ContentOperations.contentToString
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.contentToString(): String {
|
||||
return joinToString(", ", "[", "]")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
*
|
||||
* @sample samples.collections.Arrays.ContentOperations.contentToString
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.contentToString(): String {
|
||||
return joinToString(", ", "[", "]")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the contents of the specified array as if it is [List].
|
||||
*
|
||||
* @sample samples.collections.Arrays.ContentOperations.contentToString
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.contentToString(): String {
|
||||
return joinToString(", ", "[", "]")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array.
|
||||
*
|
||||
* @sample samples.collections.Arrays.CopyOfOperations.copyOf
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.copyOf(): UIntArray {
|
||||
return UIntArray(storage.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array.
|
||||
*
|
||||
* @sample samples.collections.Arrays.CopyOfOperations.copyOf
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.copyOf(): ULongArray {
|
||||
return ULongArray(storage.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array.
|
||||
*
|
||||
* @sample samples.collections.Arrays.CopyOfOperations.copyOf
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.copyOf(): UByteArray {
|
||||
return UByteArray(storage.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array.
|
||||
*
|
||||
* @sample samples.collections.Arrays.CopyOfOperations.copyOf
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.copyOf(): UShortArray {
|
||||
return UShortArray(storage.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array, resized to the given [newSize].
|
||||
* The copy is either truncated or padded at the end with zero values if necessary.
|
||||
*
|
||||
* - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize].
|
||||
* - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.copyOf(newSize: Int): UIntArray {
|
||||
return UIntArray(storage.copyOf(newSize))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array, resized to the given [newSize].
|
||||
* The copy is either truncated or padded at the end with zero values if necessary.
|
||||
*
|
||||
* - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize].
|
||||
* - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.copyOf(newSize: Int): ULongArray {
|
||||
return ULongArray(storage.copyOf(newSize))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array, resized to the given [newSize].
|
||||
* The copy is either truncated or padded at the end with zero values if necessary.
|
||||
*
|
||||
* - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize].
|
||||
* - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.copyOf(newSize: Int): UByteArray {
|
||||
return UByteArray(storage.copyOf(newSize))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array which is a copy of the original array, resized to the given [newSize].
|
||||
* The copy is either truncated or padded at the end with zero values if necessary.
|
||||
*
|
||||
* - If [newSize] is less than the size of the original array, the copy array is truncated to the [newSize].
|
||||
* - If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with zero values.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.copyOf(newSize: Int): UShortArray {
|
||||
return UShortArray(storage.copyOf(newSize))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new array which is a copy of the specified range of the original array.
|
||||
*
|
||||
* @param fromIndex the start of the range (inclusive), must be in `0..array.size`
|
||||
* @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size`
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.copyOfRange(fromIndex: Int, toIndex: Int): UIntArray {
|
||||
return UIntArray(storage.copyOfRange(fromIndex, toIndex))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new array which is a copy of the specified range of the original array.
|
||||
*
|
||||
* @param fromIndex the start of the range (inclusive), must be in `0..array.size`
|
||||
* @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size`
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.copyOfRange(fromIndex: Int, toIndex: Int): ULongArray {
|
||||
return ULongArray(storage.copyOfRange(fromIndex, toIndex))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new array which is a copy of the specified range of the original array.
|
||||
*
|
||||
* @param fromIndex the start of the range (inclusive), must be in `0..array.size`
|
||||
* @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size`
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.copyOfRange(fromIndex: Int, toIndex: Int): UByteArray {
|
||||
return UByteArray(storage.copyOfRange(fromIndex, toIndex))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new array which is a copy of the specified range of the original array.
|
||||
*
|
||||
* @param fromIndex the start of the range (inclusive), must be in `0..array.size`
|
||||
* @param toIndex the end of the range (exclusive), must be in `fromIndex..array.size`
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.copyOfRange(fromIndex: Int, toIndex: Int): UShortArray {
|
||||
return UShortArray(storage.copyOfRange(fromIndex, toIndex))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [ByteArray], which is a copy of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.toByteArray(): ByteArray {
|
||||
return storage.copyOf()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [IntArray], which is a copy of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.toIntArray(): IntArray {
|
||||
return storage.copyOf()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [LongArray], which is a copy of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.toLongArray(): LongArray {
|
||||
return storage.copyOf()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [ShortArray], which is a copy of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.toShortArray(): ShortArray {
|
||||
return storage.copyOf()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.toTypedArray(): Array<UInt> {
|
||||
return Array(size) { index -> this[index] }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.toTypedArray(): Array<ULong> {
|
||||
return Array(size) { index -> this[index] }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.toTypedArray(): Array<UByte> {
|
||||
return Array(size) { index -> this[index] }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.toTypedArray(): Array<UShort> {
|
||||
return Array(size) { index -> this[index] }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [UByteArray], which is a copy of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.toUByteArray(): UByteArray {
|
||||
return UByteArray(this.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [UIntArray], which is a copy of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.toUIntArray(): UIntArray {
|
||||
return UIntArray(this.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [ULongArray], which is a copy of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.toULongArray(): ULongArray {
|
||||
return ULongArray(this.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of type [UShortArray], which is a copy of this array where each element is an unsigned reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.toUShortArray(): UShortArray {
|
||||
return UShortArray(this.copyOf())
|
||||
}
|
||||
|
||||
@@ -1126,6 +1126,6 @@ public actual fun BooleanArray.toTypedArray(): Array<Boolean> {
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public actual fun CharArray.toTypedArray(): Array<Char> {
|
||||
return Array<Char>(size, { i -> this[i] })
|
||||
return Array(size) { index -> this[index] }
|
||||
}
|
||||
|
||||
|
||||
@@ -64,4 +64,158 @@ class UnsignedArraysTest {
|
||||
assertEquals(index.toULong(), initArray[index])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun contentHashCode() {
|
||||
ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) }
|
||||
uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) }
|
||||
ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) }
|
||||
ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).let { assertEquals(it.toList().hashCode(), it.contentHashCode()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun contentToString() {
|
||||
ulongArrayOf(1uL, ULong.MAX_VALUE, ULong.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) }
|
||||
uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) }
|
||||
ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) }
|
||||
ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).let { assertEquals(it.toList().toString(), it.contentToString()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun contentEquals() {
|
||||
uintArrayOf(1u, UInt.MAX_VALUE, UInt.MIN_VALUE).let { arr ->
|
||||
assertTrue(arr contentEquals UIntArray(arr.size, arr::get))
|
||||
assertFalse(arr contentEquals UIntArray(arr.size - 1, arr::get))
|
||||
}
|
||||
ulongArrayOf(1u, ULong.MAX_VALUE, ULong.MIN_VALUE).let { arr ->
|
||||
assertTrue(arr contentEquals ULongArray(arr.size, arr::get))
|
||||
assertFalse(arr contentEquals ULongArray(arr.size - 1, arr::get))
|
||||
}
|
||||
ushortArrayOf(1u, UShort.MAX_VALUE, UShort.MIN_VALUE).let { arr ->
|
||||
assertTrue(arr contentEquals UShortArray(arr.size, arr::get))
|
||||
assertFalse(arr contentEquals UShortArray(arr.size - 1, arr::get))
|
||||
}
|
||||
ubyteArrayOf(1u, UByte.MAX_VALUE, UByte.MIN_VALUE).let { arr ->
|
||||
assertTrue(arr contentEquals UByteArray(arr.size, arr::get))
|
||||
assertFalse(arr contentEquals UByteArray(arr.size - 1, arr::get))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun asArray() {
|
||||
val uintArray = uintArrayOf(1, UInt.MAX_VALUE)
|
||||
val intArray = uintArray.asIntArray()
|
||||
assertTrue(intArray contentEquals intArrayOf(1, -1))
|
||||
|
||||
intArray.reverse()
|
||||
val uintArray2 = intArray.asUIntArray()
|
||||
|
||||
assertTrue(uintArray contentEquals uintArray2)
|
||||
assertTrue(uintArray contentEquals uintArrayOf(UInt.MAX_VALUE, 1))
|
||||
|
||||
|
||||
val ulongArray = ulongArrayOf(1, ULong.MAX_VALUE)
|
||||
val longArray = ulongArray.asLongArray()
|
||||
assertTrue(longArray contentEquals longArrayOf(1, -1))
|
||||
|
||||
longArray.reverse()
|
||||
val ulongArray2 = longArray.asULongArray()
|
||||
assertTrue(ulongArray contentEquals ulongArray2)
|
||||
assertTrue(ulongArray contentEquals ulongArrayOf(ULong.MAX_VALUE, 1))
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun toArray() {
|
||||
val uintArray = uintArrayOf(UInt.MAX_VALUE)
|
||||
val intArray = uintArray.toIntArray()
|
||||
assertTrue(intArray contentEquals intArrayOf(-1))
|
||||
|
||||
intArray[0] = 0
|
||||
val uintArray2 = intArray.toUIntArray()
|
||||
assertEquals(UInt.MAX_VALUE, uintArray[0])
|
||||
|
||||
intArray[0] = 1
|
||||
assertEquals(0u, uintArray2[0])
|
||||
|
||||
|
||||
val ulongArray = ulongArrayOf(ULong.MAX_VALUE)
|
||||
val longArray = ulongArray.toLongArray()
|
||||
assertTrue(longArray contentEquals longArrayOf(-1))
|
||||
|
||||
longArray[0] = 0
|
||||
val ulongArray2 = longArray.toULongArray()
|
||||
assertEquals(ULong.MAX_VALUE, ulongArray[0])
|
||||
|
||||
longArray[0] = 1
|
||||
assertEquals(0u, ulongArray2[0])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toTypedArray() {
|
||||
uintArrayOf(UInt.MIN_VALUE, UInt.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) }
|
||||
ulongArrayOf(ULong.MIN_VALUE, ULong.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) }
|
||||
ushortArrayOf(UShort.MIN_VALUE, UShort.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) }
|
||||
ubyteArrayOf(UByte.MIN_VALUE, UByte.MAX_VALUE).let { assertTrue(it.toTypedArray() contentEquals it.toList().toTypedArray()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyOf() {
|
||||
uintArrayOf(UInt.MAX_VALUE).let { arr ->
|
||||
val copy = arr.copyOf()
|
||||
assertTrue(arr contentEquals copy)
|
||||
copy[0] = 1u
|
||||
assertFalse(arr contentEquals copy)
|
||||
arr[0] = 2u
|
||||
assertFalse(arr contentEquals copy)
|
||||
}
|
||||
ulongArrayOf(ULong.MAX_VALUE).let { arr ->
|
||||
val copy = arr.copyOf()
|
||||
assertTrue(arr contentEquals copy)
|
||||
copy[0] = 1u
|
||||
assertFalse(arr contentEquals copy)
|
||||
arr[0] = 2u
|
||||
assertFalse(arr contentEquals copy)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyAndResize() {
|
||||
assertTrue(uintArrayOf(1, 2) contentEquals uintArrayOf(1, 2, 3).copyOf(2))
|
||||
assertTrue(uintArrayOf(1, 2, 0) contentEquals uintArrayOf(1, 2).copyOf(3))
|
||||
|
||||
assertTrue(ulongArrayOf(1, 2) contentEquals ulongArrayOf(1, 2, 3).copyOf(2))
|
||||
assertTrue(ulongArrayOf(1, 2, 0) contentEquals ulongArrayOf(1, 2).copyOf(3))
|
||||
|
||||
|
||||
assertTrue(uintArrayOf() contentEquals uintArrayOf(1).copyOf(0))
|
||||
assertTrue(ulongArrayOf() contentEquals ulongArrayOf(1).copyOf(0))
|
||||
|
||||
// RuntimeException is the most specific common of JVM and JS implementations
|
||||
assertFailsWith<RuntimeException> { uintArrayOf().copyOf(-1) }
|
||||
assertFailsWith<RuntimeException> { ulongArrayOf().copyOf(-1) }
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun copyOfRange() {
|
||||
assertTrue(ubyteArrayOf(0, 1, 2) contentEquals ubyteArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
|
||||
assertTrue(ushortArrayOf(0, 1, 2) contentEquals ushortArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
|
||||
assertTrue(uintArrayOf(0, 1, 2) contentEquals uintArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
|
||||
assertTrue(ulongArrayOf(0, 1, 2) contentEquals ulongArrayOf(0, 1, 2, 3, 4, 5).copyOfRange(0, 3))
|
||||
|
||||
for (pos in 0..3) {
|
||||
assertTrue(uintArrayOf() contentEquals uintArrayOf(1, 2, 3).copyOfRange(pos, pos))
|
||||
assertTrue(ulongArrayOf() contentEquals ULongArray(3) { it.toULong() }.copyOfRange(pos, pos))
|
||||
}
|
||||
|
||||
for ((start, end) in listOf(-1 to 0, 0 to 2, 2 to 2, 1 to 0)) {
|
||||
val bounds = "start: $start, end: $end"
|
||||
val exClass = if (start > end) IllegalArgumentException::class else IndexOutOfBoundsException::class
|
||||
assertFailsWith(exClass, bounds) { uintArrayOf(1).copyOfRange(start, end) }
|
||||
assertFailsWith(exClass, bounds) { ulongArrayOf(1uL).copyOfRange(start, end) }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user