Skip to content

Commit

Permalink
Add test for InterpolatingDoubleTreeMap class from WPI. To run unit t…
Browse files Browse the repository at this point in the history
…ests, right-click on the test directory and choose the "Run Tests" option.
  • Loading branch information
dlpond917 committed Feb 19, 2024
1 parent ddca0d3 commit 13ac990
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,9 @@ idea {
// Exclude the .vscode directory from indexing and search
excludeDirs+=file(".vscode" )
}
}

test {
useJUnitPlatform()
systemProperty 'junit.jupiter.extensions.autodetection.enabled', 'true'
}
67 changes: 67 additions & 0 deletions src/test/java/InterpolationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import edu.wpi.first.math.interpolation.InterpolatingDoubleTreeMap;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* Demonstrate how WPILib class InterpolatingDoubleTreeMap works
*/
class InterpolationTest {

static final InterpolatingDoubleTreeMap SHOOTER_ANGLE_LOW_FRONT = new InterpolatingDoubleTreeMap();
static {
// First number is the distance in feet. Second number is the shooter angle.
SHOOTER_ANGLE_LOW_FRONT.put(0.0, 54.0);
SHOOTER_ANGLE_LOW_FRONT.put(6.0, 27.0);
SHOOTER_ANGLE_LOW_FRONT.put(12.0, 24.0);
SHOOTER_ANGLE_LOW_FRONT.put(18.0, 22.0);
SHOOTER_ANGLE_LOW_FRONT.put(24.0, 20.0);
}

static final InterpolatingDoubleTreeMap SHOOTER_ANGLE_LOW_BACK = new InterpolatingDoubleTreeMap();
static {
// First number is the distance in feet. Second number is the shooter angle.
SHOOTER_ANGLE_LOW_BACK.put(0.0, 54.0);
SHOOTER_ANGLE_LOW_BACK.put(6.0, 27.0);
SHOOTER_ANGLE_LOW_BACK.put(12.0, 24.0);
SHOOTER_ANGLE_LOW_BACK.put(18.0, 22.0);
SHOOTER_ANGLE_LOW_BACK.put(24.0, 20.0);
}

@BeforeEach // this method will run before each test
void setup() {
}

@AfterEach // this method will run after each test
void shutdown() throws Exception {
}

@Test // marks this method as a test
void valuesThatWereMeasured() {
assertEquals(20.0, SHOOTER_ANGLE_LOW_FRONT.get(24.0));
assertEquals(54.0, SHOOTER_ANGLE_LOW_FRONT.get(0.0));
}

@Test // marks this method as a test
void valuesThatWereNotMeasured() {
// Test the angle halfway between two measured points
assertEquals(27.0 + (54.0 - 27.0)/2.0, SHOOTER_ANGLE_LOW_FRONT.get(3.0));
assertEquals(24.0 + (27.0 - 24.0)/2.0, SHOOTER_ANGLE_LOW_FRONT.get(9.0));
}
@Test // marks this method as a test
void valueGreaterThanLargestMeasurement() {
// The angle never goes lower than the last measurement made
assertEquals(20.0, SHOOTER_ANGLE_LOW_FRONT.get(90.0));
}

@Test
void valueVeryCloseToMeasuredValue() {
double shooterAngle = SHOOTER_ANGLE_LOW_FRONT.get(18.7);
System.out.print(shooterAngle);
assertNotEquals(22.0, shooterAngle);
assertTrue(shooterAngle < 22.0);
assertTrue(shooterAngle > 21.7);
}
}

0 comments on commit 13ac990

Please sign in to comment.