Files
mg4/app/src/test/java/com/evsuite/abrp/RangeEstimatorTest.java
T
nilsandClaude Opus 5.5 2a0c4c9675 App 2.10.0: range since start, usage in its own cards
"Range since start" replaces the range at current power: remaining
energy over the consumption since the car was switched on, standing
with the climate on included. A trip starts again after 10 minutes
without samples (the car was off) or when charging.

The kWh/100 km for both estimates moved out of the range tiles into
two cards of their own, directly below them; the overview still fits
the full-screen 1920x720 display without scrolling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QLJfZQZeAKAwSqViTmLzki
2026-10-18 10:01:11 +02:00

116 lines
5.0 KiB
Java

package com.evsuite.abrp;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/** Range estimates from power and speed — pure JVM. */
public class RangeEstimatorTest {
private static final double USABLE = 50.8;
@Test
public void remainingEnergyIsSocTimesUsableCapacity() {
assertEquals(25.4, RangeEstimator.remainingKwh(50f, USABLE), 1e-9);
assertEquals(50.8, RangeEstimator.remainingKwh(120f, USABLE), 1e-9); // clamped
assertNull(RangeEstimator.remainingKwh(null, USABLE));
}
@Test
public void sinceStartCoversTheWholeTripNotJustFiveMinutes() {
RangeEstimator r = new RangeEstimator();
// 10 minutes at 30 kW then 10 minutes at 10 kW, both at 100 km/h: 20 kWh/100 km overall.
for (long t = 0; t <= 600_000; t += 15_000) r.add(t, 30f, 100f);
for (long t = 615_000; t <= 1_200_000; t += 15_000) r.add(t, 10f, 100f);
RangeEstimator.Estimate trip = r.sinceStart(25.4);
assertNotNull(trip);
assertEquals(20.0, trip.kwhPer100Km, 0.2); // plus the 15 s step between the two
assertEquals(127.0, trip.rangeKm, 1.0);
assertEquals(33.3, trip.distanceKm, 0.1);
// The 5-minute average only sees the economical end.
assertEquals(10.0, r.average(25.4, 1_200_000).kwhPer100Km, 0.1);
}
@Test
public void standingWithTheClimateOnCountsTowardsTheTrip() {
RangeEstimator r = new RangeEstimator();
for (long t = 0; t <= 120_000; t += 15_000) r.add(t, 3f, 0f); // warming up
for (long t = 135_000; t <= 735_000; t += 15_000) r.add(t, 15f, 60f);
assertTrue(r.sinceStart(25.4).kwhPer100Km > 15.0);
}
@Test
public void aLongGapOrChargingStartsANewTrip() {
RangeEstimator r = new RangeEstimator();
for (long t = 0; t <= 600_000; t += 15_000) r.add(t, 30f, 100f);
long later = 600_000 + RangeEstimator.TRIP_RESET_GAP_MS + 1; // the car was off
for (long t = later; t <= later + 600_000; t += 15_000) r.add(t, 10f, 100f);
assertEquals(10.0, r.sinceStart(25.4).kwhPer100Km, 0.1);
long charge = later + 615_000;
r.add(charge, -7f, 0f); // plugged in
assertNull(r.sinceStart(25.4));
}
@Test
public void noSinceStartBeforeAnyDistanceOrWithoutSoc() {
RangeEstimator r = new RangeEstimator();
assertNull(r.sinceStart(25.4));
for (long t = 0; t <= 60_000; t += 15_000) r.add(t, 2f, 0f);
assertNull(r.sinceStart(25.4)); // not moved yet
for (long t = 75_000; t <= 300_000; t += 15_000) r.add(t, 15f, 60f);
assertNull(r.sinceStart(null));
}
@Test
public void averageIntegratesEnergyOverDistance() {
RangeEstimator r = new RangeEstimator();
// 4 minutes at a steady 20 kW and 100 km/h, sampled every 2 s: 20 kWh/100 km.
for (long t = 0; t <= 240_000; t += 2_000) r.add(t, 20f, 100f);
RangeEstimator.Estimate e = r.average(25.4, 240_000);
assertNotNull(e);
assertEquals(20.0, e.kwhPer100Km, 1e-6);
assertEquals(127.0, e.rangeKm, 1e-6);
}
@Test
public void averageMixesTheServiceAndScreenRates() {
RangeEstimator r = new RangeEstimator();
// A slow 15 s service tick for two minutes, then the 2 s screen tick: still 20 kWh/100 km.
for (long t = 0; t < 120_000; t += 15_000) r.add(t, 20f, 100f);
for (long t = 120_000; t <= 240_000; t += 2_000) r.add(t, 20f, 100f);
assertEquals(20.0, r.average(25.4, 240_000).kwhPer100Km, 1e-6);
}
@Test
public void averageOnlyCoversTheLastFiveMinutes() {
RangeEstimator r = new RangeEstimator();
for (long t = 0; t <= 300_000; t += 2_000) r.add(t, 40f, 100f); // thirsty start
for (long t = 302_000; t <= 900_000; t += 2_000) r.add(t, 10f, 100f); // then economical
assertEquals(10.0, r.average(25.4, 900_000).kwhPer100Km, 1e-6);
}
@Test
public void noAverageWithTooLittleDataOrDistanceOrAcrossAGap() {
RangeEstimator r = new RangeEstimator();
for (long t = 0; t <= 30_000; t += 2_000) r.add(t, 20f, 100f);
assertNull("30 s is too little", r.average(25.4, 30_000));
RangeEstimator parked = new RangeEstimator();
for (long t = 0; t <= 240_000; t += 2_000) parked.add(t, 1f, 0f);
assertNull("no distance", parked.average(25.4, 240_000));
RangeEstimator gap = new RangeEstimator();
gap.add(0, 20f, 100f);
gap.add(200_000, 20f, 100f); // the app was closed in between
assertNull("a gap is not integrated", gap.average(25.4, 200_000));
RangeEstimator downhill = new RangeEstimator();
for (long t = 0; t <= 240_000; t += 2_000) downhill.add(t, -5f, 80f);
assertNull("net regeneration", downhill.average(25.4, 240_000));
}
}