From 0129039df9b4d8027461edaae39f27c496f26fd3 Mon Sep 17 00:00:00 2001 From: Infendro Date: Thu, 12 Feb 2026 20:49:25 +0100 Subject: [PATCH] improve test coverage --- .../infendro/bytes/bytearray/Conversion.kt | 80 +++++++++++++++++ .../com/infendro/bytes/bytearray/Copy.kt | 85 ------------------- .../com/infendro/bytes/int/Conversion.kt | 20 +++++ .../kotlin/com/infendro/bytes/int/Copy.kt | 24 ------ .../com/infendro/bytes/intarray/Conversion.kt | 37 ++++++++ .../com/infendro/bytes/intarray/Copy.kt | 41 --------- .../com/infendro/bytes/long/Conversion.kt | 28 ++++++ .../kotlin/com/infendro/bytes/long/Copy.kt | 32 ------- .../infendro/bytes/longarray/Conversion.kt | 45 ++++++++++ .../com/infendro/bytes/longarray/Copy.kt | 49 ----------- .../com/infendro/bytes/byte/Operator.kt | 13 +++ .../infendro/bytes/bytearray/Conversion.kt | 26 ++++++ .../com/infendro/bytes/bytearray/Operator.kt | 21 +++++ .../kotlin/com/infendro/bytes/int/Operator.kt | 13 +++ .../com/infendro/bytes/long/Operator.kt | 13 +++ 15 files changed, 296 insertions(+), 231 deletions(-) delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt create mode 100644 src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt create mode 100644 src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt create mode 100644 src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt index 2dad050..5487c4c 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt @@ -50,6 +50,42 @@ fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long { } } +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) @@ -57,6 +93,50 @@ fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteO 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) diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt deleted file mode 100644 index 9d44849..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt +++ /dev/null @@ -1,85 +0,0 @@ -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.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.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) - } - } - } -} diff --git a/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt index 00da321..d3d00ab 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt @@ -2,6 +2,26 @@ 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) diff --git a/src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt deleted file mode 100644 index 2b6bf0d..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt +++ /dev/null @@ -1,24 +0,0 @@ -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() - } - } -} diff --git a/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt index ea24bca..6e573d2 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt @@ -2,6 +2,43 @@ 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 diff --git a/src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt deleted file mode 100644 index bec0b75..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt +++ /dev/null @@ -1,41 +0,0 @@ -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() - } - } - } -} diff --git a/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt index 2a6fb31..c6b9008 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt @@ -2,6 +2,34 @@ 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) diff --git a/src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt deleted file mode 100644 index 0cbb9f4..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt +++ /dev/null @@ -1,32 +0,0 @@ -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() - } - } -} diff --git a/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt index 9423273..276e5db 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt @@ -2,6 +2,51 @@ 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 diff --git a/src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt deleted file mode 100644 index 0021fdb..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt +++ /dev/null @@ -1,49 +0,0 @@ -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() - } - } - } -} diff --git a/src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt b/src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt new file mode 100644 index 0000000..4de9311 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt index bc52e1b..1e6fa32 100644 --- a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt +++ b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt @@ -32,6 +32,19 @@ class ByteArrayConversionTest { } } + @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) @@ -51,6 +64,19 @@ class ByteArrayConversionTest { } } + @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) diff --git a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt index bee8422..d515241 100644 --- a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt +++ b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt @@ -11,6 +11,13 @@ class ByteArrayOperatorTest { 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) @@ -18,6 +25,13 @@ class ByteArrayOperatorTest { 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) @@ -25,6 +39,13 @@ class ByteArrayOperatorTest { 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) diff --git a/src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt b/src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt new file mode 100644 index 0000000..a5057d6 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt b/src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt new file mode 100644 index 0000000..32d0e30 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt @@ -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) + } +}