Skip to content

Commit

Permalink
Update test vector logic to be consistent with targets
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Aug 17, 2018
1 parent d8a42a7 commit c67f7cf
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions fault/test_vectors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
from magma import BitKind, ArrayKind, SIntKind, TupleKind
from magma import BitKind, ArrayKind, SIntKind
from magma.simulator.python_simulator import PythonSimulator
from bit_vector import BitVector, SIntVector
from inspect import signature
from itertools import product
import pytest
import fault


class TestVector:
__test__ = False

def __init__(self, test_vector):
self.test_vector = test_vector

def __eq__(self, other):
if not isinstance(other, TestVector):
raise ValueError("Expected another TestVector for __eq__")
for x, y in zip(self, other):
if x is fault.AnyValue or y is fault.AnyValue:
continue
if x != y:
return False
return True

def __len__(self):
return len(self.test_vector)

def __iter__(self):
return iter(self.test_vector)


# check that number of function arguments equals number of circuit inputs
Expand All @@ -20,6 +44,20 @@ def check(circuit, func):
assert nfuncargs == ncircargs


def flatten_tests(tests):
flattened_tests = []
for i in range(len(tests)):
flattened_tests.append(tests[i][0][:])
if i == 0:
flattened_tests[-1].extend(
[fault.AnyValue for _ in range(len(tests[i][1]))])
else:
flattened_tests[-1].extend(tests[i - 1][1])
flattened_tests.append(tests[-1][0][:])
flattened_tests[-1].extend(tests[-1][1])
return [TestVector(x) for x in flattened_tests]


@pytest.mark.skip(reason="Not a test")
def generate_function_test_vectors(circuit, func, input_ranges=None,
mode='complete'):
Expand Down Expand Up @@ -51,15 +89,14 @@ def generate_function_test_vectors(circuit, func, input_ranges=None,

tests = []
for test in product(*args):
test = list(test)
result = func(*test)
result = func(*list(test))
test = [list(test), []]
if isinstance(result, tuple):
test.extend(result)
test[-1].extend(result)
else:
test.append(result)
test[-1].append(result)
tests.append(test)

return tests
return flatten_tests(tests)


def generate_simulator_test_vectors(circuit, input_ranges=None,
Expand Down Expand Up @@ -97,15 +134,10 @@ def generate_simulator_test_vectors(circuit, input_ranges=None,

tests = []
for test in product(*args):
test = list(test)
testv = ntest*[0]
testv = [list(test), []]
j = 0
for i, (name, port) in enumerate(circuit.IO.items()):
if port.isinput():
if isinstance(port, SIntKind):
testv[i] = test[j].as_sint()
else:
testv[i] = test[j].as_uint()
val = test[j].as_bool_list()
if len(val) == 1:
val = val[0]
Expand All @@ -117,12 +149,8 @@ def generate_simulator_test_vectors(circuit, input_ranges=None,
for i, (name, port) in enumerate(circuit.IO.items()):
if port.isoutput():
val = simulator.get_value(getattr(circuit, name))
if isinstance(port, SIntKind):
val = SIntVector(val).as_sint()
else:
val = BitVector(val).as_uint()
testv[i] = val
testv[1].append(BitVector(val))

tests.append(testv)

return tests
return flatten_tests(tests)

0 comments on commit c67f7cf

Please sign in to comment.