Пример #1
0
 def test_init_bytes(self):
     a = BinData(8, bytearray(b'12'))
     self.assertEqual(a.width, 8)
     self.assertEqual(a.raw_data, b'12')
     self.assertEqual(a.octets_per_element(), 1)
     self.assertEqual(a.octets(), 2)
     self.assertEqual(len(a), 2)
     self.assertEqual(list(a), [0x31, 0x32])
     self.assertEqual(a, a[:])
     self.assertEqual(str(a), '31 32')
     self.assertEqual(repr(a),
                      'BinData.from_spaced_hex(8, \'31 32\')')
Пример #2
0
 def test_null(self):
     a = BinData(8)
     self.assertEqual(a.width, 8)
     self.assertEqual(a.raw_data, b'')
     self.assertEqual(a.octets_per_element(), 1)
     self.assertEqual(a.octets(), 0)
     self.assertEqual(len(a), 0)
     self.assertEqual(list(a), [])
     self.assertEqual(a, a[:])
     self.assertEqual(str(a), '')
     self.assertEqual(repr(a),
                      'BinData.from_spaced_hex(8, \'\')')
Пример #3
0
 def test_init_num(self):
     a = BinData(12, 12)
     self.assertEqual(a.width, 12)
     self.assertEqual(a.raw_data, bytes(bytearray(24)))
     self.assertEqual(a.octets_per_element(), 2)
     self.assertEqual(a.octets(), 24)
     self.assertEqual(len(a), 12)
     self.assertEqual(list(a), [0] * 12)
     self.assertEqual(a, a[:])
     s = ' '.join('000' for _ in range(12))
     self.assertEqual(str(a), s)
     self.assertEqual(repr(a),
                      'BinData.from_spaced_hex(12, \'{}\')'.format(s))
Пример #4
0
 def test_simple(self):
     a = BinData(8, [0x12])
     self.assertEqual(a.width, 8)
     self.assertEqual(a.raw_data, b'\x12')
     self.assertEqual(a.octets_per_element(), 1)
     self.assertEqual(a.octets(), 1)
     self.assertEqual(len(a), 1)
     self.assertEqual(a[0], 0x12)
     self.assertEqual(a[-1], 0x12)
     self.assertEqual(list(a), [0x12])
     self.assertEqual(a, a[:])
     self.assertEqual(str(a), '12')
     self.assertEqual(repr(a),
                      'BinData.from_spaced_hex(8, \'12\')')
Пример #5
0
 def test_19(self):
     a = BinData(19, [0x12345, 0x6789a])
     self.assertEqual(a.width, 19)
     self.assertEqual(a.raw_data, b'\x45\x23\x01\x9a\x78\x06')
     self.assertEqual(a.octets_per_element(), 3)
     self.assertEqual(a.octets(), 6)
     self.assertEqual(len(a), 2)
     self.assertEqual(a[0], 0x12345)
     self.assertEqual(a[1], 0x6789a)
     self.assertEqual(a[-1], 0x6789a)
     self.assertEqual(list(a), [0x12345, 0x6789a])
     self.assertEqual(a, a[:])
     self.assertEqual(str(a), '12345 6789a')
     self.assertEqual(repr(a),
                      'BinData.from_spaced_hex(19, \'12345 6789a\')')
Пример #6
0
 def test_fib_7(self):
     a = BinData(7, [1, 2, 3, 5, 8])
     self.assertEqual(a.width, 7)
     self.assertEqual(a.raw_data, b'\x01\x02\x03\x05\x08')
     self.assertEqual(a.octets_per_element(), 1)
     self.assertEqual(a.octets(), 5)
     self.assertEqual(len(a), 5)
     self.assertEqual(a[0], 1)
     self.assertEqual(a[4], 8)
     self.assertEqual(a[-1], 8)
     self.assertEqual(list(a), [1, 2, 3, 5, 8])
     self.assertEqual(a, a[:])
     self.assertEqual(str(a), '01 02 03 05 08')
     self.assertEqual(repr(a),
                      'BinData.from_spaced_hex(7, \'01 02 03 05 08\')')
Пример #7
0
 def test_fib_9(self):
     a = BinData(9, [1, 2, 3, 5, 8])
     self.assertEqual(a.width, 9)
     self.assertEqual(a.raw_data,
                      b'\x01\x00\x02\x00\x03\x00\x05\x00\x08\x00')
     self.assertEqual(a.octets_per_element(), 2)
     self.assertEqual(a.octets(), 10)
     self.assertEqual(len(a), 5)
     self.assertEqual(a[0], 1)
     self.assertEqual(a[4], 8)
     self.assertEqual(a[-1], 8)
     self.assertEqual(list(a), [1, 2, 3, 5, 8])
     self.assertEqual(a, a[:])
     self.assertEqual(str(a), '001 002 003 005 008')
     self.assertEqual(repr(a),
                      'BinData.from_spaced_hex(9, \'001 002 003 005 008\')')