Skip to content

Commit 09f76b7

Browse files
committed
Add unit tests for history
1 parent d9c80c2 commit 09f76b7

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ deploy {
4141
def includeDesktopSupport = false
4242

4343
// Defining my dependencies. In this case, WPILib (+ friends), and vendor libraries.
44-
// Also defines JUnit 4.
4544
dependencies {
4645
implementation wpi.deps.wpilib()
4746
nativeZip wpi.deps.wpilibJni(wpi.platforms.roborio)
@@ -52,7 +51,8 @@ dependencies {
5251
nativeZip wpi.deps.vendor.jni(wpi.platforms.roborio)
5352
nativeDesktopZip wpi.deps.vendor.jni(wpi.platforms.desktop)
5453

55-
testImplementation 'junit:junit:4.12'
54+
testImplementation('org.junit.jupiter:junit-jupiter-api:5.6.0')
55+
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.6.0')
5656

5757
// Enable simulation gui support. Must check the box in vscode to enable support
5858
// upon debugging
@@ -65,4 +65,4 @@ dependencies {
6565
jar {
6666
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
6767
manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS)
68-
}
68+
}

src/main/java/org/mayheminc/util/History.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,20 @@ public double getAzForTime(double t) {
3030
int i = index - 1;
3131
int count = 0;
3232

33-
// if (t < 0) {
34-
// DriverStation.reportError("Negative time in history", false);
35-
// return 0.0; // no negative times.
36-
// }
37-
3833
while (i != index) {
3934
if (i < 0) {
4035
i = HISTORY_SIZE - 1;
4136
}
4237

4338
if (time[i] <= t) {
4439
int prev = (i + 1) % HISTORY_SIZE;
45-
if (prev != index) {
40+
if (prev != index && time[i] >= 0.0) {
4641
// Interpolate between the two closest entries
4742
assert(time[i] < time[prev]);
4843
double factor = (t - time[i]) / (time[prev] - time[i]);
4944
az = azimuth[i] + factor * (azimuth[prev] - azimuth[i]);
5045
} else {
51-
// Only one entry; no interpolation possible
46+
// Only one (real) entry; no interpolation possible
5247
az = azimuth[i];
5348
}
5449
break;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.mayheminc.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class HistoryTest {
7+
@Test
8+
public void runHistoryTest() {
9+
History hist = new History();
10+
11+
// Returns 0 before any data
12+
assertEquals(0.0, hist.getAzForTime(1.0));
13+
14+
// Add first "real" data
15+
hist.add(1.0, 101.0);
16+
assertEquals(101.0, hist.getAzForTime(1.0));
17+
assertEquals(101.0, hist.getAzForTime(1.5));
18+
assertEquals(0.0, hist.getAzForTime(0.5));
19+
20+
// Add second entry and check interpolation
21+
hist.add(2.0, 102.0);
22+
assertEquals(102.0, hist.getAzForTime(2.0));
23+
assertEquals(101.0, hist.getAzForTime(1.0));
24+
assertEquals(101.5, hist.getAzForTime(1.5));
25+
assertEquals(101.8, hist.getAzForTime(1.8));
26+
27+
// Fill history -- depends on the value of HISTORY_SIZE
28+
for (int i=3; i <= 50; i++) {
29+
hist.add(i, 100 + i);
30+
}
31+
assertEquals(150.0, hist.getAzForTime(50));
32+
assertEquals(150.0, hist.getAzForTime(51));
33+
assertEquals(150.0, hist.getAzForTime(100));
34+
assertEquals(101.0, hist.getAzForTime(1.0));
35+
36+
// Overflow history
37+
hist.add(51, 151);
38+
assertEquals(151.0, hist.getAzForTime(51));
39+
assertEquals(151.0, hist.getAzForTime(100));
40+
assertEquals(102.0, hist.getAzForTime(0.1));
41+
assertEquals(150.7, hist.getAzForTime(50.7));
42+
}
43+
}

0 commit comments

Comments
 (0)