def test_main(verbose=None): from test.support import verify import sys for func in tests: expected = tests[func] result = list(func()) verify(result == expected, "%s: expected %s, got %s" % ( func.__name__, expected, result))
def __init__(self, formatpair, bytesize): assert len(formatpair) == 2 self.formatpair = formatpair for direction in "<>!=": for code in formatpair: format = direction + code verify(struct.calcsize(format) == bytesize) self.bytesize = bytesize self.bitsize = bytesize * 8 self.signed_code, self.unsigned_code = formatpair self.unsigned_min = 0 self.unsigned_max = 2**self.bitsize - 1 self.signed_min = -(2**(self.bitsize-1)) self.signed_max = 2**(self.bitsize-1) - 1
def test_native_qQ(): bytes = struct.calcsize('q') # The expected values here are in big-endian format, primarily because # I'm on a little-endian machine and so this is the clearest way (for # me) to force the code to get exercised. for format, input, expected in ( ('q', -1, '\xff' * bytes), ('q', 0, '\x00' * bytes), ('Q', 0, '\x00' * bytes), ('q', 1, '\x00' * (bytes-1) + '\x01'), ('Q', (1 << (8*bytes))-1, '\xff' * bytes), ('q', (1 << (8*bytes-1))-1, '\x7f' + '\xff' * (bytes - 1))): got = struct.pack(format, input) native_expected = bigendian_to_native(expected) verify(got == native_expected, "%r-pack of %r gave %r, not %r" % (format, input, got, native_expected)) retrieved = struct.unpack(format, got)[0] verify(retrieved == input, "%r-unpack of %r gave %r, not %r" % (format, got, retrieved, input))
def test_705836(): import math for base in range(1, 33): # smaller <- largest representable float less than base. delta = 0.5 while base - delta / 2.0 != base: delta /= 2.0 smaller = base - delta # Packing this rounds away a solid string of trailing 1 bits. packed = struct.pack("<f", smaller) unpacked = struct.unpack("<f", packed)[0] # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and # 16, respectively. verify(base == unpacked) bigpacked = struct.pack(">f", smaller) verify(bigpacked == string_reverse(packed), ">f pack should be byte-reversal of <f pack") unpacked = struct.unpack(">f", bigpacked)[0] verify(base == unpacked) # Largest finite IEEE single. big = (1 << 24) - 1 big = math.ldexp(big, 127 - 23) packed = struct.pack(">f", big) unpacked = struct.unpack(">f", packed)[0] verify(big == unpacked) # The same, but tack on a 1 bit so it rounds up to infinity. big = (1 << 25) - 1 big = math.ldexp(big, 127 - 24) try: packed = struct.pack(">f", big) except OverflowError: pass else: TestFailed("expected OverflowError")
def test_one(self, x, pack=struct.pack, unpack=struct.unpack, unhexlify=binascii.unhexlify): if verbose: print("trying std", self.formatpair, "on", x, "==", hex(x)) # Try signed. code = self.signed_code if self.signed_min <= x <= self.signed_max: # Try big-endian. expected = int(x) if x < 0: expected += 1 << self.bitsize assert expected > 0 expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' if len(expected) & 1: expected = "0" + expected expected = unhexlify(expected) expected = "\x00" * (self.bytesize - len(expected)) + expected # Pack work? format = ">" + code got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) # Try little-endian. format = "<" + code expected = string_reverse(expected) # Pack work? got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) else: # x is out of range -- verify pack realizes that. if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: if verbose: print("Skipping buggy range check for code", code) else: deprecated_err(pack, ">" + code, x) deprecated_err(pack, "<" + code, x) # Much the same for unsigned. code = self.unsigned_code if self.unsigned_min <= x <= self.unsigned_max: # Try big-endian. format = ">" + code expected = int(x) expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' if len(expected) & 1: expected = "0" + expected expected = unhexlify(expected) expected = "\x00" * (self.bytesize - len(expected)) + expected # Pack work? got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) # Try little-endian. format = "<" + code expected = string_reverse(expected) # Pack work? got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) else: # x is out of range -- verify pack realizes that. if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: if verbose: print("Skipping buggy range check for code", code) else: deprecated_err(pack, ">" + code, x) deprecated_err(pack, "<" + code, x)
from test.support import TestFailed, verbose, verify, vereq, is_jython import struct import array import warnings if is_jython: # currently no buffer type, but this will work fine for this testing def buffer(s): return str(s) import sys ISBIGENDIAN = sys.byteorder == "big" del sys verify((struct.pack('=i', 1)[0] == chr(0)) == ISBIGENDIAN, "bigendian determination appears wrong") try: import _struct except ImportError: PY_STRUCT_RANGE_CHECKING = 0 PY_STRUCT_OVERFLOW_MASKING = 1 PY_STRUCT_FLOAT_COERCE = 2 else: PY_STRUCT_RANGE_CHECKING = getattr(_struct, '_PY_STRUCT_RANGE_CHECKING', 0) PY_STRUCT_OVERFLOW_MASKING = getattr(_struct, '_PY_STRUCT_OVERFLOW_MASKING', 0) PY_STRUCT_FLOAT_COERCE = getattr(_struct, '_PY_STRUCT_FLOAT_COERCE', 0) def string_reverse(s): return "".join(reversed(s))