<commit_before>from math import ceil

from hwt.hdlObjects.constants import Time
from hwt.hdlObjects.frameTemplate import FrameTemplate
from hwt.hdlObjects.transTmpl import TransTmpl
from hwt.simulator.simTestCase import SimTestCase
from hwtLib.amba.sim.axi3DenseMem import Axi3DenseMem
from hwtLib.samples.iLvl.builders.ethAddrUpdater import EthAddrUpdater, \
    frameHeader


class EthAddrUpdaterTC(SimTestCase):
    def test_simpleOp(self):
        DW = 64
        AW = 32

        u = EthAddrUpdater()
        u.DATA_WIDTH.set(DW)
        u.ADDR_WIDTH.set(AW)

        self.prepareUnit(u)

        r = self.randomize
        #r(u.axi_m.ar)
        #r(u.axi_m.r)
        #r(u.axi_m.aw)
        #r(u.axi_m.w)
        #r(u.axi_m.b)

        m = Axi3DenseMem(u.clk, u.axi_m)
        tmpl = TransTmpl(frameHeader)
        frameTmpl = list(FrameTemplate.framesFromTransTmpl(tmpl, DW))[0]

        def randFrame():
            rand = self._rand.getrandbits
            data = {"eth": {"src": 1, # rand(48),
                            "dst": 2  # rand(48)
                            },
                    "ipv4":{"src": 3, # rand(32),
                            "dst": 4 # rand(32)
                           }
                    }
            d = list(frameTmpl.packData(data))
            print(frameTmpl)
            print(d)
            ptr = m.calloc(ceil(frameHeader.bit_length() / DW),
                           DW // 8,
                           initValues=d)
            return ptr, data

        framePtr, frameData = randFrame()

        u.packetAddr._ag.data.append(framePtr)

        # print(m.data)
        self.doSim(1000 * Time.ns)
        # print(m.data)
        updatedFrame = m.getStruct(framePtr, tmpl)
        # print(updatedFrame)
        self.assertValEqual(updatedFrame.eth.src, frameData["eth"]['dst'])
        self.assertValEqual(updatedFrame.eth.dst, frameData["eth"]['src'])
        self.assertValEqual(updatedFrame.eth.src, frameData["ipv4"]['dst'])
        self.assertValEqual(updatedFrame.eth.dst, frameData["ipv4"]['src'])


if __name__ == "__main__":
    import unittest
    suite = unittest.TestSuite()
    # suite.addTest(EthAddrUpdaterTC('test_simpleOp'))
    suite.addTest(unittest.makeSuite(EthAddrUpdaterTC))
    runner = unittest.TextTestRunner(verbosity=3)
    runner.run(suite)<commit_msg>rm debug from EthAddrUpdaterTC<commit_after>from math import ceil

from hwt.hdlObjects.constants import Time
from hwt.hdlObjects.frameTemplate import FrameTemplate
from hwt.hdlObjects.transTmpl import TransTmpl
from hwt.simulator.simTestCase import SimTestCase
from hwtLib.amba.sim.axi3DenseMem import Axi3DenseMem
from hwtLib.samples.iLvl.builders.ethAddrUpdater import EthAddrUpdater, \
    frameHeader


class EthAddrUpdaterTC(SimTestCase):
    def test_simpleOp(self):
        DW = 64
        AW = 32

        u = EthAddrUpdater()
        u.DATA_WIDTH.set(DW)
        u.ADDR_WIDTH.set(AW)

        self.prepareUnit(u)

        r = self.randomize
        r(u.axi_m.ar)
        r(u.axi_m.r)
        r(u.axi_m.aw)
        r(u.axi_m.w)
        r(u.axi_m.b)

        m = Axi3DenseMem(u.clk, u.axi_m)
        tmpl = TransTmpl(frameHeader)
        frameTmpl = list(FrameTemplate.framesFromTransTmpl(tmpl, DW))[0]

        def randFrame():
            rand = self._rand.getrandbits
            data = {"eth": {"src": rand(48),
                            "dst": rand(48)
                            },
                    "ipv4":{"src": rand(32),
                            "dst": rand(32)
                           }
                    }
            d = list(frameTmpl.packData(data))
            ptr = m.calloc(ceil(frameHeader.bit_length() / DW),
                           DW // 8,
                           initValues=d)
            return ptr, data

        framePtr, frameData = randFrame()

        u.packetAddr._ag.data.append(framePtr)

        self.doSim(1000 * Time.ns)
        updatedFrame = m.getStruct(framePtr, tmpl)
        self.assertValEqual(updatedFrame.eth.src, frameData["eth"]['dst'])
        self.assertValEqual(updatedFrame.eth.dst, frameData["eth"]['src'])
        self.assertValEqual(updatedFrame.ipv4.src, frameData["ipv4"]['dst'])
        self.assertValEqual(updatedFrame.ipv4.dst, frameData["ipv4"]['src'])


if __name__ == "__main__":
    import unittest
    suite = unittest.TestSuite()
    # suite.addTest(EthAddrUpdaterTC('test_simpleOp'))
    suite.addTest(unittest.makeSuite(EthAddrUpdaterTC))
    runner = unittest.TextTestRunner(verbosity=3)
    runner.run(suite)
{'fraction_non_alphanumeric': 0.11730981256890849, 'fraction_numerical': 0.012348401323042999, 'mean_word_length': 2.0669371196754565, 'pattern_counts': {'":': 14, '<': 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': '3133190', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1481, 'n_words': 233}