def test_split_gate(a, b): circ = common.split_gate('input', 4, 'a', 4, 'b') val = circ({ 'input': common.encode_int(4, a) + common.encode_int(4, b), })[0] assert common.decode_int(val['a']) == a assert common.decode_int(val['b']) == b
def test_combine_gate(a, b): circ = common.combine_gate(4, 'a', 4, 'b', output='out') val = circ({ 'a': common.encode_int(4, a), 'b': common.encode_int(4, b) })[0]['out'] assert common.decode_int(val[:4]) == a assert common.decode_int(val[4:]) == b
def test_subtraction(a, b): circ = common.subtract_gate(4, left='a', right='b', output='out') val = circ({ 'a': common.encode_int(4, a), 'b': common.encode_int(4, b), })[0]['out'] assert common.decode_int(val) == a - b
def test_bitwise_or(a, b): circ = common.bitwise_or(4, left='a', right='b', output='out') val = circ({ 'a': common.encode_int(4, a), 'b': common.encode_int(4, b), })[0]['out'] assert common.decode_int(val) == a | b
def test_dec(a): circ = common.dec_gate(4, input='a', output='out') assert circ.inputs == {'a'} assert len(circ.aig.inputs) == 4 assert circ.outputs == {'out'} assert len(circ.aig.outputs) == 4 val = circ({'a': common.encode_int(4, a)})[0]['out'] assert common.decode_int(val) == a - 1
def test_unsigned_multiply(x, y): if x * y > 15: return multiplier = atom(4, 'a', signed=False) * atom(4, 'b', signed=False) assert len(multiplier.inputs) == 2 x_atom, y_atom = atom(4, x, signed=False), atom(4, y, signed=False) res = multiplier(inputs={'a': x_atom(), 'b': y_atom()}) assert common.decode_int(res, signed=False) == x * y
def test_logical_right_shift(a, b): circ = common.logical_right_shift_gate(8, b, 'a', output='out') val = circ({ 'a': common.encode_int(8, a, signed=True), })[0]['out'] val2 = common.decode_int(val) assert (val2 & (0xff >> b)) == ((a >> b) & (0xff >> b)) if b != 0: assert val[-1] is False
def test_seqcomp(a): circ1 = common.identity_gate(4, input='a', output='tmp') circ2 = common.identity_gate(4, input='tmp', output='out') circ3 = circ1 >> circ2 assert circ3.inputs == circ1.inputs assert circ3.outputs == circ2.outputs val = circ3({ 'a': common.encode_int(4, a), })[0]['out'] assert common.decode_int(val) == a
def test_lookup(a): lookup = {0: 0, 1: 1, 2: 0, 3: -1} circ = common.lookup(mapping=lookup, input='x', output='out', inlen=2, outlen=4, in_signed=False) val = circ({ 'x': common.encode_int(2, a, signed=False), })[0]['out'] assert common.decode_int(val) == lookup[a]
def test_abs(a): circ = common.abs_gate(8, 'a', output='out') val = circ({ 'a': common.encode_int(8, a, signed=True), })[0]['out'] assert common.decode_int(val) == abs(a)
def test_expr_add(a, b): expr = atom(4, a) + atom(4, b) assert common.decode_int(expr()) == a + b
def test_arithmetic_right_shift(a, b): circ = common.arithmetic_right_shift_gate(8, b, 'a', output='out') val = circ({ 'a': common.encode_int(8, a, signed=True), })[0]['out'] assert common.decode_int(val) == a >> b
def test_source(int_value): var = common.source(wordlen=4, value=int_value, name='x') assert common.decode_int(var({})[0]['x']) == int_value
def test_expr_sub(a, b): expr = atom(4, a) - atom(4, b) assert common.decode_int(expr()) == a - b
def test_srl_signed(a, b): expr = atom(4, a) >> b assert common.decode_int(expr()) == a >> b
def test_sll(a, b): wordlen = 4 expr = atom(wordlen, a, signed=False) << b mask = (1 << wordlen) - 1 assert bin(common.decode_int(expr(), signed=False)) == bin((a << b) & mask)
def test_expr_abs(a): expr = abs(atom(4, a)) assert common.decode_int(expr()) == abs(a)
def test_expr_bitwise_invert(a): expr = ~atom(4, a) assert common.decode_int(expr()) == ~a
def test_identity(a): circ = common.identity_gate(4, input='a', output='out') val = circ({'a': common.encode_int(4, a)})[0]['out'] assert common.decode_int(val) == a
def test_bitwise_negate(a): circ = common.bitwise_negate(4, input='a', output='out') val = circ({'a': common.encode_int(4, a)})[0]['out'] assert common.decode_int(val) == ~a
def test_multiply(x, y): multiplier = atom(6, 'a') * atom(6, 'b') x_atom, y_atom = atom(6, x), atom(6, y) res = multiplier(inputs={'a': x_atom(), 'b': y_atom()}) assert common.decode_int(res) == x * y
def test_expr_abs_max_negative(): expr = abs(atom(4, -8)) assert common.decode_int(expr()) == -8 # undefined behavior
def test_ite(test, a, b): _test, _a, _b = atom(1, test, signed=False), atom(4, a), atom(4, b) expr = ite(_test, _a, _b) val = common.decode_int(expr()) assert val == (a if test else b)
def test_expr_neg(a): expr = -atom(4, a) assert common.decode_int(expr()) == -a
def test_expr_bitwise_and2(a): expr = atom(4, a) & atom(4, a) assert common.decode_int(expr()) == a
def test_expr_getitem(a): expr = atom(4, a) for i in range(4): assert common.decode_int(expr[i](), signed=False) == (a >> i) & 1
def test_srl_unsigned(a, b): expr = atom(4, a) >> b assert common.decode_int(expr(), signed=False) == a >> b
def test_expr_bitwise_xor(a, b): expr = atom(4, a) ^ atom(4, b) assert common.decode_int(expr()) == a ^ b
def test_reverse(a): circ = common.reverse_gate(4, input='a', output='out') val = circ({'a': common.encode_int(4, a)})[0]['out'] assert common.decode_int(val[::-1]) == a
def test_encoded_add(): adder = uatom(3, 'x') + 2 res = adder(inputs={'x': 0b000}) assert common.decode_int(res) == 2