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