Compare commits
4 Commits
9b7335ead2
...
23b565ca0a
| Author | SHA1 | Date | |
|---|---|---|---|
|
23b565ca0a
|
|||
|
cc9961084c
|
|||
|
9391009403
|
|||
|
1195ad5acc
|
92
.gitea/workflows/ci.yaml
Normal file
92
.gitea/workflows/ci.yaml
Normal file
@@ -0,0 +1,92 @@
|
||||
on:
|
||||
push:
|
||||
branches: [ develop, main ]
|
||||
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: 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: Test
|
||||
run: ./gradlew allTests
|
||||
|
||||
publish:
|
||||
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
|
||||
needs: 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: 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 }}
|
||||
@@ -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/
|
||||
@@ -1,6 +1,6 @@
|
||||
# bytearray-kt
|
||||
# bytes-kt
|
||||
|
||||
`bytearray-kt` is a Kotlin Multiplatform library that provides helper functions for working with `ByteArray`.
|
||||
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `UByteArray` and `ByteArray`.
|
||||
|
||||
## Targets
|
||||
|
||||
@@ -21,6 +21,6 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("com.infendro:bytearray:1.2.4")
|
||||
implementation("com.infendro:bytearray:1.3.0")
|
||||
}
|
||||
```
|
||||
|
||||
@@ -2,7 +2,7 @@ import com.infendro.plugin.infendro
|
||||
import com.infendro.plugin.token
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.2.4"
|
||||
version = "1.3.0"
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.infendro)
|
||||
@@ -12,10 +12,10 @@ plugins {
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
linuxX64()
|
||||
js {
|
||||
nodejs()
|
||||
}
|
||||
linuxX64()
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -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()
|
||||
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,32 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ubytearray.toInt
|
||||
import com.infendro.bytes.ubytearray.toLong
|
||||
import com.infendro.bytes.ubytearray.toUInt
|
||||
import com.infendro.bytes.ubytearray.toULong
|
||||
|
||||
fun ByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toUInt(startIndex, order)
|
||||
|
||||
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toInt(startIndex, order)
|
||||
|
||||
fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
IntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toULong(startIndex, order)
|
||||
|
||||
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toLong(startIndex, order)
|
||||
|
||||
fun ByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
LongArray(size / 8).also { copyInto(it, order = order) }
|
||||
37
src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt
Normal file
37
src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ubytearray.copyInto
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: UIntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: ULongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ubytearray.*
|
||||
|
||||
fun ByteArray.invInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) = asUByteArray().invInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex)
|
||||
|
||||
fun ByteArray.inv() =
|
||||
(asUByteArray().inv()).asByteArray()
|
||||
|
||||
fun ByteArray.andInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().andInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
|
||||
infix fun ByteArray.and(that: ByteArray) =
|
||||
(this.asUByteArray() and that.asUByteArray()).asByteArray()
|
||||
|
||||
fun ByteArray.orInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().orInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
|
||||
infix fun ByteArray.or(that: ByteArray) =
|
||||
(this.asUByteArray() or that.asUByteArray()).asByteArray()
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().xorInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
|
||||
infix fun ByteArray.xor(that: ByteArray) =
|
||||
(this.asUByteArray() xor that.asUByteArray()).asByteArray()
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
33
src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt
Normal file
33
src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.uint.copyInto
|
||||
|
||||
fun Int.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toUInt().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun Int.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toUInt().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun IntArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun IntArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
33
src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt
Normal file
33
src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ulong.copyInto
|
||||
|
||||
fun Long.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toULong().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun Long.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toULong().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun LongArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asULongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun LongArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asULongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun UByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt {
|
||||
require(startIndex in 0..(size - 4))
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
(this[startIndex].toUInt() shl 24) or (this[startIndex + 1].toUInt() shl 16) or
|
||||
(this[startIndex + 2].toUInt() shl 8) or this[startIndex + 3].toUInt()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
this[startIndex].toUInt() or (this[startIndex + 1].toUInt() shl 8) or
|
||||
(this[startIndex + 2].toUInt() shl 16) or (this[startIndex + 3].toUInt() shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
toUInt(startIndex, order).toInt()
|
||||
|
||||
fun UByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
IntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong {
|
||||
require(startIndex in 0..(size - 8))
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
(this[startIndex].toULong() shl 56) or (this[startIndex + 1].toULong() shl 48) or
|
||||
(this[startIndex + 2].toULong() shl 40) or (this[startIndex + 3].toULong() shl 32) or
|
||||
(this[startIndex + 4].toULong() shl 24) or (this[startIndex + 5].toULong() shl 16) or
|
||||
(this[startIndex + 6].toULong() shl 8) or this[startIndex + 7].toULong()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
this[startIndex].toULong() or (this[startIndex + 1].toULong() shl 8) or
|
||||
(this[startIndex + 2].toULong() shl 16) or (this[startIndex + 3].toULong() shl 24) or
|
||||
(this[startIndex + 4].toULong() shl 32) or (this[startIndex + 5].toULong() shl 40) or
|
||||
(this[startIndex + 6].toULong() shl 48) or (this[startIndex + 7].toULong() shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
toULong(startIndex, order).toLong()
|
||||
|
||||
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
LongArray(size / 8).also { copyInto(it, order = order) }
|
||||
85
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt
Normal file
85
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt
Normal file
@@ -0,0 +1,85 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: UIntArray,
|
||||
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 ->
|
||||
destination[destinationOffset + i] =
|
||||
(this[startIndex].toUInt() shl 24) or (this[startIndex + 1].toUInt() shl 16) or
|
||||
(this[startIndex + 2].toUInt() shl 8) or this[startIndex + 3].toUInt()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
destination[destinationOffset + i] =
|
||||
this[startIndex].toUInt() or (this[startIndex + 1].toUInt() shl 8) or
|
||||
(this[startIndex + 2].toUInt() shl 16) or (this[startIndex + 3].toUInt() shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUIntArray(), destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: ULongArray,
|
||||
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 / 4) { i ->
|
||||
destination[destinationOffset + i] =
|
||||
(this[startIndex].toULong() shl 56) or (this[startIndex + 1].toULong() shl 48) or
|
||||
(this[startIndex + 2].toULong() shl 40) or (this[startIndex + 3].toULong() shl 32) or
|
||||
(this[startIndex + 4].toULong() shl 24) or (this[startIndex + 5].toULong() shl 16) or
|
||||
(this[startIndex + 6].toULong() shl 8) or this[startIndex + 7].toULong()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
destination[destinationOffset + i] =
|
||||
this[startIndex].toULong() or (this[startIndex + 1].toULong() shl 8) or
|
||||
(this[startIndex + 2].toULong() shl 16) or (this[startIndex + 3].toULong() shl 24) or
|
||||
(this[startIndex + 4].toULong() shl 32) or (this[startIndex + 5].toULong() shl 40) or
|
||||
(this[startIndex + 6].toULong() shl 48) or (this[startIndex + 7].toULong() shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asULongArray(), destinationOffset, startIndex, endIndex, order)
|
||||
105
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt
Normal file
105
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
fun UByteArray.invInto(
|
||||
destination: UByteArray,
|
||||
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))
|
||||
|
||||
repeat(length) { i ->
|
||||
destination[destinationOffset + i] = this[startIndex + i].inv()
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.inv() = UByteArray(size).also { invInto(it) }
|
||||
|
||||
private inline fun UByteArray.combineInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
combine: (UByte, UByte) -> UByte,
|
||||
) {
|
||||
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))
|
||||
|
||||
repeat(length) { i ->
|
||||
destination[destinationOffset + i] = combine(this[startIndex + i], that[thatStartIndex + i])
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun UByteArray.combine(
|
||||
that: UByteArray,
|
||||
combine: (UByte, UByte) -> UByte,
|
||||
): UByteArray {
|
||||
require(this.size == that.size)
|
||||
|
||||
return UByteArray(size).also { combineInto(that, it, combine = combine) }
|
||||
}
|
||||
|
||||
fun UByteArray.andInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a and b }
|
||||
|
||||
infix fun UByteArray.and(that: UByteArray) =
|
||||
combine(that) { a, b -> a and b }
|
||||
|
||||
fun UByteArray.orInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a or b }
|
||||
|
||||
infix fun UByteArray.or(that: UByteArray) =
|
||||
combine(that) { a, b -> a or b }
|
||||
|
||||
fun UByteArray.xorInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a xor b }
|
||||
|
||||
infix fun UByteArray.xor(that: UByteArray) =
|
||||
combine(that) { a, b -> a xor b }
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.infendro.bytearray
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
fun String.encodeToUByteArray(): UByteArray =
|
||||
encodeToByteArray().asUByteArray()
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.uint
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
59
src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt
Normal file
59
src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.infendro.bytes.uint
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun UInt.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
require(destinationOffset in 0..(destination.size - 4))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 24).toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 3] = this.toUByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toUByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UInt.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, order)
|
||||
|
||||
fun UIntArray.copyInto(
|
||||
destination: UByteArray,
|
||||
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)))
|
||||
|
||||
repeat(length) { i ->
|
||||
this[startIndex + i].copyInto(destination, destinationOffset + (i * 4), order)
|
||||
}
|
||||
}
|
||||
|
||||
fun UIntArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex, order)
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.ulong
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
67
src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt
Normal file
67
src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
package com.infendro.bytes.ulong
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun ULong.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
require(destinationOffset in 0..(destination.size - 8))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 56).toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 48).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 40).toUByte()
|
||||
destination[destinationOffset + 3] = (this shr 32).toUByte()
|
||||
destination[destinationOffset + 4] = (this shr 24).toUByte()
|
||||
destination[destinationOffset + 5] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 6] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 7] = this.toUByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toUByte()
|
||||
destination[destinationOffset + 4] = (this shr 32).toUByte()
|
||||
destination[destinationOffset + 5] = (this shr 40).toUByte()
|
||||
destination[destinationOffset + 6] = (this shr 48).toUByte()
|
||||
destination[destinationOffset + 7] = (this shr 56).toUByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ULong.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, order)
|
||||
|
||||
fun ULongArray.copyInto(
|
||||
destination: UByteArray,
|
||||
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)))
|
||||
|
||||
repeat(length) { i ->
|
||||
this[startIndex + i].copyInto(destination, destinationOffset + (i * 8), order)
|
||||
}
|
||||
}
|
||||
|
||||
fun ULongArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex, order)
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `UByteArray Conversion Test` {
|
||||
@Test
|
||||
fun toUInt() {
|
||||
val expected = 0x12345678U
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt(),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toInt() {
|
||||
val expected = 0x12345678
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt(),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUIntArray() {
|
||||
val expected = uintArrayOf(0x12345678U, 0x12345678U)
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
).toUIntArray(),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
).toUIntArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun 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 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(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLong() {
|
||||
val expected = 0x1234567812345678L
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong(),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toULongArray() {
|
||||
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
).toULongArray(),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
).toULongArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLongArray() {
|
||||
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
).toLongArray(),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
).toLongArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertFails
|
||||
|
||||
class `UByteArray Operators Test` {
|
||||
@Test
|
||||
fun inv() {
|
||||
val expected = ubyteArrayOf(0xFFU, 0xE0U)
|
||||
val actual = ubyteArrayOf(0x00U, 0x1FU).inv()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `and - size`() {
|
||||
assertFails {
|
||||
ubyteArrayOf() and ubyteArrayOf(0x00U)
|
||||
}
|
||||
assertFails {
|
||||
ubyteArrayOf(0x00U) and ubyteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun and() {
|
||||
val expected = ubyteArrayOf(0x18U, 0x00U)
|
||||
val actual = ubyteArrayOf(0x5CU, 0x70U) and ubyteArrayOf(0x3AU, 0x0FU)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `or - size`() {
|
||||
assertFails {
|
||||
ubyteArrayOf() or ubyteArrayOf(0x00U)
|
||||
}
|
||||
assertFails {
|
||||
ubyteArrayOf(0x00U) or ubyteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun or() {
|
||||
val expected = ubyteArrayOf(0x7EU, 0x7FU)
|
||||
val actual = ubyteArrayOf(0x5CU, 0x70U) or ubyteArrayOf(0x3AU, 0x0FU)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `xor - size`() {
|
||||
assertFails {
|
||||
ubyteArrayOf() xor ubyteArrayOf(0x00U)
|
||||
}
|
||||
assertFails {
|
||||
ubyteArrayOf(0x00U) xor ubyteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xor() {
|
||||
val expected = ubyteArrayOf(0x66U, 0x7FU)
|
||||
val actual = ubyteArrayOf(0x5CU, 0x70U) xor ubyteArrayOf(0x3AU, 0x0FU)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
package com.infendro.bytearray
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `String Test` {
|
||||
class `UByteArray String Test` {
|
||||
@Test
|
||||
fun `encodeToUByteArray()`() {
|
||||
fun encodeToUByteArray() {
|
||||
val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U)
|
||||
val actual = "PASSWORD".encodeToUByteArray()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `decodeToString()`() {
|
||||
fun decodeToString() {
|
||||
val expected = "PASSWORD"
|
||||
val actual = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U).decodeToString()
|
||||
assertEquals(expected, actual)
|
||||
91
src/commonTest/kotlin/com/infendro/bytes/uint/Conversion.kt
Normal file
91
src/commonTest/kotlin/com/infendro/bytes/uint/Conversion.kt
Normal file
@@ -0,0 +1,91 @@
|
||||
package com.infendro.bytes.uint
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class `UInt Conversion Test` {
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x12345678U.toUByteArray(),
|
||||
0x12345678U.toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x12345678U.toByteArray(),
|
||||
0x12345678U.toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Array 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 `Array 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])
|
||||
}
|
||||
}
|
||||
}
|
||||
91
src/commonTest/kotlin/com/infendro/bytes/ulong/Conversion.kt
Normal file
91
src/commonTest/kotlin/com/infendro/bytes/ulong/Conversion.kt
Normal file
@@ -0,0 +1,91 @@
|
||||
package com.infendro.bytes.ulong
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class `ULong Conversion Test` {
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x1234567812345678UL.toUByteArray(),
|
||||
0x1234567812345678UL.toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x1234567812345678UL.toByteArray(),
|
||||
0x1234567812345678UL.toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Array toUByteArray`() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toUByteArray(),
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Array toByteArray`() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toByteArray(),
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user