362 lines
17 KiB
Python
362 lines
17 KiB
Python
"""EV-69 T21: the fuzz for the carry (N1-N9 of reports/EV-69-test-plan.md), 10
|
|
seeds x 1200 s for Solar and for Manual, both cycle orders.
|
|
`python3 -B test_ev69_fuzz.py`
|
|
|
|
Writer level (`tools/ev69_kit.py`: `SolarWriter` / `ManualWriter` on a stub
|
|
driver). Random: clamp steps, a peak cut cap, refused and raised writes (0 A
|
|
and the select are taken), phase changes begun by the plan (Solar) or the user
|
|
(Manual), late takes of ANY value the writer tried, foreign values (some equal
|
|
to a tried value), presses, unknown and stale reads, the charger gone and
|
|
back. Order A applies a cycle's events before its step, order B after it (the
|
|
event is read one cycle later). The phase gap is shortened (as test_ev22_fuzz
|
|
does) so a run holds many changes.
|
|
|
|
Every cycle checks, by an oracle that does not read the carry:
|
|
- N1: after a phase change ends without its value taken (MAX_TRIES, or
|
|
Solar's cap end), while nothing has ended the bound (a write, a press, a
|
|
hold or count, a return, a take of what the writer sent or wants), the
|
|
charger showing a value the change tried gives no mismatch and no hold.
|
|
- N1b: once a bound has ended (an own take, a new own write, a user change, a
|
|
hold, Solar's return) a late take of an old value is another writer's: the
|
|
fuzz shows it for WRITER_S + 3 s with nothing else happening and asserts
|
|
the hold (skipped when no own value is left to judge against: the EV-22
|
|
start-state blind spot).
|
|
- N2: no non-zero write while a count runs (Solar) or a hold stands.
|
|
- N4: no carry under a hold, after a press, or after a write that is not
|
|
itself followed by an end.
|
|
- N5: no write above the clamp or the cap, none in 0-6 A.
|
|
- N6: at most MAX_TRIES cut tries per quarter key.
|
|
- N7: no 0 A written under a hold.
|
|
- N8: a select only at 0 A.
|
|
- N9: at most one CHARGER_OTHER (Solar) / one INFO line (Manual) per hold.
|
|
N3 (the cut never waits on a false hold) follows from N1 and the rows T1, T5,
|
|
T19. The plan's floors are asserted per writer / per bound end, so a fuzz that
|
|
never reaches a branch fails.
|
|
|
|
Mutating a module this file grades? Read MUTATION.md first (purge __pycache__).
|
|
"""
|
|
|
|
import os
|
|
import random
|
|
import sys
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, os.path.join(HERE, "tools"))
|
|
|
|
import ev69_kit as K # noqa: E402
|
|
from app import carmode as CM # noqa: E402
|
|
|
|
CM.PHASE_GAP_S = 40.0 # many changes in 1200 s (test_ev22_fuzz does too)
|
|
SEEDS = int(os.environ.get("EV69_FUZZ_SEEDS", "10"))
|
|
STEPS = int(os.environ.get("EV69_FUZZ_STEPS", "1200"))
|
|
COUNT = {}
|
|
CAUSES = ("take", "write", "user", "hold", "return")
|
|
|
|
|
|
def bump(k, n=1):
|
|
COUNT[k] = COUNT.get(k, 0) + n
|
|
|
|
|
|
def fuzz(kind, seed):
|
|
rnd = random.Random(seed * 7 + (0 if kind == "solar" else 1))
|
|
order_b = seed % 2 == 1
|
|
st = {"a": rnd.choice([8.0, 10.0, 12.0]), "n": 1, "rev": 1,
|
|
"clamp": 16.0}
|
|
if kind == "solar":
|
|
w = K.swriter(8.0, ph=1)
|
|
w.cut_key = 0
|
|
else:
|
|
w = CM.ManualWriter()
|
|
mode = {"refuse": 1.0 if kind == "solar" else 0.97, "raise": 0.0}
|
|
|
|
def take(a, d):
|
|
if a == 0.0:
|
|
return True
|
|
if rnd.random() < mode["raise"]:
|
|
return "raise"
|
|
return rnd.random() >= mode["refuse"]
|
|
drv = K.Drv(8.0 if kind == "solar" else st["a"], take, ph=1)
|
|
run = K.Run(w, drv, kind=kind, st=st)
|
|
bad = []
|
|
s = dict(armed=False, vals=[], seq_own=[], hold_rises=0, press_t=-1,
|
|
prev_hold=False, was_gone=False, gone=False, tried=set(),
|
|
kind=None, cap_counted=False, seq_n0=0, pend=None, inj=None, end_t=-9999)
|
|
clamp = {"v": 16.0}
|
|
|
|
def ev(t):
|
|
if s["inj"] is not None: # a directed old-value take
|
|
drv.cur = s["inj"]["v"]
|
|
return
|
|
closed = w.seq is not None and (kind == "manual" or [
|
|
1 for tt, a in drv.calls[s["seq_n0"]:] if a > 0])
|
|
if rnd.random() < (0.4 if closed else 0.01):
|
|
clamp["v"] = rnd.choice([16.0, 14.0, 12.0, 10.0, 8.0, 7.0, 6.0])
|
|
st["clamp"] = clamp["v"]
|
|
if kind == "solar" and rnd.random() < 0.01:
|
|
K.set_cap(w, rnd.choice([None, 6.0, 6.0, 7.0]))
|
|
if kind == "solar" and w.seq is not None and w.cut_a is None \
|
|
and [1 for tt, a in drv.calls[s["seq_n0"]:] if a > 0] \
|
|
and rnd.random() < 0.4:
|
|
# the EV-60 cap end shape: a close went, then the cap comes, the
|
|
# earlier cut tries of this key being used up
|
|
K.set_cap(w, 6.0)
|
|
w.cut_fails, w.cut_fail_at = max(w.cut_fails, 2), -100.0
|
|
if w.hold and rnd.random() < 0.03:
|
|
w.user_changed() # a press lifts a hold
|
|
if kind == "manual":
|
|
st["rev"] += 1
|
|
s["press_t"] = t
|
|
bump("press")
|
|
if kind == "solar" and not s["armed"] and w.seq is None \
|
|
and (w.gave_up or w.fail_at is not None) \
|
|
and rnd.random() < 0.05:
|
|
w.user_changed() # a press lifts the give-up
|
|
s["press_t"] = t
|
|
bump("press")
|
|
if rnd.random() < 0.004:
|
|
mode["refuse"] = rnd.choice([0.0, 0.5, 0.97, 1.0, 1.0, 1.0]
|
|
if kind == "manual" else
|
|
[0.97, 1.0, 1.0, 1.0, 1.0, 1.0])
|
|
mode["raise"] = rnd.choice([0.0, 0.0, 0.3])
|
|
# a phase change: Solar's plan decides it, Manual's user asks for it
|
|
if w.seq is None and rnd.random() < (0.1 if kind == "solar" else 0.03):
|
|
n_to = 3 if drv.ph == 1 else 1
|
|
if kind == "solar":
|
|
if not w.plan.switch:
|
|
w.plan.n = drv.ph # the plan believes the phases read
|
|
w.plan._set(rnd.choice([10.0, 12.0, 14.0]), float(t), n_to)
|
|
clamp["v"] = st["clamp"] = 16.0 # it falls in the change
|
|
K.set_cap(w, None) # a cap comes later
|
|
w.cut_fails = 0
|
|
else:
|
|
w.user_changed()
|
|
st["rev"] += 1
|
|
st["n"] = n_to
|
|
st["a"] = rnd.choice([8.0, 10.0, 12.0, 14.0])
|
|
s["press_t"] = t
|
|
# a late take of any value the writer tried (the ended change's
|
|
# values first)
|
|
if rnd.random() < (0.0 if (kind == "solar" and w.seq is not None)
|
|
else 0.08):
|
|
pool = [v for v in s["vals"] if v > 0] if (
|
|
s["armed"] and rnd.random() < 0.6) else sorted(
|
|
v for v in s["tried"] if v > 0)
|
|
if pool:
|
|
v = rnd.choice(pool)
|
|
drv.cur = v
|
|
bump("late_take")
|
|
if s["armed"] and v in s["vals"]:
|
|
bump("late_take_ended_val")
|
|
if v != s["vals"][-1]:
|
|
bump(f"late_nonlast_{kind}")
|
|
calm = kind == "solar" and w.seq is not None # let a change run
|
|
if rnd.random() < (0.0 if calm else 0.005):
|
|
drv.cur = rnd.choice([5.0, 7.0, 9.0, 11.0, 13.0, 15.0]
|
|
+ sorted(s["tried"]))
|
|
bump("foreign")
|
|
if rnd.random() < 0.005:
|
|
w.user_changed()
|
|
if kind == "manual":
|
|
st["rev"] += 1
|
|
s["press_t"] = t
|
|
bump("press")
|
|
if rnd.random() < (0.0 if calm else 0.01):
|
|
drv.cur = 0.0
|
|
s["gone"] = s["gone"] and rnd.random() < 0.8 or (
|
|
not s["gone"] and rnd.random() < 0.003)
|
|
if kind == "solar" and s["armed"] and rnd.random() < 0.08:
|
|
s["gone"] = True # a charger return inside the bound
|
|
|
|
for t in range(STEPS):
|
|
if not order_b:
|
|
ev(t)
|
|
inj = s["inj"]
|
|
gone = False if inj else s["gone"]
|
|
unk = False if inj else rnd.random() < 0.02
|
|
stale = False if inj else rnd.random() < 0.02
|
|
n0, sel0 = len(drv.calls), len(drv.sel)
|
|
before_seq = w.seq is not None
|
|
if before_seq:
|
|
s["seq_own"] = list(w.seq.get("own", ()))
|
|
drv.now = float(t)
|
|
K.CLOCK["t"] = t
|
|
if hasattr(w, "cut_quarter"):
|
|
w.cut_quarter(int(t // 900))
|
|
seen = None if unk else drv.cur
|
|
rd = K.Rd(seen, drv.ph, power=0.0 if drv.cur == 0 else 1840.0)
|
|
rd.available = not gone
|
|
run.trk.obs_at = (w.last_done if (stale and w.last_done is not None)
|
|
else None)
|
|
cl = clamp["v"]
|
|
if kind == "solar":
|
|
K.LOOP.run_until_complete(w.step(
|
|
drv, rd, mode=CM.SOLAR, sun=CM.SunInputs(target_w=2000.0),
|
|
clamp=cl, now=float(t), tracker=run.trk, writing=True,
|
|
status="charging"))
|
|
else:
|
|
K.LOOP.run_until_complete(w.step(
|
|
drv, rd, mode=CM.MANUAL, want_a=st["a"],
|
|
want_phases=st["n"], rev=st["rev"], clamp=cl, now=float(t),
|
|
tracker=run.trk, writing=True, status="charging"))
|
|
new = drv.calls[n0:]
|
|
wrote = bool(new)
|
|
press_now = s["press_t"] == t
|
|
for tt, a in new:
|
|
if a > 0:
|
|
s["tried"].add(a)
|
|
if a > 0 and (w.hold or (kind == "solar"
|
|
and w.mismatch_since is not None)):
|
|
# judged after the step's own watch (a count a read cleared
|
|
# in this step is no count when the write goes out); Manual
|
|
# writes through a count and stops at the hold (EV-22)
|
|
bad.append((kind, seed, t, "N2 write while a count/hold "
|
|
"stood", a))
|
|
if a == 0.0 and w.hold:
|
|
bad.append((kind, seed, t, "N7 0 A under a hold"))
|
|
cutv = getattr(w, "cut_a", None)
|
|
if a > cl + 1e-9 and not (cutv is not None and a <= cutv + 1e-9):
|
|
bad.append((kind, seed, t, "N5 above the clamp", a, cl))
|
|
if 0 < a < CM.CH.MIN_A - 1e-9:
|
|
bad.append((kind, seed, t, "N5 in 0-6 A", a))
|
|
if getattr(w, "cut_fails", 0) > CM.MAX_TRIES:
|
|
bad.append((kind, seed, t, "N6 cut tries over the cap"))
|
|
for tt, n_, cur_ in drv.sel[sel0:]:
|
|
if cur_ > CM.TOL_A:
|
|
bad.append((kind, seed, t, "N8 select over load", cur_))
|
|
if w.hold and not s["prev_hold"]:
|
|
s["hold_rises"] += 1
|
|
bump("hold")
|
|
if t - s["end_t"] <= 300:
|
|
bump("foreign_hold_after_end")
|
|
s["prev_hold"] = w.hold
|
|
lines = len([1 for _t, lv, m in K.LOG
|
|
if (kind == "solar" and K.OTHER in m)
|
|
or (kind == "manual" and K.SOMETHING in m)])
|
|
if lines > s["hold_rises"]:
|
|
bad.append((kind, seed, t, "N9 more lines than holds", lines,
|
|
s["hold_rises"]))
|
|
ended_now = before_seq and w.seq is None
|
|
if not before_seq and w.seq is not None:
|
|
bump(f"seq_start_{kind}")
|
|
s["seq_n0"] = len(drv.calls)
|
|
if ended_now:
|
|
tk = (kind == "solar" and w.taken) or (
|
|
kind == "manual" and w.last_a is not None)
|
|
bump(f"seq_end_{kind}_{'taken' if tk else 'refused'}")
|
|
if w.hold and w._carry:
|
|
bad.append((kind, seed, t, "N4 carry under a hold", w._carry))
|
|
if press_now and w._carry and not ended_now:
|
|
bad.append((kind, seed, t, "N4 carry after a press", w._carry))
|
|
if wrote and w._carry and not ended_now:
|
|
bad.append((kind, seed, t, "N4 carry after a write", w._carry))
|
|
# the directed old-value take (N1b)
|
|
if inj is not None:
|
|
inj["n"] += 1
|
|
if wrote or press_now or w.seq is not None \
|
|
or drv.cur != inj["v"] or inj["n"] > 60:
|
|
s["inj"] = None # something else happened: no verdict
|
|
bump("inj_aborted")
|
|
elif inj["n"] >= CM.WRITER_S + 3:
|
|
s["inj"] = None
|
|
if w.hold:
|
|
bump(f"bound_{inj['cause']}_held")
|
|
bump("bound_held")
|
|
else:
|
|
bad.append((kind, seed, t, "N1b an old value after the "
|
|
f"bound ended ({inj['cause']}) is not held",
|
|
inj["v"], w.mismatch_since))
|
|
# the oracle's bound: armed by an end whose last value is not taken,
|
|
# disarmed by anything that ends it (a superset of the code's ends)
|
|
took = (kind == "solar" and w.taken) or (
|
|
kind == "manual" and w.last_a is not None)
|
|
if ended_now and not took and not gone:
|
|
s["vals"] = [0.0] + [a for a in s["seq_own"] if a > 0] + [
|
|
a for tt, a in new if a > 0]
|
|
s["armed"] = True
|
|
s["end_t"] = t
|
|
s["cap_counted"] = False
|
|
s["pend"] = None
|
|
s["kind"] = "cap" if (kind == "solar" and not wrote
|
|
and w.cut_fails >= CM.MAX_TRIES) else "max"
|
|
bump("end")
|
|
bump(f"end_{kind}")
|
|
if len(set(a for a in s["vals"] if a > 0)) > 1:
|
|
bump(f"end_distinct_{kind}")
|
|
if s["kind"] == "cap":
|
|
bump("end_cap")
|
|
elif s["armed"]:
|
|
cause = ("hold" if w.hold else "user" if press_now
|
|
else "write" if wrote
|
|
else "return" if (gone or s["was_gone"])
|
|
else "take" if took else None)
|
|
if cause is not None:
|
|
s["armed"] = False
|
|
if not (kind == "manual" and cause == "return"):
|
|
s["pend"] = (cause, list(s["vals"]))
|
|
elif w.mismatch_since is not None:
|
|
if seen is not None and seen > CM.TOL_A and seen in s["vals"]:
|
|
bad.append((kind, seed, t, "N1 a late take of a tried "
|
|
"value is held as another writer's", seen,
|
|
s["vals"]))
|
|
s["armed"] = False # a foreign value: judged as ever
|
|
s["pend"] = ("hold", list(s["vals"]))
|
|
bump("armed_foreign")
|
|
elif seen is not None and seen > CM.TOL_A and seen in s["vals"]:
|
|
bump("armed_take_seen")
|
|
if s["kind"] == "cap" and not s["cap_counted"]:
|
|
s["cap_counted"] = True
|
|
bump("cap_end_take")
|
|
# start a directed old-value take once the bound has ended and the
|
|
# writer is calm: the value must not be own by any other route
|
|
if s["pend"] is not None and s["pend"][0] == "hold" \
|
|
and not w.hold and w.mismatch_since is None:
|
|
s["pend"] = None # the foreign value went: no hold
|
|
if s["pend"] is not None and s["inj"] is None and not gone \
|
|
and not unk and not stale and w.seq is None and not wrote \
|
|
and not press_now and rnd.random() < 0.5 \
|
|
and (s["pend"][0] != "hold" or w.hold):
|
|
cause, vals = s["pend"]
|
|
own = w._own_values()
|
|
pool = [v for v in vals if v > CM.TOL_A
|
|
and all(abs(v - o) > CM.TOL_A for o in own)]
|
|
if not pool: # any other value the writer tried
|
|
pool = [v for v in sorted(s["tried"]) if v > CM.TOL_A
|
|
and all(abs(v - o) > CM.TOL_A for o in own)]
|
|
s["pend"] = None
|
|
if not pool:
|
|
bump(f"inj_no_value_{cause}")
|
|
elif not own and not w.hold:
|
|
bump("inj_blind") # nothing own to judge against
|
|
elif cause != "hold" and w.hold:
|
|
bump("inj_other_hold")
|
|
else:
|
|
s["inj"] = dict(v=rnd.choice(pool), cause=cause, n=0)
|
|
drv.cur = s["inj"]["v"]
|
|
bump("inj_started")
|
|
s["was_gone"] = gone
|
|
if order_b:
|
|
ev(t)
|
|
return bad
|
|
|
|
|
|
allbad = []
|
|
for kind in ("solar", "manual"):
|
|
for seed in range(SEEDS):
|
|
allbad += fuzz(kind, seed)
|
|
for tag in ("N1", "N1b", "N2", "N4", "N5", "N6", "N7", "N8", "N9"):
|
|
K.check(f"T21 {tag}: holds in every cycle of {SEEDS} seeds x {STEPS} s, "
|
|
f"Solar and Manual, both orders "
|
|
f"({[b for b in allbad if b[3].startswith(tag + ' ')][:2]})",
|
|
not [b for b in allbad if b[3].startswith(tag + ' ')])
|
|
print(" counts", dict(sorted(COUNT.items())))
|
|
floor = {"late_nonlast_solar": 10, "late_nonlast_manual": 5,
|
|
"cap_end_take": 5, "bound_take_held": 5, "bound_write_held": 5,
|
|
"bound_user_held": 5, "bound_hold_held": 5, "bound_return_held": 5,
|
|
"foreign_hold_after_end": 5, "end": 20, "end_cap": 3,
|
|
"armed_take_seen": 20, "foreign": 20, "hold": 5}
|
|
if STEPS >= 1200 and SEEDS >= 10:
|
|
K.check(f"T21: the fuzz reaches the plan's floors "
|
|
f"({ {k: COUNT.get(k, 0) for k in floor} }, floor {floor})",
|
|
all(COUNT.get(k, 0) >= v for k, v in floor.items()))
|
|
K.done()
|