annotate Lib/test/test_struct.py @ 49258:25f809123df6 legacy-trunk

Issue #3925: Ignores shutil.rmtree error on cygwin too. Reviewed by Benjamin Peterson.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Sun, 21 Sep 2008 20:48:41 +0000
parents ff96a313cd1c
children df3923c36ec5
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
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
5
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
6 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
7 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
8
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
9 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
10 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
11 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
12 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
13
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
14 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
15 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
16 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
17 PY_STRUCT_RANGE_CHECKING = 0
38515
bb0d2b955439 Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents: 38495
diff changeset
18 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
19 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
20 else:
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_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
22 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
23 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
24
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
25 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
26 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
27
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
28 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
29 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
30 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
31 else:
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 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
33
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
34 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
35 @wraps(func)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
36 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
37 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
38 # 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
39 # 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
40 warnings.simplefilter("always")
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
41 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
42 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
43 return decorator
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
44
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
45 @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
46 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
47 try:
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
48 func(*args)
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
49 except (struct.error, TypeError):
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
50 pass
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
51 except DeprecationWarning:
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
52 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
53 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
54 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
55 else:
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
56 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
57 func.__name__, args)
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
58
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
59
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
60 class StructTest(unittest.TestCase):
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
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 @with_warning_restore
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
63 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
64 # 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
65 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
66 # 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
67 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
68 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
69 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
70 "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
71 return
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
72 try:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
73 struct.pack(format, number)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
74 except (struct.error, TypeError):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
75 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
76 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
77 except DeprecationWarning:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
78 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
79 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
80 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
81 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
82
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
83 def test_isbigendian(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
84 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
85
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
86 def test_consistence(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
87 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
88
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
89 sz = struct.calcsize('i')
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
90 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
91
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
92 fmt = 'cbxxxxxxhhhhiillffd?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
93 fmt3 = '3c3b18x12h6i6l6f3d3?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
94 sz = struct.calcsize(fmt)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
95 sz3 = struct.calcsize(fmt3)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
96 self.assertEqual(sz * 3, sz3)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
97
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
107 def test_transitiveness(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
108 c = 'a'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
109 b = 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
110 h = 255
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
111 i = 65535
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
112 l = 65536
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
113 f = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
114 d = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
115 t = True
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
116
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
117 for prefix in ('', '@', '<', '>', '=', '!'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
118 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
119 format = prefix + format
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
120 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
121 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
122 self.assertEqual(cp, c)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
123 self.assertEqual(bp, b)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
124 self.assertEqual(hp, h)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
125 self.assertEqual(ip, i)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
126 self.assertEqual(lp, l)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
127 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
128 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
129 self.assertEqual(tp, t)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
130
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
131 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
132 # 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
133 # (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
134 tests = [
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
135 ('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
136 ('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
137 ('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
138 ('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
139 ('0s', 'helloworld', '', '', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
140 ('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
141 ('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
142 ('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
143 ('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
144 ('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
145 ('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
146 ('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
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', 249, '\371', '\371', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
149 ('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
150 ('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
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', 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
153 ('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
154 ('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
155 ('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
156 ('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
157 ('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
158 ('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
159 ('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
160 ('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
161 ('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
162 ('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
163 '\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
164 ('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
165 ('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
166 '\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
167 ('?', 0, '\0', '\0', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
168 ('?', 3, '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
169 ('?', True, '\1', '\1', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
170 ('?', [], '\0', '\0', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
171 ('?', (1,), '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
172 ]
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
173
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
174 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
175 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
176 ('='+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
177 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
178 self.assertEqual(res, exp)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
179 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
180 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
181 if rev != arg:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
182 self.assert_(asy)
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
183
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
184 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
185 # can't pack -1 as unsigned regardless
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
186 self.assertRaises((struct.error, TypeError), struct.pack, "Q", -1)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
187 # 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
188 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
189 # ditto, but 'Q'
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
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
192 try:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
193 struct.pack("q", 5)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
194 except struct.error:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
195 # 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
196 pass
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
197 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
198 bytes = struct.calcsize('q')
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
199 # 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
200 # 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
201 # 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
202 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
203 ('q', -1, '\xff' * bytes),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
204 ('q', 0, '\x00' * bytes),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
205 ('Q', 0, '\x00' * bytes),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
206 ('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
207 ('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
208 ('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
209 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
210 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
211 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
212 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
213 self.assertEqual(retrieved, input)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
214
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
215 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
216 # Standard integer tests (bBhHiIlLqQ).
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
217 import binascii
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
218
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
219 class IntTester(unittest.TestCase):
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 # 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
222 # 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
223 # 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
224 # reported by Mark Favas).
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
225 BUGGY_RANGE_CHECK = "bBhHiIlL"
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
226
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
227 def __init__(self, formatpair, bytesize):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
228 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
229 self.formatpair = formatpair
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
230 for direction in "<>!=":
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
231 for code in formatpair:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
232 format = direction + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
233 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
234 self.bytesize = bytesize
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
235 self.bitsize = bytesize * 8
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
236 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
237 self.unsigned_min = 0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
238 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
239 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
240 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
241
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
242 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
243 unpack=struct.unpack,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
244 unhexlify=binascii.unhexlify):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
245 # Try signed.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
246 code = self.signed_code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
247 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
248 # Try big-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
249 expected = long(x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
250 if x < 0:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
251 expected += 1L << self.bitsize
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
252 self.assert_(expected > 0)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
253 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
254 if len(expected) & 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
255 expected = "0" + expected
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
256 expected = unhexlify(expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
257 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
258
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
259 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
260 format = ">" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
261 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
262 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
263
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
264 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
265 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
266 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
267
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
268 # 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
269 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
270 '\x01' + got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
271 # Try little-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
272 format = "<" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
273 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
274
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
275 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
276 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
277 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
278
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
279 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
280 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
281 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
282
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
283 # 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
284 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
285 '\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
286
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
287 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
288 # 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
289 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
290 if verbose:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
291 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
292 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
293 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
294 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
295
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
296 # 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
297 code = self.unsigned_code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
298 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
299 # Try big-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
300 format = ">" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
301 expected = long(x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
302 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
303 if len(expected) & 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
304 expected = "0" + expected
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
305 expected = unhexlify(expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
306 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
307
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
308 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
309 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
310 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
311
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
312 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
313 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
314 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
315
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
316 # 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
317 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
318 '\x01' + got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
319
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
320 # Try little-endian.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
321 format = "<" + code
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
322 expected = string_reverse(expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
323
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
324 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
325 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
326 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
327
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
328 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
329 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
330 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
331
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
332 # 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
333 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
334 '\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
335
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
336 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
337 # 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
338 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
339 if verbose:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
340 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
341 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
342 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
343 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
344
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
345 def run(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
346 from random import randrange
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 # 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
349 values = []
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
350 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
351 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
352
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
353 # Add some random values.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
354 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
355 val = 0L
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
356 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
357 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
358 values.append(val)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
359
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
360 # 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
361 # 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
362 # 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
363 for base in values:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
364 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
365 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
366 x = val + incr
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
367 try:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
368 x = int(x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
369 except OverflowError:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
370 pass
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
371 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
372
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
373 # Some error cases.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
374 for direction in "<>":
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
375 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
376 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
377 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
378 struct.pack, direction + code,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
379 badobject)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
380
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
381 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
382 ("hH", 2),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
383 ("iI", 4),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
384 ("lL", 4),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
385 ("qQ", 8)]:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
386 t = IntTester(*args)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
387 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
388
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
389 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
390 # 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
391 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
392 ('p','abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
393 ('1p', 'abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
394 ('2p', 'abc', '\x01a', 'a'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
395 ('3p', 'abc', '\x02ab', 'ab'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
396 ('4p', 'abc', '\x03abc', 'abc'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
397 ('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
398 ('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
399 ('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
400 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
401 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
402 (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
403 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
404
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
405 def test_705836(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
406 # 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
407 # 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
408 # 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
409 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
410
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
411 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
412 # 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
413 delta = 0.5
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
414 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
415 delta /= 2.0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
416 smaller = base - delta
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
417 # 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
418 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
419 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
420 # 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
421 # 16, respectively.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
422 self.assertEqual(base, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
423 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
424 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
425 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
426 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
427
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
428 # Largest finite IEEE single.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
429 big = (1 << 24) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
430 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
431 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
432 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
433 self.assertEqual(big, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
434
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
435 # 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
436 big = (1 << 25) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
437 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
438 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
439
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
440 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
441 def test_1229380(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
442 # 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
443 # range integers
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
444 import sys
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
445 for endian in ('', '>', '<'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
446 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
447 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
448 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
449
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
450 deprecated_err(struct.pack, endian + 'B', cls(300))
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 + 'H', cls(70000))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
452
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 + 'I', sys.maxint * 4L)
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 + 'L', sys.maxint * 4L)
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 def XXXtest_1530559(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
457 # 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
458 # 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
459 for endian in ('', '>', '<'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
460 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
461 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
462 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
463
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
464 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
465 test_string = 'abcd01234'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
466 fmt = '4s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
467 s = struct.Struct(fmt)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
468 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
469 data = cls(test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
470 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
471 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
472 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
473 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
474 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
475 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
476 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
477 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
478 data = cls(test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
479 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
480 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
481 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
482 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
483 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
484 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
485 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
486
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
487 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
488 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
489 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
490 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
491 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
492
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
493 # Test without offset
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
494 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
495 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
496 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
497
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
498 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
499 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
500 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
501 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
502
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
503 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
504 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
505 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
506 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
507
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
508 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
509 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
510 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
511 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
512 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
513
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
514 # Test without offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
515 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
516 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
517 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
518
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
519 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
520 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
521 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
522 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
523
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
524 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
525 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
526 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
527 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
528
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
529 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
530 # 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
531 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
532 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
533 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
534 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
535 self.assertEqual(value, 0x12345678)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
536
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
537 def test_bool(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
538 for prefix in tuple("<>!=")+('',):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
539 false = (), [], [], '', 0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
540 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
541
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
542 falseFormat = prefix + '?' * len(false)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
543 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
544 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
545
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
546 trueFormat = prefix + '?' * len(true)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
547 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
548 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
549
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
550 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
551 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
552
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
553 for t in unpackedFalse:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
554 self.assertFalse(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
555 for t in unpackedTrue:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
556 self.assertTrue(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
557
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
558 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
559
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
560 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
561
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
562 if len(packed) != 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
563 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
564 %packed)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
565
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
566 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
567 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
568
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
569 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
570 def test_crasher(self):
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
571 self.assertRaises(MemoryError, struct.pack, "357913941c", "a")
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
572
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
573
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
574
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
575 def test_main():
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
576 run_unittest(StructTest)
38545
21e8dd3e3945 Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents: 38522
diff changeset
577
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
578 if __name__ == '__main__':
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
579 test_main()