refactor lib structure

This commit is contained in:
2025-12-13 14:49:00 +01:00
parent 3ab2bea4f6
commit f5d559e8fe
28 changed files with 1017 additions and 1301 deletions

View File

@@ -1,159 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
infix fun ByteArray.and(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun ByteArray.or(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun ByteArray.xor(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] xor that[i]
}
}
fun ByteArray.padStart(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
repeat(length - size) {
bytes += padByte
}
bytes += this.toList()
return bytes.toByteArray()
}
fun ByteArray.padEnd(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
bytes += this.toList()
repeat(length - size) {
bytes += padByte
}
return bytes.toByteArray()
}
fun ByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0) { acc, byte ->
(acc shl 8) or (byte.toInt() and 0xFF)
}
}
fun ByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return toList()
.chunked(4) { it.toByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
fun ByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0L) { acc, byte ->
(acc shl 8) or (byte.toLong() and 0xFFL)
}
}
fun ByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return toList()
.chunked(8) { it.toByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
}
}
fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return toList()
.chunked(4) { it.toByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0UL) { acc, byte ->
(acc shl 8) or (byte.toULong() and 0xFFUL)
}
}
fun ByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return toList()
.chunked(8) { it.toByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -16,7 +16,4 @@ fun ByteArray.normalize(
fun UByteArray.normalize( fun UByteArray.normalize(
order: ByteOrder, order: ByteOrder,
): UByteArray = when (order) { ): UByteArray = asByteArray().normalize(order).asUByteArray()
BIG_ENDIAN -> this
LITTLE_ENDIAN -> reversedArray()
}

View File

@@ -0,0 +1,181 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun ByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0) { acc, byte -> (acc shl 8) or (byte.toInt() and 0xFF) }
}
fun UByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int = asByteArray().toInt(order)
fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt = toInt(order).toUInt()
fun UByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt = asByteArray().toUInt(order)
fun ByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return asList()
.chunked(4) { it.toByteArray().toInt(order) }
.toIntArray()
}
fun UByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray = asByteArray().toIntArray(order)
fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray = toIntArray(order).asUIntArray()
fun UByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray = asByteArray().toUIntArray(order)
fun Int.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = toInt().toByteArray(order)
fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toInt().toUByteArray(order)
fun IntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun UIntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = asIntArray().toByteArray(order)
fun IntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun UIntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = asIntArray().toUByteArray(order)
fun ByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0L) { acc, byte -> (acc shl 8) or (byte.toLong() and 0xFFL) }
}
fun UByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long = asByteArray().toLong(order)
fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong = toLong(order).toULong()
fun UByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong = asByteArray().toULong(order)
fun ByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return asList()
.chunked(8) { it.toByteArray().toLong(order) }
.toLongArray()
}
fun UByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray = asByteArray().toLongArray(order)
fun ByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray = toLongArray(order).asULongArray()
fun UByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray = asByteArray().toULongArray(order)
fun Long.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun ULong.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = toLong().toByteArray(order)
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toLong().toUByteArray(order)
fun LongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).asList()
}
return bytes.toByteArray()
}
fun ULongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = asLongArray().toByteArray(order)
fun LongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun ULongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = asLongArray().toUByteArray(order)

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun Int.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun IntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun IntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun Long.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun LongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun LongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -0,0 +1,35 @@
package com.infendro.bytearray
fun ByteArray.padStart(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
repeat(length - size) {
bytes += padByte
}
bytes += this.asList()
return bytes.toByteArray()
}
fun UByteArray.padStart(
length: Int,
padByte: UByte,
): UByteArray = asByteArray().padStart(length, padByte.toByte()).asUByteArray()
fun ByteArray.padEnd(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
bytes += this.asList()
repeat(length - size) {
bytes += padByte
}
return bytes.toByteArray()
}
fun UByteArray.padEnd(
length: Int,
padByte: UByte,
): UByteArray = asByteArray().padEnd(length, padByte.toByte()).asUByteArray()

View File

