| 1 |
from __future__ import print_function |
| 2 |
import sys |
| 3 |
import timeit |
| 4 |
import unittest |
| 5 |
from bitstruct import * |
| 6 |
import bitstruct |
| 7 |
|
| 8 |
class BitStructTest(unittest.TestCase): |
| 9 |
|
| 10 |
def test_pack(self): |
| 11 |
"""Pack values. |
| 12 |
|
| 13 |
""" |
| 14 |
|
| 15 |
packed = pack('u1u1s6u7u9', 0, 0, -2, 65, 22) |
| 16 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 17 |
|
| 18 |
packed = pack('u1', 1) |
| 19 |
self.assertEqual(packed, b'\x80') |
| 20 |
|
| 21 |
packed = pack('u77', 0x100000000001000000) |
| 22 |
ref = b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00' |
| 23 |
self.assertEqual(packed, ref) |
| 24 |
|
| 25 |
packed = pack('u8000', int(8000 * '1', 2)) |
| 26 |
ref = 1000 * b'\xff' |
| 27 |
self.assertEqual(packed, ref) |
| 28 |
|
| 29 |
packed = pack('s4000', int(8000 * '0', 2)) |
| 30 |
ref = 500 * b'\x00' |
| 31 |
self.assertEqual(packed, ref) |
| 32 |
|
| 33 |
packed = pack('p1u1s6u7u9', 0, -2, 65, 22) |
| 34 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 35 |
|
| 36 |
packed = pack('P1u1s6u7u9', 0, -2, 65, 22) |
| 37 |
self.assertEqual(packed, b'\xbe\x82\x16') |
| 38 |
|
| 39 |
packed = pack('p1u1s6p7u9', 0, -2, 22) |
| 40 |
self.assertEqual(packed, b'\x3e\x00\x16') |
| 41 |
|
| 42 |
packed = pack('P1u1s6p7u9', 0, -2, 22) |
| 43 |
self.assertEqual(packed, b'\xbe\x00\x16') |
| 44 |
|
| 45 |
packed = pack('u1s6f32r43', 0, -2, 3.75, b'\x00\xff\x00\xff\x00\xff') |
| 46 |
self.assertEqual(packed, b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0') |
| 47 |
|
| 48 |
packed = pack('b1', True) |
| 49 |
self.assertEqual(packed, b'\x80') |
| 50 |
|
| 51 |
packed = pack('b1p6b1', True, True) |
| 52 |
self.assertEqual(packed, b'\x81') |
| 53 |
|
| 54 |
packed = pack('b1P6b1', True, True) |
| 55 |
self.assertEqual(packed, b'\xff') |
| 56 |
|
| 57 |
packed = pack('u5b2u1', 31, False, 1) |
| 58 |
self.assertEqual(packed, b'\xf9') |
| 59 |
|
| 60 |
packed = pack('b1t24', False, u'Hi!') |
| 61 |
self.assertEqual(packed, b'$4\x90\x80') |
| 62 |
|
| 63 |
packed = pack('b1t24', False, 'Hi!') |
| 64 |
self.assertEqual(packed, b'$4\x90\x80') |
| 65 |
|
| 66 |
packed = pack('t8000', 1000 * '7') |
| 67 |
self.assertEqual(packed, 1000 * b'\x37') |
| 68 |
|
| 69 |
if sys.version_info >= (3, 6): |
| 70 |
packed = pack('f16', 1.0) |
| 71 |
self.assertEqual(packed, b'\x3c\x00') |
| 72 |
|
| 73 |
packed = pack('f32', 1.0) |
| 74 |
self.assertEqual(packed, b'\x3f\x80\x00\x00') |
| 75 |
|
| 76 |
packed = pack('f64', 1.0) |
| 77 |
self.assertEqual(packed, b'\x3f\xf0\x00\x00\x00\x00\x00\x00') |
| 78 |
|
| 79 |
# Too many values to pack. |
| 80 |
with self.assertRaises(Error) as cm: |
| 81 |
pack('b1t24', False) |
| 82 |
|
| 83 |
self.assertEqual(str(cm.exception), |
| 84 |
'pack expected 2 item(s) for packing (got 1)') |
| 85 |
|
| 86 |
# Cannot convert argument to integer. |
| 87 |
with self.assertRaises(ValueError) as cm: |
| 88 |
pack('u1', 'foo') |
| 89 |
|
| 90 |
self.assertEqual(str(cm.exception), |
| 91 |
"invalid literal for int() with base 10: 'foo'") |
| 92 |
|
| 93 |
# Cannot convert argument to float. |
| 94 |
with self.assertRaises(ValueError) as cm: |
| 95 |
pack('f32', 'foo') |
| 96 |
|
| 97 |
if sys.version_info[0] < 3: |
| 98 |
self.assertEqual(str(cm.exception), |
| 99 |
'could not convert string to float: foo') |
| 100 |
else: |
| 101 |
self.assertEqual(str(cm.exception), |
| 102 |
"could not convert string to float: 'foo'") |
| 103 |
|
| 104 |
# Cannot convert argument to bytearray. |
| 105 |
with self.assertRaises(TypeError) as cm: |
| 106 |
pack('r5', 1.0) |
| 107 |
|
| 108 |
self.assertIn("'float' has no", str(cm.exception)) |
| 109 |
|
| 110 |
# Cannot encode argument as utf-8. |
| 111 |
with self.assertRaises(AttributeError) as cm: |
| 112 |
pack('t8', 1.0) |
| 113 |
|
| 114 |
self.assertEqual(str(cm.exception), |
| 115 |
"'float' object has no attribute 'encode'") |
| 116 |
|
| 117 |
def test_unpack(self): |
| 118 |
"""Unpack values. |
| 119 |
|
| 120 |
""" |
| 121 |
|
| 122 |
unpacked = unpack('u1u1s6u7u9', b'\x3e\x82\x16') |
| 123 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 124 |
|
| 125 |
unpacked = unpack('u1', bytearray(b'\x80')) |
| 126 |
self.assertEqual(unpacked, (1, )) |
| 127 |
|
| 128 |
packed = b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00' |
| 129 |
unpacked = unpack('u77', packed) |
| 130 |
self.assertEqual(unpacked, (0x100000000001000000,)) |
| 131 |
|
| 132 |
packed = 1000 * b'\xff' |
| 133 |
unpacked = unpack('u8000', packed) |
| 134 |
self.assertEqual(unpacked, (int(8000 * '1', 2), )) |
| 135 |
|
| 136 |
packed = 500 * b'\x00' |
| 137 |
unpacked = unpack('s4000', packed) |
| 138 |
self.assertEqual(unpacked, (0, )) |
| 139 |
|
| 140 |
packed = b'\xbe\x82\x16' |
| 141 |
unpacked = unpack('P1u1s6u7u9', packed) |
| 142 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 143 |
|
| 144 |
packed = b'\x3e\x82\x16' |
| 145 |
unpacked = unpack('P1u1s6u7u9', packed) |
| 146 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 147 |
|
| 148 |
packed = b'\xbe\x82\x16' |
| 149 |
unpacked = unpack('p1u1s6u7u9', packed) |
| 150 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 151 |
|
| 152 |
packed = b'\x3e\x82\x16' |
| 153 |
unpacked = unpack('p1u1s6u7u9', packed) |
| 154 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 155 |
|
| 156 |
packed = b'\x3e\x82\x16' |
| 157 |
unpacked = unpack('p1u1s6p7u9', packed) |
| 158 |
self.assertEqual(unpacked, (0, -2, 22)) |
| 159 |
|
| 160 |
packed = b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0' |
| 161 |
unpacked = unpack('u1s6f32r43', packed) |
| 162 |
self.assertEqual(unpacked, (0, -2, 3.75, b'\x00\xff\x00\xff\x00\xe0')) |
| 163 |
|
| 164 |
packed = bytearray(b'\x80') |
| 165 |
unpacked = unpack('b1', packed) |
| 166 |
self.assertEqual(unpacked, (True, )) |
| 167 |
|
| 168 |
packed = b'\x80' |
| 169 |
unpacked = unpack('b1p6b1', packed) |
| 170 |
self.assertEqual(unpacked, (True, False)) |
| 171 |
|
| 172 |
packed = b'\x06' |
| 173 |
unpacked = unpack('u5b2u1', packed) |
| 174 |
self.assertEqual(unpacked, (0, True, 0)) |
| 175 |
|
| 176 |
packed = b'\x04' |
| 177 |
unpacked = unpack('u5b2u1', packed) |
| 178 |
self.assertEqual(unpacked, (0, True, 0)) |
| 179 |
|
| 180 |
packed = b'$4\x90\x80' |
| 181 |
unpacked = unpack('b1t24', packed) |
| 182 |
self.assertEqual(unpacked, (False, u'Hi!')) |
| 183 |
|
| 184 |
packed = 1000 * b'7' |
| 185 |
unpacked = unpack('t8000', packed) |
| 186 |
self.assertEqual(packed, 1000 * b'\x37') |
| 187 |
|
| 188 |
# Bad float size. |
| 189 |
with self.assertRaises(Error) as cm: |
| 190 |
unpack('f33', b'\x00\x00\x00\x00\x00') |
| 191 |
|
| 192 |
self.assertEqual(str(cm.exception), |
| 193 |
'expected float size of 16, 32, or 64 bits (got 33)') |
| 194 |
|
| 195 |
# Too many bits to unpack. |
| 196 |
with self.assertRaises(Error) as cm: |
| 197 |
unpack('u9', b'\x00') |
| 198 |
|
| 199 |
# partial unpacking of truncated data |
| 200 |
unpacked = unpack('u4u5', b'\x5f', allow_truncated=True) |
| 201 |
self.assertEqual(unpacked, (5, )) |
| 202 |
unpacked = unpack('p8u4u5', b'\x00\x5f', allow_truncated=True) |
| 203 |
self.assertEqual(unpacked, (5, )) |
| 204 |
|
| 205 |
self.assertEqual(str(cm.exception), |
| 206 |
'unpack requires at least 9 bits to unpack (got 8)') |
| 207 |
|
| 208 |
# gcc packed struct with bitfields |
| 209 |
# |
| 210 |
# struct foo_t { |
| 211 |
# int a; |
| 212 |
# char b; |
| 213 |
# uint32_t c : 7; |
| 214 |
# uint32_t d : 25; |
| 215 |
# } foo; |
| 216 |
# |
| 217 |
# foo.a = 1; |
| 218 |
# foo.b = 1; |
| 219 |
# foo.c = 0x67; |
| 220 |
# foo.d = 0x12345; |
| 221 |
unpacked = unpack('s32s8u25u7', |
| 222 |
byteswap('414', |
| 223 |
b'\x01\x00\x00\x00\x01\xe7\xa2\x91\x00')) |
| 224 |
self.assertEqual(unpacked, (1, 1, 0x12345, 0x67)) |
| 225 |
|
| 226 |
def test_pack_unpack(self): |
| 227 |
"""Pack and unpack values. |
| 228 |
|
| 229 |
""" |
| 230 |
|
| 231 |
packed = pack('u1u1s6u7u9', 0, 0, -2, 65, 22) |
| 232 |
unpacked = unpack('u1u1s6u7u9', packed) |
| 233 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 234 |
|
| 235 |
packed = pack('f64', 1.0) |
| 236 |
unpacked = unpack('f64', packed) |
| 237 |
self.assertEqual(unpacked, (1.0, )) |
| 238 |
|
| 239 |
if sys.version_info >= (3, 6): |
| 240 |
packed = pack('f16', 1.0) |
| 241 |
unpacked = unpack('f16', packed) |
| 242 |
self.assertEqual(unpacked, (1.0, )) |
| 243 |
|
| 244 |
def test_calcsize(self): |
| 245 |
"""Calculate size. |
| 246 |
|
| 247 |
""" |
| 248 |
|
| 249 |
size = calcsize('u1u1s6u7u9') |
| 250 |
self.assertEqual(size, 24) |
| 251 |
|
| 252 |
size = calcsize('u1') |
| 253 |
self.assertEqual(size, 1) |
| 254 |
|
| 255 |
size = calcsize('u77') |
| 256 |
self.assertEqual(size, 77) |
| 257 |
|
| 258 |
size = calcsize('u1s6u7u9') |
| 259 |
self.assertEqual(size, 23) |
| 260 |
|
| 261 |
size = calcsize('b1s6u7u9p1t8') |
| 262 |
self.assertEqual(size, 32) |
| 263 |
|
| 264 |
size = calcsize('b1s6u7u9P1t8') |
| 265 |
self.assertEqual(size, 32) |
| 266 |
|
| 267 |
def test_byteswap(self): |
| 268 |
"""Byte swap. |
| 269 |
|
| 270 |
""" |
| 271 |
|
| 272 |
res = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a' |
| 273 |
ref = b'\x01\x03\x02\x04\x08\x07\x06\x05\x0a\x09' |
| 274 |
self.assertEqual(byteswap('12142', ref), res) |
| 275 |
|
| 276 |
packed = pack('u1u5u2u16', 1, 2, 3, 4) |
| 277 |
unpacked = unpack('u1u5u2u16', byteswap('12', packed)) |
| 278 |
self.assertEqual(unpacked, (1, 2, 3, 1024)) |
| 279 |
|
| 280 |
def test_endianness(self): |
| 281 |
"""Test pack/unpack with endianness information in the format string. |
| 282 |
|
| 283 |
""" |
| 284 |
|
| 285 |
# Big endian. |
| 286 |
ref = b'\x02\x46\x9a\xfe\x00\x00\x00' |
| 287 |
packed = pack('>u19s3f32', 0x1234, -2, -1.0) |
| 288 |
self.assertEqual(packed, ref) |
| 289 |
unpacked = unpack('>u19s3f32', packed) |
| 290 |
self.assertEqual(unpacked, (0x1234, -2, -1.0)) |
| 291 |
|
| 292 |
# Little endian. |
| 293 |
ref = b'\x2c\x48\x0c\x00\x00\x07\xf4' |
| 294 |
packed = pack('<u19s3f32', 0x1234, -2, -1.0) |
| 295 |
self.assertEqual(packed, ref) |
| 296 |
unpacked = unpack('<u19s3f32', packed) |
| 297 |
self.assertEqual(unpacked, (0x1234, -2, -1.0)) |
| 298 |
|
| 299 |
# Mixed endianness. |
| 300 |
ref = b'\x00\x00\x2f\x3f\xf0\x00\x00\x00\x00\x00\x00\x80' |
| 301 |
packed = pack('>u19<s5>f64r3p4', 1, -2, 1.0, b'\x80') |
| 302 |
self.assertEqual(packed, ref) |
| 303 |
unpacked = unpack('>u19<s5>f64r3p4', packed) |
| 304 |
self.assertEqual(unpacked, (1, -2, 1.0, b'\x80')) |
| 305 |
|
| 306 |
# Opposite endianness of the 'mixed endianness' test. |
| 307 |
ref = b'\x80\x00\x1e\x00\x00\x00\x00\x00\x00\x0f\xfc\x20' |
| 308 |
packed = pack('<u19>s5<f64r3p4', 1, -2, 1.0, b'\x80') |
| 309 |
self.assertEqual(packed, ref) |
| 310 |
unpacked = unpack('<u19>s5<f64r3p4', packed) |
| 311 |
self.assertEqual(unpacked, (1, -2, 1.0, b'\x80')) |
| 312 |
|
| 313 |
# Pack as big endian, unpack as little endian. |
| 314 |
ref = b'\x40' |
| 315 |
packed = pack('u2', 1) |
| 316 |
self.assertEqual(packed, ref) |
| 317 |
unpacked = unpack('<u2', packed) |
| 318 |
self.assertEqual(unpacked, (2, )) |
| 319 |
|
| 320 |
def test_byte_order(self): |
| 321 |
"""Test pack/unpack with byte order information in the format string. |
| 322 |
|
| 323 |
""" |
| 324 |
|
| 325 |
# Most significant byte first (default). |
| 326 |
ref = b'\x02\x46\x9a\xfe\x00\x00\x00' |
| 327 |
packed = pack('u19s3f32>', 0x1234, -2, -1.0) |
| 328 |
self.assertEqual(packed, ref) |
| 329 |
unpacked = unpack('u19s3f32>', packed) |
| 330 |
self.assertEqual(unpacked, (0x1234, -2, -1.0)) |
| 331 |
|
| 332 |
# Least significant byte first. |
| 333 |
ref = b'\x34\x12\x18\x00\x00\xe0\xbc' |
| 334 |
packed = pack('u19s3f32<', 0x1234, -2, -1.0) |
| 335 |
self.assertEqual(packed, ref) |
| 336 |
unpacked = unpack('u19s3f32<', packed) |
| 337 |
self.assertEqual(unpacked, (0x1234, -2, -1.0)) |
| 338 |
|
| 339 |
# Least significant byte first. |
| 340 |
ref = b'\x34\x12' |
| 341 |
packed = pack('u8s8<', 0x34, 0x12) |
| 342 |
self.assertEqual(packed, ref) |
| 343 |
unpacked = unpack('u8s8<', packed) |
| 344 |
self.assertEqual(unpacked, (0x34, 0x12)) |
| 345 |
|
| 346 |
# Least significant byte first. |
| 347 |
ref = b'\x34\x22' |
| 348 |
packed = pack('u3u12<', 1, 0x234) |
| 349 |
self.assertEqual(packed, ref) |
| 350 |
unpacked = unpack('u3s12<', packed) |
| 351 |
self.assertEqual(unpacked, (1, 0x234)) |
| 352 |
|
| 353 |
# Least significant byte first. |
| 354 |
ref = b'\x34\x11\x00' |
| 355 |
packed = pack('u3u17<', 1, 0x234) |
| 356 |
self.assertEqual(packed, ref) |
| 357 |
unpacked = unpack('u3s17<', packed) |
| 358 |
self.assertEqual(unpacked, (1, 0x234)) |
| 359 |
|
| 360 |
# Least significant byte first. |
| 361 |
ref = b'\x80' |
| 362 |
packed = pack('u1<', 1) |
| 363 |
self.assertEqual(packed, ref) |
| 364 |
unpacked = unpack('u1<', packed) |
| 365 |
self.assertEqual(unpacked, (1, )) |
| 366 |
|
| 367 |
# Least significant byte first. |
| 368 |
ref = b'\x45\x23\x25\x82' |
| 369 |
packed = pack('u19u5u1u7<', 0x12345, 5, 1, 2) |
| 370 |
self.assertEqual(packed, ref) |
| 371 |
unpacked = unpack('u19u5u1u7<', packed) |
| 372 |
self.assertEqual(unpacked, (0x12345, 5, 1, 2)) |
| 373 |
|
| 374 |
# Least significant byte first does not affect raw and text. |
| 375 |
ref = b'123abc' |
| 376 |
packed = pack('r24t24<', b'123', 'abc') |
| 377 |
self.assertEqual(packed, ref) |
| 378 |
unpacked = unpack('r24t24<', packed) |
| 379 |
self.assertEqual(unpacked, (b'123', 'abc')) |
| 380 |
|
| 381 |
def test_compile(self): |
| 382 |
cf = bitstruct.compile('u1u1s6u7u9') |
| 383 |
|
| 384 |
packed = cf.pack(0, 0, -2, 65, 22) |
| 385 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 386 |
|
| 387 |
unpacked = cf.unpack(b'\x3e\x82\x16') |
| 388 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 389 |
|
| 390 |
def test_signed_integer(self): |
| 391 |
"""Pack and unpack signed integer values. |
| 392 |
|
| 393 |
""" |
| 394 |
|
| 395 |
datas = [ |
| 396 |
('s2', 0x01, b'\x40'), |
| 397 |
('s3', 0x03, b'\x60'), |
| 398 |
('s4', 0x07, b'\x70'), |
| 399 |
('s5', 0x0f, b'\x78'), |
| 400 |
('s6', 0x1f, b'\x7c'), |
| 401 |
('s7', 0x3f, b'\x7e'), |
| 402 |
('s8', 0x7f, b'\x7f'), |
| 403 |
('s9', 0xff, b'\x7f\x80'), |
| 404 |
('s1', -1, b'\x80'), |
| 405 |
('s2', -1, b'\xc0') |
| 406 |
] |
| 407 |
|
| 408 |
for fmt, value, packed in datas: |
| 409 |
self.assertEqual(pack(fmt, value), packed) |
| 410 |
self.assertEqual(unpack(fmt, packed), (value, )) |
| 411 |
|
| 412 |
def test_unsigned_integer(self): |
| 413 |
"""Pack and unpack unsigned integer values. |
| 414 |
|
| 415 |
""" |
| 416 |
|
| 417 |
datas = [ |
| 418 |
('u1', 0x001, b'\x80'), |
| 419 |
('u2', 0x003, b'\xc0'), |
| 420 |
('u3', 0x007, b'\xe0'), |
| 421 |
('u4', 0x00f, b'\xf0'), |
| 422 |
('u5', 0x01f, b'\xf8'), |
| 423 |
('u6', 0x03f, b'\xfc'), |
| 424 |
('u7', 0x07f, b'\xfe'), |
| 425 |
('u8', 0x0ff, b'\xff'), |
| 426 |
('u9', 0x1ff, b'\xff\x80') |
| 427 |
] |
| 428 |
|
| 429 |
for fmt, value, packed in datas: |
| 430 |
self.assertEqual(pack(fmt, value), packed) |
| 431 |
self.assertEqual(unpack(fmt, packed), (value, )) |
| 432 |
|
| 433 |
def test_bad_float_size(self): |
| 434 |
"""Test of bad float size. |
| 435 |
|
| 436 |
""" |
| 437 |
|
| 438 |
with self.assertRaises(Error) as cm: |
| 439 |
pack('f31', 1.0) |
| 440 |
|
| 441 |
self.assertEqual(str(cm.exception), |
| 442 |
'expected float size of 16, 32, or 64 bits (got 31)') |
| 443 |
|
| 444 |
with self.assertRaises(Error) as cm: |
| 445 |
unpack('f33', 8 * b'\x00') |
| 446 |
|
| 447 |
self.assertEqual(str(cm.exception), |
| 448 |
'expected float size of 16, 32, or 64 bits (got 33)') |
| 449 |
|
| 450 |
def test_bad_format(self): |
| 451 |
"""Test of bad format. |
| 452 |
|
| 453 |
""" |
| 454 |
|
| 455 |
formats = [ |
| 456 |
('g1', "bad char 'g' in format"), |
| 457 |
('s1u1f32b1t8r8G13', "bad char 'G' in format"), |
| 458 |
('s1u1f32b1t8r8G13S3', "bad char 'G' in format"), |
| 459 |
('s', "bad format 's'"), |
| 460 |
('1', "bad format '1'"), |
| 461 |
('ss1', "bad format 'ss1'"), |
| 462 |
('1s', "bad format '1s'"), |
| 463 |
('foo', "bad format 'foo'"), |
| 464 |
('s>1>', "bad format 's>1>'"), |
| 465 |
('s0', "bad format 's0'") |
| 466 |
] |
| 467 |
|
| 468 |
for fmt, expected_error in formats: |
| 469 |
with self.assertRaises(Error) as cm: |
| 470 |
bitstruct.compile(fmt) |
| 471 |
|
| 472 |
self.assertEqual(str(cm.exception), expected_error) |
| 473 |
|
| 474 |
def test_empty_format(self): |
| 475 |
"""Test of empty format type. |
| 476 |
|
| 477 |
""" |
| 478 |
|
| 479 |
cf = bitstruct.compile('') |
| 480 |
|
| 481 |
self.assertEqual(cf.pack(), b'') |
| 482 |
self.assertEqual(cf.pack(1), b'') |
| 483 |
|
| 484 |
self.assertEqual(cf.unpack(b''), ()) |
| 485 |
self.assertEqual(cf.unpack(b'\x00'), ()) |
| 486 |
|
| 487 |
def test_byte_order_format(self): |
| 488 |
"""Test of a format with only byte order information. |
| 489 |
|
| 490 |
""" |
| 491 |
|
| 492 |
cf = bitstruct.compile('>') |
| 493 |
|
| 494 |
self.assertEqual(cf.pack(), b'') |
| 495 |
self.assertEqual(cf.pack(1), b'') |
| 496 |
|
| 497 |
self.assertEqual(cf.unpack(b''), ()) |
| 498 |
self.assertEqual(cf.unpack(b'\x00'), ()) |
| 499 |
|
| 500 |
def test_pack_into(self): |
| 501 |
"""Pack values into a buffer. |
| 502 |
|
| 503 |
""" |
| 504 |
|
| 505 |
packed = bytearray(3) |
| 506 |
pack_into('u1u1s6u7u9', packed, 0, 0, 0, -2, 65, 22) |
| 507 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 508 |
|
| 509 |
datas = [ |
| 510 |
(0, b'\x80\x00'), |
| 511 |
(1, b'\x40\x00'), |
| 512 |
(7, b'\x01\x00'), |
| 513 |
(15, b'\x00\x01') |
| 514 |
] |
| 515 |
|
| 516 |
for offset, expected in datas: |
| 517 |
packed = bytearray(2) |
| 518 |
pack_into('u1', packed, offset, 1) |
| 519 |
self.assertEqual(packed, expected) |
| 520 |
|
| 521 |
packed = bytearray(b'\xff\xff\xff') |
| 522 |
pack_into('p4u4p4u4p4u4', packed, 0, 1, 2, 3, fill_padding=False) |
| 523 |
self.assertEqual(packed, b'\xf1\xf2\xf3') |
| 524 |
|
| 525 |
packed = bytearray(b'\xff\xff\xff') |
| 526 |
pack_into('p4u4p4u4p4u4', packed, 0, 1, 2, 3, fill_padding=True) |
| 527 |
self.assertEqual(packed, b'\x01\x02\x03') |
| 528 |
|
| 529 |
packed = bytearray(2) |
| 530 |
|
| 531 |
with self.assertRaises(Error) as cm: |
| 532 |
pack_into('u17', packed, 0, 1) |
| 533 |
|
| 534 |
self.assertEqual(str(cm.exception), |
| 535 |
'pack_into requires a buffer of at least 17 bits') |
| 536 |
|
| 537 |
packed = bytearray(b'\x00') |
| 538 |
pack_into('P4u4', packed, 0, 1) |
| 539 |
self.assertEqual(packed, b'\xf1') |
| 540 |
|
| 541 |
# Too many values to pack. |
| 542 |
with self.assertRaises(Error) as cm: |
| 543 |
packed = bytearray(b'\x00') |
| 544 |
pack_into('b1t24', packed, 0, False) |
| 545 |
|
| 546 |
self.assertEqual(str(cm.exception), |
| 547 |
'pack expected 2 item(s) for packing (got 1)') |
| 548 |
|
| 549 |
def test_unpack_from(self): |
| 550 |
"""Unpack values at given bit offset. |
| 551 |
|
| 552 |
""" |
| 553 |
|
| 554 |
unpacked = unpack_from('u1u1s6u7u9', b'\x1f\x41\x0b\x00', 1) |
| 555 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 556 |
|
| 557 |
with self.assertRaises(Error) as cm: |
| 558 |
unpack_from('u1u1s6u7u9', b'\x1f\x41\x0b', 1) |
| 559 |
|
| 560 |
self.assertEqual(str(cm.exception), |
| 561 |
'unpack requires at least 24 bits to unpack ' |
| 562 |
'(got 23)') |
| 563 |
|
| 564 |
def test_pack_integers_value_checks(self): |
| 565 |
"""Pack integer values range checks. |
| 566 |
|
| 567 |
""" |
| 568 |
|
| 569 |
# Formats with minimum and maximum allowed values. |
| 570 |
datas = [ |
| 571 |
('s1', -1, 0), |
| 572 |
('s2', -2, 1), |
| 573 |
('s3', -4, 3), |
| 574 |
('u1', 0, 1), |
| 575 |
('u2', 0, 3), |
| 576 |
('u3', 0, 7) |
| 577 |
] |
| 578 |
|
| 579 |
for fmt, minimum, maximum in datas: |
| 580 |
# No exception should be raised for numbers in range. |
| 581 |
pack(fmt, minimum) |
| 582 |
pack(fmt, maximum) |
| 583 |
|
| 584 |
# Numbers out of range. |
| 585 |
for number in [minimum - 1, maximum + 1]: |
| 586 |
with self.assertRaises(Error) as cm: |
| 587 |
pack(fmt, number) |
| 588 |
|
| 589 |
self.assertEqual( |
| 590 |
str(cm.exception), |
| 591 |
'"{}" requires {} <= integer <= {} (got {})'.format( |
| 592 |
fmt, |
| 593 |
minimum, |
| 594 |
maximum, |
| 595 |
number)) |
| 596 |
|
| 597 |
def test_pack_unpack_raw(self): |
| 598 |
"""Pack and unpack raw values. |
| 599 |
|
| 600 |
""" |
| 601 |
|
| 602 |
packed = pack('r24', b'') |
| 603 |
self.assertEqual(packed, b'\x00\x00\x00') |
| 604 |
packed = pack('r24', b'12') |
| 605 |
self.assertEqual(packed, b'12\x00') |
| 606 |
packed = pack('r24', b'123') |
| 607 |
self.assertEqual(packed, b'123') |
| 608 |
packed = pack('r24', b'1234') |
| 609 |
self.assertEqual(packed, b'123') |
| 610 |
|
| 611 |
unpacked = unpack('r24', b'\x00\x00\x00')[0] |
| 612 |
self.assertEqual(unpacked, b'\x00\x00\x00') |
| 613 |
unpacked = unpack('r24', b'12\x00')[0] |
| 614 |
self.assertEqual(unpacked, b'12\x00') |
| 615 |
unpacked = unpack('r24', b'123')[0] |
| 616 |
self.assertEqual(unpacked, b'123') |
| 617 |
unpacked = unpack('r24', b'1234')[0] |
| 618 |
self.assertEqual(unpacked, b'123') |
| 619 |
|
| 620 |
def test_pack_unpack_text(self): |
| 621 |
"""Pack and unpack text values. |
| 622 |
|
| 623 |
""" |
| 624 |
|
| 625 |
packed = pack('t24', '') |
| 626 |
self.assertEqual(packed, b'\x00\x00\x00') |
| 627 |
packed = pack('t24', '12') |
| 628 |
self.assertEqual(packed, b'12\x00') |
| 629 |
packed = pack('t24', '123') |
| 630 |
self.assertEqual(packed, b'123') |
| 631 |
packed = pack('t24', '1234') |
| 632 |
self.assertEqual(packed, b'123') |
| 633 |
|
| 634 |
unpacked = unpack('t24', b'\x00\x00\x00')[0] |
| 635 |
self.assertEqual(unpacked, '\x00\x00\x00') |
| 636 |
unpacked = unpack('t24', b'12\x00')[0] |
| 637 |
self.assertEqual(unpacked, '12\x00') |
| 638 |
unpacked = unpack('t24', b'123')[0] |
| 639 |
self.assertEqual(unpacked, '123') |
| 640 |
unpacked = unpack('t24', b'1234')[0] |
| 641 |
self.assertEqual(unpacked, '123') |
| 642 |
|
| 643 |
unpacked = unpack('t8', b'\xff', text_errors='replace')[0] |
| 644 |
self.assertEqual(unpacked, '�') |
| 645 |
unpacked = unpack('t8', b'\xff', text_errors='ignore')[0] |
| 646 |
self.assertEqual(unpacked, '') |
| 647 |
|
| 648 |
cf = bitstruct.compile('t8', text_encoding='latin-1', text_errors='replace') |
| 649 |
unpacked = cf.unpack(b'\xff')[0] |
| 650 |
self.assertEqual(unpacked, 'ÿ') |
| 651 |
|
| 652 |
cf = bitstruct.compile('t8', text_encoding='utf-8', text_errors='ignore') |
| 653 |
unpacked = cf.unpack(b'\xff')[0] |
| 654 |
self.assertEqual(unpacked, '') |
| 655 |
|
| 656 |
cf = bitstruct.compile('t8', |
| 657 |
names=['a'], |
| 658 |
text_encoding='utf-8', |
| 659 |
text_errors='replace') |
| 660 |
unpacked = cf.unpack(b'\xff') |
| 661 |
self.assertEqual(unpacked, {'a': '�'}) |
| 662 |
|
| 663 |
def test_pack_unpack_dict(self): |
| 664 |
unpacked = { |
| 665 |
'foo': 0, |
| 666 |
'bar': 0, |
| 667 |
'fie': -2, |
| 668 |
'fum': 65, |
| 669 |
'fam': 22 |
| 670 |
} |
| 671 |
packed = b'\x3e\x82\x16' |
| 672 |
fmt = 'u1u1s6u7u9' |
| 673 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 674 |
|
| 675 |
self.assertEqual(pack_dict(fmt, names, unpacked), packed) |
| 676 |
self.assertEqual(unpack_dict(fmt, names, packed), unpacked) |
| 677 |
|
| 678 |
def test_pack_into_unpack_from_dict(self): |
| 679 |
unpacked = { |
| 680 |
'foo': 0, |
| 681 |
'bar': 0, |
| 682 |
'fie': -2, |
| 683 |
'fum': 65, |
| 684 |
'fam': 22 |
| 685 |
} |
| 686 |
packed = b'\x3e\x82\x16' |
| 687 |
fmt = 'u1u1s6u7u9' |
| 688 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 689 |
|
| 690 |
actual = bytearray(3) |
| 691 |
pack_into_dict(fmt, names, actual, 0, unpacked) |
| 692 |
self.assertEqual(actual, packed) |
| 693 |
self.assertEqual(unpack_from_dict(fmt, names, packed), unpacked) |
| 694 |
|
| 695 |
def test_pack_dict_missing_key(self): |
| 696 |
unpacked = { |
| 697 |
'foo': 0, |
| 698 |
'bar': 0, |
| 699 |
'fie': -2, |
| 700 |
'fum': 65 |
| 701 |
} |
| 702 |
fmt = 'u1u1s6u7u9' |
| 703 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 704 |
|
| 705 |
with self.assertRaises(Error) as cm: |
| 706 |
pack_dict(fmt, names, unpacked) |
| 707 |
|
| 708 |
self.assertEqual(str(cm.exception), |
| 709 |
"'fam' not found in data dictionary") |
| 710 |
|
| 711 |
with self.assertRaises(Error) as cm: |
| 712 |
data = bytearray(3) |
| 713 |
pack_into_dict(fmt, names, data, 0, unpacked) |
| 714 |
|
| 715 |
self.assertEqual(str(cm.exception), |
| 716 |
"'fam' not found in data dictionary") |
| 717 |
|
| 718 |
def test_compile_pack_unpack_formats(self): |
| 719 |
fmts = [ |
| 720 |
('u1s2p3', None, (1, -1)), |
| 721 |
('u1 s2 p3', None, (1, -1)), |
| 722 |
('u1s2p3', ['a', 'b'], {'a': 1, 'b': -1}) |
| 723 |
] |
| 724 |
|
| 725 |
for fmt, names, decoded in fmts: |
| 726 |
if names is None: |
| 727 |
cf = bitstruct.compile(fmt) |
| 728 |
packed_1 = cf.pack(*decoded) |
| 729 |
packed_2 = pack(fmt, *decoded) |
| 730 |
else: |
| 731 |
cf = bitstruct.compile(fmt, names) |
| 732 |
packed_1 = cf.pack(decoded) |
| 733 |
packed_2 = pack_dict(fmt, names, decoded) |
| 734 |
|
| 735 |
self.assertEqual(packed_1, b'\xe0') |
| 736 |
self.assertEqual(packed_2, b'\xe0') |
| 737 |
|
| 738 |
def test_compile_formats(self): |
| 739 |
bitstruct.compile('p1u1') |
| 740 |
bitstruct.compile('p1u1', ['a']) |
| 741 |
|
| 742 |
def test_pack_unpack_signed(self): |
| 743 |
datas = [ |
| 744 |
('s1', 0, b'\x00'), |
| 745 |
('s1', -1, b'\x80'), |
| 746 |
('s63', -1, b'\xff\xff\xff\xff\xff\xff\xff\xfe'), |
| 747 |
('s64', -1, b'\xff\xff\xff\xff\xff\xff\xff\xff') |
| 748 |
] |
| 749 |
|
| 750 |
for fmt, value, packed in datas: |
| 751 |
self.assertEqual(pack(fmt, value), packed) |
| 752 |
self.assertEqual(unpack(fmt, packed), (value, )) |
| 753 |
|
| 754 |
def test_pack_unpack_unsigned(self): |
| 755 |
datas = [ |
| 756 |
('u1', 0, b'\x00'), |
| 757 |
('u1', 1, b'\x80'), |
| 758 |
('u63', 0x1234567890abcdef, b'$h\xac\xf1!W\x9b\xde'), |
| 759 |
('u64', 0x1234567890abcdef, b'\x124Vx\x90\xab\xcd\xef') |
| 760 |
] |
| 761 |
|
| 762 |
for fmt, value, packed in datas: |
| 763 |
self.assertEqual(pack(fmt, value), packed) |
| 764 |
self.assertEqual(unpack(fmt, packed), (value, )) |
| 765 |
|
| 766 |
|
| 767 |
if __name__ == '__main__': |
| 768 |
unittest.main() |