Die pgrep-Zaehlung blockierte sich selbst: wartende Laeufe zaehlten sich gegenseitig mit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Spezifikation messung/innovation/ampel.py (Rechen-Ampel: hoechstens N Plaetze, Wartende zaehlen nicht, tote Besitzer verfallen).
|
|
python3 tests/innovation_ampel_spec.py -v"""
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while _w != "/" and not os.path.isdir(os.path.join(_w, "messung")):
|
|
_w = os.path.dirname(_w)
|
|
sys.path.insert(0, os.path.join(_w, "messung", "innovation"))
|
|
import ampel as A # noqa: E402
|
|
|
|
LEBT = lambda pid: pid < 1000 # noqa: E731 (Testwelt: PIDs unter 1000 leben)
|
|
|
|
|
|
class Ampel(unittest.TestCase):
|
|
def test_hoechstens_n_plaetze(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
a = A.nimm_platz(d, 2, 1, lebt_fn=LEBT); b = A.nimm_platz(d, 2, 2, lebt_fn=LEBT)
|
|
self.assertTrue(a and b and a != b)
|
|
self.assertIsNone(A.nimm_platz(d, 2, 3, lebt_fn=LEBT))
|
|
|
|
def test_freigabe_macht_platz(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
a = A.nimm_platz(d, 1, 1, lebt_fn=LEBT)
|
|
self.assertIsNone(A.nimm_platz(d, 1, 2, lebt_fn=LEBT))
|
|
self.assertTrue(A.gib_frei(a, 1))
|
|
self.assertIsNotNone(A.nimm_platz(d, 1, 2, lebt_fn=LEBT))
|
|
|
|
def test_fremder_gibt_nicht_frei(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
a = A.nimm_platz(d, 1, 1, lebt_fn=LEBT)
|
|
self.assertFalse(A.gib_frei(a, 2))
|
|
self.assertIsNone(A.nimm_platz(d, 1, 2, lebt_fn=LEBT))
|
|
|
|
def test_toter_besitzer_verfaellt(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
A.nimm_platz(d, 1, 5000, lebt_fn=LEBT) # PID 5000 "lebt nicht"
|
|
self.assertIsNotNone(A.nimm_platz(d, 1, 2, lebt_fn=LEBT))
|
|
|
|
def test_platz_frei(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p = os.path.join(d, "platz0.pid")
|
|
self.assertTrue(A.platz_frei(p, LEBT))
|
|
open(p, "w").write("7 x\n")
|
|
self.assertFalse(A.platz_frei(p, LEBT))
|
|
open(p, "w").write("7000 x\n")
|
|
self.assertTrue(A.platz_frei(p, LEBT))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|