blob: 3d54314475ea0da1fb8ab6870bb9feef1ccb55fb [file] [log] [blame]
Guido van Rossum2a378501996-12-31 17:25:47 +00001from test_support import TestFailed, verbose
Barry Warsaw07a0eec1996-12-12 23:34:06 +00002import struct
3## import pdb
4
5def simple_err(func, *args):
6 try:
7 apply(func, args)
8 except struct.error:
9 pass
10 else:
Guido van Rossum2a378501996-12-31 17:25:47 +000011 raise TestFailed, "%s%s did not raise struct.error" % (
12 func.__name__, args)
Barry Warsaw07a0eec1996-12-12 23:34:06 +000013## pdb.set_trace()
14
15simple_err(struct.calcsize, 'Q')
16
17sz = struct.calcsize('i')
18if sz * 3 <> struct.calcsize('iii'):
Guido van Rossum2a378501996-12-31 17:25:47 +000019 raise TestFailed, 'inconsistent sizes'
Barry Warsaw07a0eec1996-12-12 23:34:06 +000020
21sz = struct.calcsize('cbhilfd')
22if sz * 3 <> struct.calcsize('3c3b3h3i3l3f3d'):
Guido van Rossum2a378501996-12-31 17:25:47 +000023 raise TestFailed, 'inconsistent sizes'
Barry Warsaw07a0eec1996-12-12 23:34:06 +000024
25simple_err(struct.pack, 'iii', 3)
26simple_err(struct.pack, 'i', 3, 3, 3)
27simple_err(struct.pack, 'i', 'foo')
28simple_err(struct.unpack, 'd', 'flap')
29s = struct.pack('ii', 1, 2)
30simple_err(struct.unpack, 'iii', s)
31simple_err(struct.unpack, 'i', s)
32
33c = 'a'
Guido van Rossum2a378501996-12-31 17:25:47 +000034b = 1
Barry Warsaw07a0eec1996-12-12 23:34:06 +000035h = 255
36i = 65535
37l = 65536
38f = 3.1415
39d = 3.1415
40
Guido van Rossum2a378501996-12-31 17:25:47 +000041for format in ('xcbhilfd', 'xcBHILfd', '@xsbhilfd'):
42 s = struct.pack(format, c, b, h, i, l, f, d)
43 cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s)
44 if (cp <> c or bp <> b or hp <> h or ip <> i or lp <> l or
45 int(100 * fp) <> int(100 * f) or int(100 * dp) <> int(100 * d)):
46 # ^^^ calculate only to two decimal places
47 raise TestFailed, "unpack/pack not transitive (%s, %s)" % (
48 str(format), str((cp, bp, hp, ip, lp, fp, dp)))
49
50# Test some of the new features
51
52# (format, argument, big-endian result, little-endian result, asymmetric)
53tests = [
54 ('c', 'a', 'a', 'a', 0),
55 ('xc', 'a', '\0a', '\0a', 0),
56 ('cx', 'a', 'a\0', 'a\0', 0),
57 ('s', 'a', 'a', 'a', 0),
58 ('0s', 'helloworld', '', '', 1),
59 ('1s', 'helloworld', 'h', 'h', 1),
60 ('9s', 'helloworld', 'helloworl', 'helloworl', 1),
61 ('10s', 'helloworld', 'helloworld', 'helloworld', 0),
62 ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1),
63 ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1),
64 ('b', 7, '\7', '\7', 0),
65 ('b', -7, '\371', '\371', 0),
66 ('B', 7, '\7', '\7', 0),
67 ('B', 249, '\371', '\371', 0),
68 ('h', 700, '\002\274', '\274\002', 0),
69 ('h', -700, '\375D', 'D\375', 0),
70 ('H', 700, '\002\274', '\274\002', 0),
71 ('H', 0x10000-700, '\375D', 'D\375', 0),
72 ('i', 70000000, '\004,\035\200', '\200\035,\004', 0),
73 ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
74 ('I', 70000000L, '\004,\035\200', '\200\035,\004', 0),
75 ('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
76 ('l', 70000000, '\004,\035\200', '\200\035,\004', 0),
77 ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
78 ('L', 70000000L, '\004,\035\200', '\200\035,\004', 0),
79 ('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
80]
81
82def badpack(fmt, arg, got, exp):
83 return
84
85def badunpack(fmt, arg, got, exp):
86 return "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
87 `fmt`, `arg`, `got`, `exp`)
88
89isbigendian = struct.pack('=h', 1) == '\0\1'
90
91for fmt, arg, big, lil, asy in tests:
92 if verbose:
93 print `fmt`, `arg`, `big`, `lil`
94 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
95 ('='+fmt, isbigendian and big or lil)]:
96 res = struct.pack(xfmt, arg)
97 if res != exp:
98 raise TestFailed, "pack(%s, %s) -> %s # expected %s" % (
99 `fmt`, `arg`, `res`, `exp`)
100 n = struct.calcsize(xfmt)
101 if n != len(res):
102 raise TestFailed, "calcsize(%s) -> %d # expected %d" % (
103 `xfmt`, n, len(res))
104 rev = struct.unpack(xfmt, res)[0]
105 if rev != arg and not asy:
106 raise TestFailed, "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
107 `fmt`, `res`, `rev`, `arg`)