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

✅ Add tests to Interpolator #845

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
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
}
})
Loading