def test_pack_signed(self): self.assertEqual(_S.pack('i\x00\x00\x08', ((-1,), (2,), (-3,))), '\xff\x02\xfd') fmt = 'i\x00\x00\x02' * 4 self.assertEqual(_S.pack(fmt, ((1, -1) * 2,)), '\x77') v = _S.pack(fmt, ((1, -1) * 4,), offset=4) self.assertEqual(ord(v[0]) & 15, 7) self.assertEqual(ord(v[1]) & (15 << 4), 7 << 4) self.assertRaises(ValueError, _S.pack, 'i\x00\x00\x08', (('a',), (2,)))
def test_pack_float(self): fmt1 = 'f\x00\x00\x20' fmt2 = 'f\x00\x00\x40' self.assertEqual(_S.pack(fmt1, ((1.,), (2.,))), struct.pack('>ff', 1., 2.)) self.assertEqual(_S.pack(fmt2, ((1.,), (2.,))), struct.pack('>dd', 1., 2.)) self.assertEqual(_S.pack(fmt1, ((1,), (2,))), struct.pack('>ff', 1., 2.)) self.assertEqual(_S.pack(fmt2, ((1,), (2,))), struct.pack('>dd', 1., 2.)) self.assertRaises(ValueError, _S.pack, fmt2, (('a',), (2,)))
def test_pack_char(self): fmt = 'c\x00\x00\x08' self.assertRaises(ValueError, _S.pack, fmt, (('',), ('',))) self.assertEqual(_S.pack(fmt, (('a',), ('b',))), 'ab') self.assertEqual(_S.pack(fmt, 'abcd'), 'abcd') self.assertEqual(_S.pack(fmt, '\x7f\x80\x7f\x80\x00', offset=7)[1:5], '\xff\x00\xff\x00') self.assertEqual(_S.pack(fmt, '\x3f\xc0\x3f\xc0\x00', offset=6)[1:5], '\xff\x00\xff\x00') self.assertEqual(_S.pack(fmt, '\x1f\xe0\x1f\xe0\x00', offset=5)[1:5], '\xff\x00\xff\x00') self.assertEqual(_S.pack(fmt, '\x0f\xf0\x0f\xf0\x00', offset=4)[1:5], '\xff\x00\xff\x00') self.assertEqual(_S.pack(fmt, '\x07\xf8\x07\xf8\x00', offset=3)[1:5], '\xff\x00\xff\x00') self.assertEqual(_S.pack(fmt, '\x03\xfc\x03\xfc\x00', offset=2)[1:5], '\xff\x00\xff\x00') self.assertEqual(_S.pack(fmt, '\x01\xfe\x01\xfe\x00', offset=1)[1:5], '\xff\x00\xff\x00')
def test_pack_unsigned(self): self.assertEqual(_S.pack('u\x00\x00\x08', ((1,), (2,), (3,))), '\x01\x02\x03') fmt = 'u\x00\x00\x01' * 8 self.assertEqual(_S.pack(fmt, ((1, 0) * 4,)), '\xaa') v = _S.pack(fmt, ((1, 0) * 4,), offset=4) self.assertEqual(ord(v[0]) & 15, ord('\x0a')) self.assertEqual(ord(v[1]) & (15 << 4), ord('\xa0')) fmt = 'u\x00\x00\x02u\x00\x00\x04u\x00\x00\x0fu\x00\x00\x03' self.assertEqual(_S.pack(fmt, ((1, 2, 3, 4),)), '\x48\x00\x1c') self.assertEqual(_S.pack(fmt, ((1., 2., 3., 4.),)), '\x48\x00\x1c') self.assertRaises(ValueError, _S.pack, 'u\x00\x00\x08', (('a',), (2,))) fmt = 'u\x00\x00\x28' self.assertEqual(_S.pack(fmt, ((2 ** 32 + 2 ** 8,),)), '\x01\x00\x00\x01\x00')
def test_pack_mixed(self): fmt = 'c\x00\x00\x08u\x00\x00\x18' self.assertEqual(_S.pack(fmt, (('c', 8), ('u', 24))), fmt)