Skip to content

Commit

Permalink
Return an array containing 0 when big integer is zero, to be more con…
Browse files Browse the repository at this point in the history
…sistent. Fixes #280
  • Loading branch information
ionspin committed Feb 6, 2024
1 parent 6ab1048 commit d917cf0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,9 @@ internal object BigInteger63Arithmetic : BigIntegerArithmetic {
override fun toUByteArray(
operand: ULongArray
): UByteArray {
if (operand == ZERO) {
return UByteArray(1) { 0U }
}
val as64Bit = convertTo64BitRepresentation(operand).reversedArray()
val result = UByteArray(as64Bit.size * 8)
for (i in 0 until as64Bit.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,11 @@ class ByteArrayConversionTest {
val kotlinBigBytes = IonSpinBigInteger.parseString("21000").toByteArray()
Assert.assertArrayEquals(javaBigBytes, kotlinBigBytes)
}

@Test
fun zeroToByteArray() {
val javaBigBytes = BigInteger("0").toByteArray()
val kotlinBigBytes = IonSpinBigInteger.parseString("0").toByteArray()
Assert.assertArrayEquals(javaBigBytes, kotlinBigBytes)
}
}

0 comments on commit d917cf0

Please sign in to comment.