Skip to content

Initialising with ByteArray differs from java's BigInteger  #153

@fullkomnun

Description

@fullkomnun

Describe the bug
For a specific overflow byte array, initialising creates a different value comapred to java's BigInteger.

To Reproduce
Steps to reproduce the behavior:
sample unit test:

@Test
fun foo() {
    val bytes = byteArrayOf(-128, 0, 0, 0, 0, 0, 0, 0) // bytes: [-128, 0, 0, 0, 0, 0, 0, 0]
    val javaBig = BigInteger(1, bytes) // javaBig: "9223372036854775808"
    val kotlinBig = IonSpinBigInteger.fromByteArray(bytes, Sign.POSITIVE) // bytes: [-128, 0, 0, 0, 0, 0, 0, 0], kotlinBig: "0"
    Assert.assertArrayEquals(javaBig.toByteArray(), kotlinBig.toByteArray()) // javaBig: "9223372036854775808", kotlinBig: "0"
}

Expected behavior
Expected same numeric value as with java's BigInteger but it differs and this test fails.

Platform
JVM

Additional context
Although i encountered this issue for these specific values, there obviously could be more and randomised testing can help here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions