2001-06-10 23:40:19 +00:00
|
|
|
from test_support import TestFailed, verbose, verify
|
1996-12-12 23:34:06 +00:00
|
|
|
import struct
|
|
|
|
## import pdb
|
|
|
|
|
|
|
|
def simple_err(func, *args):
|
|
|
|
try:
|
1998-03-26 19:42:58 +00:00
|
|
|
apply(func, args)
|
1996-12-12 23:34:06 +00:00
|
|
|
except struct.error:
|
1998-03-26 19:42:58 +00:00
|
|
|
pass
|
1996-12-12 23:34:06 +00:00
|
|
|
else:
|
1998-03-26 19:42:58 +00:00
|
|
|
raise TestFailed, "%s%s did not raise struct.error" % (
|
|
|
|
func.__name__, args)
|
|
|
|
## pdb.set_trace()
|
1996-12-12 23:34:06 +00:00
|
|
|
|
2001-06-12 01:22:22 +00:00
|
|
|
def any_err(func, *args):
|
|
|
|
try:
|
|
|
|
apply(func, args)
|
|
|
|
except (struct.error, OverflowError, TypeError):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TestFailed, "%s%s did not raise error" % (
|
|
|
|
func.__name__, args)
|
|
|
|
## pdb.set_trace()
|
|
|
|
|
2001-06-10 23:40:19 +00:00
|
|
|
simple_err(struct.calcsize, 'Z')
|
1996-12-12 23:34:06 +00:00
|
|
|
|
|
|
|
sz = struct.calcsize('i')
|
2000-12-12 23:11:42 +00:00
|
|
|
if sz * 3 != struct.calcsize('iii'):
|
1996-12-31 17:25:47 +00:00
|
|
|
raise TestFailed, 'inconsistent sizes'
|
1996-12-12 23:34:06 +00:00
|
|
|
|
1997-01-03 19:00:37 +00:00
|
|
|
fmt = 'cbxxxxxxhhhhiillffd'
|
|
|
|
fmt3 = '3c3b18x12h6i6l6f3d'
|
|
|
|
sz = struct.calcsize(fmt)
|
|
|
|
sz3 = struct.calcsize(fmt3)
|
2000-12-12 23:11:42 +00:00
|
|
|
if sz * 3 != sz3:
|
1997-01-03 19:00:37 +00:00
|
|
|
raise TestFailed, 'inconsistent sizes (3*%s -> 3*%d = %d, %s -> %d)' % (
|
1998-03-26 19:42:58 +00:00
|
|
|
`fmt`, sz, 3*sz, `fmt3`, sz3)
|
1996-12-12 23:34:06 +00:00
|
|
|
|
|
|
|
simple_err(struct.pack, 'iii', 3)
|
|
|
|
simple_err(struct.pack, 'i', 3, 3, 3)
|
|
|
|
simple_err(struct.pack, 'i', 'foo')
|
|
|
|
simple_err(struct.unpack, 'd', 'flap')
|
|
|
|
s = struct.pack('ii', 1, 2)
|
|
|
|
simple_err(struct.unpack, 'iii', s)
|
|
|
|
simple_err(struct.unpack, 'i', s)
|
|
|
|
|
|
|
|
c = 'a'
|
1996-12-31 17:25:47 +00:00
|
|
|
b = 1
|
1996-12-12 23:34:06 +00:00
|
|
|
h = 255
|
|
|
|
i = 65535
|
|
|
|
l = 65536
|
|
|
|
f = 3.1415
|
|
|
|
d = 3.1415
|
|
|
|
|
1997-01-03 00:09:46 +00:00
|
|
|
for prefix in ('', '@', '<', '>', '=', '!'):
|
|
|
|
for format in ('xcbhilfd', 'xcBHILfd'):
|
1998-03-26 19:42:58 +00:00
|
|
|
format = prefix + format
|
|
|
|
if verbose:
|
|
|
|
print "trying:", format
|
|
|
|
s = struct.pack(format, c, b, h, i, l, f, d)
|
|
|
|
cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s)
|
2000-12-12 23:11:42 +00:00
|
|
|
if (cp != c or bp != b or hp != h or ip != i or lp != l or
|
|
|
|
int(100 * fp) != int(100 * f) or int(100 * dp) != int(100 * d)):
|
1998-03-26 19:42:58 +00:00
|
|
|
# ^^^ calculate only to two decimal places
|
|
|
|
raise TestFailed, "unpack/pack not transitive (%s, %s)" % (
|
|
|
|
str(format), str((cp, bp, hp, ip, lp, fp, dp)))
|
1996-12-31 17:25:47 +00:00
|
|
|
|
1997-01-03 00:09:46 +00:00
|
|
|
# Test some of the new features in detail
|
1996-12-31 17:25:47 +00:00
|
|
|
|
|
|
|
# (format, argument, big-endian result, little-endian result, asymmetric)
|
|
|
|
tests = [
|
|
|
|
('c', 'a', 'a', 'a', 0),
|
|
|
|
('xc', 'a', '\0a', '\0a', 0),
|
|
|
|
('cx', 'a', 'a\0', 'a\0', 0),
|
|
|
|
('s', 'a', 'a', 'a', 0),
|
|
|
|
('0s', 'helloworld', '', '', 1),
|
|
|
|
('1s', 'helloworld', 'h', 'h', 1),
|
|
|
|
('9s', 'helloworld', 'helloworl', 'helloworl', 1),
|
|
|
|
('10s', 'helloworld', 'helloworld', 'helloworld', 0),
|
|
|
|
('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1),
|
|
|
|
('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1),
|
|
|
|
('b', 7, '\7', '\7', 0),
|
|
|
|
('b', -7, '\371', '\371', 0),
|
|
|
|
('B', 7, '\7', '\7', 0),
|
|
|
|
('B', 249, '\371', '\371', 0),
|
|
|
|
('h', 700, '\002\274', '\274\002', 0),
|
|
|
|
('h', -700, '\375D', 'D\375', 0),
|
|
|
|
('H', 700, '\002\274', '\274\002', 0),
|
|
|
|
('H', 0x10000-700, '\375D', 'D\375', 0),
|
|
|
|
('i', 70000000, '\004,\035\200', '\200\035,\004', 0),
|
|
|
|
('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
|
|
|
|
('I', 70000000L, '\004,\035\200', '\200\035,\004', 0),
|
|
|
|
('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
|
|
|
|
('l', 70000000, '\004,\035\200', '\200\035,\004', 0),
|
|
|
|
('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
|
|
|
|
('L', 70000000L, '\004,\035\200', '\200\035,\004', 0),
|
|
|
|
('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
|
1997-01-03 00:09:46 +00:00
|
|
|
('f', 2.0, '@\000\000\000', '\000\000\000@', 0),
|
|
|
|
('d', 2.0, '@\000\000\000\000\000\000\000',
|
|
|
|
'\000\000\000\000\000\000\000@', 0),
|
|
|
|
('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0),
|
|
|
|
('d', -2.0, '\300\000\000\000\000\000\000\000',
|
|
|
|
'\000\000\000\000\000\000\000\300', 0),
|
1996-12-31 17:25:47 +00:00
|
|
|
]
|
|
|
|
|
2001-06-10 23:40:19 +00:00
|
|
|
isbigendian = struct.pack('=i', 1)[0] == chr(0)
|
1996-12-31 17:25:47 +00:00
|
|
|
|
|
|
|
for fmt, arg, big, lil, asy in tests:
|
|
|
|
if verbose:
|
1998-03-26 19:42:58 +00:00
|
|
|
print `fmt`, `arg`, `big`, `lil`
|
1996-12-31 17:25:47 +00:00
|
|
|
for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
|
1998-03-26 19:42:58 +00:00
|
|
|
('='+fmt, isbigendian and big or lil)]:
|
|
|
|
res = struct.pack(xfmt, arg)
|
|
|
|
if res != exp:
|
|
|
|
raise TestFailed, "pack(%s, %s) -> %s # expected %s" % (
|
|
|
|
`fmt`, `arg`, `res`, `exp`)
|
|
|
|
n = struct.calcsize(xfmt)
|
|
|
|
if n != len(res):
|
|
|
|
raise TestFailed, "calcsize(%s) -> %d # expected %d" % (
|
|
|
|
`xfmt`, n, len(res))
|
|
|
|
rev = struct.unpack(xfmt, res)[0]
|
|
|
|
if rev != arg and not asy:
|
|
|
|
raise TestFailed, "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
|
|
|
|
`fmt`, `res`, `rev`, `arg`)
|
2001-06-10 23:40:19 +00:00
|
|
|
|
2001-06-12 01:22:22 +00:00
|
|
|
###########################################################################
|
|
|
|
# q/Q tests.
|
2001-06-10 23:40:19 +00:00
|
|
|
|
|
|
|
has_native_qQ = 1
|
|
|
|
try:
|
|
|
|
struct.pack("q", 5)
|
|
|
|
except struct.error:
|
|
|
|
has_native_qQ = 0
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print "Platform has native q/Q?", has_native_qQ and "Yes." or "No."
|
|
|
|
|
2001-06-12 01:22:22 +00:00
|
|
|
any_err(struct.pack, "Q", -1) # can't pack -1 as unsigned regardless
|
2001-06-10 23:40:19 +00:00
|
|
|
simple_err(struct.pack, "q", "a") # can't pack string as 'q' regardless
|
|
|
|
simple_err(struct.pack, "Q", "a") # ditto, but 'Q'
|
|
|
|
|
2001-06-12 01:22:22 +00:00
|
|
|
def string_reverse(s):
|
|
|
|
chars = list(s)
|
|
|
|
chars.reverse()
|
|
|
|
return "".join(chars)
|
|
|
|
|
2001-06-10 23:52:59 +00:00
|
|
|
def bigendian_to_native(value):
|
2001-06-10 23:40:19 +00:00
|
|
|
if isbigendian:
|
|
|
|
return value
|
2001-06-12 01:22:22 +00:00
|
|
|
else:
|
|
|
|
return string_reverse(value)
|
2001-06-10 23:40:19 +00:00
|
|
|
|
2001-06-12 01:22:22 +00:00
|
|
|
def test_native_qQ():
|
2001-06-10 23:40:19 +00:00
|
|
|
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', 1L, '\x00' * (bytes-1) + '\x01'),
|
|
|
|
('Q', (1L << (8*bytes))-1, '\xff' * bytes),
|
|
|
|
('q', (1L << (8*bytes-1))-1, '\x7f' + '\xff' * (bytes - 1))):
|
|
|
|
got = struct.pack(format, input)
|
2001-06-10 23:52:59 +00:00
|
|
|
native_expected = bigendian_to_native(expected)
|
|
|
|
verify(got == native_expected,
|
2001-06-10 23:40:19 +00:00
|
|
|
"%r-pack of %r gave %r, not %r" %
|
2001-06-10 23:52:59 +00:00
|
|
|
(format, input, got, native_expected))
|
2001-06-10 23:40:19 +00:00
|
|
|
retrieved = struct.unpack(format, got)[0]
|
|
|
|
verify(retrieved == input,
|
|
|
|
"%r-unpack of %r gave %r, not %r" %
|
|
|
|
(format, got, retrieved, input))
|
2001-06-12 01:22:22 +00:00
|
|
|
|
|
|
|
if has_native_qQ:
|
|
|
|
test_native_qQ()
|
|
|
|
|
|
|
|
# Standard q/Q (8 bytes; should work on all platforms).
|
|
|
|
|
|
|
|
MIN_Q, MAX_Q = 0, 2L**64 - 1
|
|
|
|
MIN_q, MAX_q = -(2L**63), 2L**63 - 1
|
|
|
|
|
|
|
|
import binascii
|
|
|
|
def test_one_qQ(x, pack=struct.pack,
|
|
|
|
unpack=struct.unpack,
|
|
|
|
unhexlify=binascii.unhexlify):
|
|
|
|
if verbose:
|
|
|
|
print "trying std q/Q on", x, "==", hex(x)
|
|
|
|
|
|
|
|
# Try 'q'.
|
|
|
|
if MIN_q <= x <= MAX_q:
|
|
|
|
# Try '>q'.
|
|
|
|
expected = long(x)
|
|
|
|
if x < 0:
|
|
|
|
expected += 1L << 64
|
|
|
|
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" * (8 - len(expected)) + expected
|
|
|
|
|
|
|
|
# >q pack work?
|
|
|
|
got = pack(">q", x)
|
|
|
|
verify(got == expected,
|
|
|
|
"'>q'-pack of %r gave %r, not %r" %
|
|
|
|
(x, got, expected))
|
|
|
|
|
|
|
|
# >q unpack work?
|
|
|
|
retrieved = unpack(">q", got)[0]
|
|
|
|
verify(x == retrieved,
|
|
|
|
"'>q'-unpack of %r gave %r, not %r" %
|
|
|
|
(got, retrieved, x))
|
|
|
|
|
|
|
|
# Adding any byte should cause a "too big" error.
|
|
|
|
any_err(unpack, ">q", '\x01' + got)
|
|
|
|
|
|
|
|
# Try '<q'.
|
|
|
|
expected = string_reverse(expected)
|
|
|
|
|
|
|
|
# <q pack work?
|
|
|
|
got = pack("<q", x)
|
|
|
|
verify(got == expected,
|
|
|
|
"'<q'-pack of %r gave %r, not %r" %
|
|
|
|
(x, got, expected))
|
|
|
|
|
|
|
|
# <q unpack work?
|
|
|
|
retrieved = unpack("<q", got)[0]
|
|
|
|
verify(x == retrieved,
|
|
|
|
"'<q'-unpack of %r gave %r, not %r" %
|
|
|
|
(got, retrieved, x))
|
|
|
|
|
|
|
|
# Adding any byte should cause a "too big" error.
|
|
|
|
any_err(unpack, "<q", '\x01' + got)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# x is out of q's range -- verify pack realizes that.
|
|
|
|
any_err(pack, '>q', x)
|
|
|
|
any_err(pack, '<q', x)
|
|
|
|
|
|
|
|
# Much the same for 'Q'.
|
|
|
|
if MIN_Q <= x <= MAX_Q:
|
|
|
|
# Try '>Q'.
|
|
|
|
expected = long(x)
|
|
|
|
expected = hex(expected)[2:-1] # chop "0x" and trailing 'L'
|
|
|
|
if len(expected) & 1:
|
|
|
|
expected = "0" + expected
|
|
|
|
expected = unhexlify(expected)
|
|
|
|
expected = "\x00" * (8 - len(expected)) + expected
|
|
|
|
|
|
|
|
# >Q pack work?
|
|
|
|
got = pack(">Q", x)
|
|
|
|
verify(got == expected,
|
|
|
|
"'>Q'-pack of %r gave %r, not %r" %
|
|
|
|
(x, got, expected))
|
|
|
|
|
|
|
|
# >Q unpack work?
|
|
|
|
retrieved = unpack(">Q", got)[0]
|
|
|
|
verify(x == retrieved,
|
|
|
|
"'>Q'-unpack of %r gave %r, not %r" %
|
|
|
|
(got, retrieved, x))
|
|
|
|
|
|
|
|
# Adding any byte should cause a "too big" error.
|
|
|
|
any_err(unpack, ">Q", '\x01' + got)
|
|
|
|
|
|
|
|
# Try '<Q'.
|
|
|
|
expected = string_reverse(expected)
|
|
|
|
|
|
|
|
# <Q pack work?
|
|
|
|
got = pack("<Q", x)
|
|
|
|
verify(got == expected,
|
|
|
|
"'<Q'-pack of %r gave %r, not %r" %
|
|
|
|
(x, got, expected))
|
|
|
|
|
|
|
|
# <Q unpack work?
|
|
|
|
retrieved = unpack("<Q", got)[0]
|
|
|
|
verify(x == retrieved,
|
|
|
|
"'<Q'-unpack of %r gave %r, not %r" %
|
|
|
|
(got, retrieved, x))
|
|
|
|
|
|
|
|
# Adding any byte should cause a "too big" error.
|
|
|
|
any_err(unpack, "<Q", '\x01' + got)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# x is out of Q's range -- verify pack realizes that.
|
|
|
|
any_err(pack, '>Q', x)
|
|
|
|
any_err(pack, '<Q', x)
|
|
|
|
|
|
|
|
def test_std_qQ():
|
|
|
|
from random import randrange
|
|
|
|
|
|
|
|
# Create all interesting powers of 2.
|
|
|
|
values = []
|
|
|
|
for exp in range(70):
|
|
|
|
values.append(1L << exp)
|
|
|
|
|
|
|
|
# Add some random 64-bit values.
|
|
|
|
for i in range(50):
|
|
|
|
val = 0L
|
|
|
|
for j in range(8):
|
|
|
|
val = (val << 8) | randrange(256)
|
|
|
|
values.append(val)
|
|
|
|
|
|
|
|
# Try all those, and their negations, and +-1 from them. Note
|
|
|
|
# that this tests all power-of-2 boundaries in range, and a few out
|
|
|
|
# of range, plus +-(2**n +- 1).
|
|
|
|
for base in values:
|
|
|
|
for val in -base, base:
|
|
|
|
for incr in -1, 0, 1:
|
|
|
|
x = val + incr
|
|
|
|
try:
|
|
|
|
x = int(x)
|
|
|
|
except OverflowError:
|
|
|
|
pass
|
|
|
|
test_one_qQ(x)
|
|
|
|
|
2001-06-13 01:26:35 +00:00
|
|
|
# Some error cases.
|
|
|
|
for direction in "<>":
|
|
|
|
for letter in "qQ":
|
|
|
|
for badobject in "a string", 3+42j, randrange:
|
|
|
|
any_err(struct.pack, direction + letter, badobject)
|
|
|
|
|
2001-06-12 01:22:22 +00:00
|
|
|
test_std_qQ()
|