blob: 2c599d720bbddfc314c1071da4baa11a1c750983 [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:
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
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
Guido van Rossum04ebf5c1997-01-03 19:00:37 +000021fmt = 'cbxxxxxxhhhhiillffd'
22fmt3 = '3c3b18x12h6i6l6f3d'
23sz = struct.calcsize(fmt)
24sz3 = struct.calcsize(fmt3)
25if sz * 3 <> sz3:
26 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)
52 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)):
54 # ^^^ 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
96def badpack(fmt, arg, got, exp):
97 return
98
99def badunpack(fmt, arg, got, exp):
100 return "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
Guido van Rossum41360a41998-03-26 19:42:58 +0000101 `fmt`, `arg`, `got`, `exp`)
Guido van Rossum2a378501996-12-31 17:25:47 +0000102
103isbigendian = struct.pack('=h', 1) == '\0\1'
104
105for fmt, arg, big, lil, asy in tests:
106 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000107 print `fmt`, `arg`, `big`, `lil`
Guido van Rossum2a378501996-12-31 17:25:47 +0000108 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
Guido van Rossum41360a41998-03-26 19:42:58 +0000109 ('='+fmt, isbigendian and big or lil)]:
110 res = struct.pack(xfmt, arg)
111 if res != exp:
112 raise TestFailed, "pack(%s, %s) -> %s # expected %s" % (
113 `fmt`, `arg`, `res`, `exp`)
114 n = struct.calcsize(xfmt)
115 if n != len(res):
116 raise TestFailed, "calcsize(%s) -> %d # expected %d" % (
117 `xfmt`, n, len(res))
118 rev = struct.unpack(xfmt, res)[0]
119 if rev != arg and not asy:
120 raise TestFailed, "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
121 `fmt`, `res`, `rev`, `arg`)