From 4922a73c5d2773eec1b55ac262dcde80e840d994 Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 18 Dec 2018 18:05:37 +0000 Subject: [PATCH] test.compat: import tests from Migen as appropriate. test_signed and test_coding are adjusted slightly to account for differences in comb propagation between the simulators; we might want to revert that eventually. --- doc/COMPAT_SUMMARY.md | 4 +- nmigen/compat/sim/__init__.py | 21 ++++- nmigen/test/compat/__init__.py | 0 nmigen/test/compat/support.py | 13 ++++ nmigen/test/compat/test_coding.py | 114 ++++++++++++++++++++++++++++ nmigen/test/compat/test_constant.py | 29 +++++++ nmigen/test/compat/test_fifo.py | 56 ++++++++++++++ nmigen/test/compat/test_fsm.py | 87 +++++++++++++++++++++ nmigen/test/compat/test_passive.py | 23 ++++++ nmigen/test/compat/test_signed.py | 42 ++++++++++ nmigen/test/compat/test_size.py | 19 +++++ 11 files changed, 403 insertions(+), 5 deletions(-) create mode 100644 nmigen/test/compat/__init__.py create mode 100644 nmigen/test/compat/support.py create mode 100644 nmigen/test/compat/test_coding.py create mode 100644 nmigen/test/compat/test_constant.py create mode 100644 nmigen/test/compat/test_fifo.py create mode 100644 nmigen/test/compat/test_fsm.py create mode 100644 nmigen/test/compat/test_passive.py create mode 100644 nmigen/test/compat/test_signed.py create mode 100644 nmigen/test/compat/test_size.py diff --git a/doc/COMPAT_SUMMARY.md b/doc/COMPAT_SUMMARY.md index ab58ae8..0b2eb4a 100644 --- a/doc/COMPAT_SUMMARY.md +++ b/doc/COMPAT_SUMMARY.md @@ -185,8 +185,8 @@ Compatibility summary - (⊙) `core` **brk** - (⊙) `vcd` **brk** → `vcd` - (⊙) `Simulator` **brk** - - (+) `run_simulation` **obs** → `.back.pysim.Simulator` - - (−) `passive` **obs** → `.hdl.ast.Passive` + - (⊕) `run_simulation` **obs** → `.back.pysim.Simulator` + - (⊕) `passive` **obs** → `.hdl.ast.Passive` - (−) `build` ? - (+) `util` **obs** - (+) `misc` ⇒ `.tools` diff --git a/nmigen/compat/sim/__init__.py b/nmigen/compat/sim/__init__.py index 8219cf9..23aab8b 100644 --- a/nmigen/compat/sim/__init__.py +++ b/nmigen/compat/sim/__init__.py @@ -1,7 +1,10 @@ +import functools +import collections +import inspect from ...back.pysim import * -__all__ = ["run_simulation"] +__all__ = ["run_simulation", "passive"] def run_simulation(fragment_or_module, generators, clocks={"sync": 10}, vcd_name=None, @@ -19,6 +22,18 @@ def run_simulation(fragment_or_module, generators, clocks={"sync": 10}, vcd_name with Simulator(fragment, vcd_file=open(vcd_name, "w") if vcd_name else None) as sim: for domain, period in clocks.items(): sim.add_clock(period / 1e9, domain=domain) - for domain, process in generators.items(): - sim.add_sync_process(process, domain=domain) + for domain, processes in generators.items(): + if isinstance(processes, collections.Iterable) and not inspect.isgenerator(processes): + for process in processes: + sim.add_sync_process(process, domain=domain) + else: + sim.add_sync_process(processes, domain=domain) sim.run() + + +def passive(generator): + @functools.wraps(generator) + def wrapper(*args, **kwargs): + yield Passive() + yield from generator(*args, **kwargs) + return wrapper diff --git a/nmigen/test/compat/__init__.py b/nmigen/test/compat/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nmigen/test/compat/support.py b/nmigen/test/compat/support.py new file mode 100644 index 0000000..2a1292f --- /dev/null +++ b/nmigen/test/compat/support.py @@ -0,0 +1,13 @@ +from ...compat import * +# from ...compat.fhdl import verilog + + +class SimCase: + def setUp(self, *args, **kwargs): + self.tb = self.TestBench(*args, **kwargs) + + # def test_to_verilog(self): + # verilog.convert(self.tb) + + def run_with(self, generator): + run_simulation(self.tb, generator) diff --git a/nmigen/test/compat/test_coding.py b/nmigen/test/compat/test_coding.py new file mode 100644 index 0000000..5d945b3 --- /dev/null +++ b/nmigen/test/compat/test_coding.py @@ -0,0 +1,114 @@ +import unittest + +from ...compat import * +from ...compat.genlib.coding import * + +from .support import SimCase + + +class EncCase(SimCase, unittest.TestCase): + class TestBench(Module): + def __init__(self): + self.submodules.dut = Encoder(8) + + def test_sizes(self): + self.assertEqual(len(self.tb.dut.i), 8) + self.assertEqual(len(self.tb.dut.o), 3) + self.assertEqual(len(self.tb.dut.n), 1) + + def test_run_sequence(self): + seq = list(range(1<<8)) + def gen(): + for _ in range(256): + if seq: + yield self.tb.dut.i.eq(seq.pop(0)) + yield + if (yield self.tb.dut.n): + self.assertNotIn((yield self.tb.dut.i), [1< 0: + self.assertEqual(i & 1<<(o - 1), 0) + self.assertGreaterEqual(i, 1< 0: + self.assertEqual(i & 1<<(o - 1), 0) + self.assertGreaterEqual(i, 1< q, + lambda p, q: p >= q, + lambda p, q: p < q, + lambda p, q: p <= q, + lambda p, q: p == q, + lambda p, q: p != q, + ] + self.vals = [] + for asign in 1, -1: + for bsign in 1, -1: + for f in comps: + r = Signal() + r0 = f(asign*self.a, bsign*self.b) + self.comb += r.eq(r0) + self.vals.append((asign, bsign, f, r, r0.op)) + + def test_comparisons(self): + def gen(): + for i in range(-4, 4): + yield self.tb.a.eq(i) + yield self.tb.b.eq(i) + yield + a = yield self.tb.a + b = yield self.tb.b + for asign, bsign, f, r, op in self.tb.vals: + r, r0 = (yield r), f(asign*a, bsign*b) + self.assertEqual(r, int(r0), + "got {}, want {}*{} {} {}*{} = {}".format( + r, asign, a, op, bsign, b, r0)) + self.run_with(gen()) diff --git a/nmigen/test/compat/test_size.py b/nmigen/test/compat/test_size.py new file mode 100644 index 0000000..7c34e6c --- /dev/null +++ b/nmigen/test/compat/test_size.py @@ -0,0 +1,19 @@ +import unittest + +from ...compat import * + + +def _same_slices(a, b): + return a.value is b.value and a.start == b.start and a.stop == b.stop + + +class SignalSizeCase(unittest.TestCase): + def setUp(self): + self.i = C(0xaa) + self.j = C(-127) + self.s = Signal((13, True)) + + def test_len(self): + self.assertEqual(len(self.s), 13) + self.assertEqual(len(self.i), 8) + self.assertEqual(len(self.j), 8) -- 2.30.2