Compare commits

..

9 Commits

Author SHA1 Message Date
9b7335ead2 update README to match wiki 2025-12-13 15:23:18 +01:00
f5d559e8fe refactor lib structure 2025-12-13 14:49:00 +01:00
3ab2bea4f6 improve test coverage 2025-12-13 11:35:11 +01:00
a9f77d6211 fix workflow 2025-12-13 01:30:31 +01:00
ecbeab8531 small refactor 2025-12-13 01:09:41 +01:00
b8639c7cf0 refactor build.gradle.kts 2025-12-13 00:59:15 +01:00
300ad7511a use common plugin 2025-12-13 00:54:00 +01:00
22cfdd90ab improve test coverage 2025-12-12 21:21:44 +01:00
01327ef4b2 refactor 2025-12-12 21:20:44 +01:00
26 changed files with 1082 additions and 837 deletions

View File

@@ -4,7 +4,7 @@ on:
- main - main
env: env:
GIT__TOKEN: ${{ secrets.TOKEN }} INFENDRO__TOKEN: ${{ secrets.TOKEN }}
jobs: jobs:
publish: publish:

View File

@@ -1,13 +1,15 @@
# Kotlin ByteArray Utilities # bytearray-kt
This library provides various utilities for working with ByteArray in Kotlin Multiplatform. `bytearray-kt` is a Kotlin Multiplatform library that provides helper functions for working with `ByteArray`.
## Features ## Targets
* Multiplatform support | Target | Status |
* JVM |------------------|---------------|
* JavaScript | JVM | Supported |
* Native (Linux) | JavaScript | Supported |
| Native (Linux) | Supported |
| Native (Windows) | Not Supported |
## Installation ## Installation
@@ -19,6 +21,6 @@ repositories {
} }
dependencies { dependencies {
implementation("com.infendro:bytearray:1.1.1") implementation("com.infendro:bytearray:1.2.4")
} }
``` ```

View File

@@ -1,11 +1,11 @@
import com.infendro.plugin.infendro
import com.infendro.plugin.token
group = "com.infendro" group = "com.infendro"
version = "1.2.4" version = "1.2.4"
repositories {
mavenCentral()
}
plugins { plugins {
alias(libs.plugins.infendro)
alias(libs.plugins.multiplatform) alias(libs.plugins.multiplatform)
id("maven-publish") id("maven-publish")
} }
@@ -17,6 +17,10 @@ kotlin {
} }
linuxX64() linuxX64()
repositories {
mavenCentral()
}
sourceSets { sourceSets {
commonTest.dependencies { commonTest.dependencies {
implementation(libs.test) implementation(libs.test)
@@ -28,31 +32,6 @@ kotlin {
} }
} }
publishing { publishing.repositories {
repositories { infendro(token)
maven {
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
authentication.create("header", HttpHeaderAuthentication::class)
credentials(HttpHeaderCredentials::class) {
val token = secret("git.token") ?: throw GradleException()
name = "Authorization"
value = "token $token"
}
}
}
}
fun secret(
key: String,
): String? {
val property = key
val environment = key
.uppercase()
.replace(".", "__")
val prop = properties[property] as String?
val env = System.getenv(environment)
return prop ?: env
} }

View File

@@ -1,7 +1,9 @@
[versions] [versions]
infendro = "1.0.0"
kotlin = "2.2.21" kotlin = "2.2.21"
[plugins] [plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
[libraries] [libraries]

View File

@@ -1 +1,6 @@
rootProject.name = "bytearray" rootProject.name = "bytearray"
pluginManagement.repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
mavenCentral()
}

View File

@@ -1,148 +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 Exception()
return ByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun ByteArray.or(
that: ByteArray,
): ByteArray {
if (this.size != that.size) throw Exception()
return ByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun ByteArray.xor(
that: ByteArray,
): ByteArray {
if (this.size != that.size) throw Exception()
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 Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return toList()
.chunked(4) { it.toByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
fun ByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return toList()
.chunked(8) { it.toByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4) throw Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return toList()
.chunked(4) { it.toByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return toList()
.chunked(8) { it.toByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -1,38 +1,19 @@
package com.infendro.bytearray package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
enum class ByteOrder { enum class ByteOrder {
BIG_ENDIAN { BIG_ENDIAN, LITTLE_ENDIAN;
override fun normalize(
bytes: ByteArray,
): ByteArray {
return bytes
}
override fun normalize(
bytes: UByteArray,
): UByteArray {
return bytes
}
},
LITTLE_ENDIAN {
override fun normalize(
bytes: ByteArray,
): ByteArray {
return bytes.reversedArray()
}
override fun normalize(
bytes: UByteArray,
): UByteArray {
return bytes.reversedArray()
}
};
internal abstract fun normalize(
bytes: ByteArray,
): ByteArray
internal abstract fun normalize(
bytes: UByteArray,
): UByteArray
} }
fun ByteArray.normalize(
order: ByteOrder,
): ByteArray = when (order) {
BIG_ENDIAN -> this
LITTLE_ENDIAN -> reversedArray()
}
fun UByteArray.normalize(
order: ByteOrder,
): UByteArray = asByteArray().normalize(order).asUByteArray()

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,43 +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 order.normalize(bytes)
}
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 Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toUByte()
}
return order.normalize(bytes)
}
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,43 +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 order.normalize(bytes)
}
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 Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toUByte()
}
return order.normalize(bytes)
}
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,4 +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 =
asByteArray().decodeToString()

