73 lines
3.4 KiB
Nim
73 lines
3.4 KiB
Nim
## Tier A unit tests for the CHIMERA physics mirror (chimera/physics.nim).
|
|
## Run: from ChimeraBot_garage/src, `nimble test` (or
|
|
## `nim c -r ../tests/test_physics.nim`).
|
|
##
|
|
## Ground truth: robocode-tankroyale server `rules/math.kt`. The exact fixtures
|
|
## below are hand-derived from that source, not from our own output.
|
|
|
|
import std/math
|
|
import chimera/physics
|
|
|
|
var failed = 0
|
|
proc check(name: string, cond: bool, detail: string = "") =
|
|
if cond: echo " ok ", name
|
|
else: echo " FAIL ", name, " [", detail, "]"; inc failed
|
|
|
|
proc approx(a, b: float64): bool = abs(a - b) < 1e-9
|
|
|
|
block:
|
|
echo "test 1: stepSpeed truth table (calcNewBotSpeed)"
|
|
check("0 -> target 5 accelerates by 1", approx(stepSpeed(0.0, 5.0), 1.0),
|
|
$stepSpeed(0.0, 5.0))
|
|
check("0 -> target 0.5 snaps to target", approx(stepSpeed(0.0, 0.5), 0.5))
|
|
check("0 -> target -0.5 snaps to target", approx(stepSpeed(0.0, -0.5), -0.5))
|
|
check("3 -> target 8 accelerates by 1", approx(stepSpeed(3.0, 8.0), 4.0))
|
|
check("8 -> target 0.5 decelerates by 2", approx(stepSpeed(8.0, 0.5), 6.0))
|
|
check("8 -> target 7 meets it (diff -1)", approx(stepSpeed(8.0, 7.0), 7.0))
|
|
check("1 -> target 0.5 meets it", approx(stepSpeed(1.0, 0.5), 0.5))
|
|
# crossing-zero branch: v_new = (1 - v/2) * -1, a direct assignment
|
|
check("8 crossing to -5 -> +3", approx(stepSpeed(8.0, -5.0), 3.0))
|
|
check("3 crossing to -5 -> +0.5", approx(stepSpeed(3.0, -5.0), 0.5))
|
|
check("0.5 crossing to -5 -> -0.75", approx(stepSpeed(0.5, -5.0), -0.75))
|
|
check("2 crossing to -5 -> 0.0", approx(stepSpeed(2.0, -5.0), 0.0))
|
|
check("target 0 from 8 stops", approx(stepSpeed(8.0, 0.0), 0.0))
|
|
# odd symmetry handles negative current speeds
|
|
check("-3 -> target -8 accelerates by -1", approx(stepSpeed(-3.0, -8.0), -4.0))
|
|
check("-8 crossing to +5 -> -3", approx(stepSpeed(-8.0, 5.0), -3.0))
|
|
check("clamp at +8", approx(stepSpeed(8.0, 20.0), 8.0))
|
|
check("clamp at -8", approx(stepSpeed(-8.0, -20.0), -8.0))
|
|
|
|
block:
|
|
echo "test 2: maxTurnRate identities (calcMaxTurnRate)"
|
|
check("v=0 -> 10", approx(maxTurnRate(0.0), 10.0))
|
|
check("v=8 -> 4", approx(maxTurnRate(8.0), 4.0))
|
|
check("v=-8 -> 4", approx(maxTurnRate(-8.0), 4.0))
|
|
check("v=4 -> 7", approx(maxTurnRate(4.0), 7.0))
|
|
check("clamped above max speed", approx(maxTurnRate(100.0), 4.0))
|
|
check("limitTurnRate clamps", approx(limitTurnRate(50.0, 8.0), 4.0))
|
|
|
|
block:
|
|
echo "test 3: bullet speed/damage/heat fixtures"
|
|
check("bulletSpeed(0.1)=19.7", approx(bulletSpeed(0.1), 19.7))
|
|
check("bulletSpeed(1)=17", approx(bulletSpeed(1.0), 17.0))
|
|
check("bulletSpeed(3)=11", approx(bulletSpeed(3.0), 11.0))
|
|
check("bulletSpeed clamps p>3", approx(bulletSpeed(5.0), 11.0))
|
|
check("bulletDamage(1)=4", approx(bulletDamage(1.0), 4.0))
|
|
check("bulletDamage(2)=10", approx(bulletDamage(2.0), 10.0))
|
|
check("bulletDamage(3)=16", approx(bulletDamage(3.0), 16.0))
|
|
check("bulletDamage(0.5)=2", approx(bulletDamage(0.5), 2.0))
|
|
check("gunHeat(1)=1.2", approx(gunHeat(1.0), 1.2))
|
|
check("gunHeat(3)=1.6", approx(gunHeat(3.0), 1.6))
|
|
|
|
block:
|
|
echo "test 4: angles"
|
|
check("wrap180(370)=10", approx(wrap180(370.0), 10.0))
|
|
check("wrap180(-190)=170", approx(wrap180(-190.0), 170.0))
|
|
check("wrap180(180)=180", approx(wrap180(180.0), 180.0))
|
|
check("bearing east", approx(bearingTo(0.0, 0.0, 1.0, 0.0), 0.0))
|
|
check("bearing north", approx(bearingTo(0.0, 0.0, 0.0, 1.0), 90.0))
|
|
|
|
echo ""
|
|
if failed == 0: echo "all physics tests passed"
|
|
else: echo failed, " physics test(s) FAILED"; quit(1)
|