annotate Lib/test/test_struct.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 32f6b0d3a66f
children aedd2d0dcb31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
1 from test_support import TestFailed, verbose
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
2 import struct
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 ## import pdb
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
4
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
5 def simple_err(func, *args):
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
6 try:
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
7 apply(func, args)
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
8 except struct.error:
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
9 pass
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
10 else:
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
11 raise TestFailed, "%s%s did not raise struct.error" % (
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
12 func.__name__, args)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
13 ## pdb.set_trace()
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
14
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 simple_err(struct.calcsize, 'Q')
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
16
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
17 sz = struct.calcsize('i')
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
18 if sz * 3 <> struct.calcsize('iii'):
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
19 raise TestFailed, 'inconsistent sizes'
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
20
4873
32f6b0d3a66f Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4862
diff changeset
21 fmt = 'cbxxxxxxhhhhiillffd'
32f6b0d3a66f Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4862
diff changeset
22 fmt3 = '3c3b18x12h6i6l6f3d'
32f6b0d3a66f Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4862
diff changeset
23 sz = struct.calcsize(fmt)
32f6b0d3a66f Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4862
diff changeset
24 sz3 = struct.calcsize(fmt3)
32f6b0d3a66f Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4862
diff changeset
25 if sz * 3 <> sz3:
32f6b0d3a66f Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4862
diff changeset
26 raise TestFailed, 'inconsistent sizes (3*%s -> 3*%d = %d, %s -> %d)' % (
32f6b0d3a66f Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4862
diff changeset
27 `fmt`, sz, 3*sz, `fmt3`, sz3)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
28
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
29 simple_err(struct.pack, 'iii', 3)
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
30 simple_err(struct.pack, 'i', 3, 3, 3)
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
31 simple_err(struct.pack, 'i', 'foo')
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
32 simple_err(struct.unpack, 'd', 'flap')
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
33 s = struct.pack('ii', 1, 2)
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
34 simple_err(struct.unpack, 'iii', s)
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
35 simple_err(struct.unpack, 'i', s)
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
36
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
37 c = 'a'
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
38 b = 1
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
39 h = 255
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
40 i = 65535
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
41 l = 65536
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
42 f = 3.1415
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
43 d = 3.1415
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
44
4862
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
45 for prefix in ('', '@', '<', '>', '=', '!'):
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
46 for format in ('xcbhilfd', 'xcBHILfd'):
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
47 format = prefix + format
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
48 if verbose:
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
49 print "trying:", format
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
50 s = struct.pack(format, c, b, h, i, l, f, d)
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
51 cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s)
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
52 if (cp <> c or bp <> b or hp <> h or ip <> i or lp <> l or
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
53 int(100 * fp) <> int(100 * f) or int(100 * dp) <> int(100 * d)):
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
54 # ^^^ calculate only to two decimal places
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
55 raise TestFailed, "unpack/pack not transitive (%s, %s)" % (
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
56 str(format), str((cp, bp, hp, ip, lp, fp, dp)))
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
57
4862
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
58 # Test some of the new features in detail
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
59
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
60 # (format, argument, big-endian result, little-endian result, asymmetric)
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
61 tests = [
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
62 ('c', 'a', 'a', 'a', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
63 ('xc', 'a', '\0a', '\0a', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
64 ('cx', 'a', 'a\0', 'a\0', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
65 ('s', 'a', 'a', 'a', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
66 ('0s', 'helloworld', '', '', 1),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
67 ('1s', 'helloworld', 'h', 'h', 1),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
68 ('9s', 'helloworld', 'helloworl', 'helloworl', 1),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
69 ('10s', 'helloworld', 'helloworld', 'helloworld', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
70 ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
71 ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
72 ('b', 7, '\7', '\7', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
73 ('b', -7, '\371', '\371', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
74 ('B', 7, '\7', '\7', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
75 ('B', 249, '\371', '\371', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
76 ('h', 700, '\002\274', '\274\002', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
77 ('h', -700, '\375D', 'D\375', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
78 ('H', 700, '\002\274', '\274\002', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
79 ('H', 0x10000-700, '\375D', 'D\375', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
80 ('i', 70000000, '\004,\035\200', '\200\035,\004', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
81 ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
82 ('I', 70000000L, '\004,\035\200', '\200\035,\004', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
83 ('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
84 ('l', 70000000, '\004,\035\200', '\200\035,\004', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
85 ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
86 ('L', 70000000L, '\004,\035\200', '\200\035,\004', 0),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
87 ('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
4862
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
88 ('f', 2.0, '@\000\000\000', '\000\000\000@', 0),
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
89 ('d', 2.0, '@\000\000\000\000\000\000\000',
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
90 '\000\000\000\000\000\000\000@', 0),
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
91 ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0),
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
92 ('d', -2.0, '\300\000\000\000\000\000\000\000',
7d7319811ff0 Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents: 4847
diff changeset
93 '\000\000\000\000\000\000\000\300', 0),
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
94 ]
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
95
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
96 def badpack(fmt, arg, got, exp):
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
97 return
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
98
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
99 def badunpack(fmt, arg, got, exp):
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
100 return "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
101 `fmt`, `arg`, `got`, `exp`)
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
102
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
103 isbigendian = struct.pack('=h', 1) == '\0\1'
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
104
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
105 for fmt, arg, big, lil, asy in tests:
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
106 if verbose:
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
107 print `fmt`, `arg`, `big`, `lil`
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
108 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
109 ('='+fmt, isbigendian and big or lil)]:
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
110 res = struct.pack(xfmt, arg)
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
111 if res != exp:
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
112 raise TestFailed, "pack(%s, %s) -> %s # expected %s" % (
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
113 `fmt`, `arg`, `res`, `exp`)
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
114 n = struct.calcsize(xfmt)
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
115 if n != len(res):
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
116 raise TestFailed, "calcsize(%s) -> %d # expected %d" % (
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
117 `xfmt`, n, len(res))
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
118 rev = struct.unpack(xfmt, res)[0]
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
119 if rev != arg and not asy:
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
120 raise TestFailed, "unpack(%s, %s) -> (%s,) # expected (%s,)" % (
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
121 `fmt`, `res`, `rev`, `arg`)