Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
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]
Exemple #10
0
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)
Exemple #11
0
def test_expr_add(a, b):
    expr = atom(4, a) + atom(4, b)
    assert common.decode_int(expr()) == a + b
Exemple #12
0
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
Exemple #13
0
def test_source(int_value):
    var = common.source(wordlen=4, value=int_value, name='x')
    assert common.decode_int(var({})[0]['x']) == int_value
Exemple #14
0
def test_expr_sub(a, b):
    expr = atom(4, a) - atom(4, b)
    assert common.decode_int(expr()) == a - b
Exemple #15
0
def test_srl_signed(a, b):
    expr = atom(4, a) >> b
    assert common.decode_int(expr()) == a >> b
Exemple #16
0
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)
Exemple #17
0
def test_expr_abs(a):
    expr = abs(atom(4, a))
    assert common.decode_int(expr()) == abs(a)
Exemple #18
0
def test_expr_bitwise_invert(a):
    expr = ~atom(4, a)
    assert common.decode_int(expr()) == ~a
Exemple #19
0
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
Exemple #20
0
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
Exemple #21
0
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
Exemple #22
0
def test_expr_abs_max_negative():
    expr = abs(atom(4, -8))
    assert common.decode_int(expr()) == -8  # undefined behavior
Exemple #23
0
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)
Exemple #24
0
def test_expr_neg(a):
    expr = -atom(4, a)
    assert common.decode_int(expr()) == -a
Exemple #25
0
def test_expr_bitwise_and2(a):
    expr = atom(4, a) & atom(4, a)
    assert common.decode_int(expr()) == a
Exemple #26
0
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
Exemple #27
0
def test_srl_unsigned(a, b):
    expr = atom(4, a) >> b
    assert common.decode_int(expr(), signed=False) == a >> b
Exemple #28
0
def test_expr_bitwise_xor(a, b):
    expr = atom(4, a) ^ atom(4, b)
    assert common.decode_int(expr()) == a ^ b
Exemple #29
0
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
Exemple #30
0
def test_encoded_add():
    adder = uatom(3, 'x') + 2
    res = adder(inputs={'x': 0b000})
    assert common.decode_int(res) == 2