| 1 |
from __future__ import print_function |
| 2 |
import sys |
| 3 |
import timeit |
| 4 |
import pickle |
| 5 |
import unittest |
| 6 |
import platform |
| 7 |
import copy |
| 8 |
|
| 9 |
|
| 10 |
def is_cpython_3(): |
| 11 |
if platform.python_implementation() != 'CPython': |
| 12 |
return False |
| 13 |
|
| 14 |
if sys.version_info[0] < 3: |
| 15 |
return False |
| 16 |
|
| 17 |
return True |
| 18 |
|
| 19 |
|
| 20 |
if is_cpython_3(): |
| 21 |
import bitstruct.c |
| 22 |
from bitstruct.c import * |
| 23 |
else: |
| 24 |
print('Skipping C extension tests for non-CPython 3.') |
| 25 |
|
| 26 |
|
| 27 |
class CTest(unittest.TestCase): |
| 28 |
|
| 29 |
def test_pack(self): |
| 30 |
"""Pack values. |
| 31 |
|
| 32 |
""" |
| 33 |
|
| 34 |
if not is_cpython_3(): |
| 35 |
return |
| 36 |
|
| 37 |
packed = pack('u1u1s6u7u9', 0, 0, -2, 65, 22) |
| 38 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 39 |
|
| 40 |
packed = pack('u1', 1) |
| 41 |
self.assertEqual(packed, b'\x80') |
| 42 |
|
| 43 |
with self.assertRaises(NotImplementedError): |
| 44 |
packed = pack('u77', 0x100000000001000000) |
| 45 |
ref = b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00' |
| 46 |
self.assertEqual(packed, ref) |
| 47 |
|
| 48 |
with self.assertRaises(NotImplementedError): |
| 49 |
packed = pack('u8000', int(8000 * '1', 2)) |
| 50 |
ref = 1000 * b'\xff' |
| 51 |
self.assertEqual(packed, ref) |
| 52 |
|
| 53 |
with self.assertRaises(NotImplementedError): |
| 54 |
packed = pack('s4000', int(8000 * '0', 2)) |
| 55 |
ref = 500 * b'\x00' |
| 56 |
self.assertEqual(packed, ref) |
| 57 |
|
| 58 |
packed = pack('p1u1s6u7u9', 0, -2, 65, 22) |
| 59 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 60 |
|
| 61 |
packed = pack('P1u1s6u7u9', 0, -2, 65, 22) |
| 62 |
self.assertEqual(packed, b'\xbe\x82\x16') |
| 63 |
|
| 64 |
packed = pack('p1u1s6p7u9', 0, -2, 22) |
| 65 |
self.assertEqual(packed, b'\x3e\x00\x16') |
| 66 |
|
| 67 |
packed = pack('P1u1s6p7u9', 0, -2, 22) |
| 68 |
self.assertEqual(packed, b'\xbe\x00\x16') |
| 69 |
|
| 70 |
with self.assertRaises(NotImplementedError): |
| 71 |
packed = pack('u1s6f32r43', 0, -2, 3.75, b'\x00\xff\x00\xff\x00\xff') |
| 72 |
self.assertEqual(packed, b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0') |
| 73 |
|
| 74 |
packed = pack('b1', True) |
| 75 |
self.assertEqual(packed, b'\x80') |
| 76 |
|
| 77 |
packed = pack('b1p6b1', True, True) |
| 78 |
self.assertEqual(packed, b'\x81') |
| 79 |
|
| 80 |
packed = pack('b1P6b1', True, True) |
| 81 |
self.assertEqual(packed, b'\xff') |
| 82 |
|
| 83 |
packed = pack('u5b2u1', 31, False, 1) |
| 84 |
self.assertEqual(packed, b'\xf9') |
| 85 |
|
| 86 |
packed = pack('b1t24', False, u'Hi!') |
| 87 |
self.assertEqual(packed, b'$4\x90\x80') |
| 88 |
|
| 89 |
packed = pack('b1t24', False, 'Hi!') |
| 90 |
self.assertEqual(packed, b'$4\x90\x80') |
| 91 |
|
| 92 |
packed = pack('t8000', 1000 * '7') |
| 93 |
self.assertEqual(packed, 1000 * b'\x37') |
| 94 |
|
| 95 |
if sys.version_info >= (3, 6): |
| 96 |
packed = pack('f16', 1.0) |
| 97 |
self.assertEqual(packed, b'\x3c\x00') |
| 98 |
|
| 99 |
packed = pack('f32', 1.0) |
| 100 |
self.assertEqual(packed, b'\x3f\x80\x00\x00') |
| 101 |
|
| 102 |
packed = pack('f64', 1.0) |
| 103 |
self.assertEqual(packed, b'\x3f\xf0\x00\x00\x00\x00\x00\x00') |
| 104 |
|
| 105 |
def test_unpack(self): |
| 106 |
"""Unpack values. |
| 107 |
|
| 108 |
""" |
| 109 |
|
| 110 |
if not is_cpython_3(): |
| 111 |
return |
| 112 |
|
| 113 |
unpacked = unpack('u1u1s6u7u9', b'\x3e\x82\x16') |
| 114 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 115 |
|
| 116 |
# unpacked = unpack('u1', bytearray(b'\x80')) |
| 117 |
unpacked = unpack('u1', b'\x80') |
| 118 |
self.assertEqual(unpacked, (1, )) |
| 119 |
|
| 120 |
with self.assertRaises(NotImplementedError): |
| 121 |
packed = b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00' |
| 122 |
unpacked = unpack('u77', packed) |
| 123 |
self.assertEqual(unpacked, (0x100000000001000000,)) |
| 124 |
|
| 125 |
with self.assertRaises(NotImplementedError): |
| 126 |
packed = 1000 * b'\xff' |
| 127 |
unpacked = unpack('u8000', packed) |
| 128 |
self.assertEqual(unpacked, (int(8000 * '1', 2), )) |
| 129 |
|
| 130 |
with self.assertRaises(NotImplementedError): |
| 131 |
packed = 500 * b'\x00' |
| 132 |
unpacked = unpack('s4000', packed) |
| 133 |
self.assertEqual(unpacked, (0, )) |
| 134 |
|
| 135 |
packed = b'\xbe\x82\x16' |
| 136 |
unpacked = unpack('P1u1s6u7u9', packed) |
| 137 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 138 |
|
| 139 |
packed = b'\x3e\x82\x16' |
| 140 |
unpacked = unpack('P1u1s6u7u9', packed) |
| 141 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 142 |
|
| 143 |
packed = b'\xbe\x82\x16' |
| 144 |
unpacked = unpack('p1u1s6u7u9', packed) |
| 145 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 146 |
|
| 147 |
packed = b'\x3e\x82\x16' |
| 148 |
unpacked = unpack('p1u1s6u7u9', packed) |
| 149 |
self.assertEqual(unpacked, (0, -2, 65, 22)) |
| 150 |
|
| 151 |
packed = b'\x3e\x82\x16' |
| 152 |
unpacked = unpack('p1u1s6p7u9', packed) |
| 153 |
self.assertEqual(unpacked, (0, -2, 22)) |
| 154 |
|
| 155 |
with self.assertRaises(NotImplementedError): |
| 156 |
packed = b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0' |
| 157 |
unpacked = unpack('u1s6f32r43', packed) |
| 158 |
self.assertEqual(unpacked, (0, -2, 3.75, b'\x00\xff\x00\xff\x00\xe0')) |
| 159 |
|
| 160 |
# packed = bytearray(b'\x80') |
| 161 |
packed = b'\x80' |
| 162 |
unpacked = unpack('b1', packed) |
| 163 |
self.assertEqual(unpacked, (True, )) |
| 164 |
|
| 165 |
packed = b'\x80' |
| 166 |
unpacked = unpack('b1p6b1', packed) |
| 167 |
self.assertEqual(unpacked, (True, False)) |
| 168 |
|
| 169 |
packed = b'\x06' |
| 170 |
unpacked = unpack('u5b2u1', packed) |
| 171 |
self.assertEqual(unpacked, (0, True, 0)) |
| 172 |
|
| 173 |
packed = b'\x04' |
| 174 |
unpacked = unpack('u5b2u1', packed) |
| 175 |
self.assertEqual(unpacked, (0, True, 0)) |
| 176 |
|
| 177 |
packed = b'$4\x90\x80' |
| 178 |
unpacked = unpack('b1t24', packed) |
| 179 |
self.assertEqual(unpacked, (False, u'Hi!')) |
| 180 |
|
| 181 |
packed = 1000 * b'7' |
| 182 |
unpacked = unpack('t8000', packed) |
| 183 |
self.assertEqual(packed, 1000 * b'\x37') |
| 184 |
|
| 185 |
if sys.version_info >= (3, 6): |
| 186 |
unpacked = unpack('f16', b'\x3c\x00') |
| 187 |
self.assertEqual(unpacked, (1.0, )) |
| 188 |
|
| 189 |
unpacked = unpack('f32', b'\x3f\x80\x00\x00') |
| 190 |
self.assertEqual(unpacked, (1.0, )) |
| 191 |
|
| 192 |
unpacked = unpack('f64', b'\x3f\xf0\x00\x00\x00\x00\x00\x00') |
| 193 |
self.assertEqual(unpacked, (1.0, )) |
| 194 |
|
| 195 |
# Too many bits to unpack. |
| 196 |
with self.assertRaises(ValueError) as cm: |
| 197 |
unpack('u9', b'\x00') |
| 198 |
|
| 199 |
self.assertEqual(str(cm.exception), |
| 200 |
'Short data.') |
| 201 |
|
| 202 |
# partial unpacking of truncated data |
| 203 |
unpacked = unpack('u4u5', b'\x5f', allow_truncated=True) |
| 204 |
self.assertEqual(unpacked, (5, )) |
| 205 |
unpacked = unpack('p8u4u5', b'\x00\x5f', allow_truncated=True) |
| 206 |
self.assertEqual(unpacked, (5, )) |
| 207 |
|
| 208 |
# test bytearray unpacking |
| 209 |
unpacked = unpack('u1u1s6u7u9', bytearray(b'\x3e\x82\x16')) |
| 210 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 211 |
|
| 212 |
def test_pack_unpack(self): |
| 213 |
"""Pack and unpack values. |
| 214 |
|
| 215 |
""" |
| 216 |
|
| 217 |
if not is_cpython_3(): |
| 218 |
return |
| 219 |
|
| 220 |
packed = pack('u1u1s6u7u9', 0, 0, -2, 65, 22) |
| 221 |
unpacked = unpack('u1u1s6u7u9', packed) |
| 222 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 223 |
|
| 224 |
packed = pack('f64', 1.0) |
| 225 |
unpacked = unpack('f64', packed) |
| 226 |
self.assertEqual(unpacked, (1.0, )) |
| 227 |
|
| 228 |
if sys.version_info >= (3, 6): |
| 229 |
packed = pack('f16', 1.0) |
| 230 |
unpacked = unpack('f16', packed) |
| 231 |
self.assertEqual(unpacked, (1.0, )) |
| 232 |
|
| 233 |
def test_calcsize(self): |
| 234 |
"""Calculate size. |
| 235 |
|
| 236 |
""" |
| 237 |
|
| 238 |
if not is_cpython_3(): |
| 239 |
return |
| 240 |
|
| 241 |
size = calcsize('u1u1s6u7u9') |
| 242 |
self.assertEqual(size, 24) |
| 243 |
|
| 244 |
size = calcsize('u1') |
| 245 |
self.assertEqual(size, 1) |
| 246 |
|
| 247 |
size = calcsize('u1s6u7u9') |
| 248 |
self.assertEqual(size, 23) |
| 249 |
|
| 250 |
size = calcsize('b1s6u7u9p1t8') |
| 251 |
self.assertEqual(size, 32) |
| 252 |
|
| 253 |
size = calcsize('b1s6u7u9P1t8') |
| 254 |
self.assertEqual(size, 32) |
| 255 |
|
| 256 |
# The total number of bits does not fit in an int. |
| 257 |
with self.assertRaises(ValueError) as cm: |
| 258 |
calcsize('p214748369' * 20) |
| 259 |
|
| 260 |
self.assertEqual(str(cm.exception), 'Format too long.') |
| 261 |
|
| 262 |
def test_compiled_calcsize(self): |
| 263 |
"""Calculate size. |
| 264 |
|
| 265 |
""" |
| 266 |
|
| 267 |
if not is_cpython_3(): |
| 268 |
return |
| 269 |
|
| 270 |
cf = bitstruct.c.compile('u1u1s6u7u9') |
| 271 |
self.assertEqual(cf.calcsize(), 24) |
| 272 |
|
| 273 |
cf = bitstruct.c.compile('u1u1s6u7u9', ['a', 'b', 'c', 'd', 'e']) |
| 274 |
self.assertEqual(cf.calcsize(), 24) |
| 275 |
|
| 276 |
def test_byteswap(self): |
| 277 |
"""Byte swap. |
| 278 |
|
| 279 |
""" |
| 280 |
|
| 281 |
if not is_cpython_3(): |
| 282 |
return |
| 283 |
|
| 284 |
res = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a' |
| 285 |
ref = b'\x01\x03\x02\x04\x08\x07\x06\x05\x0a\x09' |
| 286 |
self.assertEqual(byteswap('12142', ref), res) |
| 287 |
|
| 288 |
packed = pack('u1u5u2u16', 1, 2, 3, 4) |
| 289 |
unpacked = unpack('u1u5u2u16', byteswap('12', packed)) |
| 290 |
self.assertEqual(unpacked, (1, 2, 3, 1024)) |
| 291 |
|
| 292 |
res = b'\x01\x02\x03\x04\x05\x06\x07\x08' |
| 293 |
ref = b'\x08\x07\x06\x05\x04\x03\x02\x01' |
| 294 |
self.assertEqual(byteswap('8', ref), res) |
| 295 |
|
| 296 |
def test_pack_into(self): |
| 297 |
"""Pack values into a buffer. |
| 298 |
|
| 299 |
""" |
| 300 |
|
| 301 |
if not is_cpython_3(): |
| 302 |
return |
| 303 |
|
| 304 |
packed = bytearray(3) |
| 305 |
pack_into('u1u1s6u7u9', packed, 0, 0, 0, -2, 65, 22) |
| 306 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 307 |
|
| 308 |
datas = [ |
| 309 |
(0, b'\x80\x00'), |
| 310 |
(1, b'\x40\x00'), |
| 311 |
(7, b'\x01\x00'), |
| 312 |
(15, b'\x00\x01') |
| 313 |
] |
| 314 |
|
| 315 |
for offset, expected in datas: |
| 316 |
packed = bytearray(2) |
| 317 |
pack_into('u1', packed, offset, 1) |
| 318 |
self.assertEqual(packed, expected) |
| 319 |
|
| 320 |
with self.assertRaises(AssertionError): |
| 321 |
packed = bytearray(b'\xff\xff\xff') |
| 322 |
pack_into('p4u4p4u4p4u4', packed, 0, 1, 2, 3, fill_padding=False) |
| 323 |
self.assertEqual(packed, b'\xf1\xf2\xf3') |
| 324 |
|
| 325 |
packed = bytearray(b'\xff\xff\xff') |
| 326 |
pack_into('p4u4p4u4p4u4', packed, 0, 1, 2, 3, fill_padding=True) |
| 327 |
self.assertEqual(packed, b'\x01\x02\x03') |
| 328 |
|
| 329 |
packed = bytearray(2) |
| 330 |
|
| 331 |
with self.assertRaises(ValueError) as cm: |
| 332 |
pack_into('u17', packed, 0, 1) |
| 333 |
|
| 334 |
self.assertEqual(str(cm.exception), |
| 335 |
'pack_into requires a buffer of at least 17 bits') |
| 336 |
|
| 337 |
packed = bytearray(b'\x00') |
| 338 |
pack_into('P4u4', packed, 0, 1) |
| 339 |
self.assertEqual(packed, b'\xf1') |
| 340 |
|
| 341 |
datas = [ |
| 342 |
(0, b'\x7f\xff\xff\xff'), |
| 343 |
(1, b'\xbf\xff\xff\xff'), |
| 344 |
(7, b'\xfe\xff\xff\xff'), |
| 345 |
(8, b'\xff\x7f\xff\xff'), |
| 346 |
(15, b'\xff\xfe\xff\xff'), |
| 347 |
(16, b'\xff\xff\x7f\xff'), |
| 348 |
(31, b'\xff\xff\xff\xfe') |
| 349 |
] |
| 350 |
|
| 351 |
for offset, expected in datas: |
| 352 |
packed = bytearray(b'\xff\xff\xff\xff') |
| 353 |
pack_into('u1', packed, offset, 0) |
| 354 |
self.assertEqual(packed, expected) |
| 355 |
|
| 356 |
# Check for non-writable buffer. |
| 357 |
with self.assertRaises(TypeError): |
| 358 |
pack_into('u1', b'\x00', 0, 0) |
| 359 |
|
| 360 |
def test_unpack_from(self): |
| 361 |
"""Unpack values at given bit offset. |
| 362 |
|
| 363 |
""" |
| 364 |
|
| 365 |
if not is_cpython_3(): |
| 366 |
return |
| 367 |
|
| 368 |
unpacked = unpack_from('u1u1s6u7u9', b'\x1f\x41\x0b\x00', 1) |
| 369 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 370 |
|
| 371 |
unpacked = unpack_from('u1', b'\x80') |
| 372 |
self.assertEqual(unpacked, (1, )) |
| 373 |
|
| 374 |
unpacked = unpack_from('u1', b'\x08', 4) |
| 375 |
self.assertEqual(unpacked, (1, )) |
| 376 |
|
| 377 |
with self.assertRaises(ValueError): |
| 378 |
unpack_from('u1', b'\xff', 8) |
| 379 |
|
| 380 |
with self.assertRaises(TypeError): |
| 381 |
unpack_from('u1', b'\xff', None) |
| 382 |
|
| 383 |
def test_compiled_pack_into(self): |
| 384 |
"""Pack values at given bit offset. |
| 385 |
|
| 386 |
""" |
| 387 |
|
| 388 |
if not is_cpython_3(): |
| 389 |
return |
| 390 |
|
| 391 |
cf = bitstruct.c.compile('u2') |
| 392 |
packed = bytearray(2) |
| 393 |
cf.pack_into(packed, 7, 3) |
| 394 |
self.assertEqual(packed, b'\x01\x80') |
| 395 |
|
| 396 |
def test_compiled_unpack_from(self): |
| 397 |
"""Unpack values at given bit offset. |
| 398 |
|
| 399 |
""" |
| 400 |
|
| 401 |
if not is_cpython_3(): |
| 402 |
return |
| 403 |
|
| 404 |
cf = bitstruct.c.compile('u1') |
| 405 |
unpacked = cf.unpack_from(b'\x40', 1) |
| 406 |
self.assertEqual(unpacked, (1, )) |
| 407 |
|
| 408 |
unpacked = cf.unpack_from(b'\x80') |
| 409 |
self.assertEqual(unpacked, (1, )) |
| 410 |
|
| 411 |
def test_pack_unpack_raw(self): |
| 412 |
"""Pack and unpack raw values. |
| 413 |
|
| 414 |
""" |
| 415 |
|
| 416 |
if not is_cpython_3(): |
| 417 |
return |
| 418 |
|
| 419 |
with self.assertRaises(NotImplementedError): |
| 420 |
packed = pack('r24', b'') |
| 421 |
self.assertEqual(packed, b'\x00\x00\x00') |
| 422 |
|
| 423 |
with self.assertRaises(NotImplementedError): |
| 424 |
packed = pack('r24', b'12') |
| 425 |
self.assertEqual(packed, b'12\x00') |
| 426 |
|
| 427 |
packed = pack('r24', b'123') |
| 428 |
self.assertEqual(packed, b'123') |
| 429 |
packed = pack('r24', b'1234') |
| 430 |
self.assertEqual(packed, b'123') |
| 431 |
|
| 432 |
unpacked = unpack('r24', b'\x00\x00\x00')[0] |
| 433 |
self.assertEqual(unpacked, b'\x00\x00\x00') |
| 434 |
unpacked = unpack('r24', b'12\x00')[0] |
| 435 |
self.assertEqual(unpacked, b'12\x00') |
| 436 |
unpacked = unpack('r24', b'123')[0] |
| 437 |
self.assertEqual(unpacked, b'123') |
| 438 |
unpacked = unpack('r24', b'1234')[0] |
| 439 |
self.assertEqual(unpacked, b'123') |
| 440 |
|
| 441 |
def test_pack_unpack_dict(self): |
| 442 |
if not is_cpython_3(): |
| 443 |
return |
| 444 |
|
| 445 |
unpacked = { |
| 446 |
'foo': 0, |
| 447 |
'bar': 0, |
| 448 |
'fie': -2, |
| 449 |
'fum': 65, |
| 450 |
'fam': 22 |
| 451 |
} |
| 452 |
packed = b'\x3e\x82\x16' |
| 453 |
fmt = 'u1u1s6u7u9' |
| 454 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 455 |
|
| 456 |
self.assertEqual(pack_dict(fmt, names, unpacked), packed) |
| 457 |
self.assertEqual(unpack_dict(fmt, names, packed), unpacked) |
| 458 |
|
| 459 |
def test_pack_into_unpack_from_dict(self): |
| 460 |
if not is_cpython_3(): |
| 461 |
return |
| 462 |
|
| 463 |
unpacked = { |
| 464 |
'foo': 0, |
| 465 |
'bar': 0, |
| 466 |
'fie': -2, |
| 467 |
'fum': 65, |
| 468 |
'fam': 22 |
| 469 |
} |
| 470 |
packed = b'\x3e\x82\x16' |
| 471 |
fmt = 'u1u1s6u7u9' |
| 472 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 473 |
|
| 474 |
actual = bytearray(3) |
| 475 |
pack_into_dict(fmt, names, actual, 0, unpacked) |
| 476 |
self.assertEqual(actual, packed) |
| 477 |
|
| 478 |
self.assertEqual(unpack_from_dict(fmt, names, packed), unpacked) |
| 479 |
|
| 480 |
def test_compiled_pack_into_unpack_from_dict(self): |
| 481 |
if not is_cpython_3(): |
| 482 |
return |
| 483 |
|
| 484 |
unpacked = { |
| 485 |
'foo': 0, |
| 486 |
'bar': 0, |
| 487 |
'fie': -2, |
| 488 |
'fum': 65, |
| 489 |
'fam': 22 |
| 490 |
} |
| 491 |
packed = b'\x3e\x82\x16' |
| 492 |
fmt = 'u1u1s6u7u9' |
| 493 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 494 |
cf = bitstruct.c.compile(fmt, names) |
| 495 |
|
| 496 |
actual = bytearray(3) |
| 497 |
cf.pack_into(actual, 0, unpacked) |
| 498 |
self.assertEqual(actual, packed) |
| 499 |
|
| 500 |
self.assertEqual(cf.unpack_from(packed), unpacked) |
| 501 |
|
| 502 |
def test_compile(self): |
| 503 |
if not is_cpython_3(): |
| 504 |
return |
| 505 |
|
| 506 |
cf = bitstruct.c.compile('u1u1s6u7u9') |
| 507 |
|
| 508 |
packed = cf.pack(0, 0, -2, 65, 22) |
| 509 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 510 |
|
| 511 |
unpacked = cf.unpack(b'\x3e\x82\x16') |
| 512 |
self.assertEqual(unpacked, (0, 0, -2, 65, 22)) |
| 513 |
|
| 514 |
def test_compile_pack_unpack_formats(self): |
| 515 |
if not is_cpython_3(): |
| 516 |
return |
| 517 |
|
| 518 |
fmts = [ |
| 519 |
('u1s2p3', None, (1, -1)), |
| 520 |
('u1 s2 p3', None, (1, -1)), |
| 521 |
('u1s2p3', ['a', 'b'], {'a': 1, 'b': -1}) |
| 522 |
] |
| 523 |
|
| 524 |
for fmt, names, decoded in fmts: |
| 525 |
if names is None: |
| 526 |
cf = bitstruct.c.compile(fmt) |
| 527 |
packed_1 = cf.pack(*decoded) |
| 528 |
packed_2 = pack(fmt, *decoded) |
| 529 |
else: |
| 530 |
cf = bitstruct.c.compile(fmt, names) |
| 531 |
packed_1 = cf.pack(decoded) |
| 532 |
packed_2 = pack_dict(fmt, names, decoded) |
| 533 |
|
| 534 |
self.assertEqual(packed_1, b'\xe0') |
| 535 |
self.assertEqual(packed_2, b'\xe0') |
| 536 |
|
| 537 |
if names is None: |
| 538 |
unpacked_1 = cf.unpack(packed_1) |
| 539 |
unpacked_2 = unpack(fmt, packed_2) |
| 540 |
unpacked_3 = cf.unpack(bytearray(packed_1)) # test bytearray unpacking |
| 541 |
else: |
| 542 |
unpacked_1 = cf.unpack(packed_1) |
| 543 |
unpacked_2 = unpack_dict(fmt, names, packed_2) |
| 544 |
unpacked_3 = cf.unpack(bytearray(packed_1)) # test bytearray unpacking |
| 545 |
|
| 546 |
self.assertEqual(unpacked_1, decoded) |
| 547 |
self.assertEqual(unpacked_2, decoded) |
| 548 |
self.assertEqual(unpacked_3, decoded) |
| 549 |
|
| 550 |
def test_compile_formats(self): |
| 551 |
if not is_cpython_3(): |
| 552 |
return |
| 553 |
|
| 554 |
bitstruct.c.compile('p1u1') |
| 555 |
bitstruct.c.compile('p1u1', ['a']) |
| 556 |
|
| 557 |
with self.assertRaises(TypeError): |
| 558 |
bitstruct.c.compile() |
| 559 |
|
| 560 |
def test_pack_unpack_signed(self): |
| 561 |
if not is_cpython_3(): |
| 562 |
return |
| 563 |
|
| 564 |
datas = [ |
| 565 |
('s1', 0, b'\x00'), |
| 566 |
('s1', -1, b'\x80'), |
| 567 |
('s63', -1, b'\xff\xff\xff\xff\xff\xff\xff\xfe'), |
| 568 |
('s64', -1, b'\xff\xff\xff\xff\xff\xff\xff\xff') |
| 569 |
] |
| 570 |
|
| 571 |
for fmt, value, packed in datas: |
| 572 |
self.assertEqual(pack(fmt, value), packed) |
| 573 |
self.assertEqual(unpack(fmt, packed), (value, )) |
| 574 |
|
| 575 |
def test_pack_unpack_unsigned(self): |
| 576 |
if not is_cpython_3(): |
| 577 |
return |
| 578 |
|
| 579 |
datas = [ |
| 580 |
('u1', 0, b'\x00'), |
| 581 |
('u1', 1, b'\x80'), |
| 582 |
('u63', 0x1234567890abcdef, b'$h\xac\xf1!W\x9b\xde'), |
| 583 |
('u64', 0x1234567890abcdef, b'\x124Vx\x90\xab\xcd\xef') |
| 584 |
] |
| 585 |
|
| 586 |
for fmt, value, packed in datas: |
| 587 |
self.assertEqual(pack(fmt, value), packed) |
| 588 |
self.assertEqual(unpack(fmt, packed), (value, )) |
| 589 |
|
| 590 |
def test_various(self): |
| 591 |
if not is_cpython_3(): |
| 592 |
return |
| 593 |
|
| 594 |
with self.assertRaises(ValueError): |
| 595 |
pack('u89999888888888888888899', 1) |
| 596 |
|
| 597 |
# Fewer names than fields in the format. |
| 598 |
with self.assertRaises(ValueError): |
| 599 |
pack_dict('u1u1', ['foo'], {'foo': 1}) |
| 600 |
|
| 601 |
# Missing value for name. |
| 602 |
with self.assertRaises(KeyError): |
| 603 |
pack_dict('u1', ['foo'], {}) |
| 604 |
|
| 605 |
# Fewer names than fields in the format. |
| 606 |
with self.assertRaises(ValueError): |
| 607 |
unpack_dict('u1u1', ['foo'], b'\xff') |
| 608 |
|
| 609 |
# Short data. |
| 610 |
with self.assertRaises(ValueError): |
| 611 |
unpack_dict('u1', ['foo'], b'') |
| 612 |
|
| 613 |
# Padding last. |
| 614 |
self.assertEqual(pack('u1p1', 1), b'\x80') |
| 615 |
self.assertEqual(pack_dict('u1p1', ['foo'], {'foo': 1}), b'\x80') |
| 616 |
|
| 617 |
# Short text. |
| 618 |
with self.assertRaises(NotImplementedError) as cm: |
| 619 |
pack('t16', '1') |
| 620 |
|
| 621 |
self.assertEqual(str(cm.exception), 'Short text.') |
| 622 |
|
| 623 |
# Bad float length. |
| 624 |
with self.assertRaises(NotImplementedError) as cm: |
| 625 |
pack('f1', 1.0) |
| 626 |
|
| 627 |
if sys.version_info >= (3, 6): |
| 628 |
self.assertEqual(str(cm.exception), 'Float not 16, 32 or 64 bits.') |
| 629 |
else: |
| 630 |
self.assertEqual(str(cm.exception), 'Float not 32 or 64 bits.') |
| 631 |
|
| 632 |
# Long bool. |
| 633 |
with self.assertRaises(NotImplementedError) as cm: |
| 634 |
pack('b65', True) |
| 635 |
|
| 636 |
self.assertEqual(str(cm.exception), 'Bool over 64 bits.') |
| 637 |
|
| 638 |
# Text not multiple of 8 bits. |
| 639 |
with self.assertRaises(NotImplementedError) as cm: |
| 640 |
pack('t1', '') |
| 641 |
|
| 642 |
self.assertEqual(str(cm.exception), 'Text not multiple of 8 bits.') |
| 643 |
|
| 644 |
# Bad format kind. |
| 645 |
with self.assertRaises(ValueError) as cm: |
| 646 |
pack('x1', '') |
| 647 |
|
| 648 |
self.assertEqual(str(cm.exception), "Bad format field type 'x'.") |
| 649 |
|
| 650 |
# Too few arguments. |
| 651 |
with self.assertRaises(ValueError) as cm: |
| 652 |
pack('u1u1', 1) |
| 653 |
|
| 654 |
self.assertEqual(str(cm.exception), 'Too few arguments.') |
| 655 |
|
| 656 |
# No format string. |
| 657 |
with self.assertRaises(ValueError) as cm: |
| 658 |
pack() |
| 659 |
|
| 660 |
self.assertEqual(str(cm.exception), 'No format string.') |
| 661 |
|
| 662 |
# No format string. |
| 663 |
with self.assertRaises(ValueError) as cm: |
| 664 |
unpack('u1', b'') |
| 665 |
|
| 666 |
self.assertEqual(str(cm.exception), 'Short data.') |
| 667 |
|
| 668 |
# Bad format in compile. |
| 669 |
with self.assertRaises(ValueError) as cm: |
| 670 |
bitstruct.c.compile('x1') |
| 671 |
|
| 672 |
self.assertEqual(str(cm.exception), "Bad format field type 'x'.") |
| 673 |
|
| 674 |
# Bad format in compile dict. |
| 675 |
with self.assertRaises(ValueError) as cm: |
| 676 |
bitstruct.c.compile('x1', ['foo']) |
| 677 |
|
| 678 |
self.assertEqual(str(cm.exception), "Bad format field type 'x'.") |
| 679 |
|
| 680 |
# Offset too big. |
| 681 |
with self.assertRaises(ValueError) as cm: |
| 682 |
packed = bytearray(1) |
| 683 |
pack_into('u1', packed, 0x80000000, 1) |
| 684 |
|
| 685 |
self.assertIn("Offset must be less or equal to ", str(cm.exception)) |
| 686 |
|
| 687 |
# Offset too big. |
| 688 |
with self.assertRaises(ValueError) as cm: |
| 689 |
packed = bytearray(1) |
| 690 |
pack_into_dict('u1', ['a'], packed, 0x80000000, {'a': 1}) |
| 691 |
|
| 692 |
self.assertIn("Offset must be less or equal to ", str(cm.exception)) |
| 693 |
|
| 694 |
# Offset too big. |
| 695 |
with self.assertRaises(ValueError) as cm: |
| 696 |
unpack_from('u1', b'\x00', 0x80000000) |
| 697 |
|
| 698 |
self.assertIn("Offset must be less or equal to ", str(cm.exception)) |
| 699 |
|
| 700 |
# Offset too big. |
| 701 |
with self.assertRaises(ValueError) as cm: |
| 702 |
packed = bytearray(1) |
| 703 |
unpack_from_dict('u1', ['a'], b'\x00', 0x80000000) |
| 704 |
|
| 705 |
self.assertIn("Offset must be less or equal to ", str(cm.exception)) |
| 706 |
|
| 707 |
# Out of data to swap. |
| 708 |
datas = ['11', '2', '4', '8'] |
| 709 |
|
| 710 |
for fmt in datas: |
| 711 |
with self.assertRaises(ValueError) as cm: |
| 712 |
byteswap(fmt, b'\x00') |
| 713 |
|
| 714 |
self.assertEqual(str(cm.exception), 'Out of data to swap.') |
| 715 |
|
| 716 |
# Bad swap format. |
| 717 |
with self.assertRaises(ValueError) as cm: |
| 718 |
byteswap('3', b'\x00') |
| 719 |
|
| 720 |
# Bad swap format type. |
| 721 |
with self.assertRaises(TypeError): |
| 722 |
byteswap(None, b'\x00') |
| 723 |
|
| 724 |
self.assertEqual(str(cm.exception), 'Expected 1, 2, 4 or 8, but got 3.') |
| 725 |
|
| 726 |
# Bad format type. |
| 727 |
with self.assertRaises(TypeError): |
| 728 |
pack(None, 1) |
| 729 |
|
| 730 |
# Out of range checks. |
| 731 |
datas = [ |
| 732 |
('s64', (1 << 63)), |
| 733 |
('s64', -(1 << 63) - 1), |
| 734 |
('u64', 1 << 64), |
| 735 |
('u64', -1), |
| 736 |
('u1', 2), |
| 737 |
('s1', 1), |
| 738 |
('s1', -2), |
| 739 |
('s2', 2) |
| 740 |
] |
| 741 |
|
| 742 |
for fmt, value in datas: |
| 743 |
with self.assertRaises(OverflowError) as cm: |
| 744 |
pack(fmt, value) |
| 745 |
|
| 746 |
# Bad value types. |
| 747 |
with self.assertRaises(TypeError) as cm: |
| 748 |
pack('s1', None) |
| 749 |
|
| 750 |
with self.assertRaises(TypeError) as cm: |
| 751 |
pack('u1', None) |
| 752 |
|
| 753 |
with self.assertRaises(TypeError) as cm: |
| 754 |
pack('f32', None) |
| 755 |
|
| 756 |
with self.assertRaises(TypeError) as cm: |
| 757 |
pack('r8', None) |
| 758 |
|
| 759 |
with self.assertRaises(TypeError) as cm: |
| 760 |
pack('t8', None) |
| 761 |
|
| 762 |
# Everything can be bool. |
| 763 |
self.assertEqual(pack('b8', None), b'\x00') |
| 764 |
|
| 765 |
# Zero bits bool. |
| 766 |
with self.assertRaises(ValueError) as cm: |
| 767 |
pack('b0', None) |
| 768 |
|
| 769 |
self.assertEqual(str(cm.exception), 'Field of size 0.') |
| 770 |
|
| 771 |
# pack/unpack dict with names as a non-list. Caused an segfault. |
| 772 |
with self.assertRaises(TypeError) as cm: |
| 773 |
pack_into_dict('u1', None, bytearray(b'\x00'), 0, {'a': 1}) |
| 774 |
|
| 775 |
self.assertEqual(str(cm.exception), 'Names is not a list.') |
| 776 |
|
| 777 |
with self.assertRaises(TypeError) as cm: |
| 778 |
pack_dict('u1', None, {'a': 1}) |
| 779 |
|
| 780 |
self.assertEqual(str(cm.exception), 'Names is not a list.') |
| 781 |
|
| 782 |
with self.assertRaises(TypeError) as cm: |
| 783 |
bitstruct.c.compile('u1', 1) |
| 784 |
|
| 785 |
self.assertEqual(str(cm.exception), 'Names is not a list.') |
| 786 |
|
| 787 |
def test_whitespaces(self): |
| 788 |
if not is_cpython_3(): |
| 789 |
return |
| 790 |
|
| 791 |
fmts = [ |
| 792 |
' ', |
| 793 |
' u1 s2 p3 ' |
| 794 |
] |
| 795 |
|
| 796 |
for fmt in fmts: |
| 797 |
bitstruct.c.compile(fmt) |
| 798 |
|
| 799 |
def test_copy(self): |
| 800 |
if not is_cpython_3(): |
| 801 |
return |
| 802 |
|
| 803 |
cf = bitstruct.c.compile('u1u1s6u7u9') |
| 804 |
|
| 805 |
cf = copy.copy(cf) |
| 806 |
packed = cf.pack(0, 0, -2, 65, 22) |
| 807 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 808 |
|
| 809 |
cf = copy.deepcopy(cf) |
| 810 |
packed = cf.pack(0, 0, -2, 65, 22) |
| 811 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 812 |
|
| 813 |
def test_copy_dict(self): |
| 814 |
if not is_cpython_3(): |
| 815 |
return |
| 816 |
|
| 817 |
unpacked = { |
| 818 |
'foo': 0, |
| 819 |
'bar': 0, |
| 820 |
'fie': -2, |
| 821 |
'fum': 65, |
| 822 |
'fam': 22 |
| 823 |
} |
| 824 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 825 |
cf = bitstruct.c.compile('u1u1s6u7u9', names) |
| 826 |
|
| 827 |
cf = copy.copy(cf) |
| 828 |
packed = cf.pack(unpacked) |
| 829 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 830 |
self.assertEqual(cf.unpack(packed), unpacked) |
| 831 |
|
| 832 |
cf = copy.deepcopy(cf) |
| 833 |
packed = cf.pack(unpacked) |
| 834 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 835 |
self.assertEqual(cf.unpack(packed), unpacked) |
| 836 |
|
| 837 |
def test_pickle(self): |
| 838 |
if not is_cpython_3(): |
| 839 |
return |
| 840 |
|
| 841 |
unpacked = (0, 0, -2, 65, 22) |
| 842 |
cf = bitstruct.c.compile('u1u1s6u7u9') |
| 843 |
|
| 844 |
cf = pickle.loads(pickle.dumps(cf)) |
| 845 |
packed = cf.pack(*unpacked) |
| 846 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 847 |
self.assertEqual(cf.unpack(packed), unpacked) |
| 848 |
|
| 849 |
# Dump and load once more. |
| 850 |
cf = pickle.loads(pickle.dumps(cf)) |
| 851 |
packed = cf.pack(*unpacked) |
| 852 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 853 |
self.assertEqual(cf.unpack(packed), unpacked) |
| 854 |
|
| 855 |
def test_pickle_dict(self): |
| 856 |
if not is_cpython_3(): |
| 857 |
return |
| 858 |
|
| 859 |
unpacked = { |
| 860 |
'foo': 0, |
| 861 |
'bar': 0, |
| 862 |
'fie': -2, |
| 863 |
'fum': 65, |
| 864 |
'fam': 22 |
| 865 |
} |
| 866 |
names = ['foo', 'bar', 'fie', 'fum', 'fam'] |
| 867 |
cf = bitstruct.c.compile('u1u1s6u7u9', names) |
| 868 |
|
| 869 |
cf = pickle.loads(pickle.dumps(cf)) |
| 870 |
packed = cf.pack(unpacked) |
| 871 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 872 |
self.assertEqual(cf.unpack(packed), unpacked) |
| 873 |
|
| 874 |
# Dump and load once more. |
| 875 |
cf = pickle.loads(pickle.dumps(cf)) |
| 876 |
packed = cf.pack(unpacked) |
| 877 |
self.assertEqual(packed, b'\x3e\x82\x16') |
| 878 |
self.assertEqual(cf.unpack(packed), unpacked) |
| 879 |
|
| 880 |
|
| 881 |
if __name__ == '__main__': |
| 882 |
unittest.main() |