261 lines
4.5 KiB
Python
261 lines
4.5 KiB
Python
from app.auto.co2_policy import (
|
|
Co2SpeedPolicy,
|
|
)
|
|
|
|
|
|
def main():
|
|
policy = Co2SpeedPolicy(
|
|
base_speed=1,
|
|
thresholds=(
|
|
(800, 2),
|
|
(1000, 3),
|
|
(1300, 4),
|
|
(1600, 5),
|
|
(2000, 6),
|
|
),
|
|
hysteresis=100,
|
|
)
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print("TEST 1 — INITIAL SPEED")
|
|
print("=" * 70)
|
|
|
|
cases = (
|
|
(500, 1),
|
|
(799, 1),
|
|
(800, 2),
|
|
(999, 2),
|
|
(1000, 3),
|
|
(1299, 3),
|
|
(1300, 4),
|
|
(1599, 4),
|
|
(1600, 5),
|
|
(1999, 5),
|
|
(2000, 6),
|
|
(2500, 6),
|
|
)
|
|
|
|
for co2, expected in cases:
|
|
|
|
result = policy.select_speed(
|
|
co2,
|
|
current_speed=None,
|
|
)
|
|
|
|
print(
|
|
f"CO2={co2:<4} "
|
|
f"-> speed={result}"
|
|
)
|
|
|
|
assert result == expected
|
|
|
|
|
|
# ========================================================
|
|
# TEST 2
|
|
#
|
|
# Переход 1 -> 2 при 800 ppm.
|
|
# ========================================================
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print("TEST 2 — SPEED UP")
|
|
print("=" * 70)
|
|
|
|
speed = 1
|
|
|
|
speed = policy.select_speed(
|
|
790,
|
|
speed,
|
|
)
|
|
|
|
assert speed == 1
|
|
|
|
speed = policy.select_speed(
|
|
805,
|
|
speed,
|
|
)
|
|
|
|
assert speed == 2
|
|
|
|
print(
|
|
"790 -> speed 1"
|
|
)
|
|
|
|
print(
|
|
"805 -> speed 2"
|
|
)
|
|
|
|
|
|
# ========================================================
|
|
# TEST 3
|
|
#
|
|
# CO2 немного упал ниже 800.
|
|
#
|
|
# Без гистерезиса получили бы:
|
|
# 2 -> 1.
|
|
#
|
|
# Но пока CO2 > 700,
|
|
# остаёмся на второй скорости.
|
|
# ========================================================
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print("TEST 3 — HYSTERESIS")
|
|
print("=" * 70)
|
|
|
|
speed = policy.select_speed(
|
|
790,
|
|
current_speed=2,
|
|
)
|
|
|
|
print(
|
|
"speed=2, CO2=790 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
assert speed == 2
|
|
|
|
speed = policy.select_speed(
|
|
750,
|
|
current_speed=2,
|
|
)
|
|
|
|
print(
|
|
"speed=2, CO2=750 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
assert speed == 2
|
|
|
|
speed = policy.select_speed(
|
|
700,
|
|
current_speed=2,
|
|
)
|
|
|
|
print(
|
|
"speed=2, CO2=700 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
assert speed == 1
|
|
|
|
|
|
# ========================================================
|
|
# TEST 4
|
|
#
|
|
# Гистерезис между speed 2 и speed 3.
|
|
#
|
|
# Вверх:
|
|
# 1000 ppm.
|
|
#
|
|
# Вниз:
|
|
# 900 ppm.
|
|
# ========================================================
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print("TEST 4 — SPEED 2 / SPEED 3")
|
|
print("=" * 70)
|
|
|
|
speed = policy.select_speed(
|
|
1005,
|
|
current_speed=2,
|
|
)
|
|
|
|
assert speed == 3
|
|
|
|
print(
|
|
"speed=2, CO2=1005 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
speed = policy.select_speed(
|
|
950,
|
|
current_speed=3,
|
|
)
|
|
|
|
assert speed == 3
|
|
|
|
print(
|
|
"speed=3, CO2=950 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
speed = policy.select_speed(
|
|
900,
|
|
current_speed=3,
|
|
)
|
|
|
|
assert speed == 2
|
|
|
|
print(
|
|
"speed=3, CO2=900 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
|
|
# ========================================================
|
|
# TEST 5
|
|
#
|
|
# Резкий рост CO2.
|
|
#
|
|
# Не нужно ждать:
|
|
# 1 -> 2 -> 3 -> 4 -> 5
|
|
#
|
|
# Можно сразу выбрать необходимую скорость.
|
|
# ========================================================
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print("TEST 5 — LARGE CO2 JUMP")
|
|
print("=" * 70)
|
|
|
|
speed = policy.select_speed(
|
|
2200,
|
|
current_speed=1,
|
|
)
|
|
|
|
print(
|
|
"speed=1, CO2=2200 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
assert speed == 6
|
|
|
|
|
|
# ========================================================
|
|
# TEST 6
|
|
#
|
|
# Аналогично при сильном падении CO2
|
|
# разрешаем сразу снизить несколько ступеней.
|
|
# ========================================================
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print("TEST 6 — LARGE CO2 DROP")
|
|
print("=" * 70)
|
|
|
|
speed = policy.select_speed(
|
|
650,
|
|
current_speed=6,
|
|
)
|
|
|
|
print(
|
|
"speed=6, CO2=650 "
|
|
f"-> speed={speed}"
|
|
)
|
|
|
|
assert speed == 1
|
|
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print(
|
|
"ALL CO2 POLICY TESTS PASSED"
|
|
)
|
|
print("=" * 70)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |