Skip to content

Commit

Permalink
add units compatible clamp function
Browse files Browse the repository at this point in the history
  • Loading branch information
sswadkar committed Feb 12, 2023
1 parent 63c02cd commit 1b64d87
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ publishing {
release(MavenPublication) {
groupId = 'org.team4099'
artifactId = 'falconutils'
version = '1.1.11'
version = '1.1.12'

from(components["kotlin"])
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/org/team4099/lib/math/MathUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.team4099.lib.math

import edu.wpi.first.math.MathUtil
import org.team4099.lib.units.UnitKey
import org.team4099.lib.units.Value

fun <K : UnitKey> clamp(value: Value<K>, lowerBound: Value<K>, upperBound: Value<K>): Value<K> {
return Value(MathUtil.clamp(value.value, lowerBound.value, upperBound.value))
}
34 changes: 34 additions & 0 deletions src/test/kotlin/team4099/math/MathTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package team4099.math

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.team4099.lib.math.clamp
import org.team4099.lib.units.base.inInches
import org.team4099.lib.units.base.inches

class MathTest {

@Test
fun noClamp() {
val value = 5.0.inches
val lowerBound = 0.0.inches
val upperBound = 10.0.inches
assertEquals(clamp(value, lowerBound, upperBound).inInches, value.inInches)
}

@Test
fun upperClamp() {
val value = 15.0.inches
val lowerBound = 0.0.inches
val upperBound = 10.0.inches
assertEquals(clamp(value, lowerBound, upperBound).inInches, upperBound.inInches)
}

@Test
fun lowerClamp() {
val value = -5.0.inches
val lowerBound = 0.0.inches
val upperBound = 10.0.inches
assertEquals(clamp(value, lowerBound, upperBound).inInches, lowerBound.inInches)
}
}

0 comments on commit 1b64d87

Please sign in to comment.