Compare commits

..

3 Commits

Author SHA1 Message Date
e40ef3168d bump version to 1.2.0 2025-11-18 11:22:27 +01:00
9e4109c9f3 implement tests 2025-11-18 11:22:05 +01:00
12344283f7 implement Base8 encoding, rename Binary and Hex encodings 2025-11-18 11:21:46 +01:00
14 changed files with 222 additions and 68 deletions

View File

@@ -1,5 +1,5 @@
group = "com.infendro"
version = "1.1.0"
version = "1.2.0"
repositories {
mavenCentral()
@@ -17,6 +17,12 @@ kotlin {
}
linuxX64()
sourceSets {
commonTest.dependencies {
implementation(libs.test)
}
}
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}

View File

@@ -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" }

View File

@@ -1,6 +1,6 @@
package com.infendro.encoding
object Hex : Encoding() {
object Base16 : Encoding() {
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U,
0x38U, 0x39U, 0x61U, 0x62U, 0x63U, 0x64U, 0x65U, 0x66U,
@@ -8,19 +8,9 @@ object Hex : Encoding() {
override fun encode(
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
)
}
) = encode(bytes, CHARACTERS)
override fun decode(
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,
)
}
) = decode(bytes, CHARACTERS)
}

View File

@@ -0,0 +1,15 @@
package com.infendro.encoding
object Base2 : Encoding() {
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U,
)
override fun encode(
bytes: UByteArray,
) = encode(bytes, CHARACTERS)
override fun decode(
bytes: UByteArray,
) = decode(bytes, CHARACTERS)
}

View File

@@ -11,21 +11,9 @@ object Base32 : Encoding() {
override fun encode(
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
PADDING,
)
}
) = encode(bytes, CHARACTERS, PADDING)
override fun decode(
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,
PADDING,
)
}
) = decode(bytes, CHARACTERS, PADDING)
}

View File

@@ -15,21 +15,9 @@ object Base64 : Encoding() {
override fun encode(
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
PADDING,
)
}
) = encode(bytes, CHARACTERS, PADDING)
override fun decode(
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,
PADDING,
)
}
) = decode(bytes, CHARACTERS, PADDING)
}

View File

@@ -0,0 +1,15 @@
package com.infendro.encoding
object Base8 : Encoding() {
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U,
)
override fun encode(
bytes: UByteArray,
) = encode(bytes, CHARACTERS)
override fun decode(
bytes: UByteArray,
) = decode(bytes, CHARACTERS)
}

View File

@@ -1,25 +0,0 @@
package com.infendro.encoding
object Binary : Encoding() {
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U,
)
override fun encode(
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
)
}
override fun decode(
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,
)
}
}

View File

