Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 1 | """Test the arraymodule. |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 2 | Roger E. Masse |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 3 | """ |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 4 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 5 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 7 | import weakref |
| 8 | import pickle |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 9 | import operator |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 10 | import io |
| 11 | import math |
| 12 | import struct |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 13 | import sys |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 14 | import warnings |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 15 | |
| 16 | import array |
| 17 | from array import _array_reconstructor as array_reconstructor |
| 18 | |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 19 | try: |
| 20 | # Try to determine availability of long long independently |
| 21 | # of the array module under test |
| 22 | struct.calcsize('@q') |
| 23 | have_long_long = True |
| 24 | except struct.error: |
| 25 | have_long_long = False |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 26 | |
Victor Stinner | 29c0034 | 2013-03-08 02:33:06 +0100 | [diff] [blame] | 27 | sizeof_wchar = array.array('u').itemsize |
Victor Stinner | c472c5d | 2013-02-26 22:52:11 +0100 | [diff] [blame] | 28 | |
| 29 | |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 30 | class ArraySubclass(array.array): |
| 31 | pass |
Guido van Rossum | c9f8f14 | 1997-04-09 20:51:54 +0000 | [diff] [blame] | 32 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 33 | class ArraySubclassWithKwargs(array.array): |
| 34 | def __init__(self, typecode, newarg=None): |
Victor Stinner | 7a6a009 | 2011-01-04 00:04:44 +0000 | [diff] [blame] | 35 | array.array.__init__(self) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 31f72d7 | 2007-06-18 18:44:28 +0000 | [diff] [blame] | 37 | typecodes = "ubBhHiIlLfd" |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 38 | if have_long_long: |
| 39 | typecodes += 'qQ' |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 40 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 41 | class BadConstructorTest(unittest.TestCase): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 42 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 43 | def test_constructor(self): |
| 44 | self.assertRaises(TypeError, array.array) |
| 45 | self.assertRaises(TypeError, array.array, spam=42) |
| 46 | self.assertRaises(TypeError, array.array, 'xx') |
| 47 | self.assertRaises(ValueError, array.array, 'x') |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 49 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 50 | # Machine format codes. |
| 51 | # |
| 52 | # Search for "enum machine_format_code" in Modules/arraymodule.c to get the |
| 53 | # authoritative values. |
| 54 | UNKNOWN_FORMAT = -1 |
| 55 | UNSIGNED_INT8 = 0 |
| 56 | SIGNED_INT8 = 1 |
| 57 | UNSIGNED_INT16_LE = 2 |
| 58 | UNSIGNED_INT16_BE = 3 |
| 59 | SIGNED_INT16_LE = 4 |
| 60 | SIGNED_INT16_BE = 5 |
| 61 | UNSIGNED_INT32_LE = 6 |
| 62 | UNSIGNED_INT32_BE = 7 |
| 63 | SIGNED_INT32_LE = 8 |
| 64 | SIGNED_INT32_BE = 9 |
| 65 | UNSIGNED_INT64_LE = 10 |
| 66 | UNSIGNED_INT64_BE = 11 |
| 67 | SIGNED_INT64_LE = 12 |
| 68 | SIGNED_INT64_BE = 13 |
| 69 | IEEE_754_FLOAT_LE = 14 |
| 70 | IEEE_754_FLOAT_BE = 15 |
| 71 | IEEE_754_DOUBLE_LE = 16 |
| 72 | IEEE_754_DOUBLE_BE = 17 |
| 73 | UTF16_LE = 18 |
| 74 | UTF16_BE = 19 |
| 75 | UTF32_LE = 20 |
| 76 | UTF32_BE = 21 |
| 77 | |
| 78 | class ArrayReconstructorTest(unittest.TestCase): |
| 79 | |
| 80 | def test_error(self): |
| 81 | self.assertRaises(TypeError, array_reconstructor, |
| 82 | "", "b", 0, b"") |
| 83 | self.assertRaises(TypeError, array_reconstructor, |
| 84 | str, "b", 0, b"") |
| 85 | self.assertRaises(TypeError, array_reconstructor, |
| 86 | array.array, "b", '', b"") |
| 87 | self.assertRaises(TypeError, array_reconstructor, |
| 88 | array.array, "b", 0, "") |
| 89 | self.assertRaises(ValueError, array_reconstructor, |
| 90 | array.array, "?", 0, b"") |
| 91 | self.assertRaises(ValueError, array_reconstructor, |
| 92 | array.array, "b", UNKNOWN_FORMAT, b"") |
| 93 | self.assertRaises(ValueError, array_reconstructor, |
| 94 | array.array, "b", 22, b"") |
| 95 | self.assertRaises(ValueError, array_reconstructor, |
| 96 | array.array, "d", 16, b"a") |
| 97 | |
| 98 | def test_numbers(self): |
| 99 | testcases = ( |
| 100 | (['B', 'H', 'I', 'L'], UNSIGNED_INT8, '=BBBB', |
| 101 | [0x80, 0x7f, 0, 0xff]), |
| 102 | (['b', 'h', 'i', 'l'], SIGNED_INT8, '=bbb', |
| 103 | [-0x80, 0x7f, 0]), |
| 104 | (['H', 'I', 'L'], UNSIGNED_INT16_LE, '<HHHH', |
| 105 | [0x8000, 0x7fff, 0, 0xffff]), |
| 106 | (['H', 'I', 'L'], UNSIGNED_INT16_BE, '>HHHH', |
| 107 | [0x8000, 0x7fff, 0, 0xffff]), |
| 108 | (['h', 'i', 'l'], SIGNED_INT16_LE, '<hhh', |
| 109 | [-0x8000, 0x7fff, 0]), |
| 110 | (['h', 'i', 'l'], SIGNED_INT16_BE, '>hhh', |
| 111 | [-0x8000, 0x7fff, 0]), |
| 112 | (['I', 'L'], UNSIGNED_INT32_LE, '<IIII', |
| 113 | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 114 | (['I', 'L'], UNSIGNED_INT32_BE, '>IIII', |
| 115 | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 116 | (['i', 'l'], SIGNED_INT32_LE, '<iii', |
| 117 | [-1<<31, (1<<31)-1, 0]), |
| 118 | (['i', 'l'], SIGNED_INT32_BE, '>iii', |
| 119 | [-1<<31, (1<<31)-1, 0]), |
| 120 | (['L'], UNSIGNED_INT64_LE, '<QQQQ', |
Alexandre Vassalotti | 0027d4b | 2009-07-15 20:53:55 +0000 | [diff] [blame] | 121 | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 122 | (['L'], UNSIGNED_INT64_BE, '>QQQQ', |
| 123 | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 124 | (['l'], SIGNED_INT64_LE, '<qqq', |
| 125 | [-1<<31, (1<<31)-1, 0]), |
| 126 | (['l'], SIGNED_INT64_BE, '>qqq', |
| 127 | [-1<<31, (1<<31)-1, 0]), |
| 128 | # The following tests for INT64 will raise an OverflowError |
| 129 | # when run on a 32-bit machine. The tests are simply skipped |
| 130 | # in that case. |
| 131 | (['L'], UNSIGNED_INT64_LE, '<QQQQ', |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 132 | [1<<63, (1<<63)-1, 0, (1<<64)-1]), |
| 133 | (['L'], UNSIGNED_INT64_BE, '>QQQQ', |
| 134 | [1<<63, (1<<63)-1, 0, (1<<64)-1]), |
| 135 | (['l'], SIGNED_INT64_LE, '<qqq', |
| 136 | [-1<<63, (1<<63)-1, 0]), |
| 137 | (['l'], SIGNED_INT64_BE, '>qqq', |
| 138 | [-1<<63, (1<<63)-1, 0]), |
| 139 | (['f'], IEEE_754_FLOAT_LE, '<ffff', |
| 140 | [16711938.0, float('inf'), float('-inf'), -0.0]), |
| 141 | (['f'], IEEE_754_FLOAT_BE, '>ffff', |
| 142 | [16711938.0, float('inf'), float('-inf'), -0.0]), |
| 143 | (['d'], IEEE_754_DOUBLE_LE, '<dddd', |
| 144 | [9006104071832581.0, float('inf'), float('-inf'), -0.0]), |
| 145 | (['d'], IEEE_754_DOUBLE_BE, '>dddd', |
| 146 | [9006104071832581.0, float('inf'), float('-inf'), -0.0]) |
| 147 | ) |
| 148 | for testcase in testcases: |
| 149 | valid_typecodes, mformat_code, struct_fmt, values = testcase |
| 150 | arraystr = struct.pack(struct_fmt, *values) |
| 151 | for typecode in valid_typecodes: |
Alexandre Vassalotti | 0027d4b | 2009-07-15 20:53:55 +0000 | [diff] [blame] | 152 | try: |
| 153 | a = array.array(typecode, values) |
| 154 | except OverflowError: |
| 155 | continue # Skip this test case. |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 156 | b = array_reconstructor( |
| 157 | array.array, typecode, mformat_code, arraystr) |
| 158 | self.assertEqual(a, b, |
| 159 | msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) |
| 160 | |
| 161 | def test_unicode(self): |
| 162 | teststr = "Bonne Journ\xe9e \U0002030a\U00020347" |
| 163 | testcases = ( |
| 164 | (UTF16_LE, "UTF-16-LE"), |
| 165 | (UTF16_BE, "UTF-16-BE"), |
| 166 | (UTF32_LE, "UTF-32-LE"), |
| 167 | (UTF32_BE, "UTF-32-BE") |
| 168 | ) |
| 169 | for testcase in testcases: |
| 170 | mformat_code, encoding = testcase |
| 171 | a = array.array('u', teststr) |
| 172 | b = array_reconstructor( |
| 173 | array.array, 'u', mformat_code, teststr.encode(encoding)) |
| 174 | self.assertEqual(a, b, |
| 175 | msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) |
| 176 | |
| 177 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 178 | class BaseTest: |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 179 | # Required class attributes (provided by subclasses |
| 180 | # typecode: the typecode to test |
| 181 | # example: an initializer usable in the constructor for this type |
| 182 | # smallerexample: the same length as example, but smaller |
| 183 | # biggerexample: the same length as example, but bigger |
| 184 | # outside: An entry that is not in example |
| 185 | # minitemsize: the minimum guaranteed itemsize |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 186 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 187 | def assertEntryEqual(self, entry1, entry2): |
| 188 | self.assertEqual(entry1, entry2) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 189 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 190 | def badtypecode(self): |
| 191 | # Return a typecode that is different from our own |
| 192 | return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 193 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 194 | def test_constructor(self): |
| 195 | a = array.array(self.typecode) |
| 196 | self.assertEqual(a.typecode, self.typecode) |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 197 | self.assertGreaterEqual(a.itemsize, self.minitemsize) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 198 | self.assertRaises(TypeError, array.array, self.typecode, None) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 199 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 200 | def test_len(self): |
| 201 | a = array.array(self.typecode) |
| 202 | a.append(self.example[0]) |
| 203 | self.assertEqual(len(a), 1) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 204 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 205 | a = array.array(self.typecode, self.example) |
| 206 | self.assertEqual(len(a), len(self.example)) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 207 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 208 | def test_buffer_info(self): |
| 209 | a = array.array(self.typecode, self.example) |
| 210 | self.assertRaises(TypeError, a.buffer_info, 42) |
| 211 | bi = a.buffer_info() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 212 | self.assertIsInstance(bi, tuple) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 213 | self.assertEqual(len(bi), 2) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 214 | self.assertIsInstance(bi[0], int) |
| 215 | self.assertIsInstance(bi[1], int) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 216 | self.assertEqual(bi[1], len(a)) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 217 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 218 | def test_byteswap(self): |
Victor Stinner | 8dba4e0 | 2011-09-30 00:51:10 +0200 | [diff] [blame] | 219 | if self.typecode == 'u': |
| 220 | example = '\U00100100' |
| 221 | else: |
| 222 | example = self.example |
| 223 | a = array.array(self.typecode, example) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 224 | self.assertRaises(TypeError, a.byteswap, 42) |
| 225 | if a.itemsize in (1, 2, 4, 8): |
Victor Stinner | 8dba4e0 | 2011-09-30 00:51:10 +0200 | [diff] [blame] | 226 | b = array.array(self.typecode, example) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 227 | b.byteswap() |
| 228 | if a.itemsize==1: |
| 229 | self.assertEqual(a, b) |
| 230 | else: |
| 231 | self.assertNotEqual(a, b) |
| 232 | b.byteswap() |
| 233 | self.assertEqual(a, b) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 234 | |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 235 | def test_copy(self): |
| 236 | import copy |
| 237 | a = array.array(self.typecode, self.example) |
| 238 | b = copy.copy(a) |
| 239 | self.assertNotEqual(id(a), id(b)) |
| 240 | self.assertEqual(a, b) |
| 241 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 242 | def test_deepcopy(self): |
| 243 | import copy |
| 244 | a = array.array(self.typecode, self.example) |
| 245 | b = copy.deepcopy(a) |
| 246 | self.assertNotEqual(id(a), id(b)) |
| 247 | self.assertEqual(a, b) |
| 248 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 249 | def test_reduce_ex(self): |
| 250 | a = array.array(self.typecode, self.example) |
| 251 | for protocol in range(3): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 252 | self.assertIs(a.__reduce_ex__(protocol)[0], array.array) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 253 | for protocol in range(3, pickle.HIGHEST_PROTOCOL): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 254 | self.assertIs(a.__reduce_ex__(protocol)[0], array_reconstructor) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 255 | |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 256 | def test_pickle(self): |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 257 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 258 | a = array.array(self.typecode, self.example) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 259 | b = pickle.loads(pickle.dumps(a, protocol)) |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 260 | self.assertNotEqual(id(a), id(b)) |
| 261 | self.assertEqual(a, b) |
| 262 | |
| 263 | a = ArraySubclass(self.typecode, self.example) |
| 264 | a.x = 10 |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 265 | b = pickle.loads(pickle.dumps(a, protocol)) |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 266 | self.assertNotEqual(id(a), id(b)) |
| 267 | self.assertEqual(a, b) |
| 268 | self.assertEqual(a.x, b.x) |
| 269 | self.assertEqual(type(a), type(b)) |
| 270 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 271 | def test_pickle_for_empty_array(self): |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 272 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 273 | a = array.array(self.typecode) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 274 | b = pickle.loads(pickle.dumps(a, protocol)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 275 | self.assertNotEqual(id(a), id(b)) |
| 276 | self.assertEqual(a, b) |
| 277 | |
| 278 | a = ArraySubclass(self.typecode) |
| 279 | a.x = 10 |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 280 | b = pickle.loads(pickle.dumps(a, protocol)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 281 | self.assertNotEqual(id(a), id(b)) |
| 282 | self.assertEqual(a, b) |
| 283 | self.assertEqual(a.x, b.x) |
| 284 | self.assertEqual(type(a), type(b)) |
| 285 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 286 | def test_iterator_pickle(self): |
Serhiy Storchaka | aabafe7 | 2016-03-06 14:10:24 +0200 | [diff] [blame] | 287 | orig = array.array(self.typecode, self.example) |
| 288 | data = list(orig) |
| 289 | data2 = data[::-1] |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 290 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
Serhiy Storchaka | aabafe7 | 2016-03-06 14:10:24 +0200 | [diff] [blame] | 291 | # initial iterator |
| 292 | itorig = iter(orig) |
| 293 | d = pickle.dumps((itorig, orig), proto) |
| 294 | it, a = pickle.loads(d) |
| 295 | a.fromlist(data2) |
| 296 | self.assertEqual(type(it), type(itorig)) |
| 297 | self.assertEqual(list(it), data + data2) |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 298 | |
Serhiy Storchaka | aabafe7 | 2016-03-06 14:10:24 +0200 | [diff] [blame] | 299 | # running iterator |
| 300 | next(itorig) |
| 301 | d = pickle.dumps((itorig, orig), proto) |
| 302 | it, a = pickle.loads(d) |
| 303 | a.fromlist(data2) |
| 304 | self.assertEqual(type(it), type(itorig)) |
| 305 | self.assertEqual(list(it), data[1:] + data2) |
| 306 | |
| 307 | # empty iterator |
| 308 | for i in range(1, len(data)): |
| 309 | next(itorig) |
| 310 | d = pickle.dumps((itorig, orig), proto) |
| 311 | it, a = pickle.loads(d) |
| 312 | a.fromlist(data2) |
| 313 | self.assertEqual(type(it), type(itorig)) |
| 314 | self.assertEqual(list(it), data2) |
| 315 | |
| 316 | # exhausted iterator |
| 317 | self.assertRaises(StopIteration, next, itorig) |
| 318 | d = pickle.dumps((itorig, orig), proto) |
| 319 | it, a = pickle.loads(d) |
| 320 | a.fromlist(data2) |
| 321 | self.assertEqual(type(it), type(itorig)) |
| 322 | self.assertEqual(list(it), data2) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 323 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 324 | def test_insert(self): |
| 325 | a = array.array(self.typecode, self.example) |
| 326 | a.insert(0, self.example[0]) |
| 327 | self.assertEqual(len(a), 1+len(self.example)) |
| 328 | self.assertEqual(a[0], a[1]) |
| 329 | self.assertRaises(TypeError, a.insert) |
| 330 | self.assertRaises(TypeError, a.insert, None) |
Walter Dörwald | ba39d9c | 2003-05-18 01:56:25 +0000 | [diff] [blame] | 331 | self.assertRaises(TypeError, a.insert, 0, None) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 332 | |
Walter Dörwald | 9e46abe | 2003-05-18 03:15:10 +0000 | [diff] [blame] | 333 | a = array.array(self.typecode, self.example) |
| 334 | a.insert(-1, self.example[0]) |
| 335 | self.assertEqual( |
| 336 | a, |
| 337 | array.array( |
| 338 | self.typecode, |
| 339 | self.example[:-1] + self.example[:1] + self.example[-1:] |
| 340 | ) |
| 341 | ) |
| 342 | |
| 343 | a = array.array(self.typecode, self.example) |
| 344 | a.insert(-1000, self.example[0]) |
| 345 | self.assertEqual( |
| 346 | a, |
| 347 | array.array(self.typecode, self.example[:1] + self.example) |
| 348 | ) |
| 349 | |
| 350 | a = array.array(self.typecode, self.example) |
| 351 | a.insert(1000, self.example[0]) |
| 352 | self.assertEqual( |
| 353 | a, |
| 354 | array.array(self.typecode, self.example + self.example[:1]) |
| 355 | ) |
| 356 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 357 | def test_tofromfile(self): |
| 358 | a = array.array(self.typecode, 2*self.example) |
| 359 | self.assertRaises(TypeError, a.tofile) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 360 | support.unlink(support.TESTFN) |
| 361 | f = open(support.TESTFN, 'wb') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 362 | try: |
| 363 | a.tofile(f) |
| 364 | f.close() |
| 365 | b = array.array(self.typecode) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 366 | f = open(support.TESTFN, 'rb') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 367 | self.assertRaises(TypeError, b.fromfile) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 368 | b.fromfile(f, len(self.example)) |
| 369 | self.assertEqual(b, array.array(self.typecode, self.example)) |
| 370 | self.assertNotEqual(a, b) |
Hirokazu Yamamoto | 54d0df6 | 2009-03-06 03:04:07 +0000 | [diff] [blame] | 371 | self.assertRaises(EOFError, b.fromfile, f, len(self.example)+1) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 372 | self.assertEqual(a, b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 373 | f.close() |
| 374 | finally: |
| 375 | if not f.closed: |
| 376 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 377 | support.unlink(support.TESTFN) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 378 | |
Antoine Pitrou | 8cb6dbf | 2010-07-21 16:41:31 +0000 | [diff] [blame] | 379 | def test_fromfile_ioerror(self): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 380 | # Issue #5395: Check if fromfile raises a proper OSError |
Antoine Pitrou | 8cb6dbf | 2010-07-21 16:41:31 +0000 | [diff] [blame] | 381 | # instead of EOFError. |
| 382 | a = array.array(self.typecode) |
| 383 | f = open(support.TESTFN, 'wb') |
| 384 | try: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 385 | self.assertRaises(OSError, a.fromfile, f, len(self.example)) |
Antoine Pitrou | 8cb6dbf | 2010-07-21 16:41:31 +0000 | [diff] [blame] | 386 | finally: |
| 387 | f.close() |
| 388 | support.unlink(support.TESTFN) |
| 389 | |
Frank Wierzbicki | 1768343 | 2009-08-16 20:30:12 +0000 | [diff] [blame] | 390 | def test_filewrite(self): |
| 391 | a = array.array(self.typecode, 2*self.example) |
| 392 | f = open(support.TESTFN, 'wb') |
| 393 | try: |
| 394 | f.write(a) |
| 395 | f.close() |
| 396 | b = array.array(self.typecode) |
| 397 | f = open(support.TESTFN, 'rb') |
| 398 | b.fromfile(f, len(self.example)) |
| 399 | self.assertEqual(b, array.array(self.typecode, self.example)) |
| 400 | self.assertNotEqual(a, b) |
| 401 | b.fromfile(f, len(self.example)) |
| 402 | self.assertEqual(a, b) |
| 403 | f.close() |
| 404 | finally: |
| 405 | if not f.closed: |
| 406 | f.close() |
| 407 | support.unlink(support.TESTFN) |
| 408 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 409 | def test_tofromlist(self): |
| 410 | a = array.array(self.typecode, 2*self.example) |
| 411 | b = array.array(self.typecode) |
| 412 | self.assertRaises(TypeError, a.tolist, 42) |
| 413 | self.assertRaises(TypeError, b.fromlist) |
| 414 | self.assertRaises(TypeError, b.fromlist, 42) |
| 415 | self.assertRaises(TypeError, b.fromlist, [None]) |
| 416 | b.fromlist(a.tolist()) |
| 417 | self.assertEqual(a, b) |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 418 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 419 | def test_tofromstring(self): |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 420 | # Warnings not raised when arguments are incorrect as Argument Clinic |
| 421 | # handles that before the warning can be raised. |
| 422 | nb_warnings = 2 |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 423 | with warnings.catch_warnings(record=True) as r: |
| 424 | warnings.filterwarnings("always", |
| 425 | message=r"(to|from)string\(\) is deprecated", |
| 426 | category=DeprecationWarning) |
| 427 | a = array.array(self.typecode, 2*self.example) |
| 428 | b = array.array(self.typecode) |
| 429 | self.assertRaises(TypeError, a.tostring, 42) |
| 430 | self.assertRaises(TypeError, b.fromstring) |
| 431 | self.assertRaises(TypeError, b.fromstring, 42) |
| 432 | b.fromstring(a.tostring()) |
| 433 | self.assertEqual(a, b) |
| 434 | if a.itemsize>1: |
| 435 | self.assertRaises(ValueError, b.fromstring, "x") |
| 436 | nb_warnings += 1 |
| 437 | self.assertEqual(len(r), nb_warnings) |
| 438 | |
| 439 | def test_tofrombytes(self): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 440 | a = array.array(self.typecode, 2*self.example) |
| 441 | b = array.array(self.typecode) |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 442 | self.assertRaises(TypeError, a.tobytes, 42) |
| 443 | self.assertRaises(TypeError, b.frombytes) |
| 444 | self.assertRaises(TypeError, b.frombytes, 42) |
| 445 | b.frombytes(a.tobytes()) |
| 446 | c = array.array(self.typecode, bytearray(a.tobytes())) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 447 | self.assertEqual(a, b) |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 448 | self.assertEqual(a, c) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 449 | if a.itemsize>1: |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 450 | self.assertRaises(ValueError, b.frombytes, b"x") |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 451 | |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 452 | def test_fromarray(self): |
| 453 | a = array.array(self.typecode, self.example) |
| 454 | b = array.array(self.typecode, a) |
| 455 | self.assertEqual(a, b) |
| 456 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 457 | def test_repr(self): |
| 458 | a = array.array(self.typecode, 2*self.example) |
| 459 | self.assertEqual(a, eval(repr(a), {"array": array.array})) |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 460 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 461 | a = array.array(self.typecode) |
| 462 | self.assertEqual(repr(a), "array('%s')" % self.typecode) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 463 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 464 | def test_str(self): |
| 465 | a = array.array(self.typecode, 2*self.example) |
| 466 | str(a) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 467 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 468 | def test_cmp(self): |
| 469 | a = array.array(self.typecode, self.example) |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 470 | self.assertIs(a == 42, False) |
| 471 | self.assertIs(a != 42, True) |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 472 | |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 473 | self.assertIs(a == a, True) |
| 474 | self.assertIs(a != a, False) |
| 475 | self.assertIs(a < a, False) |
| 476 | self.assertIs(a <= a, True) |
| 477 | self.assertIs(a > a, False) |
| 478 | self.assertIs(a >= a, True) |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 479 | |
Neal Norwitz | bd2835c | 2006-03-17 08:54:15 +0000 | [diff] [blame] | 480 | al = array.array(self.typecode, self.smallerexample) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 481 | ab = array.array(self.typecode, self.biggerexample) |
Guido van Rossum | 7f1d3aa | 1998-07-16 15:31:43 +0000 | [diff] [blame] | 482 | |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 483 | self.assertIs(a == 2*a, False) |
| 484 | self.assertIs(a != 2*a, True) |
| 485 | self.assertIs(a < 2*a, True) |
| 486 | self.assertIs(a <= 2*a, True) |
| 487 | self.assertIs(a > 2*a, False) |
| 488 | self.assertIs(a >= 2*a, False) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 489 | |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 490 | self.assertIs(a == al, False) |
| 491 | self.assertIs(a != al, True) |
| 492 | self.assertIs(a < al, False) |
| 493 | self.assertIs(a <= al, False) |
| 494 | self.assertIs(a > al, True) |
| 495 | self.assertIs(a >= al, True) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 496 | |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 497 | self.assertIs(a == ab, False) |
| 498 | self.assertIs(a != ab, True) |
| 499 | self.assertIs(a < ab, True) |
| 500 | self.assertIs(a <= ab, True) |
| 501 | self.assertIs(a > ab, False) |
| 502 | self.assertIs(a >= ab, False) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 503 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 504 | def test_add(self): |
| 505 | a = array.array(self.typecode, self.example) \ |
| 506 | + array.array(self.typecode, self.example[::-1]) |
| 507 | self.assertEqual( |
| 508 | a, |
| 509 | array.array(self.typecode, self.example + self.example[::-1]) |
| 510 | ) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 511 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 512 | b = array.array(self.badtypecode()) |
| 513 | self.assertRaises(TypeError, a.__add__, b) |
| 514 | |
| 515 | self.assertRaises(TypeError, a.__add__, "bad") |
| 516 | |
| 517 | def test_iadd(self): |
| 518 | a = array.array(self.typecode, self.example[::-1]) |
| 519 | b = a |
| 520 | a += array.array(self.typecode, 2*self.example) |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 521 | self.assertIs(a, b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 522 | self.assertEqual( |
| 523 | a, |
| 524 | array.array(self.typecode, self.example[::-1]+2*self.example) |
| 525 | ) |
Alexandre Vassalotti | e503cf9 | 2009-07-05 06:25:14 +0000 | [diff] [blame] | 526 | a = array.array(self.typecode, self.example) |
| 527 | a += a |
| 528 | self.assertEqual( |
| 529 | a, |
| 530 | array.array(self.typecode, self.example + self.example) |
| 531 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 532 | |
| 533 | b = array.array(self.badtypecode()) |
| 534 | self.assertRaises(TypeError, a.__add__, b) |
| 535 | |
| 536 | self.assertRaises(TypeError, a.__iadd__, "bad") |
| 537 | |
| 538 | def test_mul(self): |
| 539 | a = 5*array.array(self.typecode, self.example) |
| 540 | self.assertEqual( |
| 541 | a, |
| 542 | array.array(self.typecode, 5*self.example) |
| 543 | ) |
| 544 | |
| 545 | a = array.array(self.typecode, self.example)*5 |
| 546 | self.assertEqual( |
| 547 | a, |
| 548 | array.array(self.typecode, self.example*5) |
| 549 | ) |
| 550 | |
| 551 | a = 0*array.array(self.typecode, self.example) |
| 552 | self.assertEqual( |
| 553 | a, |
| 554 | array.array(self.typecode) |
| 555 | ) |
| 556 | |
| 557 | a = (-1)*array.array(self.typecode, self.example) |
| 558 | self.assertEqual( |
| 559 | a, |
| 560 | array.array(self.typecode) |
| 561 | ) |
| 562 | |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 563 | a = 5 * array.array(self.typecode, self.example[:1]) |
| 564 | self.assertEqual( |
| 565 | a, |
| 566 | array.array(self.typecode, [a[0]] * 5) |
| 567 | ) |
| 568 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 569 | self.assertRaises(TypeError, a.__mul__, "bad") |
| 570 | |
| 571 | def test_imul(self): |
| 572 | a = array.array(self.typecode, self.example) |
| 573 | b = a |
| 574 | |
| 575 | a *= 5 |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 576 | self.assertIs(a, b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 577 | self.assertEqual( |
| 578 | a, |
| 579 | array.array(self.typecode, 5*self.example) |
| 580 | ) |
| 581 | |
| 582 | a *= 0 |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 583 | self.assertIs(a, b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 584 | self.assertEqual(a, array.array(self.typecode)) |
| 585 | |
| 586 | a *= 1000 |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 587 | self.assertIs(a, b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 588 | self.assertEqual(a, array.array(self.typecode)) |
| 589 | |
| 590 | a *= -1 |
Serhiy Storchaka | 91b3e77 | 2013-11-17 00:45:17 +0200 | [diff] [blame] | 591 | self.assertIs(a, b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 592 | self.assertEqual(a, array.array(self.typecode)) |
| 593 | |
| 594 | a = array.array(self.typecode, self.example) |
| 595 | a *= -1 |
| 596 | self.assertEqual(a, array.array(self.typecode)) |
| 597 | |
| 598 | self.assertRaises(TypeError, a.__imul__, "bad") |
| 599 | |
| 600 | def test_getitem(self): |
| 601 | a = array.array(self.typecode, self.example) |
| 602 | self.assertEntryEqual(a[0], self.example[0]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 603 | self.assertEntryEqual(a[0], self.example[0]) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 604 | self.assertEntryEqual(a[-1], self.example[-1]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 605 | self.assertEntryEqual(a[-1], self.example[-1]) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 606 | self.assertEntryEqual(a[len(self.example)-1], self.example[-1]) |
| 607 | self.assertEntryEqual(a[-len(self.example)], self.example[0]) |
| 608 | self.assertRaises(TypeError, a.__getitem__) |
| 609 | self.assertRaises(IndexError, a.__getitem__, len(self.example)) |
| 610 | self.assertRaises(IndexError, a.__getitem__, -len(self.example)-1) |
| 611 | |
| 612 | def test_setitem(self): |
| 613 | a = array.array(self.typecode, self.example) |
| 614 | a[0] = a[-1] |
| 615 | self.assertEntryEqual(a[0], a[-1]) |
| 616 | |
| 617 | a = array.array(self.typecode, self.example) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 618 | a[0] = a[-1] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 619 | self.assertEntryEqual(a[0], a[-1]) |
| 620 | |
| 621 | a = array.array(self.typecode, self.example) |
| 622 | a[-1] = a[0] |
| 623 | self.assertEntryEqual(a[0], a[-1]) |
| 624 | |
| 625 | a = array.array(self.typecode, self.example) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 626 | a[-1] = a[0] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 627 | self.assertEntryEqual(a[0], a[-1]) |
| 628 | |
| 629 | a = array.array(self.typecode, self.example) |
| 630 | a[len(self.example)-1] = a[0] |
| 631 | self.assertEntryEqual(a[0], a[-1]) |
| 632 | |
| 633 | a = array.array(self.typecode, self.example) |
| 634 | a[-len(self.example)] = a[-1] |
| 635 | self.assertEntryEqual(a[0], a[-1]) |
| 636 | |
| 637 | self.assertRaises(TypeError, a.__setitem__) |
| 638 | self.assertRaises(TypeError, a.__setitem__, None) |
| 639 | self.assertRaises(TypeError, a.__setitem__, 0, None) |
| 640 | self.assertRaises( |
| 641 | IndexError, |
| 642 | a.__setitem__, |
| 643 | len(self.example), self.example[0] |
| 644 | ) |
| 645 | self.assertRaises( |
| 646 | IndexError, |
| 647 | a.__setitem__, |
| 648 | -len(self.example)-1, self.example[0] |
| 649 | ) |
| 650 | |
| 651 | def test_delitem(self): |
| 652 | a = array.array(self.typecode, self.example) |
| 653 | del a[0] |
| 654 | self.assertEqual( |
| 655 | a, |
| 656 | array.array(self.typecode, self.example[1:]) |
| 657 | ) |
| 658 | |
| 659 | a = array.array(self.typecode, self.example) |
| 660 | del a[-1] |
| 661 | self.assertEqual( |
| 662 | a, |
| 663 | array.array(self.typecode, self.example[:-1]) |
| 664 | ) |
| 665 | |
| 666 | a = array.array(self.typecode, self.example) |
| 667 | del a[len(self.example)-1] |
| 668 | self.assertEqual( |
| 669 | a, |
| 670 | array.array(self.typecode, self.example[:-1]) |
| 671 | ) |
| 672 | |
| 673 | a = array.array(self.typecode, self.example) |
| 674 | del a[-len(self.example)] |
| 675 | self.assertEqual( |
| 676 | a, |
| 677 | array.array(self.typecode, self.example[1:]) |
| 678 | ) |
| 679 | |
| 680 | self.assertRaises(TypeError, a.__delitem__) |
| 681 | self.assertRaises(TypeError, a.__delitem__, None) |
| 682 | self.assertRaises(IndexError, a.__delitem__, len(self.example)) |
| 683 | self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1) |
| 684 | |
| 685 | def test_getslice(self): |
| 686 | a = array.array(self.typecode, self.example) |
| 687 | self.assertEqual(a[:], a) |
| 688 | |
| 689 | self.assertEqual( |
| 690 | a[1:], |
| 691 | array.array(self.typecode, self.example[1:]) |
| 692 | ) |
| 693 | |
| 694 | self.assertEqual( |
| 695 | a[:1], |
| 696 | array.array(self.typecode, self.example[:1]) |
| 697 | ) |
| 698 | |
| 699 | self.assertEqual( |
| 700 | a[:-1], |
| 701 | array.array(self.typecode, self.example[:-1]) |
| 702 | ) |
| 703 | |
| 704 | self.assertEqual( |
| 705 | a[-1:], |
| 706 | array.array(self.typecode, self.example[-1:]) |
| 707 | ) |
| 708 | |
| 709 | self.assertEqual( |
| 710 | a[-1:-1], |
| 711 | array.array(self.typecode) |
| 712 | ) |
| 713 | |
| 714 | self.assertEqual( |
Walter Dörwald | 1083c24 | 2005-01-21 21:16:51 +0000 | [diff] [blame] | 715 | a[2:1], |
| 716 | array.array(self.typecode) |
| 717 | ) |
| 718 | |
| 719 | self.assertEqual( |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 720 | a[1000:], |
| 721 | array.array(self.typecode) |
| 722 | ) |
| 723 | self.assertEqual(a[-1000:], a) |
| 724 | self.assertEqual(a[:1000], a) |
| 725 | self.assertEqual( |
| 726 | a[:-1000], |
| 727 | array.array(self.typecode) |
| 728 | ) |
| 729 | self.assertEqual(a[-1000:1000], a) |
| 730 | self.assertEqual( |
| 731 | a[2000:1000], |
| 732 | array.array(self.typecode) |
| 733 | ) |
| 734 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 735 | def test_extended_getslice(self): |
| 736 | # Test extended slicing by comparing with list slicing |
| 737 | # (Assumes list conversion works correctly, too) |
| 738 | a = array.array(self.typecode, self.example) |
| 739 | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
| 740 | for start in indices: |
| 741 | for stop in indices: |
| 742 | # Everything except the initial 0 (invalid step) |
| 743 | for step in indices[1:]: |
| 744 | self.assertEqual(list(a[start:stop:step]), |
| 745 | list(a)[start:stop:step]) |
| 746 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 747 | def test_setslice(self): |
| 748 | a = array.array(self.typecode, self.example) |
| 749 | a[:1] = a |
| 750 | self.assertEqual( |
| 751 | a, |
| 752 | array.array(self.typecode, self.example + self.example[1:]) |
| 753 | ) |
| 754 | |
| 755 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 756 | a[:-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 757 | self.assertEqual( |
| 758 | a, |
| 759 | array.array(self.typecode, self.example + self.example[-1:]) |
| 760 | ) |
| 761 | |
| 762 | a = array.array(self.typecode, self.example) |
| 763 | a[-1:] = a |
| 764 | self.assertEqual( |
| 765 | a, |
| 766 | array.array(self.typecode, self.example[:-1] + self.example) |
| 767 | ) |
| 768 | |
| 769 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 770 | a[1:] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 771 | self.assertEqual( |
| 772 | a, |
| 773 | array.array(self.typecode, self.example[:1] + self.example) |
| 774 | ) |
| 775 | |
| 776 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 777 | a[1:-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 778 | self.assertEqual( |
| 779 | a, |
| 780 | array.array( |
| 781 | self.typecode, |
| 782 | self.example[:1] + self.example + self.example[-1:] |
| 783 | ) |
| 784 | ) |
| 785 | |
| 786 | a = array.array(self.typecode, self.example) |
| 787 | a[1000:] = a |
| 788 | self.assertEqual( |
| 789 | a, |
| 790 | array.array(self.typecode, 2*self.example) |
| 791 | ) |
| 792 | |
| 793 | a = array.array(self.typecode, self.example) |
| 794 | a[-1000:] = a |
| 795 | self.assertEqual( |
| 796 | a, |
| 797 | array.array(self.typecode, self.example) |
| 798 | ) |
| 799 | |
| 800 | a = array.array(self.typecode, self.example) |
| 801 | a[:1000] = a |
| 802 | self.assertEqual( |
| 803 | a, |
| 804 | array.array(self.typecode, self.example) |
| 805 | ) |
| 806 | |
| 807 | a = array.array(self.typecode, self.example) |
| 808 | a[:-1000] = a |
| 809 | self.assertEqual( |
| 810 | a, |
| 811 | array.array(self.typecode, 2*self.example) |
| 812 | ) |
| 813 | |
| 814 | a = array.array(self.typecode, self.example) |
| 815 | a[1:0] = a |
| 816 | self.assertEqual( |
| 817 | a, |
| 818 | array.array(self.typecode, self.example[:1] + self.example + self.example[1:]) |
| 819 | ) |
| 820 | |
| 821 | a = array.array(self.typecode, self.example) |
| 822 | a[2000:1000] = a |
| 823 | self.assertEqual( |
| 824 | a, |
| 825 | array.array(self.typecode, 2*self.example) |
| 826 | ) |
| 827 | |
| 828 | a = array.array(self.typecode, self.example) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 829 | self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 830 | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None) |
| 831 | |
| 832 | b = array.array(self.badtypecode()) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 833 | self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 834 | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b) |
| 835 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 836 | def test_extended_set_del_slice(self): |
| 837 | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
| 838 | for start in indices: |
| 839 | for stop in indices: |
| 840 | # Everything except the initial 0 (invalid step) |
| 841 | for step in indices[1:]: |
| 842 | a = array.array(self.typecode, self.example) |
| 843 | L = list(a) |
| 844 | # Make sure we have a slice of exactly the right length, |
| 845 | # but with (hopefully) different data. |
| 846 | data = L[start:stop:step] |
| 847 | data.reverse() |
| 848 | L[start:stop:step] = data |
| 849 | a[start:stop:step] = array.array(self.typecode, data) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 850 | self.assertEqual(a, array.array(self.typecode, L)) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 851 | |
| 852 | del L[start:stop:step] |
| 853 | del a[start:stop:step] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 854 | self.assertEqual(a, array.array(self.typecode, L)) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 855 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 856 | def test_index(self): |
| 857 | example = 2*self.example |
| 858 | a = array.array(self.typecode, example) |
| 859 | self.assertRaises(TypeError, a.index) |
| 860 | for x in example: |
| 861 | self.assertEqual(a.index(x), example.index(x)) |
| 862 | self.assertRaises(ValueError, a.index, None) |
| 863 | self.assertRaises(ValueError, a.index, self.outside) |
| 864 | |
| 865 | def test_count(self): |
| 866 | example = 2*self.example |
| 867 | a = array.array(self.typecode, example) |
| 868 | self.assertRaises(TypeError, a.count) |
| 869 | for x in example: |
| 870 | self.assertEqual(a.count(x), example.count(x)) |
| 871 | self.assertEqual(a.count(self.outside), 0) |
| 872 | self.assertEqual(a.count(None), 0) |
| 873 | |
| 874 | def test_remove(self): |
| 875 | for x in self.example: |
| 876 | example = 2*self.example |
| 877 | a = array.array(self.typecode, example) |
| 878 | pos = example.index(x) |
| 879 | example2 = example[:pos] + example[pos+1:] |
| 880 | a.remove(x) |
| 881 | self.assertEqual(a, array.array(self.typecode, example2)) |
| 882 | |
| 883 | a = array.array(self.typecode, self.example) |
| 884 | self.assertRaises(ValueError, a.remove, self.outside) |
| 885 | |
| 886 | self.assertRaises(ValueError, a.remove, None) |
| 887 | |
| 888 | def test_pop(self): |
| 889 | a = array.array(self.typecode) |
| 890 | self.assertRaises(IndexError, a.pop) |
| 891 | |
| 892 | a = array.array(self.typecode, 2*self.example) |
| 893 | self.assertRaises(TypeError, a.pop, 42, 42) |
| 894 | self.assertRaises(TypeError, a.pop, None) |
| 895 | self.assertRaises(IndexError, a.pop, len(a)) |
| 896 | self.assertRaises(IndexError, a.pop, -len(a)-1) |
| 897 | |
| 898 | self.assertEntryEqual(a.pop(0), self.example[0]) |
| 899 | self.assertEqual( |
| 900 | a, |
| 901 | array.array(self.typecode, self.example[1:]+self.example) |
| 902 | ) |
| 903 | self.assertEntryEqual(a.pop(1), self.example[2]) |
| 904 | self.assertEqual( |
| 905 | a, |
| 906 | array.array(self.typecode, self.example[1:2]+self.example[3:]+self.example) |
| 907 | ) |
| 908 | self.assertEntryEqual(a.pop(0), self.example[1]) |
| 909 | self.assertEntryEqual(a.pop(), self.example[-1]) |
| 910 | self.assertEqual( |
| 911 | a, |
| 912 | array.array(self.typecode, self.example[3:]+self.example[:-1]) |
| 913 | ) |
| 914 | |
| 915 | def test_reverse(self): |
| 916 | a = array.array(self.typecode, self.example) |
| 917 | self.assertRaises(TypeError, a.reverse, 42) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 918 | a.reverse() |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 919 | self.assertEqual( |
| 920 | a, |
| 921 | array.array(self.typecode, self.example[::-1]) |
| 922 | ) |
| 923 | |
| 924 | def test_extend(self): |
| 925 | a = array.array(self.typecode, self.example) |
| 926 | self.assertRaises(TypeError, a.extend) |
| 927 | a.extend(array.array(self.typecode, self.example[::-1])) |
| 928 | self.assertEqual( |
| 929 | a, |
| 930 | array.array(self.typecode, self.example+self.example[::-1]) |
| 931 | ) |
| 932 | |
Alexandre Vassalotti | e503cf9 | 2009-07-05 06:25:14 +0000 | [diff] [blame] | 933 | a = array.array(self.typecode, self.example) |
| 934 | a.extend(a) |
| 935 | self.assertEqual( |
| 936 | a, |
| 937 | array.array(self.typecode, self.example+self.example) |
| 938 | ) |
| 939 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 940 | b = array.array(self.badtypecode()) |
| 941 | self.assertRaises(TypeError, a.extend, b) |
| 942 | |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 943 | a = array.array(self.typecode, self.example) |
| 944 | a.extend(self.example[::-1]) |
| 945 | self.assertEqual( |
| 946 | a, |
| 947 | array.array(self.typecode, self.example+self.example[::-1]) |
| 948 | ) |
| 949 | |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 950 | def test_constructor_with_iterable_argument(self): |
| 951 | a = array.array(self.typecode, iter(self.example)) |
| 952 | b = array.array(self.typecode, self.example) |
| 953 | self.assertEqual(a, b) |
| 954 | |
| 955 | # non-iterable argument |
| 956 | self.assertRaises(TypeError, array.array, self.typecode, 10) |
| 957 | |
| 958 | # pass through errors raised in __iter__ |
| 959 | class A: |
| 960 | def __iter__(self): |
| 961 | raise UnicodeError |
| 962 | self.assertRaises(UnicodeError, array.array, self.typecode, A()) |
| 963 | |
| 964 | # pass through errors raised in next() |
| 965 | def B(): |
| 966 | raise UnicodeError |
| 967 | yield None |
| 968 | self.assertRaises(UnicodeError, array.array, self.typecode, B()) |
| 969 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 970 | def test_coveritertraverse(self): |
| 971 | try: |
| 972 | import gc |
| 973 | except ImportError: |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 974 | self.skipTest('gc module not available') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 975 | a = array.array(self.typecode) |
| 976 | l = [iter(a)] |
| 977 | l.append(l) |
| 978 | gc.collect() |
| 979 | |
| 980 | def test_buffer(self): |
| 981 | a = array.array(self.typecode, self.example) |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 982 | m = memoryview(a) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 983 | expected = m.tobytes() |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 984 | self.assertEqual(a.tobytes(), expected) |
| 985 | self.assertEqual(a.tobytes()[0], expected[0]) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 986 | # Resizing is forbidden when there are buffer exports. |
| 987 | # For issue 4509, we also check after each error that |
| 988 | # the array was not modified. |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 989 | self.assertRaises(BufferError, a.append, a[0]) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 990 | self.assertEqual(m.tobytes(), expected) |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 991 | self.assertRaises(BufferError, a.extend, a[0:1]) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 992 | self.assertEqual(m.tobytes(), expected) |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 993 | self.assertRaises(BufferError, a.remove, a[0]) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 994 | self.assertEqual(m.tobytes(), expected) |
| 995 | self.assertRaises(BufferError, a.pop, 0) |
| 996 | self.assertEqual(m.tobytes(), expected) |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 997 | self.assertRaises(BufferError, a.fromlist, a.tolist()) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 998 | self.assertEqual(m.tobytes(), expected) |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 999 | self.assertRaises(BufferError, a.frombytes, a.tobytes()) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 1000 | self.assertEqual(m.tobytes(), expected) |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 1001 | if self.typecode == 'u': |
| 1002 | self.assertRaises(BufferError, a.fromunicode, a.tounicode()) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 1003 | self.assertEqual(m.tobytes(), expected) |
Raymond Hettinger | 3471b1c | 2009-01-27 11:06:40 +0000 | [diff] [blame] | 1004 | self.assertRaises(BufferError, operator.imul, a, 2) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 1005 | self.assertEqual(m.tobytes(), expected) |
Raymond Hettinger | 3471b1c | 2009-01-27 11:06:40 +0000 | [diff] [blame] | 1006 | self.assertRaises(BufferError, operator.imul, a, 0) |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 1007 | self.assertEqual(m.tobytes(), expected) |
| 1008 | self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a) |
| 1009 | self.assertEqual(m.tobytes(), expected) |
| 1010 | self.assertRaises(BufferError, operator.delitem, a, 0) |
| 1011 | self.assertEqual(m.tobytes(), expected) |
| 1012 | self.assertRaises(BufferError, operator.delitem, a, slice(0, 1)) |
| 1013 | self.assertEqual(m.tobytes(), expected) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1014 | |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 1015 | def test_weakref(self): |
| 1016 | s = array.array(self.typecode, self.example) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1017 | p = weakref.proxy(s) |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1018 | self.assertEqual(p.tobytes(), s.tobytes()) |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 1019 | s = None |
| 1020 | self.assertRaises(ReferenceError, len, p) |
| 1021 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1022 | @unittest.skipUnless(hasattr(sys, 'getrefcount'), |
| 1023 | 'test needs sys.getrefcount()') |
Skip Montanaro | d5cf0b8 | 2003-08-03 23:02:10 +0000 | [diff] [blame] | 1024 | def test_bug_782369(self): |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1025 | for i in range(10): |
| 1026 | b = array.array('B', range(64)) |
| 1027 | rc = sys.getrefcount(10) |
| 1028 | for i in range(10): |
| 1029 | b = array.array('B', range(64)) |
| 1030 | self.assertEqual(rc, sys.getrefcount(10)) |
Skip Montanaro | d5cf0b8 | 2003-08-03 23:02:10 +0000 | [diff] [blame] | 1031 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1032 | def test_subclass_with_kwargs(self): |
| 1033 | # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 1034 | ArraySubclassWithKwargs('b', newarg=1) |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 1035 | |
Guido van Rossum | 6b826ab | 2007-07-03 16:22:09 +0000 | [diff] [blame] | 1036 | def test_create_from_bytes(self): |
Alexandre Vassalotti | 7e4f321 | 2009-07-05 05:38:18 +0000 | [diff] [blame] | 1037 | # XXX This test probably needs to be moved in a subclass or |
| 1038 | # generalized to use self.typecode. |
Guido van Rossum | 6b826ab | 2007-07-03 16:22:09 +0000 | [diff] [blame] | 1039 | a = array.array('H', b"1234") |
| 1040 | self.assertEqual(len(a) * a.itemsize, 4) |
| 1041 | |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 1042 | @support.cpython_only |
| 1043 | def test_sizeof_with_buffer(self): |
| 1044 | a = array.array(self.typecode, self.example) |
Meador Inge | 80dd1af | 2012-08-10 23:21:39 -0500 | [diff] [blame] | 1045 | basesize = support.calcvobjsize('Pn2Pi') |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 1046 | buffer_size = a.buffer_info()[1] * a.itemsize |
| 1047 | support.check_sizeof(self, a, basesize + buffer_size) |
| 1048 | |
| 1049 | @support.cpython_only |
| 1050 | def test_sizeof_without_buffer(self): |
| 1051 | a = array.array(self.typecode) |
Meador Inge | 80dd1af | 2012-08-10 23:21:39 -0500 | [diff] [blame] | 1052 | basesize = support.calcvobjsize('Pn2Pi') |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 1053 | support.check_sizeof(self, a, basesize) |
| 1054 | |
Alexandre Vassalotti | 9730e33 | 2013-11-29 20:47:15 -0800 | [diff] [blame] | 1055 | def test_initialize_with_unicode(self): |
| 1056 | if self.typecode != 'u': |
| 1057 | with self.assertRaises(TypeError) as cm: |
| 1058 | a = array.array(self.typecode, 'foo') |
| 1059 | self.assertIn("cannot use a str", str(cm.exception)) |
| 1060 | with self.assertRaises(TypeError) as cm: |
| 1061 | a = array.array(self.typecode, array.array('u', 'foo')) |
| 1062 | self.assertIn("cannot use a unicode array", str(cm.exception)) |
| 1063 | else: |
| 1064 | a = array.array(self.typecode, "foo") |
| 1065 | a = array.array(self.typecode, array.array('u', 'foo')) |
| 1066 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 1067 | @support.cpython_only |
| 1068 | def test_obsolete_write_lock(self): |
| 1069 | from _testcapi import getbuffer_with_null_view |
| 1070 | a = array.array('B', b"") |
| 1071 | self.assertRaises(BufferError, getbuffer_with_null_view, a) |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 1072 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1073 | class StringTest(BaseTest): |
| 1074 | |
| 1075 | def test_setitem(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1076 | super().test_setitem() |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1077 | a = array.array(self.typecode, self.example) |
| 1078 | self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) |
| 1079 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1080 | class UnicodeTest(StringTest, unittest.TestCase): |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 1081 | typecode = 'u' |
| 1082 | example = '\x01\u263a\x00\ufeff' |
| 1083 | smallerexample = '\x01\u263a\x00\ufefe' |
| 1084 | biggerexample = '\x01\u263a\x01\ufeff' |
| 1085 | outside = str('\x33') |
Victor Stinner | 8d4734f | 2012-08-08 20:09:21 +0200 | [diff] [blame] | 1086 | minitemsize = 2 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1087 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 1088 | def test_unicode(self): |
| 1089 | self.assertRaises(TypeError, array.array, 'b', 'foo') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1090 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 1091 | a = array.array('u', '\xa0\xc2\u1234') |
| 1092 | a.fromunicode(' ') |
| 1093 | a.fromunicode('') |
| 1094 | a.fromunicode('') |
| 1095 | a.fromunicode('\x11abc\xff\u1234') |
| 1096 | s = a.tounicode() |
| 1097 | self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234') |
Victor Stinner | 9d512ab | 2012-08-09 00:43:56 +0200 | [diff] [blame] | 1098 | self.assertEqual(a.itemsize, sizeof_wchar) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1099 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 1100 | s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234' |
| 1101 | a = array.array('u', s) |
| 1102 | self.assertEqual( |
| 1103 | repr(a), |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 1104 | "array('u', '\\x00=\"\\'a\\\\b\\x80\xff\\x00\\x01\u1234')") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1105 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 1106 | self.assertRaises(TypeError, a.fromunicode) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1107 | |
Victor Stinner | 29ec595 | 2013-02-26 00:27:38 +0100 | [diff] [blame] | 1108 | def test_issue17223(self): |
| 1109 | # this used to crash |
Victor Stinner | c472c5d | 2013-02-26 22:52:11 +0100 | [diff] [blame] | 1110 | if sizeof_wchar == 4: |
| 1111 | # U+FFFFFFFF is an invalid code point in Unicode 6.0 |
| 1112 | invalid_str = b'\xff\xff\xff\xff' |
| 1113 | else: |
Victor Stinner | 29c0034 | 2013-03-08 02:33:06 +0100 | [diff] [blame] | 1114 | # PyUnicode_FromUnicode() cannot fail with 16-bit wchar_t |
| 1115 | self.skipTest("specific to 32-bit wchar_t") |
Victor Stinner | c472c5d | 2013-02-26 22:52:11 +0100 | [diff] [blame] | 1116 | a = array.array('u', invalid_str) |
Victor Stinner | 29ec595 | 2013-02-26 00:27:38 +0100 | [diff] [blame] | 1117 | self.assertRaises(ValueError, a.tounicode) |
| 1118 | self.assertRaises(ValueError, str, a) |
| 1119 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1120 | class NumberTest(BaseTest): |
| 1121 | |
| 1122 | def test_extslice(self): |
| 1123 | a = array.array(self.typecode, range(5)) |
| 1124 | self.assertEqual(a[::], a) |
| 1125 | self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) |
| 1126 | self.assertEqual(a[1::2], array.array(self.typecode, [1,3])) |
| 1127 | self.assertEqual(a[::-1], array.array(self.typecode, [4,3,2,1,0])) |
| 1128 | self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0])) |
| 1129 | self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) |
| 1130 | self.assertEqual(a[-100:100:], a) |
| 1131 | self.assertEqual(a[100:-100:-1], a[::-1]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 1132 | self.assertEqual(a[-100:100:2], array.array(self.typecode, [0,2,4])) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1133 | self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) |
| 1134 | self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) |
| 1135 | |
| 1136 | def test_delslice(self): |
| 1137 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1138 | del a[::2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1139 | self.assertEqual(a, array.array(self.typecode, [1,3])) |
| 1140 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1141 | del a[1::2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1142 | self.assertEqual(a, array.array(self.typecode, [0,2,4])) |
| 1143 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1144 | del a[1::-2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1145 | self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) |
| 1146 | a = array.array(self.typecode, range(10)) |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 1147 | del a[::1000] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1148 | self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 1149 | # test issue7788 |
| 1150 | a = array.array(self.typecode, range(10)) |
| 1151 | del a[9::1<<333] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1152 | |
| 1153 | def test_assignment(self): |
| 1154 | a = array.array(self.typecode, range(10)) |
| 1155 | a[::2] = array.array(self.typecode, [42]*5) |
| 1156 | self.assertEqual(a, array.array(self.typecode, [42, 1, 42, 3, 42, 5, 42, 7, 42, 9])) |
| 1157 | a = array.array(self.typecode, range(10)) |
| 1158 | a[::-4] = array.array(self.typecode, [10]*3) |
| 1159 | self.assertEqual(a, array.array(self.typecode, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) |
| 1160 | a = array.array(self.typecode, range(4)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1161 | a[::-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1162 | self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) |
| 1163 | a = array.array(self.typecode, range(10)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1164 | b = a[:] |
| 1165 | c = a[:] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1166 | ins = array.array(self.typecode, range(2)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1167 | a[2:3] = ins |
| 1168 | b[slice(2,3)] = ins |
| 1169 | c[2:3:] = ins |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1170 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1171 | def test_iterationcontains(self): |
| 1172 | a = array.array(self.typecode, range(10)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1173 | self.assertEqual(list(a), list(range(10))) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1174 | b = array.array(self.typecode, [20]) |
| 1175 | self.assertEqual(a[-1] in a, True) |
| 1176 | self.assertEqual(b[0] not in a, True) |
| 1177 | |
| 1178 | def check_overflow(self, lower, upper): |
| 1179 | # method to be used by subclasses |
| 1180 | |
| 1181 | # should not overflow assigning lower limit |
| 1182 | a = array.array(self.typecode, [lower]) |
| 1183 | a[0] = lower |
| 1184 | # should overflow assigning less than lower limit |
| 1185 | self.assertRaises(OverflowError, array.array, self.typecode, [lower-1]) |
| 1186 | self.assertRaises(OverflowError, a.__setitem__, 0, lower-1) |
| 1187 | # should not overflow assigning upper limit |
| 1188 | a = array.array(self.typecode, [upper]) |
| 1189 | a[0] = upper |
| 1190 | # should overflow assigning more than upper limit |
| 1191 | self.assertRaises(OverflowError, array.array, self.typecode, [upper+1]) |
| 1192 | self.assertRaises(OverflowError, a.__setitem__, 0, upper+1) |
| 1193 | |
| 1194 | def test_subclassing(self): |
| 1195 | typecode = self.typecode |
| 1196 | class ExaggeratingArray(array.array): |
| 1197 | __slots__ = ['offset'] |
| 1198 | |
| 1199 | def __new__(cls, typecode, data, offset): |
| 1200 | return array.array.__new__(cls, typecode, data) |
| 1201 | |
| 1202 | def __init__(self, typecode, data, offset): |
| 1203 | self.offset = offset |
| 1204 | |
| 1205 | def __getitem__(self, i): |
| 1206 | return array.array.__getitem__(self, i) + self.offset |
| 1207 | |
| 1208 | a = ExaggeratingArray(self.typecode, [3, 6, 7, 11], 4) |
| 1209 | self.assertEntryEqual(a[0], 7) |
| 1210 | |
| 1211 | self.assertRaises(AttributeError, setattr, a, "color", "blue") |
| 1212 | |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 1213 | def test_frombytearray(self): |
| 1214 | a = array.array('b', range(10)) |
| 1215 | b = array.array(self.typecode, a) |
| 1216 | self.assertEqual(a, b) |
| 1217 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1218 | class SignedNumberTest(NumberTest): |
| 1219 | example = [-1, 0, 1, 42, 0x7f] |
| 1220 | smallerexample = [-1, 0, 1, 42, 0x7e] |
| 1221 | biggerexample = [-1, 0, 1, 43, 0x7f] |
| 1222 | outside = 23 |
| 1223 | |
| 1224 | def test_overflow(self): |
| 1225 | a = array.array(self.typecode) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 1226 | lower = -1 * int(pow(2, a.itemsize * 8 - 1)) |
| 1227 | upper = int(pow(2, a.itemsize * 8 - 1)) - 1 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1228 | self.check_overflow(lower, upper) |
| 1229 | |
| 1230 | class UnsignedNumberTest(NumberTest): |
| 1231 | example = [0, 1, 17, 23, 42, 0xff] |
| 1232 | smallerexample = [0, 1, 17, 23, 42, 0xfe] |
| 1233 | biggerexample = [0, 1, 17, 23, 43, 0xff] |
| 1234 | outside = 0xaa |
| 1235 | |
| 1236 | def test_overflow(self): |
| 1237 | a = array.array(self.typecode) |
| 1238 | lower = 0 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 1239 | upper = int(pow(2, a.itemsize * 8)) - 1 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1240 | self.check_overflow(lower, upper) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1242 | def test_bytes_extend(self): |
| 1243 | s = bytes(self.example) |
| 1244 | |
| 1245 | a = array.array(self.typecode, self.example) |
| 1246 | a.extend(s) |
| 1247 | self.assertEqual( |
| 1248 | a, |
| 1249 | array.array(self.typecode, self.example+self.example) |
| 1250 | ) |
| 1251 | |
| 1252 | a = array.array(self.typecode, self.example) |
| 1253 | a.extend(bytearray(reversed(s))) |
| 1254 | self.assertEqual( |
| 1255 | a, |
| 1256 | array.array(self.typecode, self.example+self.example[::-1]) |
| 1257 | ) |
| 1258 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1259 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1260 | class ByteTest(SignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1261 | typecode = 'b' |
| 1262 | minitemsize = 1 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1263 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1264 | class UnsignedByteTest(UnsignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1265 | typecode = 'B' |
| 1266 | minitemsize = 1 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1267 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1268 | class ShortTest(SignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1269 | typecode = 'h' |
| 1270 | minitemsize = 2 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1271 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1272 | class UnsignedShortTest(UnsignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1273 | typecode = 'H' |
| 1274 | minitemsize = 2 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1275 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1276 | class IntTest(SignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1277 | typecode = 'i' |
| 1278 | minitemsize = 2 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1279 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1280 | class UnsignedIntTest(UnsignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1281 | typecode = 'I' |
| 1282 | minitemsize = 2 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1283 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1284 | class LongTest(SignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1285 | typecode = 'l' |
| 1286 | minitemsize = 4 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1287 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1288 | class UnsignedLongTest(UnsignedNumberTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1289 | typecode = 'L' |
| 1290 | minitemsize = 4 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1291 | |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1292 | @unittest.skipIf(not have_long_long, 'need long long support') |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1293 | class LongLongTest(SignedNumberTest, unittest.TestCase): |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1294 | typecode = 'q' |
| 1295 | minitemsize = 8 |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1296 | |
| 1297 | @unittest.skipIf(not have_long_long, 'need long long support') |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1298 | class UnsignedLongLongTest(UnsignedNumberTest, unittest.TestCase): |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1299 | typecode = 'Q' |
| 1300 | minitemsize = 8 |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1301 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1302 | class FPTest(NumberTest): |
| 1303 | example = [-42.0, 0, 42, 1e5, -1e10] |
| 1304 | smallerexample = [-42.0, 0, 42, 1e5, -2e10] |
| 1305 | biggerexample = [-42.0, 0, 42, 1e5, 1e10] |
| 1306 | outside = 23 |
| 1307 | |
| 1308 | def assertEntryEqual(self, entry1, entry2): |
| 1309 | self.assertAlmostEqual(entry1, entry2) |
| 1310 | |
Walter Dörwald | cf99b0a | 2003-05-22 13:15:31 +0000 | [diff] [blame] | 1311 | def test_byteswap(self): |
| 1312 | a = array.array(self.typecode, self.example) |
| 1313 | self.assertRaises(TypeError, a.byteswap, 42) |
| 1314 | if a.itemsize in (1, 2, 4, 8): |
| 1315 | b = array.array(self.typecode, self.example) |
| 1316 | b.byteswap() |
| 1317 | if a.itemsize==1: |
| 1318 | self.assertEqual(a, b) |
| 1319 | else: |
| 1320 | # On alphas treating the byte swapped bit patters as |
| 1321 | # floats/doubles results in floating point exceptions |
| 1322 | # => compare the 8bit string values instead |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1323 | self.assertNotEqual(a.tobytes(), b.tobytes()) |
Walter Dörwald | cf99b0a | 2003-05-22 13:15:31 +0000 | [diff] [blame] | 1324 | b.byteswap() |
| 1325 | self.assertEqual(a, b) |
| 1326 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1327 | class FloatTest(FPTest, unittest.TestCase): |
Neal Norwitz | 35c6cd0 | 2003-05-22 13:29:15 +0000 | [diff] [blame] | 1328 | typecode = 'f' |
| 1329 | minitemsize = 4 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1330 | |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1331 | class DoubleTest(FPTest, unittest.TestCase): |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 1332 | typecode = 'd' |
| 1333 | minitemsize = 8 |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1334 | |
| 1335 | def test_alloc_overflow(self): |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 1336 | from sys import maxsize |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1337 | a = array.array('d', [-1]*65536) |
| 1338 | try: |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 1339 | a *= maxsize//65536 + 1 |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1340 | except MemoryError: |
| 1341 | pass |
| 1342 | else: |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 1343 | self.fail("Array of size > maxsize created - MemoryError expected") |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1344 | b = array.array('d', [ 2.71828183, 3.14159265, -1]) |
| 1345 | try: |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 1346 | b * (maxsize//3 + 1) |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1347 | except MemoryError: |
| 1348 | pass |
| 1349 | else: |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 1350 | self.fail("Array of size > maxsize created - MemoryError expected") |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1351 | |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 1352 | |
| 1353 | if __name__ == "__main__": |
Ezio Melotti | 1d3e96d | 2013-01-10 06:04:50 +0200 | [diff] [blame] | 1354 | unittest.main() |