Compare commits
29 Commits
9b7335ead2
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
3127b6c066
|
|||
|
e8baac9ba3
|
|||
|
d3a67c622c
|
|||
|
c80040466b
|
|||
|
20c2ba4c24
|
|||
|
47666a1e1a
|
|||
|
0129039df9
|
|||
|
3801c5759f
|
|||
|
03ec68cc40
|
|||
|
421beeed1e
|
|||
|
19b2fb36fd
|
|||
|
f18adc08b9
|
|||
|
85f10be1d2
|
|||
|
345be1cc6f
|
|||
|
d7d4e0c635
|
|||
|
3e769fe4d0
|
|||
|
17ff49ce0c
|
|||
|
d7e65ccc32
|
|||
|
dddf2bab93
|
|||
|
d55ec62be9
|
|||
|
9c7a9e4926
|
|||
|
ff0e17b5ad
|
|||
|
d06e523914
|
|||
|
afe35355a8
|
|||
|
947ef907a5
|
|||
|
23b565ca0a
|
|||
|
cc9961084c
|
|||
|
9391009403
|
|||
|
1195ad5acc
|
@@ -1,36 +0,0 @@
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
env:
|
||||
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: linux
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/cache/restore@v4
|
||||
with:
|
||||
key: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
~/.konan/
|
||||
|
||||
- uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '24'
|
||||
distribution: 'temurin'
|
||||
|
||||
- run: ./gradlew publish
|
||||
|
||||
- uses: actions/cache/save@v4
|
||||
with:
|
||||
key: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
~/.konan/
|
||||
47
.gitea/workflows/publish.yaml
Normal file
47
.gitea/workflows/publish.yaml
Normal file
@@ -0,0 +1,47 @@
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: linux
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '24'
|
||||
distribution: 'temurin'
|
||||
|
||||
- name: Cache Gradle
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
|
||||
- name: Cache Konan
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: konan-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: konan
|
||||
path: |
|
||||
~/.konan/
|
||||
|
||||
- name: Cache Node
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: node-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: node
|
||||
path: |
|
||||
~/.gradle/nodejs
|
||||
~/.gradle/yarn
|
||||
|
||||
- name: Publish
|
||||
run: ./gradlew publish
|
||||
env:
|
||||
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
|
||||
28
.gitea/workflows/test.yaml
Normal file
28
.gitea/workflows/test.yaml
Normal file
@@ -0,0 +1,28 @@
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: linux
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '24'
|
||||
distribution: 'temurin'
|
||||
|
||||
- name: Cache Gradle
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
|
||||
- name: Test
|
||||
run: ./gradlew jvmTest
|
||||
17
README.md
17
README.md
@@ -1,15 +1,6 @@
|
||||
# bytearray-kt
|
||||
# bytes.kt
|
||||
|
||||
`bytearray-kt` is a Kotlin Multiplatform library that provides helper functions for working with `ByteArray`.
|
||||
|
||||
## Targets
|
||||
|
||||
| Target | Status |
|
||||
|------------------|---------------|
|
||||
| JVM | Supported |
|
||||
| JavaScript | Supported |
|
||||
| Native (Linux) | Supported |
|
||||
| Native (Windows) | Not Supported |
|
||||
`bytes.kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -20,7 +11,7 @@ repositories {
|
||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("com.infendro:bytearray:1.2.4")
|
||||
commonMain.dependencies {
|
||||
implementation("com.infendro:bytes:1.4.2")
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
@file:OptIn(ExperimentalWasmDsl::class)
|
||||
|
||||
import com.infendro.plugin.infendro
|
||||
import com.infendro.plugin.token
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.2.4"
|
||||
version = "1.4.2"
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.infendro)
|
||||
@@ -12,10 +15,14 @@ plugins {
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
js {
|
||||
nodejs()
|
||||
}
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
js { nodejs() }
|
||||
wasmJs { nodejs() }
|
||||
wasmWasi { nodejs() }
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@@ -26,10 +33,6 @@ kotlin {
|
||||
implementation(libs.test)
|
||||
}
|
||||
}
|
||||
|
||||
compilerOptions {
|
||||
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
|
||||
}
|
||||
}
|
||||
|
||||
publishing.repositories {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[versions]
|
||||
infendro = "1.0.0"
|
||||
kotlin = "2.2.21"
|
||||
infendro = "1.1.0"
|
||||
kotlin = "2.3.0"
|
||||
|
||||
[plugins]
|
||||
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
rootProject.name = "bytearray"
|
||||
rootProject.name = "bytes"
|
||||
|
||||
pluginManagement.repositories {
|
||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
enum class ByteOrder {
|
||||
BIG_ENDIAN, LITTLE_ENDIAN;
|
||||
}
|
||||
|
||||
fun ByteArray.normalize(
|
||||
order: ByteOrder,
|
||||
): ByteArray = when (order) {
|
||||
BIG_ENDIAN -> this
|
||||
LITTLE_ENDIAN -> reversedArray()
|
||||
}
|
||||
|
||||
fun UByteArray.normalize(
|
||||
order: ByteOrder,
|
||||
): UByteArray = asByteArray().normalize(order).asUByteArray()
|
||||
@@ -1,181 +0,0 @@
|
||||
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)
|
||||
@@ -1,35 +0,0 @@
|
||||
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()
|
||||
@@ -1,50 +0,0 @@
|
||||
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()
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
fun String.encodeToUByteArray(): UByteArray =
|
||||
encodeToByteArray().asUByteArray()
|
||||
|
||||
fun UByteArray.decodeToString(): String =
|
||||
asByteArray().decodeToString()
|
||||
5
src/commonMain/kotlin/com/infendro/bytes/ByteOrder.kt
Normal file
5
src/commonMain/kotlin/com/infendro/bytes/ByteOrder.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.infendro.bytes
|
||||
|
||||
enum class ByteOrder {
|
||||
BIG_ENDIAN, LITTLE_ENDIAN;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.infendro.bytes.byte
|
||||
|
||||
import kotlin.experimental.inv
|
||||
|
||||
operator fun Byte.not(): Byte {
|
||||
return inv()
|
||||
}
|
||||
145
src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt
Normal file
145
src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt
Normal file
@@ -0,0 +1,145 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int {
|
||||
require(startIndex in 0..(size - 4))
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
((this[startIndex].toInt() and 0xFF) shl 24) or
|
||||
((this[startIndex + 1].toInt() and 0xFF) shl 16) or
|
||||
((this[startIndex + 2].toInt() and 0xFF) shl 8) or
|
||||
(this[startIndex + 3].toInt() and 0xFF)
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
(this[startIndex].toInt() and 0xFF) or
|
||||
((this[startIndex + 1].toInt() and 0xFF) shl 8) or
|
||||
((this[startIndex + 2].toInt() and 0xFF) shl 16) or
|
||||
((this[startIndex + 3].toInt() and 0xFF) shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
|
||||
require(startIndex in 0..(size - 8))
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
((this[startIndex].toLong() and 0xFFL) shl 56) or
|
||||
((this[startIndex + 1].toLong() and 0xFFL) shl 48) or
|
||||
((this[startIndex + 2].toLong() and 0xFFL) shl 40) or
|
||||
((this[startIndex + 3].toLong() and 0xFFL) shl 32) or
|
||||
((this[startIndex + 4].toLong() and 0xFFL) shl 24) or
|
||||
((this[startIndex + 5].toLong() and 0xFFL) shl 16) or
|
||||
((this[startIndex + 6].toLong() and 0xFFL) shl 8) or
|
||||
(this[startIndex + 7].toLong() and 0xFFL)
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
(this[startIndex].toLong() and 0xFFL) or
|
||||
((this[startIndex + 1].toLong() and 0xFFL) shl 8) or
|
||||
((this[startIndex + 2].toLong() and 0xFFL) shl 16) or
|
||||
((this[startIndex + 3].toLong() and 0xFFL) shl 24) or
|
||||
((this[startIndex + 4].toLong() and 0xFFL) shl 32) or
|
||||
((this[startIndex + 5].toLong() and 0xFFL) shl 40) or
|
||||
((this[startIndex + 6].toLong() and 0xFFL) shl 48) or
|
||||
((this[startIndex + 7].toLong() and 0xFFL) shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0 && length % 4 == 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length / 4)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
((this[offset].toInt() and 0xFF) shl 24) or
|
||||
((this[offset + 1].toInt() and 0xFF) shl 16) or
|
||||
((this[offset + 2].toInt() and 0xFF) shl 8) or
|
||||
(this[offset + 3].toInt() and 0xFF)
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toInt() and 0xFF) or
|
||||
((this[offset + 1].toInt() and 0xFF) shl 8) or
|
||||
((this[offset + 2].toInt() and 0xFF) shl 16) or
|
||||
((this[offset + 3].toInt() and 0xFF) shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray {
|
||||
val length = endIndex - startIndex
|
||||
val result = IntArray(length / 4)
|
||||
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0 && length % 8 == 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length / 8)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
((this[offset].toLong() and 0xFFL) shl 56) or
|
||||
((this[offset + 1].toLong() and 0xFFL) shl 48) or
|
||||
((this[offset + 2].toLong() and 0xFFL) shl 40) or
|
||||
((this[offset + 3].toLong() and 0xFFL) shl 32) or
|
||||
((this[offset + 4].toLong() and 0xFFL) shl 24) or
|
||||
((this[offset + 5].toLong() and 0xFFL) shl 16) or
|
||||
((this[offset + 6].toLong() and 0xFFL) shl 8) or
|
||||
(this[offset + 7].toLong() and 0xFFL)
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toLong() and 0xFFL) or
|
||||
((this[offset + 1].toLong() and 0xFFL) shl 8) or
|
||||
((this[offset + 2].toLong() and 0xFFL) shl 16) or
|
||||
((this[offset + 3].toLong() and 0xFFL) shl 24) or
|
||||
((this[offset + 4].toLong() and 0xFFL) shl 32) or
|
||||
((this[offset + 5].toLong() and 0xFFL) shl 40) or
|
||||
((this[offset + 6].toLong() and 0xFFL) shl 48) or
|
||||
((this[offset + 7].toLong() and 0xFFL) shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toLongArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): LongArray {
|
||||
val length = endIndex - startIndex
|
||||
val result = LongArray(length / 8)
|
||||
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||
return result
|
||||
}
|
||||
188
src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt
Normal file
188
src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt
Normal file
@@ -0,0 +1,188 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.byte.not
|
||||
import kotlin.experimental.and
|
||||
import kotlin.experimental.or
|
||||
import kotlin.experimental.xor
|
||||
|
||||
fun ByteArray.andInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(thatStartIndex in 0..(that.size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.andInto(
|
||||
that: Byte,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] and that
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||
andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
andInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.and(that: ByteArray): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
andInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
infix fun ByteArray.and(that: Byte): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
andInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.orInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(thatStartIndex in 0..(that.size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.orInto(
|
||||
that: Byte,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] or that
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.orWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||
orInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
fun ByteArray.orWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
orInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.or(that: ByteArray): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
orInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
infix fun ByteArray.or(that: Byte): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
orInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(thatStartIndex in 0..(that.size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
that: Byte,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] xor that
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.xorWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||
xorInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
fun ByteArray.xorWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
xorInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.xor(that: ByteArray): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
xorInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
infix fun ByteArray.xor(that: Byte): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
xorInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = !this[startIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.inv(startIndex: Int, endIndex: Int) =
|
||||
invInto(this, startIndex, startIndex, endIndex)
|
||||
|
||||
operator fun ByteArray.not(): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
invInto(result)
|
||||
return result
|
||||
}
|
||||
30
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
30
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||
require(destinationOffset in 0..(destination.size - 4))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 24).toByte()
|
||||
destination[destinationOffset + 1] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 3] = this.toByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val result = ByteArray(4)
|
||||
copyInto(result, order = order)
|
||||
return result
|
||||
}
|
||||
5
src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
5
src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
operator fun Int.not(): Int {
|
||||
return inv()
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.infendro.bytes.intarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun IntArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length * 4)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 4)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = (value shr 24).toByte()
|
||||
destination[offset + 1] = (value shr 16).toByte()
|
||||
destination[offset + 2] = (value shr 8).toByte()
|
||||
destination[offset + 3] = value.toByte()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 4)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = value.toByte()
|
||||
destination[offset + 1] = (value shr 8).toByte()
|
||||
destination[offset + 2] = (value shr 16).toByte()
|
||||
destination[offset + 3] = (value shr 24).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun IntArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val length = endIndex - startIndex
|
||||
val result = ByteArray(length * 4)
|
||||
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||
return result
|
||||
}
|
||||
38
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
38
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||
require(destinationOffset in 0..(destination.size - 8))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 56).toByte()
|
||||
destination[destinationOffset + 1] = (this shr 48).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 40).toByte()
|
||||
destination[destinationOffset + 3] = (this shr 32).toByte()
|
||||
destination[destinationOffset + 4] = (this shr 24).toByte()
|
||||
destination[destinationOffset + 5] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 6] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 7] = this.toByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toByte()
|
||||
destination[destinationOffset + 4] = (this shr 32).toByte()
|
||||
destination[destinationOffset + 5] = (this shr 40).toByte()
|
||||
destination[destinationOffset + 6] = (this shr 48).toByte()
|
||||
destination[destinationOffset + 7] = (this shr 56).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val result = ByteArray(8)
|
||||
copyInto(result, order = order)
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
operator fun Long.not(): Long {
|
||||
return inv()
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.infendro.bytes.longarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun LongArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length * 8)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 8)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = (value shr 56).toByte()
|
||||
destination[offset + 1] = (value shr 48).toByte()
|
||||
destination[offset + 2] = (value shr 40).toByte()
|
||||
destination[offset + 3] = (value shr 32).toByte()
|
||||
destination[offset + 4] = (value shr 24).toByte()
|
||||
destination[offset + 5] = (value shr 16).toByte()
|
||||
destination[offset + 6] = (value shr 8).toByte()
|
||||
destination[offset + 7] = value.toByte()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 8)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = value.toByte()
|
||||
destination[offset + 1] = (value shr 8).toByte()
|
||||
destination[offset + 2] = (value shr 16).toByte()
|
||||
destination[offset + 3] = (value shr 24).toByte()
|
||||
destination[offset + 4] = (value shr 32).toByte()
|
||||
destination[offset + 5] = (value shr 40).toByte()
|
||||
destination[offset + 6] = (value shr 48).toByte()
|
||||
destination[offset + 7] = (value shr 56).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun LongArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val length = endIndex - startIndex
|
||||
val result = ByteArray(length * 8)
|
||||
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||
return result
|
||||
}
|
||||
@@ -1,568 +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
|
||||
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
13
src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.infendro.bytes.byte
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ByteOperatorTest {
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = (-0x01).toByte()
|
||||
val actual = !0x00.toByte()
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ByteArrayConversionTest {
|
||||
@Test
|
||||
fun toInt() {
|
||||
val expected = 0x01234567
|
||||
run {
|
||||
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67).toInt()
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = byteArrayOf(0x67, 0x45, 0x23, 0x01).toInt(order = LITTLE_ENDIAN)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLong() {
|
||||
val expected = 0x0123456789abcdefL
|
||||
run {
|
||||
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11).toLong()
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = byteArrayOf(-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01).toLong(order = LITTLE_ENDIAN)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyIntoIntArray() {
|
||||
val expected = intArrayOf(0, 0x01234567, 0x01234567)
|
||||
val actual = IntArray(3).also { array ->
|
||||
val bytes = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67,
|
||||
0x01, 0x23, 0x45, 0x67,
|
||||
)
|
||||
bytes.copyInto(array, 1)
|
||||
}
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toIntArray() {
|
||||
val expected = intArrayOf(0x01234567, 0x67452301)
|
||||
run {
|
||||
val actual = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67,
|
||||
0x67, 0x45, 0x23, 0x01,
|
||||
).toIntArray()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = byteArrayOf(
|
||||
0x67, 0x45, 0x23, 0x01,
|
||||
0x01, 0x23, 0x45, 0x67,
|
||||
).toIntArray(order = LITTLE_ENDIAN)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyIntoLongArray() {
|
||||
val expected = longArrayOf(0, 0x0123456789abcdefL, 0x0123456789abcdefL)
|
||||
val actual = LongArray(3).also { array ->
|
||||
val bytes = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||
)
|
||||
bytes.copyInto(array, 1)
|
||||
}
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLongArray() {
|
||||
val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL)
|
||||
run {
|
||||
val actual = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||
).toLongArray()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = byteArrayOf(
|
||||
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||
).toLongArray(order = LITTLE_ENDIAN)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class ByteArrayOperatorTest {
|
||||
@Test
|
||||
fun and() {
|
||||
val expected = byteArrayOf(0x18, 0x00)
|
||||
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun andByte() {
|
||||
val expected = byteArrayOf(0x0C, 0x00)
|
||||
val actual = byteArrayOf(0x5C, 0x70) and 0x0F
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun or() {
|
||||
val expected = byteArrayOf(0x7E, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun orByte() {
|
||||
val expected = byteArrayOf(0x5F, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) or 0x0F
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xor() {
|
||||
val expected = byteArrayOf(0x66, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xorByte() {
|
||||
val expected = byteArrayOf(0x53, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) xor 0x0F
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = byteArrayOf(-0x01, -0x20)
|
||||
val actual = !byteArrayOf(0x00, 0x1F)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
20
src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
20
src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class IntConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67)
|
||||
run {
|
||||
val actual = 0x01234567.toByteArray()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = 0x67452301.toByteArray(LITTLE_ENDIAN)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class IntOperatorTest {
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = -0x00000001
|
||||
val actual = !0x00000000
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.infendro.bytes.intarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class IntArrayConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67,
|
||||
0x67, 0x45, 0x23, 0x01,
|
||||
)
|
||||
run {
|
||||
val actual = intArrayOf(0x01234567, 0x67452301).toByteArray()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = intArrayOf(0x67452301, 0x01234567).toByteArray(order = LITTLE_ENDIAN)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
20
src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class LongConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11)
|
||||
run {
|
||||
val actual = 0x0123456789abcdefL.toByteArray()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = (-0x1032547698badcffL).toByteArray(LITTLE_ENDIAN)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class LongOperatorTest {
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = -0x0000000000000001L
|
||||
val actual = !0x0000000000000000L
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.infendro.bytes.longarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class LongArrayConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||
)
|
||||
run {
|
||||
val actual = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL).toByteArray()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
run {
|
||||
val actual = longArrayOf(-0x1032547698badcffL, 0x0123456789abcdefL).toByteArray(order = LITTLE_ENDIAN)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user