test/coding: use new API
authorSebastien Bourdeauducq <sb@m-labs.hk>
Thu, 17 Sep 2015 09:22:59 +0000 (17:22 +0800)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Thu, 17 Sep 2015 09:22:59 +0000 (17:22 +0800)
migen/test/test_coding.py

index 4cbca1788c6fe8aa7fe322329a06def904c16820..965a7e9751e068cfa6f59ad7f7dec9bb66746610 100644 (file)
@@ -3,11 +3,11 @@ import unittest
 from migen import *
 from migen.genlib.coding import *
 
-from migen.test.support import SimCase, SimBench
+from migen.test.support import SimCase
 
 
 class EncCase(SimCase, unittest.TestCase):
-    class TestBench(SimBench):
+    class TestBench(Module):
         def __init__(self):
             self.submodules.dut = Encoder(8)
 
@@ -18,18 +18,20 @@ class EncCase(SimCase, unittest.TestCase):
 
     def test_run_sequence(self):
         seq = list(range(1<<8))
-        def cb(tb, tbp):
-            if seq:
-                tbp.dut.i = seq.pop(0)
-            if tbp.dut.n:
-                self.assertNotIn(tbp.dut.i, [1<<i for i in range(8)])
-            else:
-                self.assertEqual(tbp.dut.i, 1<<tbp.dut.o)
-        self.run_with(cb, 256)
+        def gen():
+            for _ in range(256):
+                if seq:
+                    yield self.tb.dut.i, seq.pop(0)
+                if (yield self.tb.dut.n):
+                    self.assertNotIn((yield self.tb.dut.i), [1<<i for i in range(8)])
+                else:
+                    self.assertEqual((yield self.tb.dut.i), 1<<(yield self.tb.dut.o))
+                yield
+        self.run_with(gen())
 
 
 class PrioEncCase(SimCase, unittest.TestCase):
-    class TestBench(SimBench):
+    class TestBench(Module):
         def __init__(self):
             self.submodules.dut = PriorityEncoder(8)
 
@@ -40,22 +42,24 @@ class PrioEncCase(SimCase, unittest.TestCase):
 
     def test_run_sequence(self):
         seq = list(range(1<<8))
-        def cb(tb, tbp):
-            if seq:
-                tbp.dut.i = seq.pop(0)
-            i = tbp.dut.i
-            if tbp.dut.n:
-                self.assertEqual(i, 0)
-            else:
-                o = tbp.dut.o
-                if o > 0:
-                    self.assertEqual(i & 1<<(o - 1), 0)
-                self.assertGreaterEqual(i, 1<<o)
-        self.run_with(cb, 256)
+        def gen():
+            for _ in range(256):
+                if seq:
+                    yield self.tb.dut.i, seq.pop(0)
+                i = yield self.tb.dut.i
+                if (yield self.tb.dut.n):
+                    self.assertEqual(i, 0)
+                else:
+                    o = yield self.tb.dut.o
+                    if o > 0:
+                        self.assertEqual(i & 1<<(o - 1), 0)
+                    self.assertGreaterEqual(i, 1<<o)
+                yield
+        self.run_with(gen())
 
 
 class DecCase(SimCase, unittest.TestCase):
-    class TestBench(SimBench):
+    class TestBench(Module):
         def __init__(self):
             self.submodules.dut = Decoder(8)
 
@@ -66,22 +70,23 @@ class DecCase(SimCase, unittest.TestCase):
 
     def test_run_sequence(self):
         seq = list(range(8*2))
-        def cb(tb, tbp):
-            if seq:
-                i = seq.pop()
-                tbp.dut.i = i//2
-                tbp.dut.n = i%2
-            i = tbp.dut.i
-            o = tbp.dut.o
-            if tbp.dut.n:
-                self.assertEqual(o, 0)
-            else:
-                self.assertEqual(o, 1<<i)
-        self.run_with(cb, 256)
+        def gen():
+            for _ in range(256):
+                if seq:
+                    i = seq.pop()
+                    yield self.tb.dut.i, i//2
+                    yield self.tb.dut.n, i%2
+                i = yield self.tb.dut.i
+                o = yield self.tb.dut.o
+                if (yield self.tb.dut.n):
+                    self.assertEqual(o, 0)
+                else:
+                    self.assertEqual(o, 1<<i)
+        self.run_with(gen())
 
 
 class SmallPrioEncCase(SimCase, unittest.TestCase):
-    class TestBench(SimBench):
+    class TestBench(Module):
         def __init__(self):
             self.submodules.dut = PriorityEncoder(1)
 
@@ -92,15 +97,17 @@ class SmallPrioEncCase(SimCase, unittest.TestCase):
 
     def test_run_sequence(self):
         seq = list(range(1))
-        def cb(tb, tbp):
-            if seq:
-                tbp.dut.i = seq.pop(0)
-            i = tbp.dut.i
-            if tbp.dut.n:
-                self.assertEqual(i, 0)
-            else:
-                o = tbp.dut.o
-                if o > 0:
-                    self.assertEqual(i & 1<<(o - 1), 0)
-                self.assertGreaterEqual(i, 1<<o)
-        self.run_with(cb, 5)
+        def gen():
+            for _ in range(5):
+                if seq:
+                    yield self.tb.dut.i, seq.pop(0)
+                i = yield self.tb.dut.i
+                if (yield self.tb.dut.n):
+                    self.assertEqual(i, 0)
+                else:
+                    o = yield self.tb.dut.o
+                    if o > 0:
+                        self.assertEqual(i & 1<<(o - 1), 0)
+                    self.assertGreaterEqual(i, 1<<o)
+                yield
+        self.run_with(gen())