def test_modulo_initdef(circuit): """Test modulo arithmetics on initial values.""" cnt24 = edzed.Counter('cnt_mod_24', modulo=24, initdef=241) cnt37 = edzed.Counter('cnt_mod_37', modulo=37, initdef=-1) init(circuit) assert cnt24.output == 1 assert cnt37.output == 36
def test_put(circuit): """Test put events.""" cnt = edzed.Counter('cnt') cnt11 = edzed.Counter('cnt_mod_11', modulo=11) init(circuit) for i in range(-300, +300, 7): cnt.put(i) assert cnt.output == i cnt11.put(i) assert cnt11.output == i % 11
def test_modulo_2(circuit): """Test modulo arithmetics.""" cnt = edzed.Counter('cnt') cnt24 = edzed.Counter('cnt_mod_24', modulo=24) cnt37 = edzed.Counter('cnt_mod_37', modulo=37) init(circuit) for v in range(-200, +300, 7): cnt.event('inc', amount=v) cnt24.event('inc', amount=v) cnt37.event('dec', amount=-v) assert cnt.output % 24 == cnt24.output # congruent mod 24 assert cnt.output % 37 == cnt37.output # congruent mod 37
def test_inc_dec(circuit): """Test the basic increment/decrement.""" cnt = edzed.Counter('cnt') cnt100 = edzed.Counter('cnt_100', initdef=100) init(circuit) for i in range(5): assert cnt.output == i assert cnt100.output == i + 100 assert cnt.event('inc') == i + 1 assert cnt100.event('inc')== i + 101 for i in reversed(range(5)): assert cnt.event('dec') == i assert cnt100.event('dec') == i + 100 assert cnt.output == i assert cnt100.output == 100 + i
def test_reset(circuit): """Test put events.""" cnt0 = edzed.Counter('cnt0') cnt9 = edzed.Counter('cnt9', initdef=9) init(circuit) cnt0.event('inc') cnt0.event('inc') assert cnt0.output == 2 cnt0.event('reset') assert cnt0.output == 0 cnt9.event('dec') cnt9.event('dec') assert cnt9.output == 7 cnt9.event('reset') assert cnt9.output == 9
def test_amount_3(circuit): """Test variable amounts.""" cnt = edzed.Counter('cnt') init(circuit) for v in range(-10, 10, 1): cnt.event('inc', amount=v) cnt.event('dec', amount=v) assert cnt.output == 0
def test_amount_1(circuit): """Test variable amounts.""" cnt = edzed.Counter('cnt') init(circuit) for i in range(10): # sum of first N odd numbers = N**2 (example: 1+3+5+7 = 16) assert cnt.output == i*i cnt.event('inc', amount=2*i + 1)
def test_ifoutput(circuit): """Test the IfOutput.""" enable = edzed.Input('enable', initdef='truthy') cnt = edzed.Counter('cnt') cnt2 = edzed.Counter('cnt2') increment = edzed.Event('cnt', 'inc', efilter=edzed.IfOutput(enable)) increment2 = edzed.Event('cnt2', 'inc', efilter=edzed.IfOutput('_not_enable')) src = Noop('src', comment='faked event source') init(circuit) inv = circuit.findblock('_not_enable') assert cnt.output == cnt2.output == 0 assert increment.send(src) assert cnt.output == 1 assert not increment2.send(src) assert cnt2.output == 0 assert increment.send(src) assert cnt.output == 2 assert not increment2.send(src) assert cnt2.output == 0 enable.put(False) inv.eval_block() assert not increment.send(src) assert cnt.output == 2 assert increment2.send(src) assert cnt2.output == 1 enable.put(0) inv.eval_block() assert not increment.send(src) assert cnt.output == 2 assert increment2.send(src) assert cnt2.output == 2 enable.put(1) inv.eval_block() assert increment.send(src) assert cnt.output == 3 assert not increment2.send(src) assert cnt2.output == 2
def test_amount_2(circuit): """Test variable amounts.""" cnt = edzed.Counter('cnt', initdef=1) init(circuit) for i in range(16): cnt.event('inc', amount=cnt.output) assert cnt.output == 2**16 for i in range(16): cnt.event('dec', amount=cnt.output//2) assert cnt.output == 1
def test_nested_conditional_events(circuit): """Test tested conditional events (an edge case that nobody needs).""" cnt = edzed.Counter('counter') init(circuit) assert cnt.output == 0 cnt.event(edzed.EventCond(edzed.EventCond('inc', 'ERR'), None), value=True) assert cnt.output == 1 cnt.event(edzed.EventCond( 'ERR', edzed.EventCond(None, edzed.EventCond('ERR', 'dec'))), value=0) assert cnt.output == 0
def test_modulo_1(circuit): """Test modulo arithmetics.""" MOD = 9 ROUNDS = 15 START = 4 # any integer 0 to MOD-1 cycle = edzed.Counter('cnt_mod', modulo=MOD, initdef=START) init(circuit) values = collections.defaultdict(int) for i in range(MOD * ROUNDS): cycle.event('inc') values[cycle.output] += 1 assert cycle.output == START assert set(values) == set(range(MOD)) assert set(values.values()) == {ROUNDS}
def test_edge_detector(circuit): """Test the edge detector's parameters rise and fall.""" SEQ1 = (False, True, False, True, False, True, False, True) SEQ2 = (True, False, True, False, True) cnt = edzed.Counter('counter') i = 0 setup = [] for r in (False, True): for f in (False, True): inp1 = edzed.Input(f"test{i}_seq1", on_output=edzed.Event(cnt, 'inc', efilter=edzed.Edge( rise=r, fall=f)), initdef=SEQ1[0]) inp2 = edzed.Input(f"test{i}_seq2", on_output=edzed.Event(cnt, 'inc', efilter=edzed.Edge( rise=r, fall=f)), initdef=SEQ2[0]) s1 = s2 = 0 # s1, s2 = expected event count for SEQ1, SEQ2 if r: s1 += 4 # 4 rising edges s2 += 2 # 3 rising edges, but 1 of them is initial # and is counted immediately at the block creation if f: s1 += 3 # 4 falling edges, but 1 of them is initial # and is suppressed by uf=False (default) s2 += 2 # 2 falling edges setup.append((inp1, SEQ1, s1, (r, f))) setup.append((inp2, SEQ2, s2, (r, f))) i += 1 init(circuit) assert cnt.output == 2 # 2 times the initial rising edge of S2R1F10 and S2R1F1 for inp, seq, result, args in setup: cnt.put(0) assert cnt.output == 0 for val in seq: inp.put(val) assert cnt.output == result, f"failed for {inp.name}, (rise, fall) = {args}"
def test_conditional_events(circuit): """Test conditional events.""" assert edzed.EventCond('T', 'F') == edzed.EventCond(efalse='F', etrue='T') cnt = edzed.Counter('counter') init(circuit) assert cnt.output == 0 cnt.event(edzed.EventCond('inc', 'dec'), value=True) assert cnt.output == 1 cnt.event(edzed.EventCond('inc', 'dec'), value=10) assert cnt.output == 2 cnt.event(edzed.EventCond('inc', None), value='yes') assert cnt.output == 3 cnt.event(edzed.EventCond(None, 'inc'), value=33) assert cnt.output == 3 cnt.event(edzed.EventCond('inc', 'dec'), value=False) assert cnt.output == 2 cnt.event(edzed.EventCond('inc', None), value=0) assert cnt.output == 2 cnt.event(edzed.EventCond(None, None), value=True) assert cnt.output == 2 cnt.event(edzed.EventCond(None, None), value=False) assert cnt.output == 2