blob: 77cda296e7d1d515038f8a3ddff9c069fd35daf6 [file] [log] [blame]
Barry Warsaw07a0eec1996-12-12 23:34:06 +00001import struct
2## import pdb
3
4def simple_err(func, *args):
5 try:
6 apply(func, args)
7 except struct.error:
8 pass
9 else:
10 print 'expected struct.error not caught'
11## pdb.set_trace()
12
13simple_err(struct.calcsize, 'Q')
14
15sz = struct.calcsize('i')
16if sz * 3 <> struct.calcsize('iii'):
17 print 'inconsistent sizes'
18
19sz = struct.calcsize('cbhilfd')
20if sz * 3 <> struct.calcsize('3c3b3h3i3l3f3d'):
21 print 'inconsistent sizes'
22
23simple_err(struct.pack, 'iii', 3)
24simple_err(struct.pack, 'i', 3, 3, 3)
25simple_err(struct.pack, 'i', 'foo')
26simple_err(struct.unpack, 'd', 'flap')
27s = struct.pack('ii', 1, 2)
28simple_err(struct.unpack, 'iii', s)
29simple_err(struct.unpack, 'i', s)
30
31c = 'a'
32b = -1
33h = 255
34i = 65535
35l = 65536
36f = 3.1415
37d = 3.1415
38
39s = struct.pack('xcbhilfd', c, b, h, i, l, f, d)
40cp, bp, hp, ip, lp, fp, dp = struct.unpack('xcbhilfd', s)
41if cp <> c or bp <> b or hp <> h or ip <> i or lp <> l or \
42 int(100 * fp) <> int(100 * f) or int(100 * dp) <> int(100 * d):
43 # ^^^ calculate only to two decimal places
44 print 'unpack/pack not transitive'