diff --git a/src/commonTest/kotlin/com/infendro/bytearray/ByteArray.kt b/src/commonTest/kotlin/com/infendro/bytearray/ByteArray.kt index 16c479b..a6c09bb 100644 --- a/src/commonTest/kotlin/com/infendro/bytearray/ByteArray.kt +++ b/src/commonTest/kotlin/com/infendro/bytearray/ByteArray.kt @@ -17,71 +17,188 @@ class `ByteArray Test` { } } + @Test + fun `and()`() { + val expected = byteArrayOf(0x18, 0x00) + val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F) + assertContentEquals(expected, actual) + } + + @Test + fun `or() - size`() { + assertFails { + byteArrayOf() or byteArrayOf(0x00) + } + assertFails { + byteArrayOf(0x00) or byteArrayOf() + } + } + + @Test + fun `or()`() { + val expected = byteArrayOf(0x7E, 0x7F) + val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F) + assertContentEquals(expected, actual) + } + + @Test + fun `xor() - size`() { + assertFails { + byteArrayOf() xor byteArrayOf(0x00) + } + assertFails { + byteArrayOf(0x00) xor byteArrayOf() + } + } + + @Test + fun `xor()`() { + val expected = byteArrayOf(0x66, 0x7F) + val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F) + assertContentEquals(expected, actual) + } + @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 - ) + 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 `padStart() - unchanged`() { + val expected = byteArrayOf(0x02, 0x02) + val actual = byteArrayOf(0x02, 0x02).padStart(2, 0x01) + assertEquals(2, actual.size) + assertContentEquals(expected, actual) } @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 - ) + 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 `padEnd() - unchanged`() { + val expected = byteArrayOf(0x02, 0x02) + val actual = byteArrayOf(0x02, 0x02).padEnd(2, 0x01) + assertEquals(2, actual.size) + assertContentEquals(expected, actual) } @Test fun `toInt()`() { - assertEquals( - 0x12345678, - byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt() + val expected = 0x12345678 + val actual = arrayOf( + byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt(), + byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN), ) - assertEquals( - 0x12345678, - byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN) + assertEquals(expected, actual[0]) + assertEquals(expected, actual[1]) + } + + @Test + fun `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 `toLong()`() { - assertEquals( - 0x1234567812345678L, - byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).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( - 0x1234567812345678L, - byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN) + assertEquals(expected, actual[0]) + assertEquals(expected, actual[1]) + } + + @Test + fun `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 `toUInt()`() { - assertEquals( - 0x12345678U, - byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt() + val expected = 0x12345678U + val actual = arrayOf( + byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt(), + byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN), ) - assertEquals( - 0x12345678U, - byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN) + assertEquals(expected, actual[0]) + assertEquals(expected, actual[1]) + } + + @Test + fun `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 `toULong()`() { - assertEquals( - 0x1234567812345678UL, - byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).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( - 0x1234567812345678UL, - byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN) + assertEquals(expected, actual[0]) + assertEquals(expected, actual[1]) + } + + @Test + fun `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]) } } diff --git a/src/commonTest/kotlin/com/infendro/bytearray/String.kt b/src/commonTest/kotlin/com/infendro/bytearray/String.kt new file mode 100644 index 0000000..9db7a40 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytearray/String.kt @@ -0,0 +1,21 @@ +package com.infendro.bytearray + +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals + +class `String Test` { + @Test + fun `encodeToUByteArray()`() { + val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U) + val actual = "PASSWORD".encodeToUByteArray() + assertContentEquals(expected, actual) + } + + @Test + fun `decodeToString()`() { + val expected = "PASSWORD" + val actual = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U).decodeToString() + assertEquals(expected, actual) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytearray/UByteArray.kt b/src/commonTest/kotlin/com/infendro/bytearray/UByteArray.kt index 4eee00b..99a3f51 100644 --- a/src/commonTest/kotlin/com/infendro/bytearray/UByteArray.kt +++ b/src/commonTest/kotlin/com/infendro/bytearray/UByteArray.kt @@ -17,71 +17,188 @@ class `UByteArray Test` { } } + @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) + } + @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 - ) + 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 `padStart() - unchanged`() { + val expected = ubyteArrayOf(0x02U, 0x02U) + val actual = ubyteArrayOf(0x02U, 0x02U).padStart(2, 0x01U) + assertEquals(2, actual.size) + assertContentEquals(expected, actual) } @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 - ) + 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 `padEnd() - unchanged`() { + val expected = ubyteArrayOf(0x02U, 0x02U) + val actual = ubyteArrayOf(0x02U, 0x02U).padEnd(2, 0x01U) + assertEquals(2, actual.size) + assertContentEquals(expected, actual) } @Test fun `toInt()`() { - assertEquals( - 0x12345678, - ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt() + val expected = 0x12345678 + val actual = arrayOf( + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt(), + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN), ) - assertEquals( - 0x12345678, - ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN) + assertEquals(expected, actual[0]) + assertEquals(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 `toLong()`() { - assertEquals( - 0x1234567812345678L, - ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).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( - 0x1234567812345678L, - ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN) + assertEquals(expected, actual[0]) + assertEquals(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]) } @Test fun `toUInt()`() { - assertEquals( - 0x12345678U, - ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt() + val expected = 0x12345678U + val actual = arrayOf( + ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt(), + ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN), ) - assertEquals( - 0x12345678U, - ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(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 `toULong()`() { - assertEquals( - 0x1234567812345678UL, - ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).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( - 0x1234567812345678UL, - ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(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]) } }