annotate Lib/test/test_struct.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 28755eb53f38
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
1 import os
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
2 import array
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
3 import unittest
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 import struct
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
5 import inspect
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
6 from test.test_support import run_unittest, check_warnings, check_py3k_warnings
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
7
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
8 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
9 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
10 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
11
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
12 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
13
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
14 testmod_filename = os.path.splitext(__file__)[0] + '.py'
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
15 # 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
16 # long long type.
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
17 try:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
18 struct.pack('q', 5)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
19 except struct.error:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
20 HAVE_LONG_LONG = False
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
21 else:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
22 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
23
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
24 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
25 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
26
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
27 def 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
28 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
29 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
30 else:
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
31 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
32
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
33 class StructTest(unittest.TestCase):
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
34
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
35 def check_float_coerce(self, format, number):
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
36 # SF bug 1530559. struct.pack raises TypeError where it used
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
37 # to convert.
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
38 with check_warnings((".*integer argument expected, got float",
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
39 DeprecationWarning)) as w:
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
40 got = struct.pack(format, number)
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
41 lineno = inspect.currentframe().f_lineno - 1
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
42 self.assertEqual(w.filename, testmod_filename)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
43 self.assertEqual(w.lineno, lineno)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
44 self.assertEqual(len(w.warnings), 1)
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
45 expected = struct.pack(format, int(number))
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
46 self.assertEqual(got, expected)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
47
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
48 def test_isbigendian(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
49 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
50
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
51 def test_consistence(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
52 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
53
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
54 sz = struct.calcsize('i')
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
55 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
56
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
57 fmt = 'cbxxxxxxhhhhiillffd?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
58 fmt3 = '3c3b18x12h6i6l6f3d3?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
59 sz = struct.calcsize(fmt)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
60 sz3 = struct.calcsize(fmt3)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
61 self.assertEqual(sz * 3, sz3)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
62
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
63 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
64 self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3)
62897
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
65 self.assertRaises((TypeError, struct.error), struct.pack, 'i', 'foo')
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
66 self.assertRaises((TypeError, struct.error), struct.pack, 'P', 'foo')
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
67 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
68 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
69 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
70 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
71
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
72 def test_transitiveness(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
73 c = 'a'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
74 b = 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
75 h = 255
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
76 i = 65535
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
77 l = 65536
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
78 f = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
79 d = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
80 t = True
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
81
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
82 for prefix in ('', '@', '<', '>', '=', '!'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
83 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
84 format = prefix + format
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
85 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
86 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
87 self.assertEqual(cp, c)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
88 self.assertEqual(bp, b)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
89 self.assertEqual(hp, h)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
90 self.assertEqual(ip, i)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
91 self.assertEqual(lp, l)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
92 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
93 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
94 self.assertEqual(tp, t)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
95
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
96 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
97 # 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
98 # (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
99 tests = [
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
100 ('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
101 ('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
102 ('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
103 ('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
104 ('0s', 'helloworld', '', '', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
105 ('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
106 ('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
107 ('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
108 ('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
109 ('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
110 ('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
111 ('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
112 ('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
113 ('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
114 ('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
115 ('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
116 ('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
117 ('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
118 ('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
119 ('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
120 ('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
121 ('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
122 ('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
123 ('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
124 ('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
125 ('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
126 ('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
127 ('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
128 '\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
129 ('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
130 ('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
131 '\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
132 ('?', 0, '\0', '\0', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
133 ('?', 3, '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
134 ('?', True, '\1', '\1', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
135 ('?', [], '\0', '\0', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
136 ('?', (1,), '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
137 ]
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
138
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
139 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
140 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
141 ('='+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
142 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
143 self.assertEqual(res, exp)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
144 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
145 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
146 if rev != arg:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52540
diff changeset
147 self.assertTrue(asy)
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
148
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
149 def test_calcsize(self):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
150 expected_size = {
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
151 'b': 1, 'B': 1,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
152 'h': 2, 'H': 2,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
153 'i': 4, 'I': 4,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
154 'l': 4, 'L': 4,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
155 'q': 8, 'Q': 8,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
156 }
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
157
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
158 # standard integer sizes
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
159 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
160 for byteorder in ('=', '<', '>', '!'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
161 format = byteorder+code
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
162 size = struct.calcsize(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
163 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
164
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
165 # 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
166 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
167 for byteorder in ['', '@']:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
168 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
169 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
170 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
171
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
172 # bounds for native integer sizes
60303
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
173 self.assertEqual(struct.calcsize('b'), 1)
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
174 self.assertLessEqual(2, struct.calcsize('h'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
175 self.assertLessEqual(4, struct.calcsize('l'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
176 self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
177 self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l'))
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
178
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
179 # 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
180 if HAVE_LONG_LONG:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
181 self.assertEqual(struct.calcsize('q'), struct.calcsize('Q'))
60303
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
182 self.assertLessEqual(8, struct.calcsize('q'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
183 self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))
55150
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 def test_integers(self):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
186 # Integer tests (bBhHiIlLqQ).
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
187 import binascii
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
188
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
189 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
190 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
191 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
192 self.format = format
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
193 self.code = format[-1]
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
194 self.direction = format[:-1]
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
195 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
196 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
197 self.direction)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
198 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
199 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
200 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
201 self.signed = True
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
202 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
203 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
204 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
205 self.signed = False
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
206 self.min_value = 0
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
207 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
208 else:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
209 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
210 self.code)
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
211
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
212 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
213 unpack=struct.unpack,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
214 unhexlify=binascii.unhexlify):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
215
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
216 format = self.format
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
217 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
218 expected = long(x)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
219 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
220 expected += 1L << self.bitsize
60303
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
221 self.assertGreaterEqual(expected, 0)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
222 expected = '%x' % expected
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
223 if len(expected) & 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
224 expected = "0" + expected
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
225 expected = unhexlify(expected)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
226 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
227 expected)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
228 if (self.direction == '<' or
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
229 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
230 expected = string_reverse(expected)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
231 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
232
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
233 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
234 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
235 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
236
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
237 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
238 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
239 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
240
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
241 # 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
242 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
243 '\x01' + got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
244 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
245 # x is out of range -- verify pack realizes that.
62933
28755eb53f38 Merged revisions 82777 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62902
diff changeset
246 self.assertRaises((OverflowError, ValueError, struct.error),
28755eb53f38 Merged revisions 82777 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62902
diff changeset
247 pack, format, x)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
248
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
249 def run(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
250 from random import randrange
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
251
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
252 # 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
253 values = []
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
254 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
255 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
256
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
257 # Add some random values.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
258 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
259 val = 0L
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
260 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
261 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
262 values.append(val)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
263
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
264 # 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
265 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
266
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
267 # 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
268 # 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
269 # 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
270 # +-(2**n +- 1).
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
271 for base in values:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
272 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
273 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
274 x = val + incr
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
275 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
276 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
277
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
278 # Some error cases.
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
279 class NotAnIntNS(object):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
280 def __int__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
281 return 42
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
282
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
283 def __long__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
284 return 1729L
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
285
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
286 class NotAnIntOS:
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
287 def __int__(self):
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
288 return 85
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
289
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
290 def __long__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
291 return -163L
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
292
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
293 # Objects with an '__index__' method should be allowed
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
294 # to pack as integers. That is assuming the implemented
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
295 # '__index__' method returns and 'int' or 'long'.
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
296 class Indexable(object):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
297 def __init__(self, value):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
298 self._value = value
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
299
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
300 def __index__(self):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
301 return self._value
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
302
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
303 # If the '__index__' method raises a type error, then
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
304 # '__int__' should be used with a deprecation warning.
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
305 class BadIndex(object):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
306 def __index__(self):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
307 raise TypeError
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
309 def __int__(self):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
310 return 42
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
311
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
312 self.assertRaises((TypeError, struct.error),
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
313 struct.pack, self.format,
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
314 "a string")
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
315 self.assertRaises((TypeError, struct.error),
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
316 struct.pack, self.format,
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
317 randrange)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
318 with check_warnings(("integer argument expected, "
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
319 "got non-integer", DeprecationWarning)):
62902
d41a31f5bf82 this makes checking for warnings less error prone
Benjamin Peterson <benjamin@python.org>
parents: 62897
diff changeset
320 with self.assertRaises((TypeError, struct.error)):
d41a31f5bf82 this makes checking for warnings less error prone
Benjamin Peterson <benjamin@python.org>
parents: 62897
diff changeset
321 struct.pack(self.format, 3+42j)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
322
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
323 # an attempt to convert a non-integer (with an
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
324 # implicit conversion via __int__) should succeed,
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
325 # with a DeprecationWarning
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
326 for nonint in NotAnIntNS(), NotAnIntOS(), BadIndex():
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
327 with check_warnings((".*integer argument expected, got non"
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
328 "-integer", DeprecationWarning)) as w:
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
329 got = struct.pack(self.format, nonint)
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
330 lineno = inspect.currentframe().f_lineno - 1
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
331 self.assertEqual(w.filename, testmod_filename)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
332 self.assertEqual(w.lineno, lineno)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
333 self.assertEqual(len(w.warnings), 1)
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
334 expected = struct.pack(self.format, int(nonint))
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
335 self.assertEqual(got, expected)
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
336
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
337 # Check for legitimate values from '__index__'.
60238
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
338 for obj in (Indexable(0), Indexable(10), Indexable(17),
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
339 Indexable(42), Indexable(100), Indexable(127)):
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
340 try:
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
341 struct.pack(format, obj)
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
342 except:
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
343 self.fail("integer code pack failed on object "
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
344 "with '__index__' method")
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
345
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
346 # Check for bogus values from '__index__'.
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
347 for obj in (Indexable('a'), Indexable(u'b'), Indexable(None),
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
348 Indexable({'a': 1}), Indexable([1, 2, 3])):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
349 self.assertRaises((TypeError, struct.error),
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
350 struct.pack, self.format,
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
351 obj)
60238
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
352
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
353 byteorders = '', '@', '=', '<', '>', '!'
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
354 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
355 for byteorder in byteorders:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
356 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
357 not HAVE_LONG_LONG):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
358 continue
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
359 format = byteorder+code
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
360 t = IntTester(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
361 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
362
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
363 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
364 # 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
365 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
366 ('p','abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
367 ('1p', 'abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
368 ('2p', 'abc', '\x01a', 'a'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
369 ('3p', 'abc', '\x02ab', 'ab'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
370 ('4p', 'abc', '\x03abc', 'abc'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
371 ('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
372 ('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
373 ('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
374 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
375 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
376 (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
377 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
378
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
379 def test_705836(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
380 # 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
381 # 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
382 # 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
383 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
384
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
385 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
386 # 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
387 delta = 0.5
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
388 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
389 delta /= 2.0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
390 smaller = base - delta
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
391 # 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
392 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
393 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
394 # 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
395 # 16, respectively.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
396 self.assertEqual(base, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
397 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
398 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
399 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
400 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
401
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
402 # Largest finite IEEE single.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
403 big = (1 << 24) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
404 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
405 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
406 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
407 self.assertEqual(big, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
408
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
409 # 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
410 big = (1 << 25) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
411 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
412 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
413
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
414 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
415 # 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
416 for endian in ('', '>', '<'):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
417 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
418 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
419 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
420
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
421 def test_unpack_from(self, cls=str):
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
422 data = cls('abcd01234')
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
423 fmt = '4s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
424 s = struct.Struct(fmt)
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
425
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
426 self.assertEqual(s.unpack_from(data), ('abcd',))
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
427 self.assertEqual(struct.unpack_from(fmt, data), ('abcd',))
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
428 for i in xrange(6):
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
429 self.assertEqual(s.unpack_from(data, i), (data[i:i+4],))
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
430 self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
431 for i in xrange(6, len(data) + 1):
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
432 self.assertRaises(struct.error, s.unpack_from, data, i)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
433 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
434
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
435 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
436 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
437 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
438 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
439 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
440
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
441 # Test without offset
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
442 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
443 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
444 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
445
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
446 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
447 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
448 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
449 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
450
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
451 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
452 small_buf = array.array('c', ' '*10)
62857
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
453 self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
454 test_string)
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
455 self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
456 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
457
51597
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
458 # Test bogus offset (issue 3694)
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
diff changeset
459 sb = small_buf
62897
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
460 self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
461 None)
51597
bf419cfd7b0a #3694: add test for fix committed in r66693.
Georg Brandl <georg@python.org>
parents: 51517
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_pack_into_fn(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
464 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
465 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
466 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
467 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
468
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
469 # Test without offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
470 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
471 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
472 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
473
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
474 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
475 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
476 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
477 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
478
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
479 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
480 small_buf = array.array('c', ' '*10)
62857
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
481 self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
482 test_string)
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
483 self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
484 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
485
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
486 def test_unpack_with_buffer(self):
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
487 with check_py3k_warnings(("buffer.. not supported in 3.x",
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
488 DeprecationWarning)):
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
489 # SF bug 1563759: struct.unpack doesn't support buffer protocol objects
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
490 data1 = array.array('B', '\x12\x34\x56\x78')
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
491 data2 = buffer('......\x12\x34\x56\x78......', 6, 4)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
492 for data in [data1, data2]:
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
493 value, = struct.unpack('>I', data)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
494 self.assertEqual(value, 0x12345678)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
495
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
496 self.test_unpack_from(cls=buffer)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
497
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
498 def test_bool(self):
62848
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
499 class ExplodingBool(object):
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
500 def __nonzero__(self):
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
501 raise IOError
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
502 for prefix in tuple("<>!=")+('',):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
503 false = (), [], [], '', 0
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
504 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
505
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
506 falseFormat = prefix + '?' * len(false)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
507 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
508 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
509
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
510 trueFormat = prefix + '?' * len(true)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
511 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
512 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
513
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
514 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
515 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
516
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
517 for t in unpackedFalse:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
518 self.assertFalse(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
519 for t in unpackedTrue:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
520 self.assertTrue(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
521
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
522 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
523
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
524 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
525
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
526 if len(packed) != 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
527 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
528 %packed)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
529
62848
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
530 self.assertRaises(IOError, struct.pack, prefix + '?',
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
531 ExplodingBool())
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
532
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
533 for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
534 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
535
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
536 @unittest.skipUnless(IS32BIT, "Specific to 32bit machines")
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
537 def test_crasher(self):
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
538 self.assertRaises(MemoryError, struct.pack, "357913941c", "a")
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47575
diff changeset
539
62252
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
540 def test_count_overflow(self):
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
541 hugecount = '{}b'.format(sys.maxsize+1)
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
542 self.assertRaises(struct.error, struct.calcsize, hugecount)
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
543
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
544 hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
545 self.assertRaises(struct.error, struct.calcsize, hugecount2)
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
546
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
547 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
548 run_unittest(StructTest)
38545
21e8dd3e3945 Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents: 38522
diff changeset
549
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
550 if __name__ == '__main__':
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
551 test_main()