"""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). 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. - N2: no non-zero write while a count runs 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 branches are counted and a floor is asserted, so a fuzz that never reaches them 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 SEEDS = int(os.environ.get("EV69_FUZZ_SEEDS", "10")) STEPS = int(os.environ.get("EV69_FUZZ_STEPS", "1200")) COUNT = {} 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": 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(), nsel=0) clamp = {"v": 16.0} def ev(t): if rnd.random() < (0.25 if w.seq is not None else 0.01): clamp["v"] = rnd.choice([16.0, 12.0, 10.0, 8.0, 8.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 rnd.random() < 0.004: mode["refuse"] = rnd.choice([0.0, 0.5, 0.97, 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.03: n_to = 3 if drv.ph == 1 else 1 if kind == "solar": if not w.plan.switch: w.plan._set(rnd.choice([8.0, 10.0, 12.0]), float(t), n_to) 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.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("late_take_nonlast") if rnd.random() < 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.01: drv.cur = 0.0 s["gone"] = rnd.random() < (0.8 if s["gone"] else 0.003) for t in range(STEPS): if not order_b: ev(t) gone, unk, stale = s["gone"], rnd.random() < 0.02, rnd.random() < 0.02 hold0, ms0 = w.hold, w.mismatch_since 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") 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 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 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 bump("end") if kind == "solar" and w.cut_fails >= CM.MAX_TRIES: bump("end_cap") elif s["armed"]: if wrote or press_now or w.hold or gone or s["was_gone"] \ or took: s["armed"] = False 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 bump("armed_foreign") elif seen is not None and seen > CM.TOL_A and seen in s["vals"]: bump("armed_take_seen") 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", "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 = {"end": 20, "end_cap": 3, "late_take_ended_val": 10, "late_take_nonlast": 5, "armed_take_seen": 20, "foreign": 20, "hold": 5} if STEPS >= 1200 and SEEDS >= 10: K.check(f"T21: the fuzz reaches its branches " 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()