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