@@ -0,0 +1,50 @@
package com.infendro.bytearray
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
infix fun ByteArray.and(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun UByteArray.and(
that: UByteArray,
): UByteArray = (this.asByteArray() and that.asByteArray()).asUByteArray()
infix fun ByteArray.or(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun UByteArray.or(
that: UByteArray,
): UByteArray = (this.asByteArray() or that.asByteArray()).asUByteArray()
infix fun ByteArray.xor(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] xor that[i]
}
}
infix fun UByteArray.xor(
that: UByteArray,
): UByteArray = (this.asByteArray() xor that.asByteArray()).asUByteArray()

View File

@@ -1,7 +1,7 @@
package com.infendro.bytearray package com.infendro.bytearray
fun String.encodeToUByteArray(): UByteArray = fun String.encodeToUByteArray(): UByteArray =
encodeToByteArray().toUByteArray() encodeToByteArray().asUByteArray()
fun UByteArray.decodeToString(): String = fun UByteArray.decodeToString(): String =
toByteArray().decodeToString() asByteArray().decodeToString()

View File

@@ -1,152 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
infix fun UByteArray.and(
that: UByteArray,
): UByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return UByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun UByteArray.or(
that: UByteArray,
): UByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return UByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun UByteArray.xor(
that: UByteArray,
): UByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return UByteArray(size) { i ->
this[i] xor that[i]
}
}
fun UByteArray.padStart(
length: Int,
padByte: UByte,
): UByteArray {
val bytes = mutableListOf<UByte>()
repeat(length - size) {
bytes += padByte
}
bytes += this
return bytes.toUByteArray()
}
fun UByteArray.padEnd(
length: Int,
padByte: UByte,
): UByteArray {
val bytes = mutableListOf<UByte>()
bytes += this
repeat(length - size) {
bytes += padByte
}
return bytes.toUByteArray()
}
fun UByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0) { acc, byte ->
(acc shl 8) or (byte.toInt() and 0xFF)
}
}
fun UByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return chunked(4) { it.toUByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
fun UByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0L) { acc, byte ->
(acc shl 8) or (byte.toLong() and 0xFFL)
}
}
fun UByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return chunked(8) { it.toUByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
fun UByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
}
}
fun UByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return chunked(4) { it.toUByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
fun UByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0UL) { acc, byte ->
(acc shl 8) or (byte.toULong() and 0xFFUL)
}
}
fun UByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return chunked(8) { it.toUByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toByte()
}
return bytes.normalize(order)
}
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun UIntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun UIntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun ULong.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toByte()
}
return bytes.normalize(order)
}
fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun ULongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun ULongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -1,204 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFails
class `ByteArray Test` {
@Test
fun `and() - size`() {
assertFails {
byteArrayOf() and byteArrayOf(0x00)
}
assertFails {
byteArrayOf(0x00) and byteArrayOf()
}
}
@Test
fun `and()`() {
val expected = byteArrayOf(0x18, 0x00)
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun `or() - size`() {
assertFails {
byteArrayOf() or byteArrayOf(0x00)
}
assertFails {
byteArrayOf(0x00) or byteArrayOf()
}
}
@Test
fun `or()`() {
val expected = byteArrayOf(0x7E, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun `xor() - size`() {
assertFails {
byteArrayOf() xor byteArrayOf(0x00)
}
assertFails {
byteArrayOf(0x00) xor byteArrayOf()
}
}
@Test
fun `xor()`() {
val expected = byteArrayOf(0x66, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun `padStart()`() {
val expected = byteArrayOf(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02)
val actual = byteArrayOf(0x02, 0x02).padStart(8, 0x01)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `padStart() - unchanged`() {
val expected = byteArrayOf(0x02, 0x02)
val actual = byteArrayOf(0x02, 0x02).padStart(2, 0x01)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `padEnd()`() {
val expected = byteArrayOf(0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01)
val actual = byteArrayOf(0x02, 0x02).padEnd(8, 0x01)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `padEnd() - unchanged`() {
val expected = byteArrayOf(0x02, 0x02)
val actual = byteArrayOf(0x02, 0x02).padEnd(2, 0x01)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `toInt()`() {
val expected = 0x12345678
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt(),
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toIntArray()`() {
val expected = intArrayOf(0x12345678, 0x12345678)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78,
).toIntArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12,
).toIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `toLong()`() {
val expected = 0x1234567812345678L
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toLong(),
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toLongArray()`() {
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
).toLongArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
).toLongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `toUInt()`() {
val expected = 0x12345678U
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt(),
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toUIntArray()`() {
val expected = uintArrayOf(0x12345678U, 0x12345678U)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78,
).toUIntArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12,
).toUIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `toULong()`() {
val expected = 0x1234567812345678UL
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toULong(),
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toULongArray()`() {
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
).toULongArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
).toULongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
}

View File

@@ -0,0 +1,568 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `Conversion Test` {
@Test
fun `ByteArray - toInt()`() {
val expected = 0x12345678
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt(),
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `UByteArray - toInt()`() {
val expected = 0x12345678
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `ByteArray - toUInt()`() {
val expected = 0x12345678U
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt(),
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `UByteArray - toUInt()`() {
val expected = 0x12345678U
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `ByteArray - toIntArray()`() {
val expected = intArrayOf(0x12345678, 0x12345678)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78,
).toIntArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12,
).toIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `UByteArray - toIntArray()`() {
val expected = intArrayOf(0x12345678, 0x12345678)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U,
).toIntArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U,
).toIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `ByteArray - toUIntArray()`() {
val expected = uintArrayOf(0x12345678U, 0x12345678U)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78,
).toUIntArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12,
).toUIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `UByteArray - toUIntArray()`() {
val expected = uintArrayOf(0x12345678U, 0x12345678U)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U,
).toUIntArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U,
).toUIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `Int - toByteArray()`() {
val expected = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
byteArrayOf(0x78, 0x56, 0x34, 0x12),
)
val actual = arrayOf(
0x12345678.toByteArray(),
0x12345678.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `UInt - toByteArray()`() {
val expected = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
byteArrayOf(0x78, 0x56, 0x34, 0x12),
)
val actual = arrayOf(
0x12345678U.toByteArray(),
0x12345678U.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `Int - toUByteArray()`() {
val expected = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
)
val actual = arrayOf(
0x12345678.toUByteArray(),
0x12345678.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `UInt - toUByteArray()`() {
val expected = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
)
val actual = arrayOf(
0x12345678U.toUByteArray(),
0x12345678U.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `IntArray - 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(
intArrayOf(
0x12345678,
0x12345678,
).toByteArray(),
intArrayOf(
0x12345678,
0x12345678,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `UIntArray - 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(
uintArrayOf(
0x12345678U,
0x12345678U,
).toByteArray(),
uintArrayOf(
0x12345678U,
0x12345678U,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `IntArray - 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(
intArrayOf(
0x12345678,
0x12345678,
).toUByteArray(),
intArrayOf(
0x12345678,
0x12345678,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `UIntArray - 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(
uintArrayOf(
0x12345678U,
0x12345678U,
).toUByteArray(),
uintArrayOf(
0x12345678U,
0x12345678U,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `ByteArray - toLong()`() {
val expected = 0x1234567812345678L
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toLong(),
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `UByteArray - toULong()`() {
val expected = 0x1234567812345678UL
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `UByteArray - toLong()`() {
val expected = 0x1234567812345678L
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `ByteArray - toULong()`() {
val expected = 0x1234567812345678UL
val actual = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toULong(),
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `ByteArray - toLongArray()`() {
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
).toLongArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
).toLongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `UByteArray - toLongArray()`() {
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
).toLongArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
).toLongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `ByteArray - toULongArray()`() {
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
val actual = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
).toULongArray(),
byteArrayOf(
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
).toULongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `UByteArray - toULongArray()`() {
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
).toULongArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
).toULongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `Long - 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(
0x1234567812345678L.toByteArray(),
0x1234567812345678L.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `ULong - 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 `Long - 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(
0x1234567812345678L.toUByteArray(),
0x1234567812345678L.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `ULong - 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 `LongArray - 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(
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toByteArray(),
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `ULongArray - 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])
}
}
@Test
fun `LongArray - 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(
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toUByteArray(),
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `ULongArray - 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])
}
}
}

View File

@@ -1,37 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Int Test` {
@Test
fun `toByteArray()`() {
val expected = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
byteArrayOf(0x78, 0x56, 0x34, 0x12),
)
val actual = arrayOf(
0x12345678.toByteArray(),
0x12345678.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `toUByteArray()`() {
val expected = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
)
val actual = arrayOf(
0x12345678.toUByteArray(),
0x12345678.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}

View File

@@ -1,61 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `IntArray Test` {
@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(
intArrayOf(
0x12345678,
0x12345678,
).toByteArray(),
intArrayOf(
0x12345678,
0x12345678,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@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(
intArrayOf(
0x12345678,
0x12345678,
).toUByteArray(),
intArrayOf(
0x12345678,
0x12345678,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}

View File

@@ -1,37 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Long Test` {
@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(
0x1234567812345678L.toByteArray(),
0x1234567812345678L.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@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(
0x1234567812345678L.toUByteArray(),
0x1234567812345678L.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}

View File

@@ -1,61 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `LongArray Test` {
@Test
fun `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(
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toByteArray(),
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `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(
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toUByteArray(),
longArrayOf(
0x1234567812345678L,
0x1234567812345678L,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}

View File

@@ -0,0 +1,71 @@
package com.infendro.bytearray
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `Modification Test` {
@Test
fun `ByteArray - padStart()`() {
val expected = byteArrayOf(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02)
val actual = byteArrayOf(0x02, 0x02).padStart(8, 0x01)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `ByteArray - padStart() - unchanged`() {
val expected = byteArrayOf(0x02, 0x02)
val actual = byteArrayOf(0x02, 0x02).padStart(2, 0x01)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `UByteArray - padStart()`() {
val expected = ubyteArrayOf(0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x02U, 0x02U)
val actual = ubyteArrayOf(0x02U, 0x02U).padStart(8, 0x01U)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `UByteArray - padStart() - unchanged`() {
val expected = ubyteArrayOf(0x02U, 0x02U)
val actual = ubyteArrayOf(0x02U, 0x02U).padStart(2, 0x01U)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `ByteArray - padEnd()`() {
val expected = byteArrayOf(0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01)
val actual = byteArrayOf(0x02, 0x02).padEnd(8, 0x01)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `ByteArray - padEnd() - unchanged`() {
val expected = byteArrayOf(0x02, 0x02)
val actual = byteArrayOf(0x02, 0x02).padEnd(2, 0x01)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `UByteArray - padEnd()`() {
val expected = ubyteArrayOf(0x02U, 0x02U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U)
val actual = ubyteArrayOf(0x02U, 0x02U).padEnd(8, 0x01U)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `UByteArray - padEnd() - unchanged`() {
val expected = ubyteArrayOf(0x02U, 0x02U)
val actual = ubyteArrayOf(0x02U, 0x02U).padEnd(2, 0x01U)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,109 @@
package com.infendro.bytearray
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertFails
class `Operators Test` {
@Test
fun `ByteArray - and() - size`() {
assertFails {
byteArrayOf() and byteArrayOf(0x00)
}
assertFails {
byteArrayOf(0x00) and byteArrayOf()
}
}
@Test
fun `ByteArray - and()`() {
val expected = byteArrayOf(0x18, 0x00)
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun `UByteArray - and() - size`() {
assertFails {
ubyteArrayOf() and ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) and ubyteArrayOf()
}
}
@Test
fun `UByteArray - and()`() {
val expected = ubyteArrayOf(0x18U, 0x00U)
val actual = ubyteArrayOf(0x5CU, 0x70U) and ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
@Test
fun `ByteArray - or() - size`() {
assertFails {
byteArrayOf() or byteArrayOf(0x00)
}
assertFails {
byteArrayOf(0x00) or byteArrayOf()
}
}
@Test
fun `ByteArray - or()`() {
val expected = byteArrayOf(0x7E, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun `UByteArray - or() - size`() {
assertFails {
ubyteArrayOf() or ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) or ubyteArrayOf()
}
}
@Test
fun `UByteArray - or()`() {
val expected = ubyteArrayOf(0x7EU, 0x7FU)
val actual = ubyteArrayOf(0x5CU, 0x70U) or ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
@Test
fun `ByteArray - xor() - size`() {
assertFails {
byteArrayOf() xor byteArrayOf(0x00)
}
assertFails {
byteArrayOf(0x00) xor byteArrayOf()
}
}
@Test
fun `ByteArray - xor()`() {
val expected = byteArrayOf(0x66, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun `UByteArray - xor() - size`() {
assertFails {
ubyteArrayOf() xor ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) xor ubyteArrayOf()
}
}
@Test
fun `UByteArray - xor()`() {
val expected = ubyteArrayOf(0x66U, 0x7FU)
val actual = ubyteArrayOf(0x5CU, 0x70U) xor ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
}

View File

@@ -1,204 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFails
class `UByteArray Test` {
@Test
fun `and() - size`() {
assertFails {
ubyteArrayOf() and ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) and ubyteArrayOf()
}
}
@Test
fun `and()`() {
val expected = ubyteArrayOf(0x18U, 0x00U)
val actual = ubyteArrayOf(0x5CU, 0x70U) and ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
@Test
fun `or() - size`() {
assertFails {
ubyteArrayOf() or ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) or ubyteArrayOf()
}
}
@Test
fun `or()`() {
val expected = ubyteArrayOf(0x7EU, 0x7FU)
val actual = ubyteArrayOf(0x5CU, 0x70U) or ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
@Test
fun `xor() - size`() {
assertFails {
ubyteArrayOf() xor ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) xor ubyteArrayOf()
}
}
@Test
fun `xor()`() {
val expected = ubyteArrayOf(0x66U, 0x7FU)
val actual = ubyteArrayOf(0x5CU, 0x70U) xor ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
@Test
fun `padStart()`() {
val expected = ubyteArrayOf(0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x02U, 0x02U)
val actual = ubyteArrayOf(0x02U, 0x02U).padStart(8, 0x01U)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `padStart() - unchanged`() {
val expected = ubyteArrayOf(0x02U, 0x02U)
val actual = ubyteArrayOf(0x02U, 0x02U).padStart(2, 0x01U)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `padEnd()`() {
val expected = ubyteArrayOf(0x02U, 0x02U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U)
val actual = ubyteArrayOf(0x02U, 0x02U).padEnd(8, 0x01U)
assertEquals(8, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `padEnd() - unchanged`() {
val expected = ubyteArrayOf(0x02U, 0x02U)
val actual = ubyteArrayOf(0x02U, 0x02U).padEnd(2, 0x01U)
assertEquals(2, actual.size)
assertContentEquals(expected, actual)
}
@Test
fun `toInt()`() {
val expected = 0x12345678
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toIntArray()`() {
val expected = intArrayOf(0x12345678, 0x12345678)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U,
).toIntArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U,
).toIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `toLong()`() {
val expected = 0x1234567812345678L
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toLongArray()`() {
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
).toLongArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
).toLongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `toUInt()`() {
val expected = 0x12345678U
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toUIntArray()`() {
val expected = uintArrayOf(0x12345678U, 0x12345678U)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U,
).toUIntArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U,
).toUIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun `toULong()`() {
val expected = 0x1234567812345678UL
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun `toULongArray()`() {
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
).toULongArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
).toULongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
}

View File

@@ -1,37 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `UInt Test` {
@Test
fun `toByteArray()`() {
val expected = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
byteArrayOf(0x78, 0x56, 0x34, 0x12),
)
val actual = arrayOf(
0x12345678U.toByteArray(),
0x12345678U.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `toUByteArray()`() {
val expected = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
)
val actual = arrayOf(
0x12345678U.toUByteArray(),
0x12345678U.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}

View File

@@ -1,61 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `UIntArray Test` {
@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(
uintArrayOf(
0x12345678U,
0x12345678U,
).toByteArray(),
uintArrayOf(
0x12345678U,
0x12345678U,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@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(
uintArrayOf(
0x12345678U,
0x12345678U,
).toUByteArray(),
uintArrayOf(
0x12345678U,
0x12345678U,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}

View File

@@ -1,37 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `ULong Test` {
@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 `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])
}
}
}

View File

@@ -1,61 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `ULongArray Test` {
@Test
fun `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])
}
}
@Test
fun `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])
}
}
}