@@ -61,7 +61,7 @@ sealed class Encoding {
padding: UByte? = null,
): UByteArray {
val length = log2(characters.size).toInt()
val mask = 0xFFU
val mask = 0xffU
val decoded = mutableListOf<UByte>()

View File

@@ -0,0 +1,33 @@
package com.infendro.encoding
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Base16 Test` {
val encoding: Encoding
get() = Base16
val `Hello World!` = ubyteArrayOf(
0x48U, 0x65U, 0x6cU, 0x6cU,
0x6fU, 0x20U, 0x57U, 0x6fU,
0x72U, 0x6cU, 0x64U, 0x21U,
)
@Test
fun `encode empty ByteArray`() {
val expected = ubyteArrayOf()
val actual = encoding.encode(ubyteArrayOf())
assertContentEquals(expected, actual)
}
@Test
fun `encode "Hello, World!"`() {
val expected = ubyteArrayOf(
0x34U, 0x38U, 0x36U, 0x35U, 0x36U, 0x63U, 0x36U, 0x63U,
0x36U, 0x66U, 0x32U, 0x30U, 0x35U, 0x37U, 0x36U, 0x66U,
0x37U, 0x32U, 0x36U, 0x63U, 0x36U, 0x34U, 0x32U, 0x31U,
)
val actual = encoding.encode(`Hello World!`)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,42 @@
package com.infendro.encoding
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Base2 Test` {
val encoding: Encoding
get() = Base2
val `Hello World!` = ubyteArrayOf(
0x48U, 0x65U, 0x6cU, 0x6cU,
0x6fU, 0x20U, 0x57U, 0x6fU,
0x72U, 0x6cU, 0x64U, 0x21U,
)
@Test
fun `encode empty ByteArray`() {
val expected = ubyteArrayOf()
val actual = encoding.encode(ubyteArrayOf())
assertContentEquals(expected, actual)
}
@Test
fun `encode "Hello, World!"`() {
val expected = ubyteArrayOf(
0x30U, 0x31U, 0x30U, 0x30U, 0x31U, 0x30U, 0x30U, 0x30U,
0x30U, 0x31U, 0x31U, 0x30U, 0x30U, 0x31U, 0x30U, 0x31U,
0x30U, 0x31U, 0x31U, 0x30U, 0x31U, 0x31U, 0x30U, 0x30U,
0x30U, 0x31U, 0x31U, 0x30U, 0x31U, 0x31U, 0x30U, 0x30U,
0x30U, 0x31U, 0x31U, 0x30U, 0x31U, 0x31U, 0x31U, 0x31U,
0x30U, 0x30U, 0x31U, 0x30U, 0x30U, 0x30U, 0x30U, 0x30U,
0x30U, 0x31U, 0x30U, 0x31U, 0x30U, 0x31U, 0x31U, 0x31U,
0x30U, 0x31U, 0x31U, 0x30U, 0x31U, 0x31U, 0x31U, 0x31U,
0x30U, 0x31U, 0x31U, 0x31U, 0x30U, 0x30U, 0x31U, 0x30U,
0x30U, 0x31U, 0x31U, 0x30U, 0x31U, 0x31U, 0x30U, 0x30U,
0x30U, 0x31U, 0x31U, 0x30U, 0x30U, 0x31U, 0x30U, 0x30U,
0x30U, 0x30U, 0x31U, 0x30U, 0x30U, 0x30U, 0x30U, 0x31U,
)
val actual = encoding.encode(`Hello World!`)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,33 @@
package com.infendro.encoding
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Base32 Test` {
val encoding: Encoding
get() = Base32
val `Hello World!` = ubyteArrayOf(
0x48U, 0x65U, 0x6cU, 0x6cU,
0x6fU, 0x20U, 0x57U, 0x6fU,
0x72U, 0x6cU, 0x64U, 0x21U,
)
@Test
fun `encode empty ByteArray`() {
val expected = ubyteArrayOf()
val actual = encoding.encode(ubyteArrayOf())
assertContentEquals(expected, actual)
}
@Test
fun `encode "Hello, World!"`() {
val expected = ubyteArrayOf(
0x4aU, 0x42U, 0x53U, 0x57U, 0x59U, 0x33U, 0x44U, 0x50U,
0x45U, 0x42U, 0x4cU, 0x57U, 0x36U, 0x34U, 0x54U, 0x4dU,
0x4dU, 0x51U, 0x51U, 0x51U, 0x3dU, 0x3dU, 0x3dU, 0x3dU,
)
val actual = encoding.encode(`Hello World!`)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,32 @@
package com.infendro.encoding
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Base64 Test` {
val encoding: Encoding
get() = Base64
val `Hello World!` = ubyteArrayOf(
0x48U, 0x65U, 0x6cU, 0x6cU,
0x6fU, 0x20U, 0x57U, 0x6fU,
0x72U, 0x6cU, 0x64U, 0x21U,
)
@Test
fun `encode empty ByteArray`() {
val expected = ubyteArrayOf()
val actual = encoding.encode(ubyteArrayOf())
assertContentEquals(expected, actual)
}
@Test
fun `encode "Hello, World!"`() {
val expected = ubyteArrayOf(
0x53U, 0x47U, 0x56U, 0x73U, 0x62U, 0x47U, 0x38U, 0x67U,
0x56U, 0x32U, 0x39U, 0x79U, 0x62U, 0x47U, 0x51U, 0x68U,
)
val actual = encoding.encode(`Hello World!`)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,34 @@
package com.infendro.encoding
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Base8 Test` {
val encoding: Encoding
get() = Base8
val `Hello World!` = ubyteArrayOf(
0x48U, 0x65U, 0x6cU, 0x6cU,
0x6fU, 0x20U, 0x57U, 0x6fU,
0x72U, 0x6cU, 0x64U, 0x21U,
)
@Test
fun `encode empty ByteArray`() {
val expected = ubyteArrayOf()
val actual = encoding.encode(ubyteArrayOf())
assertContentEquals(expected, actual)
}
@Test
fun `encode "Hello, World!"`() {
val expected = ubyteArrayOf(
0x32U, 0x32U, 0x30U, 0x36U, 0x32U, 0x35U, 0x35U, 0x34U,
0x33U, 0x33U, 0x30U, 0x36U, 0x37U, 0x34U, 0x34U, 0x30U,
0x32U, 0x35U, 0x36U, 0x36U, 0x37U, 0x35U, 0x36U, 0x32U,
0x33U, 0x33U, 0x30U, 0x36U, 0x32U, 0x30U, 0x34U, 0x31U,
)
val actual = encoding.encode(`Hello World!`)
assertContentEquals(expected, actual)
}
}