|
| 1 | +package com.evsuite.hardware.telemetry |
| 2 | + |
| 3 | +import com.evsuite.hardware.FirmwareInfo |
| 4 | +import org.junit.Assert.assertEquals |
| 5 | +import org.junit.Assert.assertNull |
| 6 | +import org.junit.Assert.assertTrue |
| 7 | +import org.junit.Test |
| 8 | + |
| 9 | +class AdaptiveRangeEstimatorTest { |
| 10 | + private val estimator = AdaptiveRangeEstimator() |
| 11 | + |
| 12 | + @Test fun `cold start is unavailable because consumption history is insufficient`() { |
| 13 | + val estimate = estimator.estimate(snapshot(energyKwh = 40f), null, emptyList()) |
| 14 | + |
| 15 | + assertNull(estimate.value) |
| 16 | + assertEquals(Provenance.UNAVAILABLE, estimate.provenance) |
| 17 | + assertEquals(UnavailableReason.INSUFFICIENT_SAMPLES, estimate.reason) |
| 18 | + } |
| 19 | + |
| 20 | + @Test fun `steady consumption matches the analytic range`() { |
| 21 | + val estimate = estimator.estimate( |
| 22 | + snapshot(energyKwh = 40f), |
| 23 | + trip(20.0, endedAtMs = 3L), |
| 24 | + listOf(trip(20.0, 2L), trip(20.0, 1L)), |
| 25 | + ) |
| 26 | + |
| 27 | + assertEquals(Provenance.ESTIMATED, estimate.provenance) |
| 28 | + assertEquals(200.0, estimate.value!!, 1e-9) |
| 29 | + assertEquals(0.0, estimate.uncertainty!!, 1e-9) |
| 30 | + } |
| 31 | + |
| 32 | + @Test fun `volatile consumption widens the band without moving the estimate`() { |
| 33 | + val steady = estimator.estimate( |
| 34 | + snapshot(energyKwh = 40f), null, |
| 35 | + listOf(trip(20.0, 3L), trip(20.0, 2L), trip(20.0, 1L)), |
| 36 | + ) |
| 37 | + val volatile = estimator.estimate( |
| 38 | + snapshot(energyKwh = 40f), null, |
| 39 | + listOf(trip(10.0, 3L), trip(20.0, 2L), trip(30.0, 1L)), |
| 40 | + ) |
| 41 | + |
| 42 | + assertEquals(steady.value!!, volatile.value!!, 1e-9) |
| 43 | + assertTrue(volatile.uncertainty!! > steady.uncertainty!!) |
| 44 | + assertEquals(100.0, volatile.uncertainty!!, 1e-9) |
| 45 | + } |
| 46 | + |
| 47 | + @Test fun `soc and capacity provide usable energy when direct energy is absent`() { |
| 48 | + val estimate = estimator.estimate( |
| 49 | + snapshot(energyKwh = null, soc = 50f, capacityKwh = 64f), null, |
| 50 | + listOf(trip(16.0, 3L), trip(16.0, 2L), trip(16.0, 1L)), |
| 51 | + ) |
| 52 | + |
| 53 | + assertEquals(200.0, estimate.value!!, 1e-9) |
| 54 | + } |
| 55 | + |
| 56 | + @Test fun `firmware without a current battery power signal never estimates`() { |
| 57 | + val estimate = estimator.estimate( |
| 58 | + snapshot(energyKwh = 40f, powerKw = null), null, |
| 59 | + listOf(trip(20.0, 3L), trip(20.0, 2L), trip(20.0, 1L)), |
| 60 | + UnavailableReason.UNSUPPORTED_FIRMWARE, |
| 61 | + ) |
| 62 | + |
| 63 | + assertNull(estimate.value) |
| 64 | + assertEquals(UnavailableReason.UNSUPPORTED_FIRMWARE, estimate.reason) |
| 65 | + } |
| 66 | + |
| 67 | + @Test fun `only the bounded newest history window affects the fit`() { |
| 68 | + val estimate = AdaptiveRangeEstimator(maxConsumptionSamples = 3).estimate( |
| 69 | + snapshot(energyKwh = 40f), null, |
| 70 | + listOf( |
| 71 | + trip(20.0, 30L), trip(20.0, 20L), trip(20.0, 10L), |
| 72 | + trip(100.0, 1L), |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + assertEquals(200.0, estimate.value!!, 1e-9) |
| 77 | + assertEquals(0.0, estimate.uncertainty!!, 1e-9) |
| 78 | + } |
| 79 | + |
| 80 | + private fun trip(consumption: Double, endedAtMs: Long): EnergyTripSummary { |
| 81 | + val distanceKm = 10.0 |
| 82 | + return EnergyTripSummary( |
| 83 | + startedAtMs = 0L, |
| 84 | + endedAtMs = endedAtMs, |
| 85 | + durationMs = 1_000L, |
| 86 | + distanceKm = distanceKm, |
| 87 | + startSocPercent = null, |
| 88 | + endSocPercent = null, |
| 89 | + consumedKwh = consumption * distanceKm / 100.0, |
| 90 | + regeneratedKwh = 0.0, |
| 91 | + distanceAvailable = true, |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + private fun snapshot( |
| 96 | + energyKwh: Float?, |
| 97 | + soc: Float? = null, |
| 98 | + capacityKwh: Float? = null, |
| 99 | + powerKw: Float? = 10f, |
| 100 | + ) = EnergySnapshot( |
| 101 | + timestampMs = 0L, |
| 102 | + firmware = FirmwareInfo.Gen.SWI68, |
| 103 | + socPercent = soc, |
| 104 | + rangeKm = null, |
| 105 | + speedKmh = 50f, |
| 106 | + batteryPowerKw = powerKw, |
| 107 | + outsideTempCelsius = null, |
| 108 | + cabinTempCelsius = null, |
| 109 | + batteryTempCelsius = null, |
| 110 | + batteryEnergyKwh = energyKwh, |
| 111 | + batteryCapacityKwh = capacityKwh, |
| 112 | + odometerKm = null, |
| 113 | + chargePortConnected = null, |
| 114 | + chargingStatus = null, |
| 115 | + parked = null, |
| 116 | + climate = ClimateSnapshot(null, null, null, null, null, null, null, null, null), |
| 117 | + tirePressures = TirePressureSnapshot(null, null, null, null), |
| 118 | + ) |
| 119 | +} |
0 commit comments