Files
kotlin-fork/libraries/stdlib/test/collections/UnsignedArraysTest.kt
T
2019-02-25 21:58:53 +03:00

231 lines
8.8 KiB
Kotlin

/*
* 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:Suppress("SIGNED_CONSTANT_CONVERTED_TO_UNSIGNED")
package test.collections
import test.collections.behaviors.collectionBehavior
import kotlin.test.*
class UnsignedArraysTest {
@Test
fun collectionBehavior() {
compare(listOf<UByte>(), ubyteArrayOf()) { this.collectionBehavior() }
compare(listOf<UShort>(1), ushortArrayOf(1)) { this.collectionBehavior() }
compare(listOf<UInt>(1, 2), uintArrayOf(1u, 2u)) { this.collectionBehavior() }
compare(listOf<ULong>(1, 2, 3), ulongArrayOf(1u, 2u, 3u)) { this.collectionBehavior() }
}
@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])
}
}
@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) }
}
}
}