Пример #1
0
def generate_function_test_vectors(circuit,
                                   func,
                                   input_ranges=None,
                                   mode='complete',
                                   flatten=True):
    check(circuit, func)

    args = []
    for i, (name, port) in enumerate(circuit.IO.items()):
        if port.isinput():
            if isinstance(port, BitKind):
                args.append([BitVector(0), BitVector(1)])
            elif isinstance(port, ArrayKind) and isinstance(port.T, BitKind):
                num_bits = port.N
                if isinstance(port, SIntKind):
                    if input_ranges is None:
                        input_range = range(-2**(num_bits - 1),
                                            2**(num_bits - 1))
                    else:
                        input_range = input_ranges[i]
                    args.append([
                        SIntVector(x, num_bits=num_bits) for x in input_range
                    ])
                else:
                    if input_ranges is None:
                        input_range = range(1 << num_bits)
                    else:
                        input_range = input_ranges[i]
                    args.append(
                        [BitVector(x, num_bits=num_bits) for x in input_range])
            else:
                raise NotImplementedError(type(port))

    tests = []
    for test in product(*args):
        result = func(*list(test))
        test = [list(test), []]
        if isinstance(result, tuple):
            test[-1].extend(result)
        else:
            test[-1].append(result)
        tests.append(test)
    if flatten:
        tests = flatten_tests(tests)
    else:
        tests = [test[0] + test[1] for test in tests]
    return tests
Пример #2
0
def generate_simulator_test_vectors(circuit,
                                    input_ranges=None,
                                    mode='complete',
                                    flatten=True):
    ntest = len(circuit.interface.ports.items())

    simulator = PythonSimulator(circuit)

    args = []
    for i, (name, port) in enumerate(circuit.IO.items()):
        if port.isinput():
            if isinstance(port, BitKind):
                args.append([BitVector(0), BitVector(1)])
            elif isinstance(port, ArrayKind) and isinstance(port.T, BitKind):
                num_bits = port.N
                if isinstance(port, SIntKind):
                    if input_ranges is None:
                        start = -2**(num_bits - 1)
                        # We don't subtract one because range end is exclusive
                        end = 2**(num_bits - 1)
                        input_range = range(start, end)
                    else:
                        input_range = input_ranges[i]
                    args.append([
                        SIntVector(x, num_bits=num_bits) for x in input_range
                    ])
                else:
                    if input_ranges is None:
                        input_range = range(1 << num_bits)
                    else:
                        input_range = input_ranges[i]
                    args.append(
                        [BitVector(x, num_bits=num_bits) for x in input_range])
            else:
                assert True, "can't test Tuples"

    tests = []
    for test in product(*args):
        testv = [list(test), []]
        j = 0
        for i, (name, port) in enumerate(circuit.IO.items()):
            if port.isinput():
                val = test[j].as_bool_list()
                if len(val) == 1:
                    val = val[0]
                simulator.set_value(getattr(circuit, name), val)
                j += 1

        simulator.evaluate()

        for i, (name, port) in enumerate(circuit.IO.items()):
            if port.isoutput():
                val = simulator.get_value(getattr(circuit, name))
                if isinstance(port, ArrayKind) and \
                        not isinstance(port, (BitsKind, SIntKind, UIntKind)):
                    val = BitVector(val)
                testv[1].append(val)

        tests.append(testv)

    if flatten:
        tests = flatten_tests(tests)
    else:
        tests = [test[0] + test[1] for test in tests]
    return tests
Пример #3
0
def signed(value):
    return SIntVector(value._value, value.num_bits)
Пример #4
0
def test_signed():
    a = SIntVector(4, 4)
    assert int(a) == 4
    a = SIntVector(-4, 4)
    assert a._value != 4, "Stored as unsigned two's complement value"
    assert int(a) == -4, "int returns the native signed int representation"
Пример #5
0
def signed(value, width):
    return SIntVector(value, width).as_sint()
Пример #6
0
def test_operator_int_shift(op, reference, width):
    for _ in range(NTESTS):
        I0, I1 = SIntVector.random(width), UIntVector.random(width)
        expected = signed(reference(int(I0), int(I1)), width)
        assert expected == int(op(I0, I1))
Пример #7
0
def test_operator_int1(op, reference, width):
    for _ in range(NTESTS):
        I = SIntVector.random(width)
        expected = signed(reference(int(I)), width)
        assert expected == int(op(I))