Skip to content

Commit

Permalink
tests + fix (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics authored May 13, 2024
1 parent f7573c3 commit 7d56a70
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/commonMain/kotlin/com/funkatronics/encoders/BaseN.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ object BaseN {

// Count leading zeros.
var zeros = input.indexOfFirst { it.toInt() != 0 }
if (zeros == -1) return CharArray(input.size) { alphabet[0] }

// Convert base-256 digits to base-N digits (plus conversion to ASCII characters)
val input = input.copyOf(input.size) // since we modify it in-place
Expand Down Expand Up @@ -120,6 +121,7 @@ object BaseN {

// Count leading zeros.
val zeros = bytes.indexOfFirst { it.toInt() != 0 }
if (zeros == -1) return ByteArray(input.length)

// Convert base-58 digits to base-256 digits.
val decoded = ByteArray(input.length)
Expand Down
16 changes: 16 additions & 0 deletions src/commonTest/kotlin/com/funkatronics/encoders/Base32Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ class Base32Tests {
assertEquals(testString, actualDecoded)
}

@Test
fun testBase32EncodeDecodeAllZeros() {
// given
val length = 9
val testString: String = ByteArray(length).decodeToString()
val expectedEncoded = "AAAAAAAAAAAAAAA="

// when
val actualEncoded: String = Base32.encodeToString(testString.encodeToByteArray())
val actualDecoded = Base32.decodeToString(actualEncoded)

// then
assertEquals(expectedEncoded, actualEncoded)
assertEquals(testString, actualDecoded)
}

@Test
fun testBase32EncodeDecodeEmptyString() {
// given
Expand Down
16 changes: 16 additions & 0 deletions src/commonTest/kotlin/com/funkatronics/encoders/Base58Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.funkatronics.encoders

import com.funkatronics.encoders.error.InvalidInputException
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertTrue

Expand Down Expand Up @@ -87,4 +88,19 @@ class Base58Tests {
assertEquals(entry.key.decodeToString(), actualDecoded)
}
}

@Test
fun testBase58EncodeDecodeAllZeros() {
// given
val bytes = ByteArray(10) { 0 }
val expectedEncoded = "1111111111"

// when
val actualEncoded: String = Base58.encodeToString(bytes)
val actualDecoded = Base58.decode(actualEncoded)

// then
assertEquals(expectedEncoded, actualEncoded)
assertContentEquals(bytes, actualDecoded)
}
}
16 changes: 16 additions & 0 deletions src/commonTest/kotlin/com/funkatronics/encoders/Base64Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ class Base64Tests {
assertEquals(testString, actualDecoded)
}

@Test
fun testBase64EncodeDecodeAllZeros() {
// given
val length = 10
val testString: String = ByteArray(length).decodeToString()
val expectedEncoded = "AAAAAAAAAAAAAA=="

// when
val actualEncoded: String = Base64.encodeToString(testString.encodeToByteArray())
val actualDecoded = Base64.decodeToString(actualEncoded)

// then
assertEquals(expectedEncoded, actualEncoded)
assertEquals(testString, actualDecoded)
}

@Test
fun testBase64EncodeDecodeEmptyString() {
// given
Expand Down

0 comments on commit 7d56a70

Please sign in to comment.