<commit_before>from __future__ import generators

import unittest
from unittest import TestCase

from myhdl import Simulation, Signal, delay, intbv, bin

from bin2gray import bin2gray

MAX_WIDTH = 10

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

## def bin2gray(B, G, width):
##     while 1:
##         yield B
##         G.next = B[0]

class TestOriginalGrayCode(TestCase):

    def testOriginalGrayCode(self):
        
        """ Check that the code is an original Gray code """

        B = Signal(intbv(1))
        G = Signal(intbv(0))
        Rn = []
        
        def stimulus(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 n in range(2, MAX_WIDTH):
            Ln = nextLn(Ln)
            del Rn[:]
            dut = bin2gray(B, G, n)
            sim = Simulation(dut, stimulus(n))
            sim.run(quiet=1)
            self.assertEqual(Ln, Rn)


class TestGrayCodeProperties(TestCase):

    def testSingleBitChange(self):
        
        """ Check that only one bit changes in successive codewords """

        B = Signal(intbv(1))
        G = Signal(intbv(0))
        G_Z = Signal(intbv(0))
        
        def test(width):
            B.next = intbv(0)
            yield delay(10)
            for i in range(1, 2**width):
                G_Z.next = G
                B.next = intbv(i)
                yield delay(10)
                diffcode = bin(G ^ G_Z)
                self.assertEqual(diffcode.count('1'), 1)
        
        for width in range(1, MAX_WIDTH):
            dut = bin2gray(B, G, width)
            sim = Simulation(dut, test(width))
            sim.run(quiet=0)


    def testUniqueCodeWords(self):
        
        """ Check that all codewords occur exactly once """

        B = Signal(intbv(1))
        G = Signal(intbv(0))

        def test(width):
            actual = []
            for i in range(2**width):
                B.next = intbv(i)
                yield delay(10)
                actual.append(int(G))
            actual.sort()
            expected = range(2**width)
            self.assertEqual(actual, expected)
       
        for width in range(1, MAX_WIDTH):
            dut = bin2gray(B, G, width)
            sim = Simulation(dut, test(width))
            sim.run(quiet=1)
            

if __name__ == '__main__':
    unittest.main()


            
            

    

    
        


                

        
<commit_msg>rewritten to avoid lingering Signals; much better performance<commit_after>from __future__ import generators

import unittest
from unittest import TestCase

from myhdl import Simulation, Signal, delay, intbv, bin

from bin2gray import bin2gray

MAX_WIDTH = 10

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

## def bin2gray(B, G, width):
##     while 1:
##         yield B
##         G.next = B[0]

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 n in range(2, MAX_WIDTH):
            Ln = nextLn(Ln)
            del Rn[:]
            B = Signal(intbv(1))
            G = Signal(intbv(0))
            dut = bin2gray(B, G, n)
            stim = stimulus(B, G, n)
            sim = Simulation(dut, stim)
            sim.run(quiet=1)
            self.assertEqual(Ln, Rn)


class TestGrayCodeProperties(TestCase):

    def testSingleBitChange(self):
        
        """ Check that only one bit changes in successive codewords """

        
        def test(B, G, G_Z, width):
            B.next = intbv(0)
            yield delay(10)
            for i in range(1, 2**width):
                G_Z.next = G
                B.next = intbv(i)
                yield delay(10)
                diffcode = bin(G ^ G_Z)
                self.assertEqual(diffcode.count('1'), 1)
        
        for width in range(2, MAX_WIDTH):
            B = Signal(intbv(1))
            G = Signal(intbv(0))
            G_Z = Signal(intbv(0))
            dut = bin2gray(B, G, width)
            check = test(B, G, G_Z, width)
            sim = Simulation(dut, check)
            sim.run(quiet=0)


    def testUniqueCodeWords(self):
        
        """ Check that all codewords occur exactly once """

        def test(B, G, width):
            actual = []
            for i in range(2**width):
                B.next = intbv(i)
                yield delay(10)
                actual.append(int(G))
            actual.sort()
            expected = range(2**width)
            self.assertEqual(actual, expected)
       
        for width in range(1, MAX_WIDTH):
            B = Signal(intbv(1))
            G = Signal(intbv(0))
            dut = bin2gray(B, G, width)
            check = test(B, G, width)
            sim = Simulation(dut, check)
            sim.run(quiet=1)
            

if __name__ == '__main__':
    unittest.main()


            
            

    

    
        


                

        

{'fraction_non_alphanumeric': 0.11160954602548, 'fraction_numerical': 0.01794365691727974, 'mean_word_length': 1.4490333919156415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6325099', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1716, 'n_words': 457}