<commit_before>from __future__ import print_function
import unittest
import pyrtl


class TestBlock(unittest.TestCase):
    def setUp(self):
        pyrtl.reset_working_block()

    def tearDown(self):
        pyrtl.reset_working_block()

    def test_add_wirevector_simple(self):
        w = pyrtl.WireVector(name='testwire', bitwidth=3)
        pyrtl.working_block().add_wirevector(w)
        self.assertTrue(w in pyrtl.working_block().wirevector_set)
        self.assertTrue('testwire' in pyrtl.working_block().wirevector_by_name)

    def invalid_wire(self, *args):
        with self.assertRaises(pyrtl.PyrtlError):
            pyrtl.working_block().add_wirevector(*args)

    def test_add_wire(self):
        self.invalid_wire(None)
        self.invalid_wire("Hi John")
        self.invalid_wire(42)

    def test_undriven_net(self):
        w = pyrtl.WireVector(name='testwire', bitwidth=3)
        self.assertRaises(pyrtl.PyrtlError, pyrtl.working_block().sanity_check)
        pyrtl.reset_working_block()
        r = pyrtl.Register(3)
        self.assertRaises(pyrtl.PyrtlError, pyrtl.working_block().sanity_check)
        pyrtl.reset_working_block()
        o = pyrtl.Output(3)
        self.assertRaises(pyrtl.PyrtlError, pyrtl.working_block().sanity_check)

    def test_no_logic_net_comparisons(self):
        a = pyrtl.WireVector(bitwidth=3)
        b = pyrtl.WireVector(bitwidth=3)
        select = pyrtl.WireVector(bitwidth=3)
        outwire = pyrtl.WireVector(bitwidth=3)
        net1 = pyrtl.LogicNet(op='x', op_param=None, args=(select, a, b), dests=(outwire,))
        net2 = pyrtl.LogicNet(op='x', op_param=None, args=(select, b, a), dests=(outwire,))
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 < net2
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 <= net2
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 > net2
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 >= net2

    def test_logicsubset_no_op(self):
        w = pyrtl.WireVector(name='testwire1', bitwidth=1)
        v = pyrtl.WireVector(name='testwire2', bitwidth=1)
        sum = w & v
        block = pyrtl.working_block()
        self.assertEqual(block.logic_subset(None), block.logic)

    def test_sanity_check(self):
        pass

    def test_block_iterators(self):
        # testing to see that it properly runs a trivial case
        inwire = pyrtl.Input(bitwidth=1, name="inwire1")
        inwire2 = pyrtl.Input(bitwidth=1)
        inwire3 = pyrtl.Input(bitwidth=1)
        tempwire = pyrtl.WireVector()
        tempwire2 = pyrtl.WireVector()
        outwire = pyrtl.Output()

        tempwire <<= inwire | inwire2
        tempwire2 <<= ~tempwire
        outwire <<= tempwire2 & inwire3

        block = pyrtl.working_block()

        i = 0
        for net in block:
            self.assertFalse(i > 100, "Too many iterations happened")
            i += 1
            print(str(net))

        for net in block.logic:
            print(net)

    def test_no_memblocks(self):
        block = pyrtl.working_block()
        self.assertFalse(block.memblocks_by_name)

    def test_bad_memblock_name_strict(self):
        block = pyrtl.working_block()
        with self.assertRaises(pyrtl.PyrtlError):
            _ = block.get_memblock_by_name('bad_mem', strict=True)

    def test_bad_memblock_name_none(self):
        block = pyrtl.working_block()
        mem = block.get_memblock_by_name('bad_mem')
        self.assertIsNone(mem)

    def test_same_memblock_referenced_across_multiple_operators(self):
        mem_name = 'mem'
        mem = pyrtl.MemBlock(32, 5, mem_name)
        x = mem[0]
        mem[1] <<= 42
        mem = pyrtl.working_block().get_memblock_by_name(mem_name)
        for net in pyrtl.working_block().logic:
            if net.op == 'm':
                self.assertIs(net.op_param[1], mem)
            if net.op == '@':
                self.assertIs(net.op_param[1], mem)


class TestSanityCheckNet(unittest.TestCase):
    def setUp(self):
        pyrtl.reset_working_block()

    def tearDown(self):
        pyrtl.reset_working_block()<commit_msg>fix errors revealed from tox<commit_after>from __future__ import print_function
import unittest
import pyrtl


