<commit_before>import unittest
from Message import Struct, Field
from binary_tools import to_bin


def uint_field(value='0x00'):
    return Field('uint', 'name', to_bin(value))

class TestMessages(unittest.TestCase):

    def test_in(self):
        msg = Struct('foo', 'foo_type')
        msg['a'] = uint_field()
        msg['b'] = uint_field()
        msg['c'] = uint_field()
        self.assertTrue('a' in msg)
        self.assertFalse('d' in msg)

    def test_parent_references(self):
        msg = Struct('foo', 'foo_type')
        child = Struct('sub', 'subelement_type')

        child['field'] = uint_field()

        msg['subbi'] = child
        self.assertEquals(msg, child._parent)
        self.assertEquals(msg, child['field']._parent._parent)

    def test_conversions(self):
        field = Field('unit', 'name', to_bin('0x00616200'))
        self.assertEquals(field.int, 0x00616200)
        self.assertEquals(field.hex, '0x00616200')
        self.assertEquals(field.ascii, 'ab')
        self.assertEquals(field.bytes, '\x00\x61\x62\x00')
        self.assertEquals(field.chars, '\x00\x61\x62\x00')
        self.assertEquals(field.bin, '0b00000000'+'01100001'+'01100010'+'00000000')

class TestFieldAlignment(unittest.TestCase):

    def _assert_align(self, value, length, raw):
        field = Field('uint', 'name', to_bin(value), aligned_len=length)
        self.assertEquals(field.int, int(value, 16))
        self.assertEquals(field.hex, value)
        self.assertEquals(field._raw, to_bin(raw))
        self.assertEquals(field.bytes, to_bin(value))        
        if length:
            self.assertEquals(len(field), length)

    def test_align_field(self):
        self._assert_align('0xff', 4, '0xff00 0000')
        self._assert_align('0x00ff', 4, '0x00ff 0000')
        self._assert_align('0x00', 4, '0x0000 0000')
        self._assert_align('0xff', None, '0xff')
        self._assert_align('0xff', 6, '0xff00 0000 0000')
        self._assert_align('0x000000ff', 4, '0x0000 00ff')
        self._assert_align('0xff', 1, '0xff')


class TestLittleEndian(unittest.TestCase):

    def test_little_endian(self):
        field = Field('uint', 'name', to_bin('0x0100'), little_endian=True)
        self.assertEquals(field._raw, to_bin('0x0100'))
        self.assertEquals(field.int, 1)
        self.assertEquals(field.bytes, to_bin('0x0001'))
        self.assertEquals(field.hex, '0x0001')

    def test_little_endian_with_align(self):
        field = Field('uint', 'name', to_bin('0x0100'), aligned_len=5, little_endian=True)
        self.assertEquals(field._raw, to_bin('0x0100000000'))
        self.assertEquals(field.int, 1)
        self.assertEquals(field.bytes, to_bin('0x0001'))
        self.assertEquals(field.hex, '0x0001')


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

<commit_msg>cleanup whitespace<commit_after>import unittest
from Message import Struct, Field
from binary_tools import to_bin


def uint_field(value='0x00'):
    return Field('uint', 'name', to_bin(value))


class TestMessages(unittest.TestCase):

    def test_in(self):
        msg = Struct('foo', 'foo_type')
        msg['a'] = uint_field()
        msg['b'] = uint_field()
        msg['c'] = uint_field()
        self.assertTrue('a' in msg)
        self.assertFalse('d' in msg)

    def test_parent_references(self):
        msg = Struct('foo', 'foo_type')
        child = Struct('sub', 'subelement_type')
        child['field'] = uint_field()
        msg['subbi'] = child
        self.assertEquals(msg, child._parent)
        self.assertEquals(msg, child['field']._parent._parent)

    def test_conversions(self):
        field = Field('unit', 'name', to_bin('0x00616200'))
        self.assertEquals(field.int, 0x00616200)
        self.assertEquals(field.hex, '0x00616200')
        self.assertEquals(field.ascii, 'ab')
        self.assertEquals(field.bytes, '\x00\x61\x62\x00')
        self.assertEquals(field.chars, '\x00\x61\x62\x00')
        self.assertEquals(field.bin, '0b00000000'+'01100001'+'01100010'+'00000000')


class TestFieldAlignment(unittest.TestCase):

    def _assert_align(self, value, length, raw):
        field = Field('uint', 'name', to_bin(value), aligned_len=length)
        self.assertEquals(field.int, int(value, 16))
        self.assertEquals(field.hex, value)
        self.assertEquals(field._raw, to_bin(raw))
        self.assertEquals(field.bytes, to_bin(value))        
        if length:
            self.assertEquals(len(field), length)

    def test_align_field(self):
        self._assert_align('0xff', 4, '0xff00 0000')
        self._assert_align('0x00ff', 4, '0x00ff 0000')
        self._assert_align('0x00', 4, '0x0000 0000')
        self._assert_align('0xff', None, '0xff')
        self._assert_align('0xff', 6, '0xff00 0000 0000')
        self._assert_align('0x000000ff', 4, '0x0000 00ff')
        self._assert_align('0xff', 1, '0xff')


class TestLittleEndian(unittest.TestCase):

    def test_little_endian(self):
        field = Field('uint', 'name', to_bin('0x0100'), little_endian=True)
        self.assertEquals(field._raw, to_bin('0x0100'))
        self.assertEquals(field.int, 1)
        self.assertEquals(field.bytes, to_bin('0x0001'))
        self.assertEquals(field.hex, '0x0001')

    def test_little_endian_with_align(self):
        field = Field('uint', 'name', to_bin('0x0100'), aligned_len=5, little_endian=True)
        self.assertEquals(field._raw, to_bin('0x0100000000'))
        self.assertEquals(field.int, 1)
        self.assertEquals(field.bytes, to_bin('0x0001'))
        self.assertEquals(field.hex, '0x0001')


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


{'fraction_non_alphanumeric': 0.14856939754753865, 'fraction_numerical': 0.06966411942420472, 'mean_word_length': 3.3425925925925926, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '1049636', 'n_tokens_mistral': 2101, 'n_tokens_neox': 1817, 'n_words': 323}