annotate Lib/test/test_struct.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 0dd1aa33265f
children 70e115c34f97
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
1 import array
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
2 import unittest
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 import struct
38493
7d510e4f3c98 struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents: 38430
diff changeset
4 import warnings
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
5 warnings.filterwarnings("ignore", "struct integer overflow masking is deprecated",
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
6 DeprecationWarning)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
7
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
8 from functools import wraps
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 48834
diff changeset
9 from test.test_support import TestFailed, verbose, run_unittest
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
10
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
11 import sys
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
12 ISBIGENDIAN = sys.byteorder == "big"
48834
75f64fd1f113 Fix bug 3625: test issues on 64bit windows. r=pitrou
Mark Hammond <mhammond@skippinet.com.au>
parents: 47942
diff changeset
13 IS32BIT = sys.maxsize == 0x7fffffff
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
14
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
15 integer_codes = 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q'
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
16
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
17 # Native 'q' packing isn't available on systems that don't have the C
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
18 # long long type.
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
19 try:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
20 struct.pack('q', 5)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
21 except struct.error:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
22 HAVE_LONG_LONG = False
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
23 else:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
24 HAVE_LONG_LONG = True
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
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
26 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
27 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
28 except ImportError:
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
29 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
30 else:
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
31 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
32
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
33 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
34 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
35
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
36 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
37 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
38 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
39 else:
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
40 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
41
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
42 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
43 @wraps(func)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
44 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
45 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
46 # 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
47 # 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
48 warnings.simplefilter("always")
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
49 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
50 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
51 return decorator
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
52
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
53 class StructTest(unittest.TestCase):
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
54
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
55 @with_warning_restore
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
56 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
57 # 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
58 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
59 # 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
60 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
61 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
62 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
63 "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
64 return
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
65 try:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
66 struct.pack(format, number)
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
67 except struct.error:
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
68 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
69 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
70 except DeprecationWarning:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
71 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
72 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
73 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
74 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
75
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
76 def test_isbigendian(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
77 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
78
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
79 def test_consistence(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
80 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
81
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
82 sz = struct.calcsize('i')
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
83 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
84
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
85 fmt = 'cbxxxxxxhhhhiillffd?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
86 fmt3 = '3c3b18x12h6i6l6f3d3?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
87 sz = struct.calcsize(fmt)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
88 sz3 = struct.calcsize(fmt3)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
89 self.assertEqual(sz * 3, sz3)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
90
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
100 def test_transitiveness(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
101 c = 'a'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
102 b = 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
103 h = 255
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
104 i = 65535
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
105 l = 65536
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
106 f = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
107 d = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
108 t = True
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
109
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
110 for prefix in ('', '@', '<', '>', '=', '!'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
111 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
112 format = prefix + format
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
113 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
114 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
115 self.assertEqual(cp, c)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
116 self.assertEqual(bp, b)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
117 self.assertEqual(hp, h)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
118 self.assertEqual(ip, i)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
119 self.assertEqual(lp, l)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
120 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
121 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
122 self.assertEqual(tp, t)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
123
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
124 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
125 # 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
126 # (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
127 tests = [
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
128 ('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
129 ('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
130 ('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
131 ('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
132 ('0s', 'helloworld', '', '', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
133 ('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
134 ('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
135 ('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
136 ('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
137 ('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
138 ('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
139 ('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
140 ('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
141 ('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
142 ('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
143 ('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
144 ('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
145 ('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
146 ('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
147 ('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
148 ('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
149 ('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
150 ('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
151 ('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
152 ('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
153 ('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
154 ('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
155 ('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
156 '\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
157 ('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
158 ('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
159 '\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
160 ('?', 0, '\0', '\0', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
161 ('?', 3, '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
162 ('?', True, '\1', '\1', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
163 ('?', [], '\0', '\0', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
164 ('?', (1,), '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
165 ]
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
166
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
167 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
168 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
169 ('='+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
170 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
171 self.assertEqual(res, exp)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
172 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
173 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
174 if rev != arg:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52540
diff changeset
175 self.assertTrue(asy)
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
176
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
177 def test_calcsize(self):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
178 expected_size = {
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
179 'b': 1, 'B': 1,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
180 'h': 2, 'H': 2,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
181 'i': 4, 'I': 4,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
182 'l': 4, 'L': 4,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
183 'q': 8, 'Q': 8,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
184 }
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
185
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
186 # standard integer sizes
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
187 for code in integer_codes:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
188 for byteorder in ('=', '<', '>', '!'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
189 format = byteorder+code
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
190 size = struct.calcsize(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
191 self.assertEqual(size, expected_size[code])
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
192
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
193 # native integer sizes, except 'q' and 'Q'
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
194 for format_pair in ('bB', 'hH', 'iI', 'lL'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
195 for byteorder in ['', '@']:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
196 signed_size = struct.calcsize(byteorder + format_pair[0])
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
197 unsigned_size = struct.calcsize(byteorder + format_pair[1])
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
198 self.assertEqual(signed_size, unsigned_size)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
199
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
200 # bounds for native integer sizes
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
201 self.assertTrue(struct.calcsize('b')==1)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
202 self.assertTrue(2 <= struct.calcsize('h'))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
203 self.assertTrue(4 <= struct.calcsize('l'))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
204 self.assertTrue(struct.calcsize('h') <= struct.calcsize('i'))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
205 self.assertTrue(struct.calcsize('i') <= struct.calcsize('l'))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
206
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
207 # tests for native 'q' and 'Q' when applicable
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
208 if HAVE_LONG_LONG:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
209 self.assertEqual(struct.calcsize('q'), struct.calcsize('Q'))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
210 self.assertTrue(8 <= struct.calcsize('q'))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
211 self.assertTrue(struct.calcsize('l') <= struct.calcsize('q'))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
212
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
213 def test_integers(self):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
214 # Integer tests (bBhHiIlLqQ).
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
215 import binascii
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
216
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
217 class IntTester(unittest.TestCase):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
218 def __init__(self, format):
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 51597
diff changeset
219 super(IntTester, self).__init__(methodName='test_one')
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
220 self.format = format
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
221 self.code = format[-1]
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
222 self.direction = format[:-1]
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
223 if not self.direction in ('', '@', '=', '<', '>', '!'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
224 raise ValueError("unrecognized packing direction: %s" %
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
225 self.direction)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
226 self.bytesize = struct.calcsize(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
227 self.bitsize = self.bytesize * 8
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
228 if self.code in tuple('bhilq'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
229 self.signed = True
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
230 self.min_value = -(2L**(self.bitsize-1))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
231 self.max_value = 2L**(self.bitsize-1) - 1
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
232 elif self.code in tuple('BHILQ'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
233 self.signed = False
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
234 self.min_value = 0
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
235 self.max_value = 2L**self.bitsize - 1
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
236 else:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
237 raise ValueError("unrecognized format code: %s" %
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
238 self.code)
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
239
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
240 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
241 unpack=struct.unpack,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
242 unhexlify=binascii.unhexlify):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
243
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
244 format = self.format
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
245 if self.min_value <= x <= self.max_value:
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
246 expected = long(x)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
247 if self.signed and x < 0:
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
248 expected += 1L << self.bitsize
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
249 self.assertTrue(expected >= 0)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
250 expected = '%x' % expected
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
251 if len(expected) & 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
252 expected = "0" + expected
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
253 expected = unhexlify(expected)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
254 expected = ("\x00" * (self.bytesize - len(expected)) +
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
255 expected)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
256 if (self.direction == '<' or
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
257 self.direction in ('', '@', '=') and not ISBIGENDIAN):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
258 expected = string_reverse(expected)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
259 self.assertEqual(len(expected), self.bytesize)
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 15540
diff changeset
260
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
261 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
262 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
263 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
264
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
265 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
266 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
267 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
268
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
269 # 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
270 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
271 '\x01' + got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
272 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
273 # x is out of range -- verify pack realizes that.
55153
b3fb5e6b7e82 Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents: 55150
diff changeset
274 self.assertRaises(struct.error, pack, format, x)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
275
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
276 def run(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
277 from random import randrange
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 # 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
280 values = []
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
281 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
282 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
283
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
284 # Add some random values.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
285 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
286 val = 0L
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
287 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
288 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
289 values.append(val)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
290
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
291 # Values absorbed from other tests
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
292 values.extend([300, 700000, sys.maxint*4])
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
293
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
294 # Try all those, and their negations, and +-1 from
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
295 # them. Note that this tests all power-of-2
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
296 # boundaries in range, and a few out of range, plus
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
297 # +-(2**n +- 1).
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
298 for base in values:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
299 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
300 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
301 x = val + incr
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
302 self.test_one(int(x))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
303 self.test_one(long(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
304
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
305 # Some error cases.
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
306 class NotAnIntNS(object):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
307 def __int__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
308 return 42
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
309
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
310 def __long__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
311 return 1729L
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
312
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
313 class NotAnIntOS:
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
314 def __int__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
315 return 10585
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
316
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
317 def __long__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
318 return -163L
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
319
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
320 for badobject in ("a string", 3+42j, randrange,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
321 NotAnIntNS(), NotAnIntOS()):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
322 self.assertRaises(struct.error,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
323 struct.pack, format,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
324 badobject)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
325
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
326 byteorders = '', '@', '=', '<', '>', '!'
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
327 for code in integer_codes:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
328 for byteorder in byteorders:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
329 if (byteorder in ('', '@') and code in ('q', 'Q') and
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
330 not HAVE_LONG_LONG):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
331 continue
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
332 format = byteorder+code
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
333 t = IntTester(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
334 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
335
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
336 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
337 # 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
338 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
339 ('p','abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
340 ('1p', 'abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
341 ('2p', 'abc', '\x01a', 'a'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
342 ('3p', 'abc', '\x02ab', 'ab'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
343 ('4p', 'abc', '\x03abc', 'abc'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
344 ('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
345 ('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
346 ('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
347 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
348 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
349 (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
350 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
351
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
352 def test_705836(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
353 # 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
354 # 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
355 # 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
356 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
357
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
358 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
359 # 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
360 delta = 0.5
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
361 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
362 delta /= 2.0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
363 smaller = base - delta
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
364 # 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
365 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
366 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
367 # 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
368 # 16, respectively.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
369 self.assertEqual(base, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
370 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
371 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
372 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
373 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
374
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
375 # Largest finite IEEE single.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
376 big = (1 << 24) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
377 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
378 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
379 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
380 self.assertEqual(big, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
381
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
382 # 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
383 big = (1 << 25) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
384 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
385 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
386
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
387 def test_1530559(self):
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
388 # 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
389 for endian in ('', '>', '<'):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
390 for fmt in integer_codes:
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
391 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
392 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
393
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
394 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
395 test_string = 'abcd01234'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
396 fmt = '4s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
397 s = struct.Struct(fmt)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
398 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
399 data = cls(test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407 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
408 data = cls(test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
409 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
410 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
411 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
412 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
413 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
414 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
415 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
416
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
417 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
418 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
419 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
420 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
421 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
422
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
423 # Test without offset
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
424 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
425 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
426 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
427
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
428 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
429 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
430 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
431 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
432
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
433 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
434 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
435 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
436 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
437
51597
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
438 # Test bogus offset (issue 3694)
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
439 sb = small_buf
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
440 self.assertRaises(TypeError, struct.pack_into, b'1', sb, None)
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
441
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
442 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
443 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
444 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
445 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
446 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
447
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
448 # Test without offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
449 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
450 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
451 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
452
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
453 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
454 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
455 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
456 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
457
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
458 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
459 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
460 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
461 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
462
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
463 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
464 # 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
465 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
466 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
467 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
468 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
469 self.assertEqual(value, 0x12345678)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
470
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
471 def test_bool(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
472 for prefix in tuple("<>!=")+('',):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
473 false = (), [], [], '', 0
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
474 true = [1], 'test', 5, -1, 0xffffffffL+1, 0xffffffff/2
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
475
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
476 falseFormat = prefix + '?' * len(false)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
477 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
478 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
479
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
480 trueFormat = prefix + '?' * len(true)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
481 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
482 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
483
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
484 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
485 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
486
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
487 for t in unpackedFalse:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
488 self.assertFalse(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
489 for t in unpackedTrue:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
490 self.assertTrue(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
491
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
492 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
493
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
494 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
495
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
496 if len(packed) != 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
497 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
498 %packed)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
499
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
500 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
501 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
502
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
503 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
504 def test_crasher(self):
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
505 self.assertRaises(MemoryError, struct.pack, "357913941c", "a")
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
506
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
507
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
508
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
509 def test_main():
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
510 run_unittest(StructTest)
38545
21e8dd3e3945 Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents: 38522
diff changeset
511
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
512 if __name__ == '__main__':
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
513 test_main()