Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ClassCastException calling divideAndRemainder #306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/commonTest/kotlin/org/gciatto/kt/math/TestIntegers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,65 @@ class TestIntegers {
assertEquals("-9223372036854775818", (BigInteger.of(Long.MIN_VALUE) - BigInteger.TEN).toString())
}

@Test
fun testDivideAndRemainder() {
fun assertArrayContains(
expectedQuotient: BigInteger,
expectedRemainder: BigInteger,
actualResult: Array<out BigInteger>
) {
assertEquals(2, actualResult.size)
assertBigIntegersAreEquals(expectedQuotient, actualResult[0])
assertBigIntegersAreEquals(expectedRemainder, actualResult[1])
}

fun doTest(
dividendInt: Int,
divisorInt: Int,
expectedQuotientInt: Int,
expectedRemainderInt: Int
) {
val divisorLong = divisorInt.toLong()
val dividend = BigInteger.of(dividendInt)
val divisor = BigInteger.of(divisorInt)
val expectedQuotient = BigInteger.of(expectedQuotientInt)
val expectedRemainder = BigInteger.of(expectedRemainderInt)

// int overloads
assertBigIntegersAreEquals(expectedQuotient, dividend.div(divisorInt))
assertBigIntegersAreEquals(expectedRemainder, dividend.rem(divisorInt))
assertBigIntegersAreEquals(expectedRemainder, dividend.remainder(divisorInt))
assertArrayContains(expectedQuotient, expectedRemainder, dividend.divideAndRemainder(divisorInt))

// long overloads
assertBigIntegersAreEquals(expectedQuotient, dividend.div(divisorLong))
assertBigIntegersAreEquals(expectedRemainder, dividend.rem(divisorLong))
assertBigIntegersAreEquals(expectedRemainder, dividend.remainder(divisorLong))
assertArrayContains(expectedQuotient, expectedRemainder, dividend.divideAndRemainder(divisorLong))

// BigInteger overloads
assertBigIntegersAreEquals(expectedQuotient, dividend.div(divisor))
assertBigIntegersAreEquals(expectedRemainder, dividend.rem(divisor))
assertBigIntegersAreEquals(expectedRemainder, dividend.remainder(divisor))
assertArrayContains(expectedQuotient, expectedRemainder, dividend.divideAndRemainder(divisor))
}

doTest(0, 3, 0, 0)
doTest(0, -3, 0, 0)
doTest(1, 3, 0, 1)
doTest(1, -3, 0, 1)
doTest(-1, 3, 0, -1)
doTest(-1, -3, 0, -1)
doTest(6, 2, 3, 0)
doTest(6, -2, -3, 0)
doTest(-6, -2, 3, 0)
doTest(-6, 2, -3, 0)
doTest(7, 2, 3, 1)
doTest(7, -2, -3, 1)
doTest(-7, -2, 3, -1)
doTest(-7, 2, -3, -1)
}

@Test
fun testDecimalParsing() {
assertEquals(0, BigInteger.of("0").toInt())
Expand Down
13 changes: 9 additions & 4 deletions src/jvmMain/kotlin/org/gciatto/kt/math/JavaBigIntegerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ internal class JavaBigIntegerAdapter(val value: JavaBigInteger) : BigInteger {
}

private inline fun adaptAll(
other: BigInteger,
other: JavaBigIntegerAdapter,
f: (JavaBigInteger) -> Array<JavaBigInteger>,
): Array<out JavaBigIntegerAdapter> {
val javaInts = f(other.castTo())
val javaInts = f(other.value)
return Array(javaInts.size) { index -> adapt { javaInts[index] } }
}

private inline fun adaptAll(
other: BigInteger,
f: (JavaBigInteger) -> Array<JavaBigInteger>,
): Array<out JavaBigIntegerAdapter> = adaptAll(other.castTo(), f)

override val absoluteValue: BigInteger
get() = adapt { value.abs() }

Expand All @@ -54,8 +59,8 @@ internal class JavaBigIntegerAdapter(val value: JavaBigInteger) : BigInteger {
override fun div(other: BigInteger): BigInteger = adapt(other) { value.divide(it) }

override fun divideAndRemainder(other: BigInteger): Array<out BigInteger> =
adaptAll(other) {
value.divideAndRemainder(it)
adaptAll(other) { otherJava: JavaBigInteger ->
value.divideAndRemainder(otherJava)
}

override fun remainder(other: BigInteger): BigInteger = adapt(other) { value.remainder(it) }
Expand Down