Improved some error messages for command line processing.
[python/dscho.git] / Lib / test / test_struct.py
blob2c599d720bbddfc314c1071da4baa11a1c750983
1 from test_support import TestFailed, verbose
2 import struct
3 ## import pdb
5 def simple_err(func, *args):
6 try:
7 apply(func, args)
8 except struct.error:
9 pass
10 else:
11 raise TestFailed, "%s%s did not raise struct.error" % (
12 func.__name__, args)
13 ## pdb.set_trace()
15 simple_err(struct.calcsize, 'Q')
17 sz = struct.calcsize('i')
18 if sz * 3 <> struct.calcsize('iii'):
19 raise TestFailed, 'inconsistent sizes'
21 fmt = 'cbxxxxxxhhhhiillffd'
22 fmt3 = '3c3b18x12h6i6l6f3d'
23 sz = struct.calcsize(fmt)
24 sz3 = struct.calcsize(fmt3)
25 if sz * 3 <> sz3:
26 raise TestFailed, 'inconsistent sizes (3*%s -> 3*%d = %d, %s -> %d)' % (
27 `fmt`, sz, 3*sz, `fmt3`, sz3)
29 simple_err(struct.pack, 'iii', 3)
30 simple_err(struct.pack, 'i', 3, 3, 3)
31 simple_err(struct.pack, 'i', 'foo')
32 simple_err(struct.unpack, 'd', 'flap')
33 s = struct.pack('ii', 1, 2)
34 simple_err(struct.unpack, 'iii', s)
35 simple_err(struct.unpack, 'i', s)
37 c = 'a'
38 b = 1
39 h = 255
40 i = 65535
41 l = 65536
42 f = 3.1415
43 d = 3.1415
45 for prefix in ('', '@', '<', '>', '=', '!'):
46 for format in ('xcbhilfd', 'xcBHILfd'):
47 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)))
58 # Test some of the new features in detail
60 # (format, argument, big-endian result, little-endian result, asymmetric)
61 tests = [
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),
88 ('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),
96 def badpack(fmt, arg, got, exp):
97 return
99 def badunpack(fmt, arg, got, exp):
100 return "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
101 `fmt`, `arg`, `got`, `exp`)
103 isbigendian = struct.pack('=h', 1) == '\0\1'
105 for fmt, arg, big, lil, asy in tests:
106 if verbose:
107 print `fmt`, `arg`, `big`, `lil`
108 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
109 ('='+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`)