Files
bytes.kt/src/commonTest/kotlin/com/infendro/bytes/ulong/Conversion.kt
2026-01-14 15:36:50 +01:00

92 lines
2.8 KiB
Kotlin

package com.infendro.bytes.ulong
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `ULong Conversion Test` {
@Test
fun toUByteArray() {
val expected = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
)
val actual = arrayOf(
0x1234567812345678UL.toUByteArray(),
0x1234567812345678UL.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun toByteArray() {
val expected = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
)
val actual = arrayOf(
0x1234567812345678UL.toByteArray(),
0x1234567812345678UL.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `Array toUByteArray`() {
val expected = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
),
)
val actual = arrayOf(
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toUByteArray(),
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `Array toByteArray`() {
val expected = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
),
byteArrayOf(
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
),
)
val actual = arrayOf(
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toByteArray(),
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}