def test_int_static_field(self): field = Int('4', "field", '-72') self.assertTrue(field.length.static) self.assertEquals(field.name, "field") self.assertEquals(field.default_value, '-72') self.assertEquals(field.type, 'int') self.assertEquals(field.encode({}, {}, None).hex, '0xffffffb8')
def test_encode_aligned_int(self): sint = Int(1, 'foo', '-72', align='4') encoded = sint.encode({}, {}, None) self.assertEquals(encoded.int, -72) self.assertEquals(encoded.hex, '0xb8') self.assertEquals(len(encoded), 4) self.assertEquals(encoded._raw, to_bin('0xb800 0000'))
def test_decode_aligned_int(self): sint = Int(1, 'foo', None, align='4') decoded = sint.decode(to_bin('0xb800 0000'), None) self.assertEquals(decoded.int, -72) self.assertEquals(decoded.hex, '0xb8') self.assertEquals(len(decoded), 4) self.assertEquals(decoded._raw, to_bin('0xb800 0000'))
def test_validate_int(self): field = Field('int', 'field', to_bin('0xffb8')) self._should_pass(Int(2, 'field', -72).validate({'field': field}, {})) self._should_pass(Int(2, 'field', '-72').validate({'field': field}, {})) self._should_pass(Int(2, 'field', '-0x48').validate({'field': field}, {})) self._should_pass(Int(2, 'field', '-0x0048').validate({'field': field}, {})) self._should_pass(Int(2, 'field', '(0|-72)').validate({'field': field}, {}))
def test_decode_int(self): field_template = Int(2, 'field', -72) decoded = field_template.decode(to_bin('0xffb8'), {}) self.assertEquals(decoded.int, -72) self.assertEquals(decoded.hex, '0xffb8')
def test_little_endian_int_encode(self): template = Int(2, 'field', -72) field = template.encode({}, {}, None, little_endian=True) self.assertEquals(field._raw, to_bin('0xb8ff')) self.assertEquals(field.int, -72) self.assertEquals(field.bytes, to_bin('0xffb8'))