mirror of
https://github.com/myhdl/myhdl.git
synced 2025-01-24 21:52:56 +08:00
split test cases for gray tests
This commit is contained in:
parent
37955746f7
commit
5651c50ede
44
example/manual/test_gray_original.py
Normal file
44
example/manual/test_gray_original.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from myhdl import Simulation, Signal, delay, intbv, bin
|
||||||
|
|
||||||
|
from bin2gray import bin2gray
|
||||||
|
|
||||||
|
MAX_WIDTH = 11
|
||||||
|
|
||||||
|
def nextLn(Ln):
|
||||||
|
""" Return Gray code Ln+1, given Ln. """
|
||||||
|
Ln0 = ['0' + codeword for codeword in Ln]
|
||||||
|
Ln1 = ['1' + codeword for codeword in Ln]
|
||||||
|
Ln1.reverse()
|
||||||
|
return Ln0 + Ln1
|
||||||
|
|
||||||
|
|
||||||
|
class TestOriginalGrayCode(unittest.TestCase):
|
||||||
|
|
||||||
|
def testOriginalGrayCode(self):
|
||||||
|
"""Check that the code is an original Gray code."""
|
||||||
|
|
||||||
|
Rn = []
|
||||||
|
|
||||||
|
def stimulus(B, G, n):
|
||||||
|
for i in range(2**n):
|
||||||
|
B.next = intbv(i)
|
||||||
|
yield delay(10)
|
||||||
|
Rn.append(bin(G, width=n))
|
||||||
|
|
||||||
|
Ln = ['0', '1'] # n == 1
|
||||||
|
for w in range(2, MAX_WIDTH):
|
||||||
|
Ln = nextLn(Ln)
|
||||||
|
del Rn[:]
|
||||||
|
B = Signal(intbv(0)[w:])
|
||||||
|
G = Signal(intbv(0)[w:])
|
||||||
|
dut = bin2gray(B, G)
|
||||||
|
stim = stimulus(B, G, w)
|
||||||
|
sim = Simulation(dut, stim)
|
||||||
|
sim.run(quiet=1)
|
||||||
|
self.assertEqual(Ln, Rn)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
@ -1,7 +1,4 @@
|
|||||||
from __future__ import generators
|
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
from myhdl import Simulation, Signal, delay, intbv, bin
|
from myhdl import Simulation, Signal, delay, intbv, bin
|
||||||
|
|
||||||
@ -9,41 +6,7 @@ from bin2gray import bin2gray
|
|||||||
|
|
||||||
MAX_WIDTH = 11
|
MAX_WIDTH = 11
|
||||||
|
|
||||||
def nextLn(Ln):
|
class TestGrayCodeProperties(unittest.TestCase):
|
||||||
""" Return Gray code Ln+1, given Ln. """
|
|
||||||
Ln0 = ['0' + codeword for codeword in Ln]
|
|
||||||
Ln1 = ['1' + codeword for codeword in Ln]
|
|
||||||
Ln1.reverse()
|
|
||||||
return Ln0 + Ln1
|
|
||||||
|
|
||||||
|
|
||||||
class TestOriginalGrayCode(TestCase):
|
|
||||||
|
|
||||||
def testOriginalGrayCode(self):
|
|
||||||
"""Check that the code is an original Gray code."""
|
|
||||||
|
|
||||||
Rn = []
|
|
||||||
|
|
||||||
def stimulus(B, G, n):
|
|
||||||
for i in range(2**n):
|
|
||||||
B.next = intbv(i)
|
|
||||||
yield delay(10)
|
|
||||||
Rn.append(bin(G, width=n))
|
|
||||||
|
|
||||||
Ln = ['0', '1'] # n == 1
|
|
||||||
for w in range(2, MAX_WIDTH):
|
|
||||||
Ln = nextLn(Ln)
|
|
||||||
del Rn[:]
|
|
||||||
B = Signal(intbv(0)[w:])
|
|
||||||
G = Signal(intbv(0)[w:])
|
|
||||||
dut = bin2gray(B, G)
|
|
||||||
stim = stimulus(B, G, w)
|
|
||||||
sim = Simulation(dut, stim)
|
|
||||||
sim.run(quiet=1)
|
|
||||||
self.assertEqual(Ln, Rn)
|
|
||||||
|
|
||||||
|
|
||||||
class TestGrayCodeProperties(TestCase):
|
|
||||||
|
|
||||||
def testSingleBitChange(self):
|
def testSingleBitChange(self):
|
||||||
"""Check that only one bit changes in successive codewords."""
|
"""Check that only one bit changes in successive codewords."""
|
Loading…
x
Reference in New Issue
Block a user