# logica.py — exhaustieve combinatorische tests en functioneel latchmodel
from itertools import product
def opteller(a,b,c):
    return a^b^c, (a and b) or ((a^b) and c)
for a,b,c in product((0,1),repeat=3):
    s,co = opteller(a,b,c)
    assert s + 2*co == a+b+c
print("volle opteller: alle 8 combinaties correct")

def stap(q, gezond, aanvraag, resetflank):
    stop = not gezond
    reset_ok = resetflank and gezond and not aanvraag
    # stopdominant; q=1 betekent vergrendeld
    qnieuw = stop or (q and not reset_ok)
    uit = gezond and aanvraag and not qnieuw
    return bool(qnieuw),bool(uit)

q = True  # opstart vergrendeld
scenario = [(1,0,1),(1,1,0),(0,1,0),(1,1,1),(1,0,1),(1,1,0)]
for k,(goed,aan,reset) in enumerate(scenario):
    q,y = stap(q,goed,aan,reset)
    print(k,"gezond",goed,"aanvraag",aan,"resetflank",reset,"slot",int(q),"uit",int(y))
assert q is False and y is True
for q,goed,aan,reset in product((False,True),repeat=4):
    qn,y = stap(q,goed,aan,reset)
    assert goed or (qn and not y)
print("stop heeft voorrang in alle 16 toestand/invoercombinaties")