class TestBlock(unittest.TestCase):
    def setUp(self):
        pyrtl.reset_working_block()

    def tearDown(self):
        pyrtl.reset_working_block()

    def test_add_wirevector_simple(self):
        w = pyrtl.WireVector(name='testwire', bitwidth=3)
        pyrtl.working_block().add_wirevector(w)
        self.assertTrue(w in pyrtl.working_block().wirevector_set)
        self.assertTrue('testwire' in pyrtl.working_block().wirevector_by_name)

    def invalid_wire(self, *args):
        with self.assertRaises(pyrtl.PyrtlError):
            pyrtl.working_block().add_wirevector(*args)

    def test_add_wire(self):
        self.invalid_wire(None)
        self.invalid_wire("Hi John")
        self.invalid_wire(42)

    def test_undriven_net(self):
        w = pyrtl.WireVector(name='testwire', bitwidth=3)
        self.assertRaises(pyrtl.PyrtlError, pyrtl.working_block().sanity_check)
        pyrtl.reset_working_block()
        r = pyrtl.Register(3)
        self.assertRaises(pyrtl.PyrtlError, pyrtl.working_block().sanity_check)
        pyrtl.reset_working_block()
        o = pyrtl.Output(3)
        self.assertRaises(pyrtl.PyrtlError, pyrtl.working_block().sanity_check)

    def test_no_logic_net_comparisons(self):
        a = pyrtl.WireVector(bitwidth=3)
        b = pyrtl.WireVector(bitwidth=3)
        select = pyrtl.WireVector(bitwidth=3)
        outwire = pyrtl.WireVector(bitwidth=3)
        net1 = pyrtl.LogicNet(op='x', op_param=None, args=(select, a, b), dests=(outwire,))
        net2 = pyrtl.LogicNet(op='x', op_param=None, args=(select, b, a), dests=(outwire,))
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 < net2
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 <= net2
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 > net2
        with self.assertRaises(pyrtl.PyrtlError):
            foo = net1 >= net2

    def test_logicsubset_no_op(self):
        w = pyrtl.WireVector(name='testwire1', bitwidth=1)
        v = pyrtl.WireVector(name='testwire2', bitwidth=1)
        sum = w & v
        block = pyrtl.working_block()
        self.assertEqual(block.logic_subset(None), block.logic)

    def test_sanity_check(self):
        pass

    def test_block_iterators(self):
        # testing to see that it properly runs a trivial case
        inwire = pyrtl.Input(bitwidth=1, name="inwire1")
        inwire2 = pyrtl.Input(bitwidth=1)
        inwire3 = pyrtl.Input(bitwidth=1)
        tempwire = pyrtl.WireVector()
        tempwire2 = pyrtl.WireVector()
        outwire = pyrtl.Output()

        tempwire <<= inwire | inwire2
        tempwire2 <<= ~tempwire
        outwire <<= tempwire2 & inwire3

        block = pyrtl.working_block()

        i = 0
        for net in block:
            self.assertFalse(i > 100, "Too many iterations happened")
            i += 1
            print(str(net))

        for net in block.logic:
            print(net)

    def test_no_memblocks(self):
        block = pyrtl.working_block()
        self.assertFalse(block.memblock_by_name)

    def test_bad_memblock_name_strict(self):
        block = pyrtl.working_block()
        with self.assertRaises(pyrtl.PyrtlError):
            _ = block.get_memblock_by_name('bad_mem', strict=True)

    def test_bad_memblock_name_none(self):
        block = pyrtl.working_block()
        mem = block.get_memblock_by_name('bad_mem')
        self.assertIsNone(mem)

    def test_same_memblock_referenced_across_multiple_operators(self):
        mem_name = 'mem'
        mem = pyrtl.MemBlock(32, 5, mem_name)
        x = mem[0]
        mem[1] <<= 42
        mem = pyrtl.working_block().get_memblock_by_name(mem_name)
        for net in pyrtl.working_block().logic:
            if net.op == 'm':
                self.assertIs(net.op_param[1], mem)
            if net.op == '@':
                self.assertIs(net.op_param[1], mem)


class TestSanityCheckNet(unittest.TestCase):
    def setUp(self):
        pyrtl.reset_working_block()

    def tearDown(self):
        pyrtl.reset_working_block()
{'fraction_non_alphanumeric': 0.11036468330134357, 'fraction_numerical': 0.011756238003838772, 'mean_word_length': 2.6613965744400527, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 9, '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': '1312002', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2722, 'n_words': 461}