Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_struct.py @ 32527:0da660e05e69 legacy-trunk
"#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD
isn't always set to an integer value when it's defined.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Wed, 07 Jul 2004 20:42:07 +0000 |
| parents | 71a294bd7950 |
| children | 32df1d05cb2d |
| rev | line source |
|---|---|
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
19924
diff
changeset
|
1 from test.test_support import TestFailed, verbose, verify |
| 4706 | 2 import struct |
| 3 | |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
4 import sys |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
5 ISBIGENDIAN = sys.byteorder == "big" |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
6 del sys |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
7 verify((struct.pack('=i', 1)[0] == chr(0)) == ISBIGENDIAN, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
8 "bigendian determination appears wrong") |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
9 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
10 def string_reverse(s): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
11 chars = list(s) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
12 chars.reverse() |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
13 return "".join(chars) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
14 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
15 def bigendian_to_native(value): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
16 if ISBIGENDIAN: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
17 return value |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
18 else: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
19 return string_reverse(value) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
20 |
| 4706 | 21 def simple_err(func, *args): |
| 22 try: | |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
23 func(*args) |
| 4706 | 24 except struct.error: |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
25 pass |
| 4706 | 26 else: |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
27 raise TestFailed, "%s%s did not raise struct.error" % ( |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
28 func.__name__, args) |
| 4706 | 29 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
30 def any_err(func, *args): |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
31 try: |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
32 func(*args) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
33 except (struct.error, OverflowError, TypeError): |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
34 pass |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
35 else: |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
36 raise TestFailed, "%s%s did not raise error" % ( |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
37 func.__name__, args) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
38 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
39 |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
40 simple_err(struct.calcsize, 'Z') |
| 4706 | 41 |
| 42 sz = struct.calcsize('i') | |
|
15540
9d43129c8c55
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
15305
diff
changeset
|
43 if sz * 3 != struct.calcsize('iii'): |
|
4847
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
44 raise TestFailed, 'inconsistent sizes' |
| 4706 | 45 |
|
4873
32f6b0d3a66f
Change the ``calcsize*3'' test to be portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4862
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 sz3 = struct.calcsize(fmt3) |
|
15540
9d43129c8c55
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
15305
diff
changeset
|
50 if sz * 3 != sz3: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
51 raise TestFailed, 'inconsistent sizes (3*%r -> 3*%d = %d, %r -> %d)' % ( |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
52 fmt, sz, 3*sz, fmt3, sz3) |
| 4706 | 53 |
| 54 simple_err(struct.pack, 'iii', 3) | |
| 55 simple_err(struct.pack, 'i', 3, 3, 3) | |
| 56 simple_err(struct.pack, 'i', 'foo') | |
| 57 simple_err(struct.unpack, 'd', 'flap') | |
| 58 s = struct.pack('ii', 1, 2) | |
| 59 simple_err(struct.unpack, 'iii', s) | |
| 60 simple_err(struct.unpack, 'i', s) | |
| 61 | |
| 62 c = 'a' | |
|
4847
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
63 b = 1 |
| 4706 | 64 h = 255 |
| 65 i = 65535 | |
| 66 l = 65536 | |
| 67 f = 3.1415 | |
| 68 d = 3.1415 | |
| 69 | |
|
4862
7d7319811ff0
Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents:
4847
diff
changeset
|
70 for prefix in ('', '@', '<', '>', '=', '!'): |
|
7d7319811ff0
Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents:
4847
diff
changeset
|
71 for format in ('xcbhilfd', 'xcBHILfd'): |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
72 format = prefix + format |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
73 if verbose: |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
74 print "trying:", format |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
75 s = struct.pack(format, c, b, h, i, l, f, d) |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
76 cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s) |
|
15540
9d43129c8c55
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
15305
diff
changeset
|
77 if (cp != c or bp != b or hp != h or ip != i or lp != l or |
|
9d43129c8c55
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
15305
diff
changeset
|
78 int(100 * fp) != int(100 * f) or int(100 * dp) != int(100 * d)): |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
79 # ^^^ calculate only to two decimal places |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
80 raise TestFailed, "unpack/pack not transitive (%s, %s)" % ( |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
81 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
|
82 |
|
4862
7d7319811ff0
Added f/d tests for specific byte orders.
Guido van Rossum <guido@python.org>
parents:
4847
diff
changeset
|
83 # 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
|
84 |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
85 # (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
|
86 tests = [ |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
87 ('c', 'a', 'a', 'a', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
88 ('xc', 'a', '\0a', '\0a', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
89 ('cx', 'a', 'a\0', 'a\0', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
90 ('s', 'a', 'a', 'a', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
91 ('0s', 'helloworld', '', '', 1), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
92 ('1s', 'helloworld', 'h', 'h', 1), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
93 ('9s', 'helloworld', 'helloworl', 'helloworl', 1), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
94 ('10s', 'helloworld', 'helloworld', 'helloworld', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
95 ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
96 ('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
|
97 ('b', 7, '\7', '\7', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
98 ('b', -7, '\371', '\371', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
99 ('B', 7, '\7', '\7', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
100 ('B', 249, '\371', '\371', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
101 ('h', 700, '\002\274', '\274\002', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
102 ('h', -700, '\375D', 'D\375', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
103 ('H', 700, '\002\274', '\274\002', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
104 ('H', 0x10000-700, '\375D', 'D\375', 0), |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
105 ('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
|
106 ('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
|
107 ('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
|
108 ('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
|
109 ('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
|
110 ('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
|
111 ('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
|
112 ('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
|
113 ('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
|
114 ('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
|
115 '\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
|
116 ('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
|
117 ('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
|
118 '\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
|
119 ] |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
120 |
|
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
121 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
|
122 if verbose: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
123 print "%r %r %r %r" % (fmt, arg, big, lil) |
|
4847
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
124 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil), |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
125 ('='+fmt, ISBIGENDIAN and big or lil)]: |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
126 res = struct.pack(xfmt, arg) |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
127 if res != exp: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
128 raise TestFailed, "pack(%r, %r) -> %r # expected %r" % ( |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
129 fmt, arg, res, exp) |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
130 n = struct.calcsize(xfmt) |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
131 if n != len(res): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
132 raise TestFailed, "calcsize(%r) -> %d # expected %d" % ( |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
133 xfmt, n, len(res)) |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
134 rev = struct.unpack(xfmt, res)[0] |
|
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4873
diff
changeset
|
135 if rev != arg and not asy: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
136 raise TestFailed, "unpack(%r, %r) -> (%r,) # expected (%r,)" % ( |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28334
diff
changeset
|
137 fmt, res, rev, arg) |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
138 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
139 ########################################################################### |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
140 # Simple native q/Q tests. |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
141 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
142 has_native_qQ = 1 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
143 try: |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
144 struct.pack("q", 5) |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
145 except struct.error: |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
146 has_native_qQ = 0 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
147 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
148 if verbose: |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
149 print "Platform has native q/Q?", has_native_qQ and "Yes." or "No." |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
150 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
151 any_err(struct.pack, "Q", -1) # can't pack -1 as unsigned regardless |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
152 simple_err(struct.pack, "q", "a") # can't pack string as 'q' regardless |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
153 simple_err(struct.pack, "Q", "a") # ditto, but 'Q' |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
154 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
155 def test_native_qQ(): |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
156 bytes = struct.calcsize('q') |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
157 # The expected values here are in big-endian format, primarily because |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
158 # I'm on a little-endian machine and so this is the clearest way (for |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
159 # me) to force the code to get exercised. |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
160 for format, input, expected in ( |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
161 ('q', -1, '\xff' * bytes), |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
162 ('q', 0, '\x00' * bytes), |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
163 ('Q', 0, '\x00' * bytes), |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
164 ('q', 1L, '\x00' * (bytes-1) + '\x01'), |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
165 ('Q', (1L << (8*bytes))-1, '\xff' * bytes), |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
166 ('q', (1L << (8*bytes-1))-1, '\x7f' + '\xff' * (bytes - 1))): |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
167 got = struct.pack(format, input) |
|
18070
f901a7dc5fb4
Renamed some stuff to tell the truth about what it does.
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
168 native_expected = bigendian_to_native(expected) |
|
f901a7dc5fb4
Renamed some stuff to tell the truth about what it does.
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
169 verify(got == native_expected, |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
170 "%r-pack of %r gave %r, not %r" % |
|
18070
f901a7dc5fb4
Renamed some stuff to tell the truth about what it does.
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
171 (format, input, got, native_expected)) |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
172 retrieved = struct.unpack(format, got)[0] |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
173 verify(retrieved == input, |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
174 "%r-unpack of %r gave %r, not %r" % |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
15540
diff
changeset
|
175 (format, got, retrieved, input)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
176 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
177 if has_native_qQ: |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
178 test_native_qQ() |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
179 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
180 ########################################################################### |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
181 # Standard integer tests (bBhHiIlLqQ). |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
182 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
183 import binascii |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
184 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
185 class IntTester: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
186 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
187 # XXX Most std integer modes fail to test for out-of-range. |
|
18149
2e4c6ba7d09c
Added "i" and "l" to the list of std-mode struct codes that don't range-
Tim Peters <tim.peters@gmail.com>
parents:
18111
diff
changeset
|
188 # The "i" and "l" codes appear to range-check OK on 32-bit boxes, but |
|
2e4c6ba7d09c
Added "i" and "l" to the list of std-mode struct codes that don't range-
Tim Peters <tim.peters@gmail.com>
parents:
18111
diff
changeset
|
189 # fail to check correctly on some 64-bit ones (Tru64 Unix + Compaq C |
|
2e4c6ba7d09c
Added "i" and "l" to the list of std-mode struct codes that don't range-
Tim Peters <tim.peters@gmail.com>
parents:
18111
diff
changeset
|
190 # reported by Mark Favas). |
|
2e4c6ba7d09c
Added "i" and "l" to the list of std-mode struct codes that don't range-
Tim Peters <tim.peters@gmail.com>
parents:
18111
diff
changeset
|
191 BUGGY_RANGE_CHECK = "bBhHiIlL" |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
192 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
193 def __init__(self, formatpair, bytesize): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
194 assert len(formatpair) == 2 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
195 self.formatpair = formatpair |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
196 for direction in "<>!=": |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
197 for code in formatpair: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
198 format = direction + code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
199 verify(struct.calcsize(format) == bytesize) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
200 self.bytesize = bytesize |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
201 self.bitsize = bytesize * 8 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
202 self.signed_code, self.unsigned_code = formatpair |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
203 self.unsigned_min = 0 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
204 self.unsigned_max = 2L**self.bitsize - 1 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
205 self.signed_min = -(2L**(self.bitsize-1)) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
206 self.signed_max = 2L**(self.bitsize-1) - 1 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
207 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
208 def test_one(self, x, pack=struct.pack, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
209 unpack=struct.unpack, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
210 unhexlify=binascii.unhexlify): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
211 if verbose: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
212 print "trying std", self.formatpair, "on", x, "==", hex(x) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
213 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
214 # Try signed. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
215 code = self.signed_code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
216 if self.signed_min <= x <= self.signed_max: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
217 # Try big-endian. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
218 expected = long(x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
219 if x < 0: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
220 expected += 1L << self.bitsize |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
221 assert expected > 0 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
222 expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
223 if len(expected) & 1: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
224 expected = "0" + expected |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
225 expected = unhexlify(expected) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
226 expected = "\x00" * (self.bytesize - len(expected)) + expected |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
227 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
228 # Pack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
229 format = ">" + code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
230 got = pack(format, x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
231 verify(got == expected, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
232 "'%s'-pack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
233 (format, x, got, expected)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
234 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
235 # Unpack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
236 retrieved = unpack(format, got)[0] |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
237 verify(x == retrieved, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
238 "'%s'-unpack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
239 (format, got, retrieved, x)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
240 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
241 # Adding any byte should cause a "too big" error. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
242 any_err(unpack, format, '\x01' + got) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
243 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
244 # Try little-endian. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
245 format = "<" + code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
246 expected = string_reverse(expected) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
247 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
248 # Pack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
249 got = pack(format, x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
250 verify(got == expected, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
251 "'%s'-pack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
252 (format, x, got, expected)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
253 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
254 # Unpack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
255 retrieved = unpack(format, got)[0] |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
256 verify(x == retrieved, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
257 "'%s'-unpack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
258 (format, got, retrieved, x)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
259 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
260 # Adding any byte should cause a "too big" error. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
261 any_err(unpack, format, '\x01' + got) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
262 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
263 else: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
264 # x is out of range -- verify pack realizes that. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
265 if code in self.BUGGY_RANGE_CHECK: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
266 if verbose: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
267 print "Skipping buggy range check for code", code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
268 else: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
269 any_err(pack, ">" + code, x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
270 any_err(pack, "<" + code, x) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
271 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
272 # Much the same for unsigned. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
273 code = self.unsigned_code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
274 if self.unsigned_min <= x <= self.unsigned_max: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
275 # Try big-endian. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
276 format = ">" + code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
277 expected = long(x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
278 expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
279 if len(expected) & 1: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
280 expected = "0" + expected |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
281 expected = unhexlify(expected) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
282 expected = "\x00" * (self.bytesize - len(expected)) + expected |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
283 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
284 # Pack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
285 got = pack(format, x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
286 verify(got == expected, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
287 "'%s'-pack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
288 (format, x, got, expected)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
289 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
290 # Unpack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
291 retrieved = unpack(format, got)[0] |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
292 verify(x == retrieved, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
293 "'%s'-unpack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
294 (format, got, retrieved, x)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
295 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
296 # Adding any byte should cause a "too big" error. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
297 any_err(unpack, format, '\x01' + got) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
298 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
299 # Try little-endian. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
300 format = "<" + code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
301 expected = string_reverse(expected) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
302 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
303 # Pack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
304 got = pack(format, x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
305 verify(got == expected, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
306 "'%s'-pack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
307 (format, x, got, expected)) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
308 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
309 # Unpack work? |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
310 retrieved = unpack(format, got)[0] |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
311 verify(x == retrieved, |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
312 "'%s'-unpack of %r gave %r, not %r" % |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
313 (format, got, retrieved, x)) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
314 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
315 # Adding any byte should cause a "too big" error. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
316 any_err(unpack, format, '\x01' + got) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
317 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
318 else: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
319 # x is out of range -- verify pack realizes that. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
320 if code in self.BUGGY_RANGE_CHECK: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
321 if verbose: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
322 print "Skipping buggy range check for code", code |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
323 else: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
324 any_err(pack, ">" + code, x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
325 any_err(pack, "<" + code, x) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
326 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
327 def run(self): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
328 from random import randrange |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
329 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
330 # Create all interesting powers of 2. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
331 values = [] |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
332 for exp in range(self.bitsize + 3): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
333 values.append(1L << exp) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
334 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
335 # Add some random values. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
336 for i in range(self.bitsize): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
337 val = 0L |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
338 for j in range(self.bytesize): |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
339 val = (val << 8) | randrange(256) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
340 values.append(val) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
341 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
342 # Try all those, and their negations, and +-1 from them. Note |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
343 # that this tests all power-of-2 boundaries in range, and a few out |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
344 # of range, plus +-(2**n +- 1). |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
345 for base in values: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
346 for val in -base, base: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
347 for incr in -1, 0, 1: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
348 x = val + incr |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
349 try: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
350 x = int(x) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
351 except OverflowError: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
352 pass |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
353 self.test_one(x) |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18070
diff
changeset
|
354 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
355 # Some error cases. |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
356 for direction in "<>": |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
357 for code in self.formatpair: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
358 for badobject in "a string", 3+42j, randrange: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
359 any_err(struct.pack, direction + code, badobject) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
360 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
361 for args in [("bB", 1), |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
362 ("hH", 2), |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
363 ("iI", 4), |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
364 ("lL", 4), |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
365 ("qQ", 8)]: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
366 t = IntTester(*args) |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
367 t.run() |
|
19924
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
368 |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
369 |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
370 ########################################################################### |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
371 # The p ("Pascal string") code. |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
372 |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
373 def test_p_code(): |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
374 for code, input, expected, expectedback in [ |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
375 ('p','abc', '\x00', ''), |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
376 ('1p', 'abc', '\x00', ''), |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
377 ('2p', 'abc', '\x01a', 'a'), |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
378 ('3p', 'abc', '\x02ab', 'ab'), |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
379 ('4p', 'abc', '\x03abc', 'abc'), |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
380 ('5p', 'abc', '\x03abc\x00', 'abc'), |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
381 ('6p', 'abc', '\x03abc\x00\x00', 'abc'), |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
382 ('1000p', 'x'*1000, '\xff' + 'x'*999, 'x'*255)]: |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
383 got = struct.pack(code, input) |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
384 if got != expected: |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
385 raise TestFailed("pack(%r, %r) == %r but expected %r" % |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
386 (code, input, got, expected)) |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
387 (got,) = struct.unpack(code, got) |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
388 if got != expectedback: |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
389 raise TestFailed("unpack(%r, %r) == %r but expected %r" % |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
390 (code, input, got, expectedback)) |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
391 |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
392 test_p_code() |
|
28334
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
393 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
394 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
395 ########################################################################### |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
396 # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
397 # from the low-order discarded bits could propagate into the exponent |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
398 # field, causing the result to be wrong by a factor of 2. |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
399 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
400 def test_705836(): |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
401 import math |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
402 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
403 for base in range(1, 33): |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
404 # smaller <- largest representable float less than base. |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
405 delta = 0.5 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
406 while base - delta / 2.0 != base: |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
407 delta /= 2.0 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
408 smaller = base - delta |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
409 # Packing this rounds away a solid string of trailing 1 bits. |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
410 packed = struct.pack("<f", smaller) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
411 unpacked = struct.unpack("<f", packed)[0] |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
412 # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
413 # 16, respectively. |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
414 verify(base == unpacked) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
415 bigpacked = struct.pack(">f", smaller) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
416 verify(bigpacked == string_reverse(packed), |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
417 ">f pack should be byte-reversal of <f pack") |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
418 unpacked = struct.unpack(">f", bigpacked)[0] |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
419 verify(base == unpacked) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
420 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
421 # Largest finite IEEE single. |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
422 big = (1 << 24) - 1 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
423 big = math.ldexp(big, 127 - 23) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
424 packed = struct.pack(">f", big) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
425 unpacked = struct.unpack(">f", packed)[0] |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
426 verify(big == unpacked) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
427 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
428 # The same, but tack on a 1 bit so it rounds up to infinity. |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
429 big = (1 << 25) - 1 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
430 big = math.ldexp(big, 127 - 24) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
431 try: |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
432 packed = struct.pack(">f", big) |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
433 except OverflowError: |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
434 pass |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
435 else: |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
436 TestFailed("expected OverflowError") |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
437 |
|
9f287e5eb037
SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
438 test_705836() |
