Skip to content

Commit

Permalink
poc - failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willem.romijn committed Jan 13, 2025
1 parent f69b582 commit e67d189
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 5 additions & 2 deletions measure/src/main/kotlin/com/alliander/open/measure/Measure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ data class Measure<U : Units>(val amount: BigDecimal, val units: U) : Comparable
infix fun <A : U> `in`(other: A): BigDecimal =
if (units == other)
amount
else (amount * units.ratio).divide(other.ratio, amount.scale() + other.ratio.precision(), RoundingMode.UP)
else {
val result = (amount * units.ratio).divide(other.ratio)
result.setScale(amount.precision() - result.precision() + result.scale(), RoundingMode.UP)
}

operator fun plus(other: Measure<U>): Measure<U> = baseUnits(
units,
Expand Down Expand Up @@ -121,4 +124,4 @@ data class Measure<U : Units>(val amount: BigDecimal, val units: U) : Comparable
}

private fun BigDecimal.roundToMultiple(factor: BigDecimal, roundingMode: RoundingMode): BigDecimal =
this.divide(factor, 0, roundingMode) * factor
this.divide(factor, 0, roundingMode) * factor
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.property.checkAll
import java.math.BigDecimal
import java.math.RoundingMode.HALF_UP
import java.math.RoundingMode.UP

class MeasureTest : StringSpec({
Expand Down Expand Up @@ -222,6 +223,10 @@ class MeasureTest : StringSpec({
"Unit conversion uses the correct scale" {
val energyInJoule = 13500000 * joule
val valueInKwh = energyInJoule `in` kiloWattHour
valueInKwh.stripTrailingZeros() shouldBe BigDecimal.valueOf(3.75)
valueInKwh shouldBe BigDecimal.valueOf(3.75).setScale(7)

val energyInKwh = 3.75 * kiloWattHour
val valueInJoule = energyInKwh `in` joule
valueInJoule shouldBe BigDecimal.valueOf(1.35E+7)
}
})

0 comments on commit e67d189

Please sign in to comment.