Skip to content

Commit

Permalink
✅ Add tests to Interpolator
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Feb 11, 2025
1 parent 9ba02b1 commit 24c22ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction
import java.time.Duration

// TODO Add tests
class Interpolator(
val data: Map<Duration, Double>
) : UnivariateInterpolator, UnivariateFunction {
Expand All @@ -21,10 +20,4 @@ class Interpolator(
override fun interpolate(xs: DoubleArray?, ys: DoubleArray?): UnivariateFunction = dataInterpolator

override fun value(x: Double): Double = dataInterpolator.value(x.coerceAtMost(maxPossibleX.toDouble()))

fun calculatePercentage(secondsQuit: Long): Double {
val maxValue = data.values.max()
val currentValue = value(secondsQuit.coerceAtMost(maxPossibleX).toDouble())
return currentValue / maxValue
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package br.com.colman.petals.withdrawal.interpolator

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import org.apache.commons.math3.analysis.UnivariateFunction
import java.time.Duration.ofSeconds as seconds

class InterpolatorTest : FunSpec({

test("Should respect maxPossibleX") {
val data = mapOf(
seconds(0) to 0.0,
seconds(5) to 10.0,
seconds(10) to 20.0
)
val target = Interpolator(data)

target.value(10.0) shouldBe 20.0
target.value(100.0) shouldBe 20.0
}

test("should implement UnivariateFunction") {
val data = mapOf(
seconds(0) to 0.0,
seconds(2) to 4.0
)
val target = Interpolator(data)

target.value(1.0) shouldBe 2.0
}

test("should interpolate ignoring given arrays") {
val data = mapOf(
seconds(0) to 0.0,
seconds(10) to 10.0
)

val interpolator = Interpolator(data)
val univariateFunction: UnivariateFunction = interpolator.interpolate(
doubleArrayOf(),
doubleArrayOf()
)

univariateFunction.value(5.0) shouldBe 5.0
}
})

0 comments on commit 24c22ef

Please sign in to comment.