Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_struct.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | f3b26a729e82 |
| children | f1ea03e37ff6 |
| rev | line source |
|---|---|
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
1 import array |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
2 import unittest |
| 4706 | 3 import struct |
|
38493
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
4 import warnings |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
5 warnings.filterwarnings("ignore", "struct integer overflow masking is deprecated", |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
6 DeprecationWarning) |
| 4706 | 7 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
8 from functools import wraps |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
48834
diff
changeset
|
9 from test.test_support import TestFailed, verbose, run_unittest |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
10 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
11 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
|
12 ISBIGENDIAN = sys.byteorder == "big" |
|
48834
75f64fd1f113
Fix bug 3625: test issues on 64bit windows. r=pitrou
Mark Hammond <mhammond@skippinet.com.au>
parents:
47942
diff
changeset
|
13 IS32BIT = sys.maxsize == 0x7fffffff |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
14 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
|
15 |
|
38493
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
16 try: |
|
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
17 import _struct |
|
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
18 except ImportError: |
|
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
19 PY_STRUCT_RANGE_CHECKING = 0 |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38495
diff
changeset
|
20 PY_STRUCT_OVERFLOW_MASKING = 1 |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
21 PY_STRUCT_FLOAT_COERCE = 2 |
|
38493
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
22 else: |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
23 PY_STRUCT_RANGE_CHECKING = getattr(_struct, '_PY_STRUCT_RANGE_CHECKING', 0) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
24 PY_STRUCT_OVERFLOW_MASKING = getattr(_struct, '_PY_STRUCT_OVERFLOW_MASKING', 0) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
25 PY_STRUCT_FLOAT_COERCE = getattr(_struct, '_PY_STRUCT_FLOAT_COERCE', 0) |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38355
diff
changeset
|
26 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
27 def string_reverse(s): |
|
38615
c4b44222a599
Access _struct attributes directly instead of mucking with getattr.
Tim Peters <tim.peters@gmail.com>
parents:
38580
diff
changeset
|
28 return "".join(reversed(s)) |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
29 |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 else: |
|
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
34 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
|
35 |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
36 def with_warning_restore(func): |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
37 @wraps(func) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
38 def decorator(*args, **kw): |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
48834
diff
changeset
|
39 with warnings.catch_warnings(): |
|
47942
b35c8b1940af
Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47721
diff
changeset
|
40 # We need this function to warn every time, so stick an |
|
b35c8b1940af
Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47721
diff
changeset
|
41 # unqualifed 'always' at the head of the filter list |
|
b35c8b1940af
Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47721
diff
changeset
|
42 warnings.simplefilter("always") |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
43 warnings.filterwarnings("error", category=DeprecationWarning) |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
44 return func(*args, **kw) |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
45 return decorator |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
46 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
47 @with_warning_restore |
|
38493
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
48 def deprecated_err(func, *args): |
|
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
49 try: |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
50 func(*args) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
51 except (struct.error, TypeError): |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
52 pass |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
53 except DeprecationWarning: |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
54 if not PY_STRUCT_OVERFLOW_MASKING: |
|
47942
b35c8b1940af
Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47721
diff
changeset
|
55 raise TestFailed, "%s%s expected to raise DeprecationWarning" % ( |
|
38493
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
56 func.__name__, args) |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
57 else: |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
58 raise TestFailed, "%s%s did not raise error" % ( |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
59 func.__name__, args) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
38615
diff
changeset
|
60 |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
61 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
62 class StructTest(unittest.TestCase): |
| 4706 | 63 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
64 @with_warning_restore |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
65 def check_float_coerce(self, format, number): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
66 # SF bug 1530559. struct.pack raises TypeError where it used to convert. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
67 if PY_STRUCT_FLOAT_COERCE == 2: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
68 # Test for pre-2.5 struct module |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
69 packed = struct.pack(format, number) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
70 floored = struct.unpack(format, packed)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
71 self.assertEqual(floored, int(number), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
72 "did not correcly coerce float to int") |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
73 return |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
74 try: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
75 struct.pack(format, number) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
76 except (struct.error, TypeError): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
77 if PY_STRUCT_FLOAT_COERCE: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
78 self.fail("expected DeprecationWarning for float coerce") |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
79 except DeprecationWarning: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
80 if not PY_STRUCT_FLOAT_COERCE: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
81 self.fail("expected to raise struct.error for float coerce") |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
82 else: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
83 self.fail("did not raise error for float coerce") |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
84 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
85 def test_isbigendian(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
86 self.assertEqual((struct.pack('=i', 1)[0] == chr(0)), ISBIGENDIAN) |
| 4706 | 87 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
88 def test_consistence(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
89 self.assertRaises(struct.error, struct.calcsize, 'Z') |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
90 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
91 sz = struct.calcsize('i') |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
92 self.assertEqual(sz * 3, struct.calcsize('iii')) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
93 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
94 fmt = 'cbxxxxxxhhhhiillffd?' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
95 fmt3 = '3c3b18x12h6i6l6f3d3?' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
96 sz = struct.calcsize(fmt) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
97 sz3 = struct.calcsize(fmt3) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
98 self.assertEqual(sz * 3, sz3) |
| 4706 | 99 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
100 self.assertRaises(struct.error, struct.pack, 'iii', 3) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
101 self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
102 self.assertRaises(struct.error, struct.pack, 'i', 'foo') |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
103 self.assertRaises(struct.error, struct.pack, 'P', 'foo') |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
104 self.assertRaises(struct.error, struct.unpack, 'd', 'flap') |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
105 s = struct.pack('ii', 1, 2) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
106 self.assertRaises(struct.error, struct.unpack, 'iii', s) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
107 self.assertRaises(struct.error, struct.unpack, 'i', s) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
108 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
109 def test_transitiveness(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
110 c = 'a' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
111 b = 1 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
112 h = 255 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
113 i = 65535 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
114 l = 65536 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
115 f = 3.1415 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
116 d = 3.1415 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
117 t = True |
| 4706 | 118 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
119 for prefix in ('', '@', '<', '>', '=', '!'): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
120 for format in ('xcbhilfd?', 'xcBHILfd?'): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
121 format = prefix + format |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
122 s = struct.pack(format, c, b, h, i, l, f, d, t) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
123 cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
124 self.assertEqual(cp, c) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
125 self.assertEqual(bp, b) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
126 self.assertEqual(hp, h) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
127 self.assertEqual(ip, i) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
128 self.assertEqual(lp, l) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
129 self.assertEqual(int(100 * fp), int(100 * f)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
130 self.assertEqual(int(100 * dp), int(100 * d)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
131 self.assertEqual(tp, t) |
| 4706 | 132 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
133 def test_new_features(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
134 # Test some of the new features in detail |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
135 # (format, argument, big-endian result, little-endian result, asymmetric) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
136 tests = [ |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
137 ('c', 'a', 'a', 'a', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
138 ('xc', 'a', '\0a', '\0a', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
139 ('cx', 'a', 'a\0', 'a\0', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
140 ('s', 'a', 'a', 'a', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
141 ('0s', 'helloworld', '', '', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
142 ('1s', 'helloworld', 'h', 'h', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
143 ('9s', 'helloworld', 'helloworl', 'helloworl', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
144 ('10s', 'helloworld', 'helloworld', 'helloworld', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
145 ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
146 ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
147 ('b', 7, '\7', '\7', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
148 ('b', -7, '\371', '\371', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
149 ('B', 7, '\7', '\7', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
150 ('B', 249, '\371', '\371', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
151 ('h', 700, '\002\274', '\274\002', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
152 ('h', -700, '\375D', 'D\375', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
153 ('H', 700, '\002\274', '\274\002', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
154 ('H', 0x10000-700, '\375D', 'D\375', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
155 ('i', 70000000, '\004,\035\200', '\200\035,\004', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
156 ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
157 ('I', 70000000L, '\004,\035\200', '\200\035,\004', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
158 ('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
159 ('l', 70000000, '\004,\035\200', '\200\035,\004', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
160 ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
161 ('L', 70000000L, '\004,\035\200', '\200\035,\004', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
162 ('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
163 ('f', 2.0, '@\000\000\000', '\000\000\000@', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
164 ('d', 2.0, '@\000\000\000\000\000\000\000', |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
165 '\000\000\000\000\000\000\000@', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
166 ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
167 ('d', -2.0, '\300\000\000\000\000\000\000\000', |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
168 '\000\000\000\000\000\000\000\300', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
169 ('?', 0, '\0', '\0', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
170 ('?', 3, '\1', '\1', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
171 ('?', True, '\1', '\1', 0), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
172 ('?', [], '\0', '\0', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
173 ('?', (1,), '\1', '\1', 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
174 ] |
|
4847
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
175 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
176 for fmt, arg, big, lil, asy in tests: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
177 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
178 ('='+fmt, ISBIGENDIAN and big or lil)]: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
179 res = struct.pack(xfmt, arg) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
180 self.assertEqual(res, exp) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
181 self.assertEqual(struct.calcsize(xfmt), len(res)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
182 rev = struct.unpack(xfmt, res)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
183 if rev != arg: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
184 self.assert_(asy) |
|
4847
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
185 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
186 def test_native_qQ(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
187 # can't pack -1 as unsigned regardless |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
188 self.assertRaises((struct.error, TypeError), struct.pack, "Q", -1) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
189 # can't pack string as 'q' regardless |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
190 self.assertRaises(struct.error, struct.pack, "q", "a") |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
191 # ditto, but 'Q' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
192 self.assertRaises(struct.error, struct.pack, "Q", "a") |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
193 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
194 try: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
195 struct.pack("q", 5) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
196 except struct.error: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
197 # does not have native q/Q |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
198 pass |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
199 else: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
200 bytes = struct.calcsize('q') |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
201 # The expected values here are in big-endian format, primarily |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
202 # because I'm on a little-endian machine and so this is the |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
203 # clearest way (for me) to force the code to get exercised. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
204 for format, input, expected in ( |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
205 ('q', -1, '\xff' * bytes), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
206 ('q', 0, '\x00' * bytes), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
207 ('Q', 0, '\x00' * bytes), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
208 ('q', 1L, '\x00' * (bytes-1) + '\x01'), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
209 ('Q', (1L << (8*bytes))-1, '\xff' * bytes), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
210 ('q', (1L << (8*bytes-1))-1, '\x7f' + '\xff' * (bytes - 1))): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
211 got = struct.pack(format, input) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
212 native_expected = bigendian_to_native(expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
213 self.assertEqual(got, native_expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
214 retrieved = struct.unpack(format, got)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
215 self.assertEqual(retrieved, input) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
216 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
217 def test_standard_integers(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
218 # Standard integer tests (bBhHiIlLqQ). |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
219 import binascii |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
220 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
221 class IntTester(unittest.TestCase): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
222 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
223 # XXX Most std integer modes fail to test for out-of-range. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
224 # The "i" and "l" codes appear to range-check OK on 32-bit boxes, but |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
225 # fail to check correctly on some 64-bit ones (Tru64 Unix + Compaq C |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
226 # reported by Mark Favas). |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
227 BUGGY_RANGE_CHECK = "bBhHiIlL" |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
228 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
229 def __init__(self, formatpair, bytesize): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
230 self.assertEqual(len(formatpair), 2) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
231 self.formatpair = formatpair |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
232 for direction in "<>!=": |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
233 for code in formatpair: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
234 format = direction + code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
235 self.assertEqual(struct.calcsize(format), bytesize) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
236 self.bytesize = bytesize |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
237 self.bitsize = bytesize * 8 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
238 self.signed_code, self.unsigned_code = formatpair |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
239 self.unsigned_min = 0 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
240 self.unsigned_max = 2L**self.bitsize - 1 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
241 self.signed_min = -(2L**(self.bitsize-1)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
242 self.signed_max = 2L**(self.bitsize-1) - 1 |
|
4847
d53812d68408
Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents:
4706
diff
changeset
|
243 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
244 def test_one(self, x, pack=struct.pack, |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
245 unpack=struct.unpack, |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
246 unhexlify=binascii.unhexlify): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
247 # Try signed. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
248 code = self.signed_code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
249 if self.signed_min <= x <= self.signed_max: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
250 # Try big-endian. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
251 expected = long(x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
252 if x < 0: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
253 expected += 1L << self.bitsize |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
254 self.assert_(expected > 0) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
255 expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
256 if len(expected) & 1: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
257 expected = "0" + expected |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
258 expected = unhexlify(expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
259 expected = "\x00" * (self.bytesize - len(expected)) + 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
|
260 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
261 # Pack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
262 format = ">" + code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
263 got = pack(format, x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
264 self.assertEqual(got, 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
|
265 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
266 # Unpack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
267 retrieved = unpack(format, got)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
268 self.assertEqual(x, retrieved) |
|
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
|
269 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
270 # Adding any byte should cause a "too big" error. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
271 self.assertRaises((struct.error, TypeError), unpack, format, |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
272 '\x01' + got) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
273 # Try little-endian. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
274 format = "<" + code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
275 expected = string_reverse(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
|
276 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
277 # Pack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
278 got = pack(format, x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
279 self.assertEqual(got, expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
280 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
281 # Unpack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
282 retrieved = unpack(format, got)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
283 self.assertEqual(x, retrieved) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
284 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
285 # Adding any byte should cause a "too big" error. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
286 self.assertRaises((struct.error, TypeError), unpack, format, |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
287 '\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
|
288 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
289 else: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
290 # x is out of range -- verify pack realizes that. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
291 if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
292 if verbose: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
293 print "Skipping buggy range check for code", code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
294 else: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
295 deprecated_err(pack, ">" + code, x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
296 deprecated_err(pack, "<" + code, x) |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
297 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
298 # Much the same for unsigned. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
299 code = self.unsigned_code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
300 if self.unsigned_min <= x <= self.unsigned_max: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
301 # Try big-endian. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
302 format = ">" + code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
303 expected = long(x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
304 expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
305 if len(expected) & 1: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
306 expected = "0" + expected |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
307 expected = unhexlify(expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
308 expected = "\x00" * (self.bytesize - len(expected)) + expected |
|
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 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
310 # Pack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
311 got = pack(format, x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
312 self.assertEqual(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
|
313 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
314 # Unpack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
315 retrieved = unpack(format, got)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
316 self.assertEqual(x, retrieved) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
317 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
318 # Adding any byte should cause a "too big" error. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
319 self.assertRaises((struct.error, TypeError), unpack, format, |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
320 '\x01' + got) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
321 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
322 # Try little-endian. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
323 format = "<" + code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
324 expected = string_reverse(expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
325 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
326 # Pack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
327 got = pack(format, x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
328 self.assertEqual(got, expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
329 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
330 # Unpack work? |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
331 retrieved = unpack(format, got)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
332 self.assertEqual(x, retrieved) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
333 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
334 # Adding any byte should cause a "too big" error. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
335 self.assertRaises((struct.error, TypeError), unpack, format, |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
336 '\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
|
337 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
338 else: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
339 # x is out of range -- verify pack realizes that. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
340 if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
341 if verbose: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
342 print "Skipping buggy range check for code", code |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
343 else: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
344 deprecated_err(pack, ">" + code, x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
345 deprecated_err(pack, "<" + code, x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
346 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
347 def run(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
348 from random import randrange |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
349 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
350 # Create all interesting powers of 2. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
351 values = [] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
352 for exp in range(self.bitsize + 3): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
353 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
|
354 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
355 # Add some random values. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
356 for i in range(self.bitsize): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
357 val = 0L |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
358 for j in range(self.bytesize): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
359 val = (val << 8) | randrange(256) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
360 values.append(val) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
361 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
362 # Try all those, and their negations, and +-1 from them. Note |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
363 # that this tests all power-of-2 boundaries in range, and a few out |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
364 # of range, plus +-(2**n +- 1). |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
365 for base in values: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
366 for val in -base, base: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
367 for incr in -1, 0, 1: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
368 x = val + incr |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
369 try: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
370 x = int(x) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
371 except OverflowError: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
372 pass |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
373 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
|
374 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
375 # Some error cases. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
376 for direction in "<>": |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
377 for code in self.formatpair: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
378 for badobject in "a string", 3+42j, randrange: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
379 self.assertRaises((struct.error, TypeError), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
380 struct.pack, direction + code, |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
381 badobject) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
382 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
383 for args in [("bB", 1), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
384 ("hH", 2), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
385 ("iI", 4), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
386 ("lL", 4), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
387 ("qQ", 8)]: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
388 t = IntTester(*args) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
389 t.run() |
|
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
|
390 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
391 def test_p_code(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
392 # Test p ("Pascal string") code. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
393 for code, input, expected, expectedback in [ |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
394 ('p','abc', '\x00', ''), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
395 ('1p', 'abc', '\x00', ''), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
396 ('2p', 'abc', '\x01a', 'a'), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
397 ('3p', 'abc', '\x02ab', 'ab'), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
398 ('4p', 'abc', '\x03abc', 'abc'), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
399 ('5p', 'abc', '\x03abc\x00', 'abc'), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
400 ('6p', 'abc', '\x03abc\x00\x00', 'abc'), |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
401 ('1000p', 'x'*1000, '\xff' + 'x'*999, 'x'*255)]: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
402 got = struct.pack(code, input) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
403 self.assertEqual(got, expected) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
404 (got,) = struct.unpack(code, got) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
405 self.assertEqual(got, expectedback) |
|
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
|
406 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
407 def test_705836(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
408 # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
409 # from the low-order discarded bits could propagate into the exponent |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
410 # field, causing the result to be wrong by a factor of 2. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
411 import math |
|
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
|
412 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
413 for base in range(1, 33): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
414 # smaller <- largest representable float less than base. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
415 delta = 0.5 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
416 while base - delta / 2.0 != base: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
417 delta /= 2.0 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
418 smaller = base - delta |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
419 # Packing this rounds away a solid string of trailing 1 bits. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
420 packed = struct.pack("<f", smaller) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
421 unpacked = struct.unpack("<f", packed)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
422 # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
423 # 16, respectively. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
424 self.assertEqual(base, unpacked) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
425 bigpacked = struct.pack(">f", smaller) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
426 self.assertEqual(bigpacked, string_reverse(packed)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
427 unpacked = struct.unpack(">f", bigpacked)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
428 self.assertEqual(base, unpacked) |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
429 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
430 # Largest finite IEEE single. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
431 big = (1 << 24) - 1 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
432 big = math.ldexp(big, 127 - 23) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
433 packed = struct.pack(">f", big) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
434 unpacked = struct.unpack(">f", packed)[0] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
435 self.assertEqual(big, unpacked) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
436 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
437 # The same, but tack on a 1 bit so it rounds up to infinity. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
438 big = (1 << 25) - 1 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
439 big = math.ldexp(big, 127 - 24) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
440 self.assertRaises(OverflowError, struct.pack, ">f", big) |
|
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
|
441 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
442 if PY_STRUCT_RANGE_CHECKING: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
443 def test_1229380(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
444 # SF bug 1229380. No struct.pack exception for some out of |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
445 # range integers |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
446 import sys |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
447 for endian in ('', '>', '<'): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
448 for cls in (int, long): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
449 for fmt in ('B', 'H', 'I', 'L'): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
450 deprecated_err(struct.pack, endian + fmt, cls(-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
|
451 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
452 deprecated_err(struct.pack, endian + 'B', cls(300)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
453 deprecated_err(struct.pack, endian + 'H', cls(70000)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
454 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
455 deprecated_err(struct.pack, endian + 'I', sys.maxint * 4L) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
456 deprecated_err(struct.pack, endian + 'L', sys.maxint * 4L) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
457 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
458 def XXXtest_1530559(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
459 # XXX This is broken: see the bug report |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
460 # SF bug 1530559. struct.pack raises TypeError where it used to convert. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
461 for endian in ('', '>', '<'): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
462 for fmt in ('B', 'H', 'I', 'L', 'b', 'h', 'i', 'l'): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
463 self.check_float_coerce(endian + fmt, 1.0) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
464 self.check_float_coerce(endian + fmt, 1.5) |
|
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
|
465 |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
466 def test_issue4228(self): |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
467 # Packing a long may yield either 32 or 64 bits |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
468 x = struct.pack('L', -1)[:4] |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
469 self.assertEqual(x, '\xff'*4) |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
470 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
471 def test_unpack_from(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
472 test_string = 'abcd01234' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
473 fmt = '4s' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
474 s = struct.Struct(fmt) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
475 for cls in (str, buffer): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
476 data = cls(test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
477 self.assertEqual(s.unpack_from(data), ('abcd',)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
478 self.assertEqual(s.unpack_from(data, 2), ('cd01',)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
479 self.assertEqual(s.unpack_from(data, 4), ('0123',)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
480 for i in xrange(6): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
481 self.assertEqual(s.unpack_from(data, i), (data[i:i+4],)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
482 for i in xrange(6, len(test_string) + 1): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
483 self.assertRaises(struct.error, s.unpack_from, data, i) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
484 for cls in (str, buffer): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
485 data = cls(test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
486 self.assertEqual(struct.unpack_from(fmt, data), ('abcd',)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
487 self.assertEqual(struct.unpack_from(fmt, data, 2), ('cd01',)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
488 self.assertEqual(struct.unpack_from(fmt, data, 4), ('0123',)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
489 for i in xrange(6): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
490 self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
491 for i in xrange(6, len(test_string) + 1): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
492 self.assertRaises(struct.error, struct.unpack_from, fmt, data, i) |
|
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
|
493 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
494 def test_pack_into(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
495 test_string = 'Reykjavik rocks, eow!' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
496 writable_buf = array.array('c', ' '*100) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
497 fmt = '21s' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
498 s = struct.Struct(fmt) |
|
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
|
499 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
500 # Test without offset |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
501 s.pack_into(writable_buf, 0, test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
502 from_buf = writable_buf.tostring()[:len(test_string)] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
503 self.assertEqual(from_buf, test_string) |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
504 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
505 # Test with offset. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
506 s.pack_into(writable_buf, 10, test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
507 from_buf = writable_buf.tostring()[:len(test_string)+10] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
508 self.assertEqual(from_buf, test_string[:10] + test_string) |
|
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
|
509 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
510 # Go beyond boundaries. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
511 small_buf = array.array('c', ' '*10) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
512 self.assertRaises(struct.error, s.pack_into, small_buf, 0, test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
513 self.assertRaises(struct.error, s.pack_into, small_buf, 2, test_string) |
|
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
|
514 |
|
52909
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
515 # Test bogus offset (issue 3694) |
|
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
516 sb = small_buf |
|
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
517 self.assertRaises(TypeError, struct.pack_into, b'1', sb, None) |
|
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
518 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
519 def test_pack_into_fn(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
520 test_string = 'Reykjavik rocks, eow!' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
521 writable_buf = array.array('c', ' '*100) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
522 fmt = '21s' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
523 pack_into = lambda *args: struct.pack_into(fmt, *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
|
524 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
525 # Test without offset. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
526 pack_into(writable_buf, 0, test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
527 from_buf = writable_buf.tostring()[:len(test_string)] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
528 self.assertEqual(from_buf, test_string) |
|
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
|
529 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
530 # Test with offset. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
531 pack_into(writable_buf, 10, test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
532 from_buf = writable_buf.tostring()[:len(test_string)+10] |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
533 self.assertEqual(from_buf, test_string[:10] + test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
534 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
535 # Go beyond boundaries. |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
536 small_buf = array.array('c', ' '*10) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
537 self.assertRaises(struct.error, pack_into, small_buf, 0, test_string) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
538 self.assertRaises(struct.error, pack_into, small_buf, 2, test_string) |
|
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
|
539 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
540 def test_unpack_with_buffer(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
541 # SF bug 1563759: struct.unpack doens't support buffer protocol objects |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
542 data1 = array.array('B', '\x12\x34\x56\x78') |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
543 data2 = buffer('......\x12\x34\x56\x78......', 6, 4) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
544 for data in [data1, data2]: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
545 value, = struct.unpack('>I', data) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
546 self.assertEqual(value, 0x12345678) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
547 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
548 def test_bool(self): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
549 for prefix in tuple("<>!=")+('',): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
550 false = (), [], [], '', 0 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
551 true = [1], 'test', 5, -1, 0xffffffffL+1, 0xffffffff/2 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
552 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
553 falseFormat = prefix + '?' * len(false) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
554 packedFalse = struct.pack(falseFormat, *false) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
555 unpackedFalse = struct.unpack(falseFormat, packedFalse) |
|
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
|
556 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
557 trueFormat = prefix + '?' * len(true) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
558 packedTrue = struct.pack(trueFormat, *true) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
559 unpackedTrue = struct.unpack(trueFormat, packedTrue) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
560 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
561 self.assertEqual(len(true), len(unpackedTrue)) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
562 self.assertEqual(len(false), len(unpackedFalse)) |
|
18111
099ae4918069
Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
563 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
564 for t in unpackedFalse: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
565 self.assertFalse(t) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
566 for t in unpackedTrue: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
567 self.assertTrue(t) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
568 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
569 packed = struct.pack(prefix+'?', 1) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
570 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
571 self.assertEqual(len(packed), struct.calcsize(prefix+'?')) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
572 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
573 if len(packed) != 1: |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
574 self.assertFalse(prefix, msg='encoded bool is not one byte: %r' |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
575 %packed) |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
576 |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
577 for c in '\x01\x7f\xff\x0f\xf0': |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
578 self.assertTrue(struct.unpack('>?', c)[0]) |
|
19924
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
579 |
|
47721
9155ff78fd06
silence the test when it is skipped on some platforms. should fix a
Gregory P. Smith <greg@mad-scientist.com>
parents:
47587
diff
changeset
|
580 if IS32BIT: |
|
9155ff78fd06
silence the test when it is skipped on some platforms. should fix a
Gregory P. Smith <greg@mad-scientist.com>
parents:
47587
diff
changeset
|
581 def test_crasher(self): |
|
47587
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47575
diff
changeset
|
582 self.assertRaises(MemoryError, struct.pack, "357913941c", "a") |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47575
diff
changeset
|
583 |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47575
diff
changeset
|
584 |
|
19924
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18149
diff
changeset
|
585 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
586 def test_main(): |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
587 run_unittest(StructTest) |
|
38545
21e8dd3e3945
Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents:
38522
diff
changeset
|
588 |
|
47575
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
589 if __name__ == '__main__': |
|
da307beeebb9
convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents:
45759
diff
changeset
|
590 test_main() |
