Skip to content

Commit

Permalink
Add namedtuple regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Jan 10, 2020
1 parent 7bc70ca commit c3fa956
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion magma/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def flip(cls):
new_fields[k] = v.flip()
for k, v in cls.field_dict.items():
if not issubclass(new_fields[k], v):
base = cls.unbound_t
base = cls.qualify(Direction.Undirected)
return cls.unbound_t._cache_handler(cls.is_cached, new_fields,
cls.__name__, (base, ), {})

Expand Down Expand Up @@ -406,6 +406,18 @@ def keys(cls):
def types(cls):
return cls.fields

def value(self):
ts = [t.value() for t in self.ts]

for t in ts:
if t is None:
return None

if len(ts) == len(self) and self.iswhole(ts):
return ts[0].name.tuple

return namedtuple(**dict(zip(self.keys(),ts)))


from .bitutils import int2seq
from .array import Array
Expand Down
26 changes: 26 additions & 0 deletions tests/test_type/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,29 @@ def definition(io):
def test_flat_length():
a = m.Product.from_fields("anon", dict(x=m.Bits[5], y=m.Bits[3], z=m.Bit))
assert a.flat_length() == 9



def test_namedtuple_seq():
class A(Product):
a0 = Bit
a1 = SInt[8]

@circuit.sequential(async_reset=False)
class TestNamedTuple:
def __init__(self):
self.a0: Bit = bit(0)
self.a1: SInt[8] = 0

def __call__(self, a: A, b: Bit) -> A:
if b:
new_a = a
else:
new_a = namedtuple(a0=self.a0, a1=self.a1)

self.a0 = new_a.a0
self.a1 = new_a.a1

return new_a

m.compile("build/test_named_tuple_seq", TestNamedTuple, output="coreir-verilog")

0 comments on commit c3fa956

Please sign in to comment.