View File

@@ -1,144 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun UByteArray.decodeToString(): String =
toByteArray().decodeToString()
infix fun UByteArray.and(
that: UByteArray,
): UByteArray {
if (this.size != that.size) throw Exception()
return UByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun UByteArray.or(
that: UByteArray,
): UByteArray {
if (this.size != that.size) throw Exception()
return UByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun UByteArray.xor(
that: UByteArray,
): UByteArray {
if (this.size != that.size) throw Exception()
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 Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return chunked(4) { it.toUByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
fun UByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return chunked(8) { it.toUByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
fun UByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4) throw Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return chunked(4) { it.toUByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
fun UByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.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 Exception()
return chunked(8) { it.toUByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -1,43 +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 order.normalize(bytes)
}
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 UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toUByte()
}
return order.normalize(bytes)
}
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,43 +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 order.normalize(bytes)
}
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 ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toUByte()
}
return order.normalize(bytes)
}
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,87 +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 `padStart()`() {
val padded = byteArrayOf(0x02, 0x02).padStart(8, 0x01)
assertEquals(8, padded.size)
assertContentEquals(
byteArrayOf(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02),
padded
)
}
@Test
fun `padEnd()`() {
val padded = byteArrayOf(0x02, 0x02).padEnd(8, 0x01)
assertEquals(8, padded.size)
assertContentEquals(
byteArrayOf(0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01),
padded
)
}
@Test
fun `toInt()`() {
assertEquals(
0x12345678,
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt()
)
assertEquals(
0x12345678,
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN)
)
}
@Test
fun `toLong()`() {
assertEquals(
0x1234567812345678L,
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toLong()
)
assertEquals(
0x1234567812345678L,
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN)
)
}
@Test
fun `toUInt()`() {
assertEquals(
0x12345678U,
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt()
)
assertEquals(
0x12345678U,
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN)
)
}
@Test
fun `toULong()`() {
assertEquals(
0x1234567812345678UL,
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toULong()
)
assertEquals(
0x1234567812345678UL,
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN)
)
}
}

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,31 +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()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
0x12345678.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12),
0x12345678.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
0x12345678.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
0x12345678.toUByteArray(LITTLE_ENDIAN)
)
}
}

View File

@@ -1,31 +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()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
0x1234567812345678L.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
0x1234567812345678L.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
0x1234567812345678L.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
0x1234567812345678L.toUByteArray(LITTLE_ENDIAN)
)
}
}

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

@@ -0,0 +1,21 @@
package com.infendro.bytearray
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `String Test` {
@Test
fun `encodeToUByteArray()`() {
val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U)
val actual = "PASSWORD".encodeToUByteArray()
assertContentEquals(expected, actual)
}
@Test
fun `decodeToString()`() {
val expected = "PASSWORD"
val actual = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U).decodeToString()
assertEquals(expected, actual)
}
}

View File

@@ -1,87 +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 `padStart()`() {
val padded = ubyteArrayOf(0x02U, 0x02U).padStart(8, 0x01U)
assertEquals(8, padded.size)
assertContentEquals(
ubyteArrayOf(0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x02U, 0x02U),
padded
)
}
@Test
fun `padEnd()`() {
val padded = ubyteArrayOf(0x02U, 0x02U).padEnd(8, 0x01U)
assertEquals(8, padded.size)
assertContentEquals(
ubyteArrayOf(0x02U, 0x02U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U),
padded
)
}
@Test
fun `toInt()`() {
assertEquals(
0x12345678,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt()
)
assertEquals(
0x12345678,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN)
)
}
@Test
fun `toLong()`() {
assertEquals(
0x1234567812345678L,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong()
)
assertEquals(
0x1234567812345678L,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN)
)
}
@Test
fun `toUInt()`() {
assertEquals(
0x12345678U,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt()
)
assertEquals(
0x12345678U,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN)
)
}
@Test
fun `toULong()`() {
assertEquals(
0x1234567812345678UL,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong()
)
assertEquals(
0x1234567812345678UL,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(LITTLE_ENDIAN)
)
}
}

View File

@@ -1,31 +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()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
0x12345678U.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12),
0x12345678U.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
0x12345678U.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
0x12345678U.toUByteArray(LITTLE_ENDIAN)
)
}
}

View File

@@ -1,31 +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()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
0x1234567812345678UL.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
0x1234567812345678UL.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
0x1234567812345678UL.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
0x1234567812345678UL.toUByteArray(LITTLE_ENDIAN)
)
}
}