163 lines
6.9 KiB
Python
163 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Maus: der Weg zum Knopf sieht nach Hand aus, nicht nach Sprung (hand/maus.py).
|
|
|
|
Läuft ohne Mac - geprüft werden die reinen Funktionen, die hand/click.py abspielt.
|
|
Gegenprobe zu jedem Merkmal ist der alte Klick: Sprung auf die Knopfmitte, drei
|
|
Ereignisse im Abstand von 30 ms.
|
|
|
|
python3 tests/maus_spec.py -v
|
|
"""
|
|
import math
|
|
import os
|
|
import random
|
|
import statistics
|
|
import sys
|
|
import unittest
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while _w != "/" and not os.path.isdir(os.path.join(_w, "lesen")):
|
|
_w = os.path.dirname(_w)
|
|
sys.path.insert(0, os.path.join(_w, "hand"))
|
|
sys.path.insert(0, os.path.join(_w, "client"))
|
|
|
|
import maus
|
|
|
|
FENSTER = (100.0, 80.0, 1200.0, 860.0) # x, y, Breite, Höhe
|
|
CALL = (100 + 1200 * 0.796, 80 + 860 * 0.943) # Mitte des Call-Knopfs wie in click.SPOTS
|
|
|
|
|
|
def abstand_zur_geraden(p, a, b):
|
|
(x, y), (ax, ay), (bx, by) = p, a, b
|
|
return abs((bx - ax) * (ay - y) - (ax - x) * (by - ay)) / math.hypot(bx - ax, by - ay)
|
|
|
|
|
|
class Treffpunkt(unittest.TestCase):
|
|
def test_immer_auf_dem_knopf_nie_zweimal_derselbe_punkt(self):
|
|
rng = random.Random(1)
|
|
halb = maus.halbe_groesse("call", FENSTER)
|
|
punkte = [maus.treffpunkt(CALL, halb, rng) for _ in range(3000)]
|
|
for x, y in punkte:
|
|
self.assertLessEqual(abs(x - CALL[0]), halb[0] * 0.8 + 1e-9)
|
|
self.assertLessEqual(abs(y - CALL[1]), halb[1] * 0.8 + 1e-9)
|
|
self.assertEqual(len(set(punkte)), len(punkte))
|
|
# gestreut, aber um die Mitte: die meisten nah dran, nicht gleichverteilt
|
|
nah = sum(abs(x - CALL[0]) < halb[0] * 0.4 for x, _ in punkte) / len(punkte)
|
|
self.assertGreater(nah, 0.6)
|
|
self.assertGreater(statistics.pstdev([x for x, _ in punkte]), halb[0] * 0.2)
|
|
|
|
def test_kleine_knoepfe_streuen_enger(self):
|
|
self.assertLess(maus.halbe_groesse("size33", FENSTER)[0], maus.halbe_groesse("raise", FENSTER)[0])
|
|
|
|
|
|
class Weg(unittest.TestCase):
|
|
def wege(self, start, n=300, seed=2):
|
|
rng = random.Random(seed)
|
|
return [maus.weg(start, maus.treffpunkt(CALL, maus.halbe_groesse("call", FENSTER), rng),
|
|
2 * maus.halbe_groesse("call", FENSTER)[0], rng) for _ in range(n)]
|
|
|
|
def test_beginnt_am_zeiger_endet_genau_im_ziel(self):
|
|
rng = random.Random(3)
|
|
start = (300.0, 200.0)
|
|
ziel = maus.treffpunkt(CALL, maus.halbe_groesse("call", FENSTER), rng)
|
|
w = maus.weg(start, ziel, 96.0, rng)
|
|
self.assertEqual(w[0][1:], start)
|
|
self.assertEqual(w[-1][1:], ziel)
|
|
|
|
def test_gleicher_seed_gleicher_weg(self):
|
|
a = maus.plan((300, 200), CALL, (48, 13), random.Random(7))
|
|
b = maus.plan((300, 200), CALL, (48, 13), random.Random(7))
|
|
c = maus.plan((300, 200), CALL, (48, 13), random.Random(8))
|
|
self.assertEqual(a, b)
|
|
self.assertNotEqual(a, c)
|
|
|
|
def test_viele_kleine_schritte_im_takt_einer_maus(self):
|
|
for w in self.wege((300.0, 200.0)):
|
|
self.assertGreater(len(w), 8) # der alte Klick: ein Sprung
|
|
zeiten = [ms for ms, _, _ in w]
|
|
luecken = [b - a for a, b in zip(zeiten, zeiten[1:])]
|
|
self.assertTrue(all(0 < l <= maus.STOCKEN_MS[1] + maus.SCHRITT_MS[1] + 1e-9 for l in luecken), luecken)
|
|
|
|
def test_gekruemmt_nicht_gerade(self):
|
|
start = (300.0, 200.0)
|
|
krumm = 0
|
|
for w in self.wege(start):
|
|
ziel = w[-1][1:]
|
|
weite = max(abstand_zur_geraden(p[1:], start, ziel) for p in w)
|
|
krumm += weite > 0.03 * math.dist(start, ziel)
|
|
self.assertGreater(krumm, 0.9 * 300)
|
|
|
|
def test_langsam_schnell_langsam(self):
|
|
alt, maus.STOCKEN_RATE = maus.STOCKEN_RATE, 0.0 # ohne Stocken: reines Tempo-Profil
|
|
try:
|
|
self._langsam_schnell_langsam()
|
|
finally:
|
|
maus.STOCKEN_RATE = alt
|
|
|
|
def _langsam_schnell_langsam(self):
|
|
start = (300.0, 200.0)
|
|
for w in self.wege(start, n=100):
|
|
if max(math.dist(p[1:], w[-1][1:]) for p in w) > math.dist(start, w[-1][1:]) * 1.02:
|
|
continue # Überschuss: eigenes Profil
|
|
tempo = [(math.dist(a[1:], b[1:]) / (b[0] - a[0]), a[0])
|
|
for a, b in zip(w, w[1:]) if b[0] > a[0] and math.dist(a[1:], b[1:]) > 0]
|
|
spitze_ms = max(tempo)[1]
|
|
self.assertTrue(0.2 * w[-1][0] < spitze_ms < 0.8 * w[-1][0], (spitze_ms, w[-1][0]))
|
|
self.assertLess(tempo[0][0], max(tempo)[0] * 0.5)
|
|
|
|
def test_weiter_dauert_laenger(self):
|
|
nah = statistics.mean(w[-1][0] for w in self.wege((CALL[0] - 120, CALL[1] - 40)))
|
|
fern = statistics.mean(w[-1][0] for w in self.wege((CALL[0] - 900, CALL[1] - 600)))
|
|
self.assertGreater(fern, nah * 1.3)
|
|
for w in self.wege((CALL[0] - 900, CALL[1] - 600)):
|
|
self.assertLessEqual(w[-1][0], 2 * maus.DAUER_S[1] * 1000 + 50)
|
|
|
|
def test_ueberschuss_nur_bei_langen_wegen_und_nur_manchmal(self):
|
|
def ueber(start):
|
|
n = 0
|
|
for w in self.wege(start, n=400, seed=5):
|
|
ziel = w[-1][1:]
|
|
n += max(math.dist(start, p[1:]) for p in w) > math.dist(start, ziel) + 3
|
|
return n / 400
|
|
self.assertEqual(ueber((CALL[0] - 60, CALL[1] - 20)), 0.0)
|
|
self.assertTrue(0.05 < ueber((CALL[0] - 900, CALL[1] - 600)) < 0.35)
|
|
|
|
|
|
class Druck(unittest.TestCase):
|
|
def test_halten_und_verweilen_gestreut_in_grenzen(self):
|
|
rng = random.Random(4)
|
|
werte = [maus.druck(rng) for _ in range(2000)]
|
|
halten = [h for _, h in werte]
|
|
self.assertTrue(all(maus.HALTEN_MS[0] <= h <= maus.HALTEN_MS[1] for h in halten))
|
|
self.assertTrue(all(maus.VERWEILEN_MS[0] <= v <= maus.VERWEILEN_MS[1] for v, _ in werte))
|
|
self.assertGreater(statistics.pstdev(halten), 10.0) # der alte Klick: immer 30 ms
|
|
self.assertTrue(60 < statistics.median(halten) < 110)
|
|
|
|
|
|
class Stocken(unittest.TestCase):
|
|
"""Menschen halten mitten im Weg an - der Weg darf einmal stehen bleiben, endet aber genau."""
|
|
|
|
def setUp(self):
|
|
self._alt = maus.STOCKEN_RATE
|
|
maus.STOCKEN_RATE = 1.0
|
|
|
|
def tearDown(self):
|
|
maus.STOCKEN_RATE = self._alt
|
|
|
|
def test_stocken_haelt_den_punkt_und_endet_auf_dem_ziel(self):
|
|
w = maus.weg((0.0, 0.0), (400.0, 300.0), 40.0, random.Random(3))
|
|
gleich = [(a, b) for a, b in zip(w, w[1:]) if a[1:] == b[1:] and b[0] - a[0] > 30.0]
|
|
self.assertTrue(gleich, "kein Stocken im Weg")
|
|
self.assertEqual(w[-1][1:], (400.0, 300.0))
|
|
# nach dem Stocken wird es wieder schnell - die Spitze liegt dahinter
|
|
a, b = gleich[0]
|
|
spitze = max(zip(w, w[1:]), key=lambda p: math.dist(p[0][1:], p[1][1:]) / max(1e-9, p[1][0] - p[0][0]))
|
|
self.assertGreaterEqual(spitze[0][0], b[0])
|
|
|
|
def test_zeit_laeuft_vorwaerts(self):
|
|
w = maus.weg((0.0, 0.0), (250.0, 100.0), 40.0, random.Random(4))
|
|
self.assertTrue(all(b[0] >= a[0] for a, b in zip(w, w[1:])))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|