Skip to content

Commit

Permalink
fix: restore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbressler13 committed Feb 1, 2024
1 parent aeafdfc commit 9629e59
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal fun createSimplifiedTerm(coefficient: ExactFraction, factorGroups: Map<
/**
* Simplify a set of irrational numbers by extracting the rational values
*
* @param values [ConstMultiSet]<IrrationalNumber<*>>: list of values, which are assumed to all have the same type
* @param values [ConstMultiSet]<IrrationalNumber<*>>: list of values, which are assumed to have the same type
* @return [Pair]<ExactFraction, ConstMultiSet<IrrationalNumber<*>>: pair of values where first value is the product of the numbers
* with rational values, and the second is a set of the irrational values
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal fun BigDecimal.divideBy(other: BigDecimal): BigDecimal {
}

/**
* Determine if number is a whole number
* Determine if decimal is a whole number
*
* @return `true` if number is a whole number, `false` otherwise
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.math.BigDecimal
*/
abstract class IrrationalNumber<T : IrrationalNumber<T>> : Comparable<T>, Number() {
/**
* Type label for number
* Type of number
*/
abstract val type: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal fun <T> castToDouble(decimal: BigDecimal, value: T, baseType: String):
}

/**
* Cast a number to another number baseType, or throw the given exception if the number exceeds the given min and max values
* Cast a number to another number type, or throw the given exception if the number exceeds the given min and max values
*
* @param value [BigDecimal]: number to cast
* @param minValue T: minimum allowed value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,77 @@
package xyz.lbres.exactnumbers.exactfraction

import xyz.lbres.kotlinutils.general.simpleIf
import xyz.lbres.exactnumbers.testutils.assertFailsWithMessage
import java.math.BigInteger
import kotlin.test.Test
import kotlin.test.assertEquals

class ExactFractionHelpersTest {
@Test fun testSimplifyFraction() = runCommonSimplifyTests(::simplifyFraction)
@Test
fun testSimplifyFraction() = runCommonSimplifyTests(::simplifyFraction)

@Test
fun testCreateDecimalString() = runCommonDecimalStringTests { ef, digits ->
simpleIf(digits == null, { createDecimalString(ef, 8) }, { createDecimalString(ef, digits!!) })
fun testCreateDecimalString() {
// whole
var ef = ExactFraction(0)
var expected = "0"
assertEquals(expected, createDecimalString(ef, 8))

ef = ExactFraction(4)
expected = "4"
assertEquals(expected, createDecimalString(ef, 8))

ef = ExactFraction(-3)
expected = "-3"
assertEquals(expected, createDecimalString(ef, 3))

ef = ExactFraction(453)
expected = "453"
assertEquals(expected, createDecimalString(ef, 1))

// decimal
ef = ExactFraction(3, 8)
expected = "0.375"
assertEquals(expected, createDecimalString(ef, 5))

ef = ExactFraction(5, 2)
expected = "2.5"
assertEquals(expected, createDecimalString(ef, 8))

ef = ExactFraction(5, 3)
expected = "2"
assertEquals(expected, createDecimalString(ef, 0))

ef = ExactFraction(4, 3)
expected = "1"
assertEquals(expected, createDecimalString(ef, 0))

ef = ExactFraction(-1, 9)
expected = "-0.111111111111"
assertEquals(expected, createDecimalString(ef, 12))

ef = ExactFraction(5, 9)
expected = "0.5555555555555555555555555555555555555556"
assertEquals(expected, createDecimalString(ef, 40))

ef = ExactFraction(-4, 19)
expected = "-0.21052632"
assertEquals(expected, createDecimalString(ef, 8))

ef = ExactFraction(3, 8)
expected = "0.38"
assertEquals(expected, createDecimalString(ef, 2))

ef = ExactFraction(-4, 19)
expected = "-0.21053"
assertEquals(expected, createDecimalString(ef, 5))

val bi = BigInteger("100000000000000000000")
ef = ExactFraction(bi, 3)
expected = "33333333333333333333.333333"
assertEquals(expected, createDecimalString(ef, 6))

// exception
val errorMessage = "Number of digits must be non-negative"
assertFailsWithMessage<IllegalArgumentException>(errorMessage) { createDecimalString(ExactFraction(3), -3) }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xyz.lbres.exactnumbers.exactfraction

import xyz.lbres.kotlinutils.general.simpleIf
import kotlin.test.Test

class ExactFractionTest {
Expand Down Expand Up @@ -40,14 +39,11 @@ class ExactFractionTest {

// parsing + toString
@Test fun testIsEFString() = runCommonCheckEFStringTests(ExactFraction.Companion::isEFString)
@Test fun testToDecimalString() = runToDecimalStringTests()
@Test fun testToFractionString() = runToFractionStringTests()
@Test fun testToPairString() = runToPairStringTests()
@Test fun testToEFString() = runToEFStringTests()

@Test fun testToDecimalString() = runCommonDecimalStringTests { ef, digits ->
simpleIf(digits == null, { ef.toDecimalString() }, { ef.toDecimalString(digits!!) })
}

// casting
@Test fun testToPair() = runToPairTests()
@Test fun testToByte() = runToByteTests()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,64 +236,64 @@ fun runCommonCheckEFStringTests(checkString: (String) -> Boolean) {
}

// toString
fun runCommonDecimalStringTests(createString: (ExactFraction, Int?) -> String) {
fun runToDecimalStringTests() {
var ef = ExactFraction(0)
var expected = "0"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(4)
expected = "4"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(-3)
expected = "-3"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(3, 8)
expected = "0.375"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(5, 2)
expected = "2.5"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(-1, 9)
expected = "-0.11111111"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(5, 9)
expected = "0.55555556"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(-4, 19)
expected = "-0.21052632"
assertEquals(expected, createString(ef, null))
assertEquals(expected, ef.toDecimalString())

ef = ExactFraction(3, 8)
expected = "0.38"
assertEquals(expected, createString(ef, 2))
assertEquals(expected, ef.toDecimalString(2))

ef = ExactFraction(-1, 9)
expected = "-0.111111111111"
assertEquals(expected, createString(ef, 12))
assertEquals(expected, ef.toDecimalString(12))

ef = ExactFraction(-4, 19)
expected = "-0.21053"
assertEquals(expected, createString(ef, 5))
assertEquals(expected, ef.toDecimalString(5))

ef = ExactFraction("45454.888888888888888")
expected = "45454.88889"
assertEquals(expected, createString(ef, 5))
assertEquals(expected, ef.toDecimalString(5))

val largeValue = "100000000000000000000"
val bi = BigInteger(largeValue)
ef = ExactFraction(bi, 3)
expected = "33333333333333333333.333333"
assertEquals(expected, createString(ef, 6))
assertEquals(expected, ef.toDecimalString(6))

// exception
val errorMessage = "Number of digits must be non-negative"
assertFailsWithMessage<IllegalArgumentException>(errorMessage) { createString(ExactFraction(3), -3) }
assertFailsWithMessage<IllegalArgumentException>(errorMessage) { createDecimalString(ExactFraction(3), -3) }
}

fun runToFractionStringTests() {
Expand Down

0 comments on commit 9629e59

Please sign in to comment.