blob: ebd6e45d290d0192155382ab13e2ac45781ea42b [file] [log] [blame]
Tim Peters7b9542a2001-06-10 23:40:19 +00001from test_support import TestFailed, verbose, verify
Barry Warsaw07a0eec1996-12-12 23:34:06 +00002import struct
3## import pdb
4
5def simple_err(func, *args):
6 try:
Guido van Rossum41360a41998-03-26 19:42:58 +00007 apply(func, args)
Barry Warsaw07a0eec1996-12-12 23:34:06 +00008 except struct.error:
Guido van Rossum41360a41998-03-26 19:42:58 +00009 pass
Barry Warsaw07a0eec1996-12-12 23:34:06 +000010 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000011 raise TestFailed, "%s%s did not raise struct.error" % (
12 func.__name__, args)
13## pdb.set_trace()
Barry Warsaw07a0eec1996-12-12 23:34:06 +000014
Tim Peters7b9542a2001-06-10 23:40:19 +000015simple_err(struct.calcsize, 'Z')
Barry Warsaw07a0eec1996-12-12 23:34:06 +000016
17sz = struct.calcsize('i')
Fred Drake132dce22000-12-12 23:11:42 +000018if 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
Guido van Rossum04ebf5c1997-01-03 19:00:37 +000021fmt = 'cbxxxxxxhhhhiillffd'
22fmt3 = '3c3b18x12h6i6l6f3d'
23sz = struct.calcsize(fmt)
24sz3 = struct.calcsize(fmt3)
Fred Drake132dce22000-12-12 23:11:42 +000025if sz * 3 != sz3:
Guido van Rossum04ebf5c1997-01-03 19:00:37 +000026 raise TestFailed, 'inconsistent sizes (3*%s -> 3*%d = %d, %s -> %d)' % (
Guido van Rossum41360a41998-03-26 19:42:58 +000027 `fmt`, sz, 3*sz, `fmt3`, sz3)
Barry Warsaw07a0eec1996-12-12 23:34:06 +000028
29simple_err(struct.pack, 'iii', 3)
30simple_err(struct.pack, 'i', 3, 3, 3)
31simple_err(struct.pack, 'i', 'foo')
32simple_err(struct.unpack, 'd', 'flap')
33s = struct.pack('ii', 1, 2)
34simple_err(struct.unpack, 'iii', s)
35simple_err(struct.unpack, 'i', s)
36
37c = 'a'
Guido van Rossum2a378501996-12-31 17:25:47 +000038b = 1
Barry Warsaw07a0eec1996-12-12 23:34:06 +000039h = 255
40i = 65535
41l = 65536
42f = 3.1415
43d = 3.1415
44
Guido van Rossum420c11c1997-01-03 00:09:46 +000045for prefix in ('', '@', '<', '>', '=', '!'):
46 for format in ('xcbhilfd', 'xcBHILfd'):
Guido van Rossum41360a41998-03-26 19:42:58 +000047 format = prefix + format
48 if verbose:
49 print "trying:", format
50 s = struct.pack(format, c, b, h, i, l, f, d)
51 cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s)
Fred Drake132dce22000-12-12 23:11:42 +000052 if (cp != c or bp != b or hp != h or ip != i or lp != l or
53 int(100 * fp) != int(100 * f) or int(100 * dp) != int(100 * d)):
Guido van Rossum41360a41998-03-26 19:42:58 +000054 # ^^^ calculate only to two decimal places
55 raise TestFailed, "unpack/pack not transitive (%s, %s)" % (
56 str(format), str((cp, bp, hp, ip, lp, fp, dp)))
Guido van Rossum2a378501996-12-31 17:25:47 +000057
Guido van Rossum420c11c1997-01-03 00:09:46 +000058# Test some of the new features in detail
Guido van Rossum2a378501996-12-31 17:25:47 +000059
60# (format, argument, big-endian result, little-endian result, asymmetric)
61tests = [
62 ('c', 'a', 'a', 'a', 0),
63 ('xc', 'a', '\0a', '\0a', 0),
64 ('cx', 'a', 'a\0', 'a\0', 0),
65 ('s', 'a', 'a', 'a', 0),
66 ('0s', 'helloworld', '', '', 1),
67 ('1s', 'helloworld', 'h', 'h', 1),
68 ('9s', 'helloworld', 'helloworl', 'helloworl', 1),
69 ('10s', 'helloworld', 'helloworld', 'helloworld', 0),
70 ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1),
71 ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1),
72 ('b', 7, '\7', '\7', 0),
73 ('b', -7, '\371', '\371', 0),
74 ('B', 7, '\7', '\7', 0),
75 ('B', 249, '\371', '\371', 0),
76 ('h', 700, '\002\274', '\274\002', 0),
77 ('h', -700, '\375D', 'D\375', 0),
78 ('H', 700, '\002\274', '\274\002', 0),
79 ('H', 0x10000-700, '\375D', 'D\375', 0),
80 ('i', 70000000, '\004,\035\200', '\200\035,\004', 0),
81 ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
82 ('I', 70000000L, '\004,\035\200', '\200\035,\004', 0),
83 ('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
84 ('l', 70000000, '\004,\035\200', '\200\035,\004', 0),
85 ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
86 ('L', 70000000L, '\004,\035\200', '\200\035,\004', 0),
87 ('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
Guido van Rossum420c11c1997-01-03 00:09:46 +000088 ('f', 2.0, '@\000\000\000', '\000\000\000@', 0),
89 ('d', 2.0, '@\000\000\000\000\000\000\000',
90 '\000\000\000\000\000\000\000@', 0),
91 ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0),
92 ('d', -2.0, '\300\000\000\000\000\000\000\000',
93 '\000\000\000\000\000\000\000\300', 0),
Guido van Rossum2a378501996-12-31 17:25:47 +000094]
95
Tim Peters7b9542a2001-06-10 23:40:19 +000096isbigendian = struct.pack('=i', 1)[0] == chr(0)
Guido van Rossum2a378501996-12-31 17:25:47 +000097
98for fmt, arg, big, lil, asy in tests:
99 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000100 print `fmt`, `arg`, `big`, `lil`
Guido van Rossum2a378501996-12-31 17:25:47 +0000101 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
Guido van Rossum41360a41998-03-26 19:42:58 +0000102 ('='+fmt, isbigendian and big or lil)]:
103 res = struct.pack(xfmt, arg)
104 if res != exp:
105 raise TestFailed, "pack(%s, %s) -> %s # expected %s" % (
106 `fmt`, `arg`, `res`, `exp`)
107 n = struct.calcsize(xfmt)
108 if n != len(res):
109 raise TestFailed, "calcsize(%s) -> %d # expected %d" % (
110 `xfmt`, n, len(res))
111 rev = struct.unpack(xfmt, res)[0]
112 if rev != arg and not asy:
113 raise TestFailed, "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
114 `fmt`, `res`, `rev`, `arg`)
Tim Peters7b9542a2001-06-10 23:40:19 +0000115
116# Some q/Q sanity checks.
117
118has_native_qQ = 1
119try:
120 struct.pack("q", 5)
121except struct.error:
122 has_native_qQ = 0
123
124if verbose:
125 print "Platform has native q/Q?", has_native_qQ and "Yes." or "No."
126
127simple_err(struct.pack, "Q", -1) # can't pack -1 as unsigned regardless
128simple_err(struct.pack, "q", "a") # can't pack string as 'q' regardless
129simple_err(struct.pack, "Q", "a") # ditto, but 'Q'
130
131def force_bigendian(value):
132 if isbigendian:
133 return value
134 chars = list(value)
135 chars.reverse()
136 return "".join(chars)
137
138if has_native_qQ:
139 bytes = struct.calcsize('q')
140 # The expected values here are in big-endian format, primarily because
141 # I'm on a little-endian machine and so this is the clearest way (for
142 # me) to force the code to get exercised.
143 for format, input, expected in (
144 ('q', -1, '\xff' * bytes),
145 ('q', 0, '\x00' * bytes),
146 ('Q', 0, '\x00' * bytes),
147 ('q', 1L, '\x00' * (bytes-1) + '\x01'),
148 ('Q', (1L << (8*bytes))-1, '\xff' * bytes),
149 ('q', (1L << (8*bytes-1))-1, '\x7f' + '\xff' * (bytes - 1))):
150 got = struct.pack(format, input)
151 bigexpected = force_bigendian(expected)
152 verify(got == bigexpected,
153 "%r-pack of %r gave %r, not %r" %
154 (format, input, got, bigexpected))
155 retrieved = struct.unpack(format, got)[0]
156 verify(retrieved == input,
157 "%r-unpack of %r gave %r, not %r" %
158 (format, got, retrieved, input))