diff --git a/build.gradle.kts b/build.gradle.kts index 4c08e89..c48ddb9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,6 +17,12 @@ kotlin { } linuxX64() + sourceSets { + commonTest.dependencies { + implementation(libs.test) + } + } + compilerOptions { freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes") } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bb704f3..7b812f8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,3 +3,6 @@ kotlin = "2.1.20" [plugins] multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } + +[libraries] +test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } diff --git a/src/commonTest/kotlin/com/infendro/bytearray/ByteArray.kt b/src/commonTest/kotlin/com/infendro/bytearray/ByteArray.kt new file mode 100644 index 0000000..16c479b --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytearray/ByteArray.kt @@ -0,0 +1,87 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFails + +class `ByteArray Test` { + @Test + fun `and() - size`() { + assertFails { + byteArrayOf() and byteArrayOf(0x00) + } + assertFails { + byteArrayOf(0x00) and byteArrayOf() + } + } + + @Test + fun `padStart()`() { + val padded = byteArrayOf(0x02, 0x02).padStart(8, 0x01) + assertEquals(8, padded.size) + assertContentEquals( + byteArrayOf(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02), + padded + ) + } + + @Test + fun `padEnd()`() { + val padded = byteArrayOf(0x02, 0x02).padEnd(8, 0x01) + assertEquals(8, padded.size) + assertContentEquals( + byteArrayOf(0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01), + padded + ) + } + + @Test + fun `toInt()`() { + assertEquals( + 0x12345678, + byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt() + ) + assertEquals( + 0x12345678, + byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN) + ) + } + + @Test + fun `toLong()`() { + assertEquals( + 0x1234567812345678L, + byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toLong() + ) + assertEquals( + 0x1234567812345678L, + byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN) + ) + } + + @Test + fun `toUInt()`() { + assertEquals( + 0x12345678U, + byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt() + ) + assertEquals( + 0x12345678U, + byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN) + ) + } + + @Test + fun `toULong()`() { + assertEquals( + 0x1234567812345678UL, + byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toULong() + ) + assertEquals( + 0x1234567812345678UL, + byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN) + ) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytearray/Int.kt b/src/commonTest/kotlin/com/infendro/bytearray/Int.kt new file mode 100644 index 0000000..356b847 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytearray/Int.kt @@ -0,0 +1,31 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN +import kotlin.test.Test +import kotlin.test.assertContentEquals + +class `Int Test` { + @Test + fun `toByteArray()`() { + assertContentEquals( + byteArrayOf(0x12, 0x34, 0x56, 0x78), + 0x12345678.toByteArray() + ) + assertContentEquals( + byteArrayOf(0x78, 0x56, 0x34, 0x12), + 0x12345678.toByteArray(LITTLE_ENDIAN) + ) + } + + @Test + fun `toUByteArray()`() { + assertContentEquals( + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U), + 0x12345678.toUByteArray() + ) + assertContentEquals( + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U), + 0x12345678.toUByteArray(LITTLE_ENDIAN) + ) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytearray/Long.kt b/src/commonTest/kotlin/com/infendro/bytearray/Long.kt new file mode 100644 index 0000000..0cba1b4 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytearray/Long.kt @@ -0,0 +1,31 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN +import kotlin.test.Test +import kotlin.test.assertContentEquals + +class `Long Test` { + @Test + fun `toByteArray()`() { + assertContentEquals( + byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78), + 0x1234567812345678L.toByteArray() + ) + assertContentEquals( + byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12), + 0x1234567812345678L.toByteArray(LITTLE_ENDIAN) + ) + } + + @Test + fun `toUByteArray()`() { + assertContentEquals( + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U), + 0x1234567812345678L.toUByteArray() + ) + assertContentEquals( + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U), + 0x1234567812345678L.toUByteArray(LITTLE_ENDIAN) + ) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytearray/UByteArray.kt b/src/commonTest/kotlin/com/infendro/bytearray/UByteArray.kt new file mode 100644 index 0000000..4eee00b --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytearray/UByteArray.kt @@ -0,0 +1,87 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFails + +class `UByteArray Test` { + @Test + fun `and() - size`() { + assertFails { + ubyteArrayOf() and ubyteArrayOf(0x00U) + } + assertFails { + ubyteArrayOf(0x00U) and ubyteArrayOf() + } + } + + @Test + fun `padStart()`() { + val padded = ubyteArrayOf(0x02U, 0x02U).padStart(8, 0x01U) + assertEquals(8, padded.size) + assertContentEquals( + ubyteArrayOf(0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x02U, 0x02U), + padded + ) + } + + @Test + fun `padEnd()`() { + val padded = ubyteArrayOf(0x02U, 0x02U).padEnd(8, 0x01U) + assertEquals(8, padded.size) + assertContentEquals( + ubyteArrayOf(0x02U, 0x02U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U), + padded + ) + } + + @Test + fun `toInt()`() { + assertEquals( + 0x12345678, + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt() + ) + assertEquals( + 0x12345678, + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN) + ) + } + + @Test + fun `toLong()`() { + assertEquals( + 0x1234567812345678L, + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong() + ) + assertEquals( + 0x1234567812345678L, + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN) + ) + } + + @Test + fun `toUInt()`() { + assertEquals( + 0x12345678U, + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt() + ) + assertEquals( + 0x12345678U, + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN) + ) + } + + @Test + fun `toULong()`() { + assertEquals( + 0x1234567812345678UL, + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong() + ) + assertEquals( + 0x1234567812345678UL, + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(LITTLE_ENDIAN) + ) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytearray/UInt.kt b/src/commonTest/kotlin/com/infendro/bytearray/UInt.kt new file mode 100644 index 0000000..cfdd28f --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytearray/UInt.kt @@ -0,0 +1,31 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN +import kotlin.test.Test +import kotlin.test.assertContentEquals + +class `UInt Test` { + @Test + fun `toByteArray()`() { + assertContentEquals( + byteArrayOf(0x12, 0x34, 0x56, 0x78), + 0x12345678U.toByteArray() + ) + assertContentEquals( + byteArrayOf(0x78, 0x56, 0x34, 0x12), + 0x12345678U.toByteArray(LITTLE_ENDIAN) + ) + } + + @Test + fun `toUByteArray()`() { + assertContentEquals( + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U), + 0x12345678U.toUByteArray() + ) + assertContentEquals( + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U), + 0x12345678U.toUByteArray(LITTLE_ENDIAN) + ) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytearray/ULong.kt b/src/commonTest/kotlin/com/infendro/bytearray/ULong.kt new file mode 100644 index 0000000..e3e50f4 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytearray/ULong.kt @@ -0,0 +1,31 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN +import kotlin.test.Test +import kotlin.test.assertContentEquals + +class `ULong Test` { + @Test + fun `toByteArray()`() { + assertContentEquals( + byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78), + 0x1234567812345678UL.toByteArray() + ) + assertContentEquals( + byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12), + 0x1234567812345678UL.toByteArray(LITTLE_ENDIAN) + ) + } + + @Test + fun `toUByteArray()`() { + assertContentEquals( + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U), + 0x1234567812345678UL.toUByteArray() + ) + assertContentEquals( + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U), + 0x1234567812345678UL.toUByteArray(LITTLE_ENDIAN) + ) + } +}