blob: 954a3f26e97495dd5638c0f90a86f35868bf6e97 [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 Rossum420c11c1997-01-03 00:09:46 +000041for prefix in ('', '@', '<', '>', '=', '!'):
42 for format in ('xcbhilfd', 'xcBHILfd'):
43 format = prefix + format
44 if verbose:
45 print "trying:", format
46 s = struct.pack(format, c, b, h, i, l, f, d)
47 cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s)
48 if (cp <> c or bp <> b or hp <> h or ip <> i or lp <> l or
49 int(100 * fp) <> int(100 * f) or int(100 * dp) <> int(100 * d)):
50 # ^^^ calculate only to two decimal places
51 raise TestFailed, "unpack/pack not transitive (%s, %s)" % (
52 str(format), str((cp, bp, hp, ip, lp, fp, dp)))
Guido van Rossum2a378501996-12-31 17:25:47 +000053
Guido van Rossum420c11c1997-01-03 00:09:46 +000054# Test some of the new features in detail
Guido van Rossum2a378501996-12-31 17:25:47 +000055
56# (format, argument, big-endian result, little-endian result, asymmetric)
57tests = [
58 ('c', 'a', 'a', 'a', 0),
59 ('xc', 'a', '\0a', '\0a', 0),
60 ('cx', 'a', 'a\0', 'a\0', 0),
61 ('s', 'a', 'a', 'a', 0),
62 ('0s', 'helloworld', '', '', 1),
63 ('1s', 'helloworld', 'h', 'h', 1),
64 ('9s', 'helloworld', 'helloworl', 'helloworl', 1),
65 ('10s', 'helloworld', 'helloworld', 'helloworld', 0),
66 ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1),
67 ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1),
68 ('b', 7, '\7', '\7', 0),
69 ('b', -7, '\371', '\371', 0),
70 ('B', 7, '\7', '\7', 0),
71 ('B', 249, '\371', '\371', 0),
72 ('h', 700, '\002\274', '\274\002', 0),
73 ('h', -700, '\375D', 'D\375', 0),
74 ('H', 700, '\002\274', '\274\002', 0),
75 ('H', 0x10000-700, '\375D', 'D\375', 0),
76 ('i', 70000000, '\004,\035\200', '\200\035,\004', 0),
77 ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
78 ('I', 70000000L, '\004,\035\200', '\200\035,\004', 0),
79 ('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
80 ('l', 70000000, '\004,\035\200', '\200\035,\004', 0),
81 ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
82 ('L', 70000000L, '\004,\035\200', '\200\035,\004', 0),
83 ('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
Guido van Rossum420c11c1997-01-03 00:09:46 +000084 ('f', 2.0, '@\000\000\000', '\000\000\000@', 0),
85 ('d', 2.0, '@\000\000\000\000\000\000\000',
86 '\000\000\000\000\000\000\000@', 0),
87 ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0),
88 ('d', -2.0, '\300\000\000\000\000\000\000\000',
89 '\000\000\000\000\000\000\000\300', 0),
Guido van Rossum2a378501996-12-31 17:25:47 +000090]
91
92def badpack(fmt, arg, got, exp):
93 return
94
95def badunpack(fmt, arg, got, exp):
96 return "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
97 `fmt`, `arg`, `got`, `exp`)
98
99isbigendian = struct.pack('=h', 1) == '\0\1'
100
101for fmt, arg, big, lil, asy in tests:
102 if verbose:
103 print `fmt`, `arg`, `big`, `lil`
104 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
105 ('='+fmt, isbigendian and big or lil)]:
106 res = struct.pack(xfmt, arg)
107 if res != exp:
108 raise TestFailed, "pack(%s, %s) -> %s # expected %s" % (
109 `fmt`, `arg`, `res`, `exp`)
110 n = struct.calcsize(xfmt)
111 if n != len(res):
112 raise TestFailed, "calcsize(%s) -> %d # expected %d" % (
113 `xfmt`, n, len(res))
114 rev = struct.unpack(xfmt, res)[0]
115 if rev != arg and not asy:
116 raise TestFailed, "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
117 `fmt`, `res`, `rev`, `arg`)