annotate Lib/test/test_struct.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 11d6e538a279
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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)
51517
20609fc13a13 Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError for
Mark Dickinson <dickinsm@gmail.com>
parents: 50424
diff changeset
51 except (struct.error, OverflowError):
39380
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
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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
51517
20609fc13a13 Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError for
Mark Dickinson <dickinsm@gmail.com>
parents: 50424
diff changeset
188 self.assertRaises((struct.error, OverflowError), struct.pack, "Q", -1)
47575
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):
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 51597
diff changeset
230 super(IntTester, self).__init__(methodName='test_one')
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
231 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
232 self.formatpair = formatpair
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
233 for direction in "<>!=":
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
234 for code in formatpair:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
235 format = direction + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
236 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
237 self.bytesize = bytesize
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
238 self.bitsize = bytesize * 8
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
239 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
240 self.unsigned_min = 0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
241 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
242 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
243 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
244
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
245 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
246 unpack=struct.unpack,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
247 unhexlify=binascii.unhexlify):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
248 # Try signed.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
249 code = self.signed_code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
250 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
251 # Try big-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
252 expected = long(x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
253 if x < 0:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
254 expected += 1L << self.bitsize
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
255 self.assert_(expected > 0)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
256 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
257 if len(expected) & 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
258 expected = "0" + expected
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
259 expected = unhexlify(expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
260 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
261
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
262 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
263 format = ">" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
264 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
265 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
266
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
267 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
268 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
269 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
270
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
271 # 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
272 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
273 '\x01' + got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
274 # Try little-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
275 format = "<" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
276 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
277
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
278 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
279 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
280 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
281
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
282 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
283 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
284 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
285
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
286 # 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
287 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
288 '\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
289
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
290 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
291 # 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
292 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
293 if verbose:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
294 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
295 else:
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)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
297 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
298
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
299 # 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
300 code = self.unsigned_code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
301 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
302 # Try big-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
303 format = ">" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
304 expected = long(x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
305 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
306 if len(expected) & 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
307 expected = "0" + expected
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
308 expected = unhexlify(expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
309 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
310
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
311 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
312 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
313 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
314
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
315 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
316 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
317 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
318
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
319 # 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
320 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
321 '\x01' + got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
322
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
323 # Try little-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
324 format = "<" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
325 expected = string_reverse(expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
326
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
327 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
328 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
329 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
330
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
331 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
332 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
333 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
334
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
335 # 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
336 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
337 '\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
338
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
339 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
340 # 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
341 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
342 if verbose:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
343 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
344 else:
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 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
347
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
348 def run(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
349 from random import randrange
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
350
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
351 # 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
352 values = []
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
353 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
354 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
355
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
356 # Add some random values.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
357 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
358 val = 0L
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
359 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
360 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
361 values.append(val)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
362
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
363 # 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
364 # 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
365 # 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
366 for base in values:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
367 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
368 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
369 x = val + incr
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
370 try:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
371 x = int(x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
372 except OverflowError:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
373 pass
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
374 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
375
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
376 # Some error cases.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
377 for direction in "<>":
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
378 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
379 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
380 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
381 struct.pack, direction + code,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
382 badobject)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
383
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
384 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
385 ("hH", 2),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
386 ("iI", 4),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
387 ("lL", 4),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
388 ("qQ", 8)]:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
389 t = IntTester(*args)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
390 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
391
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
392 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
393 # 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
394 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
395 ('p','abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
396 ('1p', 'abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
397 ('2p', 'abc', '\x01a', 'a'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
398 ('3p', 'abc', '\x02ab', 'ab'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
399 ('4p', 'abc', '\x03abc', 'abc'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
400 ('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
401 ('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
402 ('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
403 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
404 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
405 (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
406 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
407
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
408 def test_705836(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
409 # 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
410 # 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
411 # 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
412 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
413
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
414 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
415 # 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
416 delta = 0.5
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
417 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
418 delta /= 2.0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
419 smaller = base - delta
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
420 # 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
421 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
422 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
423 # 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
424 # 16, respectively.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
425 self.assertEqual(base, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
426 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
427 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
428 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
429 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
430
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
431 # Largest finite IEEE single.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
432 big = (1 << 24) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
433 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
434 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
435 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
436 self.assertEqual(big, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
437
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
438 # 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
439 big = (1 << 25) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
440 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
441 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
442
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
443 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
444 def test_1229380(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
445 # 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
446 # range integers
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
447 import sys
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
448 for endian in ('', '>', '<'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
449 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
450 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
451 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
452
47575
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 + 'B', cls(300))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
454 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
455
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 + 'I', sys.maxint * 4L)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
457 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
458
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
459 def XXXtest_1530559(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
460 # 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
461 # 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
462 for endian in ('', '>', '<'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
463 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
464 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
465 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
466
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
467 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
468 # 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
469 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
470 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
471
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
472 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
473 test_string = 'abcd01234'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
474 fmt = '4s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
475 s = struct.Struct(fmt)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
476 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
477 data = cls(test_string)
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), ('abcd',))
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, 2), ('cd01',))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
480 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
481 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
482 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
483 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
484 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
485 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
486 data = cls(test_string)
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), ('abcd',))
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, 2), ('cd01',))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
489 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
490 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
491 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
492 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
493 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
494
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
495 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
496 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
497 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
498 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
499 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
500
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
501 # Test without offset
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
502 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
503 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
504 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
505
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
506 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
507 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
508 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
509 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
510
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
511 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
512 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
513 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
514 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
515
51597
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
516 # Test bogus offset (issue 3694)
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
517 sb = small_buf
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
518 self.assertRaises(TypeError, struct.pack_into, b'1', sb, None)
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
519
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
520 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
521 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
522 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
523 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
524 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
525
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
526 # Test without offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
527 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
528 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
529 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
530
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
531 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
532 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
533 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
534 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
535
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
536 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
537 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
538 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
539 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
540
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
541 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
542 # 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
543 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
544 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
545 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
546 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
547 self.assertEqual(value, 0x12345678)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
548
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
549 def test_bool(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
550 for prefix in tuple("<>!=")+('',):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
551 false = (), [], [], '', 0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
552 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
553
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
554 falseFormat = prefix + '?' * len(false)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
555 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
556 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
557
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
558 trueFormat = prefix + '?' * len(true)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
559 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
560 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
561
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
562 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
563 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
564
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
565 for t in unpackedFalse:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
566 self.assertFalse(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
567 for t in unpackedTrue:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
568 self.assertTrue(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
569
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
570 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
571
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
572 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
573
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
574 if len(packed) != 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
575 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
576 %packed)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
577
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
578 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
579 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
580
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
581 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
582 def test_crasher(self):
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
583 self.assertRaises(MemoryError, struct.pack, "357913941c", "a")
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
584
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
585
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
586
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
587 def test_main():
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
588 run_unittest(StructTest)
38545
21e8dd3e3945 Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents: 38522
diff changeset
589
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
590 if __name__ == '__main__':
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
591 test_main()