Skip to content

Commit 1c07002

Browse files
committed
Add test case; enable mocking to prevent calls to DriverStation during test
1 parent 3193280 commit 1c07002

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ dependencies {
5151
nativeZip wpi.deps.vendor.jni(wpi.platforms.roborio)
5252
nativeDesktopZip wpi.deps.vendor.jni(wpi.platforms.desktop)
5353

54-
testImplementation('org.junit.jupiter:junit-jupiter-api:5.6.0')
55-
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.6.0')
54+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
55+
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
56+
testCompile 'org.mockito:mockito-core:3.3.0'
57+
testCompile 'org.mockito:mockito-junit-jupiter:3.3.0'
5658

5759
// Enable simulation gui support. Must check the box in vscode to enable support
5860
// upon debugging
@@ -65,4 +67,8 @@ dependencies {
6567
jar {
6668
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
6769
manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS)
70+
}
71+
72+
test {
73+
useJUnitPlatform()
6874
}

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

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,41 @@ public History() {
1515
}
1616

1717
public void add(double t, double az) {
18-
1918
time[index] = t;
2019
azimuth[index] = az;
21-
2220
index++;
23-
if (index >= HISTORY_SIZE) {
24-
index = 0;
25-
}
21+
index %= HISTORY_SIZE;
2622
}
2723

28-
public double getAzForTime(double t) {
29-
double az = azimuth[index];
30-
int i = index - 1;
31-
int count = 0;
24+
protected void reportError(String message) {
25+
DriverStation.reportError("Looking too far back", false);
26+
}
3227

33-
while (i != index) {
34-
if (i < 0) {
35-
i = HISTORY_SIZE - 1;
36-
}
28+
public double getAzForTime(double t) {
29+
int i = index; // Start just after last entry
3730

31+
do {
32+
// Move to one entry earlier
33+
i = (i + HISTORY_SIZE - 1) % HISTORY_SIZE;
34+
35+
// Check if the time entry is old enough
3836
if (time[i] <= t) {
3937
int prev = (i + 1) % HISTORY_SIZE;
4038
if (prev != index && time[i] >= 0.0) {
4139
// Interpolate between the two closest entries
4240
assert(time[i] < time[prev]);
4341
double factor = (t - time[i]) / (time[prev] - time[i]);
44-
az = azimuth[i] + factor * (azimuth[prev] - azimuth[i]);
42+
return azimuth[i] + factor * (azimuth[prev] - azimuth[i]);
4543
} else {
4644
// Only one (real) entry; no interpolation possible
47-
az = azimuth[i];
45+
return azimuth[i];
4846
}
49-
break;
50-
}
51-
52-
i--;
53-
count++;
54-
if (count > HISTORY_SIZE) {
55-
DriverStation.reportError("Looking too far back", false);
56-
az = azimuth[index];
57-
break;
5847
}
59-
}
48+
} while (i != index);
6049

61-
return az;
50+
// Oldest entry is still newer than the requested timestamp
51+
// Return the oldest entry in the history
52+
reportError("Looking too far back");
53+
return azimuth[index];
6254
}
63-
}
55+
}
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
package org.mayheminc.util;
22

33
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
45
import static org.junit.jupiter.api.Assertions.*;
56

7+
import org.mockito.junit.jupiter.MockitoExtension;
8+
import static org.mockito.Mockito.*;
9+
10+
@ExtendWith(MockitoExtension.class)
611
public class HistoryTest {
712
@Test
813
public void runHistoryTest() {
9-
History hist = new History();
14+
History hist = spy(History.class);
15+
16+
// Stub out the error reporting
17+
doNothing().when(hist).reportError(anyString());
18+
19+
// Returns 0 by default before any data
20+
assertEquals(0.0, hist.getAzForTime(1.1));
21+
assertEquals(0.0, hist.getAzForTime(0.1));
22+
verify(hist, never()).reportError(anyString());
1023

11-
// Returns 0 before any data
12-
assertEquals(0.0, hist.getAzForTime(1.0));
13-
1424
// Add first "real" data
1525
hist.add(1.0, 101.0);
1626
assertEquals(101.0, hist.getAzForTime(1.0));
17-
assertEquals(101.0, hist.getAzForTime(1.5));
18-
assertEquals(0.0, hist.getAzForTime(0.5));
27+
assertEquals(101.0, hist.getAzForTime(2.0));
28+
assertEquals(0.0, hist.getAzForTime(0.1));
29+
verify(hist, never()).reportError(anyString());
1930

2031
// Add second entry and check interpolation
2132
hist.add(2.0, 102.0);
2233
assertEquals(102.0, hist.getAzForTime(2.0));
2334
assertEquals(101.0, hist.getAzForTime(1.0));
2435
assertEquals(101.5, hist.getAzForTime(1.5));
2536
assertEquals(101.8, hist.getAzForTime(1.8));
37+
verify(hist, never()).reportError(anyString());
2638

2739
// Fill history -- depends on the value of HISTORY_SIZE
2840
for (int i=3; i <= 50; i++) {
@@ -31,13 +43,22 @@ public void runHistoryTest() {
3143
assertEquals(150.0, hist.getAzForTime(50));
3244
assertEquals(150.0, hist.getAzForTime(51));
3345
assertEquals(150.0, hist.getAzForTime(100));
46+
assertEquals(101.0, hist.getAzForTime(0.1));
47+
verify(hist, times(1)).reportError(anyString());
3448
assertEquals(101.0, hist.getAzForTime(1.0));
49+
assertEquals(102.0, hist.getAzForTime(2.0));
50+
verify(hist, times(1)).reportError(anyString());
3551

3652
// Overflow history
3753
hist.add(51, 151);
3854
assertEquals(151.0, hist.getAzForTime(51));
3955
assertEquals(151.0, hist.getAzForTime(100));
4056
assertEquals(102.0, hist.getAzForTime(0.1));
57+
verify(hist, times(2)).reportError(anyString());
58+
assertEquals(102.0, hist.getAzForTime(1.0));
59+
verify(hist, times(3)).reportError(anyString());
60+
assertEquals(102.0, hist.getAzForTime(2.0));
4161
assertEquals(150.7, hist.getAzForTime(50.7));
62+
verify(hist, times(3)).reportError(anyString());
4263
}
43-
}
64+
}

0 commit comments

Comments
 (0)