Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 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 |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 8 | from weakref import proxy |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 9 | import array, io, math |
Guido van Rossum | bf12cdb | 2006-08-17 20:24:18 +0000 | [diff] [blame] | 10 | from pickle import loads, dumps |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 11 | |
| 12 | class ArraySubclass(array.array): |
| 13 | pass |
Guido van Rossum | c9f8f14 | 1997-04-09 20:51:54 +0000 | [diff] [blame] | 14 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 15 | class ArraySubclassWithKwargs(array.array): |
| 16 | def __init__(self, typecode, newarg=None): |
| 17 | array.array.__init__(typecode) |
| 18 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 19 | tests = [] # list to accumulate all tests |
Guido van Rossum | 31f72d7 | 2007-06-18 18:44:28 +0000 | [diff] [blame] | 20 | typecodes = "ubBhHiIlLfd" |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 21 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 22 | class BadConstructorTest(unittest.TestCase): |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 23 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 24 | def test_constructor(self): |
| 25 | self.assertRaises(TypeError, array.array) |
| 26 | self.assertRaises(TypeError, array.array, spam=42) |
| 27 | self.assertRaises(TypeError, array.array, 'xx') |
| 28 | self.assertRaises(ValueError, array.array, 'x') |
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 | tests.append(BadConstructorTest) |
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 | class BaseTest(unittest.TestCase): |
| 33 | # Required class attributes (provided by subclasses |
| 34 | # typecode: the typecode to test |
| 35 | # example: an initializer usable in the constructor for this type |
| 36 | # smallerexample: the same length as example, but smaller |
| 37 | # biggerexample: the same length as example, but bigger |
| 38 | # outside: An entry that is not in example |
| 39 | # minitemsize: the minimum guaranteed itemsize |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 40 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 41 | def assertEntryEqual(self, entry1, entry2): |
| 42 | self.assertEqual(entry1, entry2) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 43 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 44 | def badtypecode(self): |
| 45 | # Return a typecode that is different from our own |
| 46 | return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 47 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 48 | def test_constructor(self): |
| 49 | a = array.array(self.typecode) |
| 50 | self.assertEqual(a.typecode, self.typecode) |
| 51 | self.assert_(a.itemsize>=self.minitemsize) |
| 52 | self.assertRaises(TypeError, array.array, self.typecode, None) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 53 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 54 | def test_len(self): |
| 55 | a = array.array(self.typecode) |
| 56 | a.append(self.example[0]) |
| 57 | self.assertEqual(len(a), 1) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 58 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 59 | a = array.array(self.typecode, self.example) |
| 60 | self.assertEqual(len(a), len(self.example)) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 61 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 62 | def test_buffer_info(self): |
| 63 | a = array.array(self.typecode, self.example) |
| 64 | self.assertRaises(TypeError, a.buffer_info, 42) |
| 65 | bi = a.buffer_info() |
| 66 | self.assert_(isinstance(bi, tuple)) |
| 67 | self.assertEqual(len(bi), 2) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 68 | self.assert_(isinstance(bi[0], int)) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 69 | self.assert_(isinstance(bi[1], int)) |
| 70 | self.assertEqual(bi[1], len(a)) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 71 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 72 | def test_byteswap(self): |
| 73 | a = array.array(self.typecode, self.example) |
| 74 | self.assertRaises(TypeError, a.byteswap, 42) |
| 75 | if a.itemsize in (1, 2, 4, 8): |
| 76 | b = array.array(self.typecode, self.example) |
| 77 | b.byteswap() |
| 78 | if a.itemsize==1: |
| 79 | self.assertEqual(a, b) |
| 80 | else: |
| 81 | self.assertNotEqual(a, b) |
| 82 | b.byteswap() |
| 83 | self.assertEqual(a, b) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 84 | |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 85 | def test_copy(self): |
| 86 | import copy |
| 87 | a = array.array(self.typecode, self.example) |
| 88 | b = copy.copy(a) |
| 89 | self.assertNotEqual(id(a), id(b)) |
| 90 | self.assertEqual(a, b) |
| 91 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 92 | def test_deepcopy(self): |
| 93 | import copy |
| 94 | a = array.array(self.typecode, self.example) |
| 95 | b = copy.deepcopy(a) |
| 96 | self.assertNotEqual(id(a), id(b)) |
| 97 | self.assertEqual(a, b) |
| 98 | |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 99 | def test_pickle(self): |
| 100 | for protocol in (0, 1, 2): |
| 101 | a = array.array(self.typecode, self.example) |
| 102 | b = loads(dumps(a, protocol)) |
| 103 | self.assertNotEqual(id(a), id(b)) |
| 104 | self.assertEqual(a, b) |
| 105 | |
| 106 | a = ArraySubclass(self.typecode, self.example) |
| 107 | a.x = 10 |
| 108 | b = loads(dumps(a, protocol)) |
| 109 | self.assertNotEqual(id(a), id(b)) |
| 110 | self.assertEqual(a, b) |
| 111 | self.assertEqual(a.x, b.x) |
| 112 | self.assertEqual(type(a), type(b)) |
| 113 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 114 | def test_pickle_for_empty_array(self): |
| 115 | for protocol in (0, 1, 2): |
| 116 | a = array.array(self.typecode) |
| 117 | b = loads(dumps(a, protocol)) |
| 118 | self.assertNotEqual(id(a), id(b)) |
| 119 | self.assertEqual(a, b) |
| 120 | |
| 121 | a = ArraySubclass(self.typecode) |
| 122 | a.x = 10 |
| 123 | b = loads(dumps(a, protocol)) |
| 124 | self.assertNotEqual(id(a), id(b)) |
| 125 | self.assertEqual(a, b) |
| 126 | self.assertEqual(a.x, b.x) |
| 127 | self.assertEqual(type(a), type(b)) |
| 128 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 129 | def test_insert(self): |
| 130 | a = array.array(self.typecode, self.example) |
| 131 | a.insert(0, self.example[0]) |
| 132 | self.assertEqual(len(a), 1+len(self.example)) |
| 133 | self.assertEqual(a[0], a[1]) |
| 134 | self.assertRaises(TypeError, a.insert) |
| 135 | self.assertRaises(TypeError, a.insert, None) |
Walter Dörwald | ba39d9c | 2003-05-18 01:56:25 +0000 | [diff] [blame] | 136 | self.assertRaises(TypeError, a.insert, 0, None) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 137 | |
Walter Dörwald | 9e46abe | 2003-05-18 03:15:10 +0000 | [diff] [blame] | 138 | a = array.array(self.typecode, self.example) |
| 139 | a.insert(-1, self.example[0]) |
| 140 | self.assertEqual( |
| 141 | a, |
| 142 | array.array( |
| 143 | self.typecode, |
| 144 | self.example[:-1] + self.example[:1] + self.example[-1:] |
| 145 | ) |
| 146 | ) |
| 147 | |
| 148 | a = array.array(self.typecode, self.example) |
| 149 | a.insert(-1000, self.example[0]) |
| 150 | self.assertEqual( |
| 151 | a, |
| 152 | array.array(self.typecode, self.example[:1] + self.example) |
| 153 | ) |
| 154 | |
| 155 | a = array.array(self.typecode, self.example) |
| 156 | a.insert(1000, self.example[0]) |
| 157 | self.assertEqual( |
| 158 | a, |
| 159 | array.array(self.typecode, self.example + self.example[:1]) |
| 160 | ) |
| 161 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 162 | def test_tofromfile(self): |
| 163 | a = array.array(self.typecode, 2*self.example) |
| 164 | self.assertRaises(TypeError, a.tofile) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 165 | support.unlink(support.TESTFN) |
| 166 | f = open(support.TESTFN, 'wb') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 167 | try: |
| 168 | a.tofile(f) |
| 169 | f.close() |
| 170 | b = array.array(self.typecode) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 171 | f = open(support.TESTFN, 'rb') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 172 | self.assertRaises(TypeError, b.fromfile) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 173 | b.fromfile(f, len(self.example)) |
| 174 | self.assertEqual(b, array.array(self.typecode, self.example)) |
| 175 | self.assertNotEqual(a, b) |
| 176 | b.fromfile(f, len(self.example)) |
| 177 | self.assertEqual(a, b) |
| 178 | self.assertRaises(EOFError, b.fromfile, f, 1) |
| 179 | f.close() |
| 180 | finally: |
| 181 | if not f.closed: |
| 182 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 183 | support.unlink(support.TESTFN) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 184 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 185 | def test_tofromlist(self): |
| 186 | a = array.array(self.typecode, 2*self.example) |
| 187 | b = array.array(self.typecode) |
| 188 | self.assertRaises(TypeError, a.tolist, 42) |
| 189 | self.assertRaises(TypeError, b.fromlist) |
| 190 | self.assertRaises(TypeError, b.fromlist, 42) |
| 191 | self.assertRaises(TypeError, b.fromlist, [None]) |
| 192 | b.fromlist(a.tolist()) |
| 193 | self.assertEqual(a, b) |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 194 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 195 | def test_tofromstring(self): |
| 196 | a = array.array(self.typecode, 2*self.example) |
| 197 | b = array.array(self.typecode) |
| 198 | self.assertRaises(TypeError, a.tostring, 42) |
| 199 | self.assertRaises(TypeError, b.fromstring) |
| 200 | self.assertRaises(TypeError, b.fromstring, 42) |
| 201 | b.fromstring(a.tostring()) |
| 202 | self.assertEqual(a, b) |
| 203 | if a.itemsize>1: |
| 204 | self.assertRaises(ValueError, b.fromstring, "x") |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 205 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 206 | def test_repr(self): |
| 207 | a = array.array(self.typecode, 2*self.example) |
| 208 | self.assertEqual(a, eval(repr(a), {"array": array.array})) |
Fred Drake | 7833447 | 2000-06-28 17:50:51 +0000 | [diff] [blame] | 209 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 210 | a = array.array(self.typecode) |
| 211 | self.assertEqual(repr(a), "array('%s')" % self.typecode) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 212 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 213 | def test_str(self): |
| 214 | a = array.array(self.typecode, 2*self.example) |
| 215 | str(a) |
Roger E. Masse | 8db1b07 | 1996-12-09 20:09:16 +0000 | [diff] [blame] | 216 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 217 | def test_cmp(self): |
| 218 | a = array.array(self.typecode, self.example) |
| 219 | self.assert_((a == 42) is False) |
| 220 | self.assert_((a != 42) is True) |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 221 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 222 | self.assert_((a == a) is True) |
| 223 | self.assert_((a != a) is False) |
| 224 | self.assert_((a < a) is False) |
| 225 | self.assert_((a <= a) is True) |
| 226 | self.assert_((a > a) is False) |
| 227 | self.assert_((a >= a) is True) |
Tim Peters | cc78e47 | 2000-11-14 21:36:07 +0000 | [diff] [blame] | 228 | |
Neal Norwitz | bd2835c | 2006-03-17 08:54:15 +0000 | [diff] [blame] | 229 | al = array.array(self.typecode, self.smallerexample) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 230 | ab = array.array(self.typecode, self.biggerexample) |
Guido van Rossum | 7f1d3aa | 1998-07-16 15:31:43 +0000 | [diff] [blame] | 231 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 232 | self.assert_((a == 2*a) is False) |
| 233 | self.assert_((a != 2*a) is True) |
| 234 | self.assert_((a < 2*a) is True) |
| 235 | self.assert_((a <= 2*a) is True) |
| 236 | self.assert_((a > 2*a) is False) |
| 237 | self.assert_((a >= 2*a) is False) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 238 | |
Neal Norwitz | bd2835c | 2006-03-17 08:54:15 +0000 | [diff] [blame] | 239 | self.assert_((a == al) is False) |
| 240 | self.assert_((a != al) is True) |
| 241 | self.assert_((a < al) is False) |
| 242 | self.assert_((a <= al) is False) |
| 243 | self.assert_((a > al) is True) |
| 244 | self.assert_((a >= al) is True) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 245 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 246 | self.assert_((a == ab) is False) |
| 247 | self.assert_((a != ab) is True) |
| 248 | self.assert_((a < ab) is True) |
| 249 | self.assert_((a <= ab) is True) |
| 250 | self.assert_((a > ab) is False) |
| 251 | self.assert_((a >= ab) is False) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 252 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 253 | def test_add(self): |
| 254 | a = array.array(self.typecode, self.example) \ |
| 255 | + array.array(self.typecode, self.example[::-1]) |
| 256 | self.assertEqual( |
| 257 | a, |
| 258 | array.array(self.typecode, self.example + self.example[::-1]) |
| 259 | ) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 260 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 261 | b = array.array(self.badtypecode()) |
| 262 | self.assertRaises(TypeError, a.__add__, b) |
| 263 | |
| 264 | self.assertRaises(TypeError, a.__add__, "bad") |
| 265 | |
| 266 | def test_iadd(self): |
| 267 | a = array.array(self.typecode, self.example[::-1]) |
| 268 | b = a |
| 269 | a += array.array(self.typecode, 2*self.example) |
| 270 | self.assert_(a is b) |
| 271 | self.assertEqual( |
| 272 | a, |
| 273 | array.array(self.typecode, self.example[::-1]+2*self.example) |
| 274 | ) |
| 275 | |
| 276 | b = array.array(self.badtypecode()) |
| 277 | self.assertRaises(TypeError, a.__add__, b) |
| 278 | |
| 279 | self.assertRaises(TypeError, a.__iadd__, "bad") |
| 280 | |
| 281 | def test_mul(self): |
| 282 | a = 5*array.array(self.typecode, self.example) |
| 283 | self.assertEqual( |
| 284 | a, |
| 285 | array.array(self.typecode, 5*self.example) |
| 286 | ) |
| 287 | |
| 288 | a = array.array(self.typecode, self.example)*5 |
| 289 | self.assertEqual( |
| 290 | a, |
| 291 | array.array(self.typecode, self.example*5) |
| 292 | ) |
| 293 | |
| 294 | a = 0*array.array(self.typecode, self.example) |
| 295 | self.assertEqual( |
| 296 | a, |
| 297 | array.array(self.typecode) |
| 298 | ) |
| 299 | |
| 300 | a = (-1)*array.array(self.typecode, self.example) |
| 301 | self.assertEqual( |
| 302 | a, |
| 303 | array.array(self.typecode) |
| 304 | ) |
| 305 | |
| 306 | self.assertRaises(TypeError, a.__mul__, "bad") |
| 307 | |
| 308 | def test_imul(self): |
| 309 | a = array.array(self.typecode, self.example) |
| 310 | b = a |
| 311 | |
| 312 | a *= 5 |
| 313 | self.assert_(a is b) |
| 314 | self.assertEqual( |
| 315 | a, |
| 316 | array.array(self.typecode, 5*self.example) |
| 317 | ) |
| 318 | |
| 319 | a *= 0 |
| 320 | self.assert_(a is b) |
| 321 | self.assertEqual(a, array.array(self.typecode)) |
| 322 | |
| 323 | a *= 1000 |
| 324 | self.assert_(a is b) |
| 325 | self.assertEqual(a, array.array(self.typecode)) |
| 326 | |
| 327 | a *= -1 |
| 328 | self.assert_(a is b) |
| 329 | self.assertEqual(a, array.array(self.typecode)) |
| 330 | |
| 331 | a = array.array(self.typecode, self.example) |
| 332 | a *= -1 |
| 333 | self.assertEqual(a, array.array(self.typecode)) |
| 334 | |
| 335 | self.assertRaises(TypeError, a.__imul__, "bad") |
| 336 | |
| 337 | def test_getitem(self): |
| 338 | a = array.array(self.typecode, self.example) |
| 339 | self.assertEntryEqual(a[0], self.example[0]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 340 | self.assertEntryEqual(a[0], self.example[0]) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 341 | self.assertEntryEqual(a[-1], self.example[-1]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 342 | self.assertEntryEqual(a[-1], self.example[-1]) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 343 | self.assertEntryEqual(a[len(self.example)-1], self.example[-1]) |
| 344 | self.assertEntryEqual(a[-len(self.example)], self.example[0]) |
| 345 | self.assertRaises(TypeError, a.__getitem__) |
| 346 | self.assertRaises(IndexError, a.__getitem__, len(self.example)) |
| 347 | self.assertRaises(IndexError, a.__getitem__, -len(self.example)-1) |
| 348 | |
| 349 | def test_setitem(self): |
| 350 | a = array.array(self.typecode, self.example) |
| 351 | a[0] = a[-1] |
| 352 | self.assertEntryEqual(a[0], a[-1]) |
| 353 | |
| 354 | a = array.array(self.typecode, self.example) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 355 | a[0] = a[-1] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 356 | self.assertEntryEqual(a[0], a[-1]) |
| 357 | |
| 358 | a = array.array(self.typecode, self.example) |
| 359 | a[-1] = a[0] |
| 360 | self.assertEntryEqual(a[0], a[-1]) |
| 361 | |
| 362 | a = array.array(self.typecode, self.example) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 363 | a[-1] = a[0] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 364 | self.assertEntryEqual(a[0], a[-1]) |
| 365 | |
| 366 | a = array.array(self.typecode, self.example) |
| 367 | a[len(self.example)-1] = a[0] |
| 368 | self.assertEntryEqual(a[0], a[-1]) |
| 369 | |
| 370 | a = array.array(self.typecode, self.example) |
| 371 | a[-len(self.example)] = a[-1] |
| 372 | self.assertEntryEqual(a[0], a[-1]) |
| 373 | |
| 374 | self.assertRaises(TypeError, a.__setitem__) |
| 375 | self.assertRaises(TypeError, a.__setitem__, None) |
| 376 | self.assertRaises(TypeError, a.__setitem__, 0, None) |
| 377 | self.assertRaises( |
| 378 | IndexError, |
| 379 | a.__setitem__, |
| 380 | len(self.example), self.example[0] |
| 381 | ) |
| 382 | self.assertRaises( |
| 383 | IndexError, |
| 384 | a.__setitem__, |
| 385 | -len(self.example)-1, self.example[0] |
| 386 | ) |
| 387 | |
| 388 | def test_delitem(self): |
| 389 | a = array.array(self.typecode, self.example) |
| 390 | del a[0] |
| 391 | self.assertEqual( |
| 392 | a, |
| 393 | array.array(self.typecode, self.example[1:]) |
| 394 | ) |
| 395 | |
| 396 | a = array.array(self.typecode, self.example) |
| 397 | del a[-1] |
| 398 | self.assertEqual( |
| 399 | a, |
| 400 | array.array(self.typecode, self.example[:-1]) |
| 401 | ) |
| 402 | |
| 403 | a = array.array(self.typecode, self.example) |
| 404 | del a[len(self.example)-1] |
| 405 | self.assertEqual( |
| 406 | a, |
| 407 | array.array(self.typecode, self.example[:-1]) |
| 408 | ) |
| 409 | |
| 410 | a = array.array(self.typecode, self.example) |
| 411 | del a[-len(self.example)] |
| 412 | self.assertEqual( |
| 413 | a, |
| 414 | array.array(self.typecode, self.example[1:]) |
| 415 | ) |
| 416 | |
| 417 | self.assertRaises(TypeError, a.__delitem__) |
| 418 | self.assertRaises(TypeError, a.__delitem__, None) |
| 419 | self.assertRaises(IndexError, a.__delitem__, len(self.example)) |
| 420 | self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1) |
| 421 | |
| 422 | def test_getslice(self): |
| 423 | a = array.array(self.typecode, self.example) |
| 424 | self.assertEqual(a[:], a) |
| 425 | |
| 426 | self.assertEqual( |
| 427 | a[1:], |
| 428 | array.array(self.typecode, self.example[1:]) |
| 429 | ) |
| 430 | |
| 431 | self.assertEqual( |
| 432 | a[:1], |
| 433 | array.array(self.typecode, self.example[:1]) |
| 434 | ) |
| 435 | |
| 436 | self.assertEqual( |
| 437 | a[:-1], |
| 438 | array.array(self.typecode, self.example[:-1]) |
| 439 | ) |
| 440 | |
| 441 | self.assertEqual( |
| 442 | a[-1:], |
| 443 | array.array(self.typecode, self.example[-1:]) |
| 444 | ) |
| 445 | |
| 446 | self.assertEqual( |
| 447 | a[-1:-1], |
| 448 | array.array(self.typecode) |
| 449 | ) |
| 450 | |
| 451 | self.assertEqual( |
Walter Dörwald | 1083c24 | 2005-01-21 21:16:51 +0000 | [diff] [blame] | 452 | a[2:1], |
| 453 | array.array(self.typecode) |
| 454 | ) |
| 455 | |
| 456 | self.assertEqual( |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 457 | a[1000:], |
| 458 | array.array(self.typecode) |
| 459 | ) |
| 460 | self.assertEqual(a[-1000:], a) |
| 461 | self.assertEqual(a[:1000], a) |
| 462 | self.assertEqual( |
| 463 | a[:-1000], |
| 464 | array.array(self.typecode) |
| 465 | ) |
| 466 | self.assertEqual(a[-1000:1000], a) |
| 467 | self.assertEqual( |
| 468 | a[2000:1000], |
| 469 | array.array(self.typecode) |
| 470 | ) |
| 471 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 472 | def test_extended_getslice(self): |
| 473 | # Test extended slicing by comparing with list slicing |
| 474 | # (Assumes list conversion works correctly, too) |
| 475 | a = array.array(self.typecode, self.example) |
| 476 | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
| 477 | for start in indices: |
| 478 | for stop in indices: |
| 479 | # Everything except the initial 0 (invalid step) |
| 480 | for step in indices[1:]: |
| 481 | self.assertEqual(list(a[start:stop:step]), |
| 482 | list(a)[start:stop:step]) |
| 483 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 484 | def test_setslice(self): |
| 485 | a = array.array(self.typecode, self.example) |
| 486 | a[:1] = a |
| 487 | self.assertEqual( |
| 488 | a, |
| 489 | array.array(self.typecode, self.example + self.example[1:]) |
| 490 | ) |
| 491 | |
| 492 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 493 | a[:-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 494 | self.assertEqual( |
| 495 | a, |
| 496 | array.array(self.typecode, self.example + self.example[-1:]) |
| 497 | ) |
| 498 | |
| 499 | a = array.array(self.typecode, self.example) |
| 500 | a[-1:] = a |
| 501 | self.assertEqual( |
| 502 | a, |
| 503 | array.array(self.typecode, self.example[:-1] + self.example) |
| 504 | ) |
| 505 | |
| 506 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 507 | a[1:] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 508 | self.assertEqual( |
| 509 | a, |
| 510 | array.array(self.typecode, self.example[:1] + self.example) |
| 511 | ) |
| 512 | |
| 513 | a = array.array(self.typecode, self.example) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 514 | a[1:-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 515 | self.assertEqual( |
| 516 | a, |
| 517 | array.array( |
| 518 | self.typecode, |
| 519 | self.example[:1] + self.example + self.example[-1:] |
| 520 | ) |
| 521 | ) |
| 522 | |
| 523 | a = array.array(self.typecode, self.example) |
| 524 | a[1000:] = a |
| 525 | self.assertEqual( |
| 526 | a, |
| 527 | array.array(self.typecode, 2*self.example) |
| 528 | ) |
| 529 | |
| 530 | a = array.array(self.typecode, self.example) |
| 531 | a[-1000:] = a |
| 532 | self.assertEqual( |
| 533 | a, |
| 534 | array.array(self.typecode, self.example) |
| 535 | ) |
| 536 | |
| 537 | a = array.array(self.typecode, self.example) |
| 538 | a[:1000] = a |
| 539 | self.assertEqual( |
| 540 | a, |
| 541 | array.array(self.typecode, self.example) |
| 542 | ) |
| 543 | |
| 544 | a = array.array(self.typecode, self.example) |
| 545 | a[:-1000] = a |
| 546 | self.assertEqual( |
| 547 | a, |
| 548 | array.array(self.typecode, 2*self.example) |
| 549 | ) |
| 550 | |
| 551 | a = array.array(self.typecode, self.example) |
| 552 | a[1:0] = a |
| 553 | self.assertEqual( |
| 554 | a, |
| 555 | array.array(self.typecode, self.example[:1] + self.example + self.example[1:]) |
| 556 | ) |
| 557 | |
| 558 | a = array.array(self.typecode, self.example) |
| 559 | a[2000:1000] = a |
| 560 | self.assertEqual( |
| 561 | a, |
| 562 | array.array(self.typecode, 2*self.example) |
| 563 | ) |
| 564 | |
| 565 | a = array.array(self.typecode, self.example) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 566 | self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 567 | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None) |
| 568 | |
| 569 | b = array.array(self.badtypecode()) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 570 | self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 571 | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b) |
| 572 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 573 | def test_extended_set_del_slice(self): |
| 574 | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
| 575 | for start in indices: |
| 576 | for stop in indices: |
| 577 | # Everything except the initial 0 (invalid step) |
| 578 | for step in indices[1:]: |
| 579 | a = array.array(self.typecode, self.example) |
| 580 | L = list(a) |
| 581 | # Make sure we have a slice of exactly the right length, |
| 582 | # but with (hopefully) different data. |
| 583 | data = L[start:stop:step] |
| 584 | data.reverse() |
| 585 | L[start:stop:step] = data |
| 586 | a[start:stop:step] = array.array(self.typecode, data) |
| 587 | self.assertEquals(a, array.array(self.typecode, L)) |
| 588 | |
| 589 | del L[start:stop:step] |
| 590 | del a[start:stop:step] |
| 591 | self.assertEquals(a, array.array(self.typecode, L)) |
| 592 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 593 | def test_index(self): |
| 594 | example = 2*self.example |
| 595 | a = array.array(self.typecode, example) |
| 596 | self.assertRaises(TypeError, a.index) |
| 597 | for x in example: |
| 598 | self.assertEqual(a.index(x), example.index(x)) |
| 599 | self.assertRaises(ValueError, a.index, None) |
| 600 | self.assertRaises(ValueError, a.index, self.outside) |
| 601 | |
| 602 | def test_count(self): |
| 603 | example = 2*self.example |
| 604 | a = array.array(self.typecode, example) |
| 605 | self.assertRaises(TypeError, a.count) |
| 606 | for x in example: |
| 607 | self.assertEqual(a.count(x), example.count(x)) |
| 608 | self.assertEqual(a.count(self.outside), 0) |
| 609 | self.assertEqual(a.count(None), 0) |
| 610 | |
| 611 | def test_remove(self): |
| 612 | for x in self.example: |
| 613 | example = 2*self.example |
| 614 | a = array.array(self.typecode, example) |
| 615 | pos = example.index(x) |
| 616 | example2 = example[:pos] + example[pos+1:] |
| 617 | a.remove(x) |
| 618 | self.assertEqual(a, array.array(self.typecode, example2)) |
| 619 | |
| 620 | a = array.array(self.typecode, self.example) |
| 621 | self.assertRaises(ValueError, a.remove, self.outside) |
| 622 | |
| 623 | self.assertRaises(ValueError, a.remove, None) |
| 624 | |
| 625 | def test_pop(self): |
| 626 | a = array.array(self.typecode) |
| 627 | self.assertRaises(IndexError, a.pop) |
| 628 | |
| 629 | a = array.array(self.typecode, 2*self.example) |
| 630 | self.assertRaises(TypeError, a.pop, 42, 42) |
| 631 | self.assertRaises(TypeError, a.pop, None) |
| 632 | self.assertRaises(IndexError, a.pop, len(a)) |
| 633 | self.assertRaises(IndexError, a.pop, -len(a)-1) |
| 634 | |
| 635 | self.assertEntryEqual(a.pop(0), self.example[0]) |
| 636 | self.assertEqual( |
| 637 | a, |
| 638 | array.array(self.typecode, self.example[1:]+self.example) |
| 639 | ) |
| 640 | self.assertEntryEqual(a.pop(1), self.example[2]) |
| 641 | self.assertEqual( |
| 642 | a, |
| 643 | array.array(self.typecode, self.example[1:2]+self.example[3:]+self.example) |
| 644 | ) |
| 645 | self.assertEntryEqual(a.pop(0), self.example[1]) |
| 646 | self.assertEntryEqual(a.pop(), self.example[-1]) |
| 647 | self.assertEqual( |
| 648 | a, |
| 649 | array.array(self.typecode, self.example[3:]+self.example[:-1]) |
| 650 | ) |
| 651 | |
| 652 | def test_reverse(self): |
| 653 | a = array.array(self.typecode, self.example) |
| 654 | self.assertRaises(TypeError, a.reverse, 42) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 655 | a.reverse() |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 656 | self.assertEqual( |
| 657 | a, |
| 658 | array.array(self.typecode, self.example[::-1]) |
| 659 | ) |
| 660 | |
| 661 | def test_extend(self): |
| 662 | a = array.array(self.typecode, self.example) |
| 663 | self.assertRaises(TypeError, a.extend) |
| 664 | a.extend(array.array(self.typecode, self.example[::-1])) |
| 665 | self.assertEqual( |
| 666 | a, |
| 667 | array.array(self.typecode, self.example+self.example[::-1]) |
| 668 | ) |
| 669 | |
| 670 | b = array.array(self.badtypecode()) |
| 671 | self.assertRaises(TypeError, a.extend, b) |
| 672 | |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 673 | a = array.array(self.typecode, self.example) |
| 674 | a.extend(self.example[::-1]) |
| 675 | self.assertEqual( |
| 676 | a, |
| 677 | array.array(self.typecode, self.example+self.example[::-1]) |
| 678 | ) |
| 679 | |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 680 | def test_constructor_with_iterable_argument(self): |
| 681 | a = array.array(self.typecode, iter(self.example)) |
| 682 | b = array.array(self.typecode, self.example) |
| 683 | self.assertEqual(a, b) |
| 684 | |
| 685 | # non-iterable argument |
| 686 | self.assertRaises(TypeError, array.array, self.typecode, 10) |
| 687 | |
| 688 | # pass through errors raised in __iter__ |
| 689 | class A: |
| 690 | def __iter__(self): |
| 691 | raise UnicodeError |
| 692 | self.assertRaises(UnicodeError, array.array, self.typecode, A()) |
| 693 | |
| 694 | # pass through errors raised in next() |
| 695 | def B(): |
| 696 | raise UnicodeError |
| 697 | yield None |
| 698 | self.assertRaises(UnicodeError, array.array, self.typecode, B()) |
| 699 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 700 | def test_coveritertraverse(self): |
| 701 | try: |
| 702 | import gc |
| 703 | except ImportError: |
| 704 | return |
| 705 | a = array.array(self.typecode) |
| 706 | l = [iter(a)] |
| 707 | l.append(l) |
| 708 | gc.collect() |
| 709 | |
| 710 | def test_buffer(self): |
| 711 | a = array.array(self.typecode, self.example) |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 712 | b = bytes(memoryview(a)) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 713 | self.assertEqual(b[0], a.tostring()[0]) |
| 714 | |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 715 | def test_weakref(self): |
| 716 | s = array.array(self.typecode, self.example) |
| 717 | p = proxy(s) |
| 718 | self.assertEqual(p.tostring(), s.tostring()) |
| 719 | s = None |
| 720 | self.assertRaises(ReferenceError, len, p) |
| 721 | |
Skip Montanaro | d5cf0b8 | 2003-08-03 23:02:10 +0000 | [diff] [blame] | 722 | def test_bug_782369(self): |
| 723 | import sys |
Skip Montanaro | ed9bf12 | 2003-08-03 23:30:40 +0000 | [diff] [blame] | 724 | if hasattr(sys, "getrefcount"): |
| 725 | for i in range(10): |
| 726 | b = array.array('B', range(64)) |
| 727 | rc = sys.getrefcount(10) |
| 728 | for i in range(10): |
| 729 | b = array.array('B', range(64)) |
| 730 | self.assertEqual(rc, sys.getrefcount(10)) |
Skip Montanaro | d5cf0b8 | 2003-08-03 23:02:10 +0000 | [diff] [blame] | 731 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 732 | def test_subclass_with_kwargs(self): |
| 733 | # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 734 | ArraySubclassWithKwargs('b', newarg=1) |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 735 | |
Guido van Rossum | 6b826ab | 2007-07-03 16:22:09 +0000 | [diff] [blame] | 736 | def test_create_from_bytes(self): |
| 737 | a = array.array('H', b"1234") |
| 738 | self.assertEqual(len(a) * a.itemsize, 4) |
| 739 | |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 740 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 741 | class StringTest(BaseTest): |
| 742 | |
| 743 | def test_setitem(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 744 | super().test_setitem() |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 745 | a = array.array(self.typecode, self.example) |
| 746 | self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) |
| 747 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 748 | class UnicodeTest(StringTest): |
| 749 | typecode = 'u' |
| 750 | example = '\x01\u263a\x00\ufeff' |
| 751 | smallerexample = '\x01\u263a\x00\ufefe' |
| 752 | biggerexample = '\x01\u263a\x01\ufeff' |
| 753 | outside = str('\x33') |
| 754 | minitemsize = 2 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 755 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 756 | def test_unicode(self): |
| 757 | self.assertRaises(TypeError, array.array, 'b', 'foo') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 758 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 759 | a = array.array('u', '\xa0\xc2\u1234') |
| 760 | a.fromunicode(' ') |
| 761 | a.fromunicode('') |
| 762 | a.fromunicode('') |
| 763 | a.fromunicode('\x11abc\xff\u1234') |
| 764 | s = a.tounicode() |
| 765 | self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234') |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 766 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 767 | s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234' |
| 768 | a = array.array('u', s) |
| 769 | self.assertEqual( |
| 770 | repr(a), |
| 771 | "array('u', '\\x00=\"\\'a\\\\b\\x80\\xff\\x00\\x01\\u1234')") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 772 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 773 | self.assertRaises(TypeError, a.fromunicode) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 774 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 775 | tests.append(UnicodeTest) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 776 | |
| 777 | class NumberTest(BaseTest): |
| 778 | |
| 779 | def test_extslice(self): |
| 780 | a = array.array(self.typecode, range(5)) |
| 781 | self.assertEqual(a[::], a) |
| 782 | self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) |
| 783 | self.assertEqual(a[1::2], array.array(self.typecode, [1,3])) |
| 784 | self.assertEqual(a[::-1], array.array(self.typecode, [4,3,2,1,0])) |
| 785 | self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0])) |
| 786 | self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) |
| 787 | self.assertEqual(a[-100:100:], a) |
| 788 | self.assertEqual(a[100:-100:-1], a[::-1]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 789 | 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] | 790 | self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) |
| 791 | self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) |
| 792 | |
| 793 | def test_delslice(self): |
| 794 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 795 | del a[::2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 796 | self.assertEqual(a, array.array(self.typecode, [1,3])) |
| 797 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 798 | del a[1::2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 799 | self.assertEqual(a, array.array(self.typecode, [0,2,4])) |
| 800 | a = array.array(self.typecode, range(5)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 801 | del a[1::-2] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 802 | self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) |
| 803 | a = array.array(self.typecode, range(10)) |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 804 | del a[::1000] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 805 | self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) |
| 806 | |
| 807 | def test_assignment(self): |
| 808 | a = array.array(self.typecode, range(10)) |
| 809 | a[::2] = array.array(self.typecode, [42]*5) |
| 810 | self.assertEqual(a, array.array(self.typecode, [42, 1, 42, 3, 42, 5, 42, 7, 42, 9])) |
| 811 | a = array.array(self.typecode, range(10)) |
| 812 | a[::-4] = array.array(self.typecode, [10]*3) |
| 813 | self.assertEqual(a, array.array(self.typecode, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) |
| 814 | a = array.array(self.typecode, range(4)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 815 | a[::-1] = a |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 816 | self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) |
| 817 | a = array.array(self.typecode, range(10)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 818 | b = a[:] |
| 819 | c = a[:] |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 820 | ins = array.array(self.typecode, range(2)) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 821 | a[2:3] = ins |
| 822 | b[slice(2,3)] = ins |
| 823 | c[2:3:] = ins |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 824 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 825 | def test_iterationcontains(self): |
| 826 | a = array.array(self.typecode, range(10)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 827 | self.assertEqual(list(a), list(range(10))) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 828 | b = array.array(self.typecode, [20]) |
| 829 | self.assertEqual(a[-1] in a, True) |
| 830 | self.assertEqual(b[0] not in a, True) |
| 831 | |
| 832 | def check_overflow(self, lower, upper): |
| 833 | # method to be used by subclasses |
| 834 | |
| 835 | # should not overflow assigning lower limit |
| 836 | a = array.array(self.typecode, [lower]) |
| 837 | a[0] = lower |
| 838 | # should overflow assigning less than lower limit |
| 839 | self.assertRaises(OverflowError, array.array, self.typecode, [lower-1]) |
| 840 | self.assertRaises(OverflowError, a.__setitem__, 0, lower-1) |
| 841 | # should not overflow assigning upper limit |
| 842 | a = array.array(self.typecode, [upper]) |
| 843 | a[0] = upper |
| 844 | # should overflow assigning more than upper limit |
| 845 | self.assertRaises(OverflowError, array.array, self.typecode, [upper+1]) |
| 846 | self.assertRaises(OverflowError, a.__setitem__, 0, upper+1) |
| 847 | |
| 848 | def test_subclassing(self): |
| 849 | typecode = self.typecode |
| 850 | class ExaggeratingArray(array.array): |
| 851 | __slots__ = ['offset'] |
| 852 | |
| 853 | def __new__(cls, typecode, data, offset): |
| 854 | return array.array.__new__(cls, typecode, data) |
| 855 | |
| 856 | def __init__(self, typecode, data, offset): |
| 857 | self.offset = offset |
| 858 | |
| 859 | def __getitem__(self, i): |
| 860 | return array.array.__getitem__(self, i) + self.offset |
| 861 | |
| 862 | a = ExaggeratingArray(self.typecode, [3, 6, 7, 11], 4) |
| 863 | self.assertEntryEqual(a[0], 7) |
| 864 | |
| 865 | self.assertRaises(AttributeError, setattr, a, "color", "blue") |
| 866 | |
| 867 | class SignedNumberTest(NumberTest): |
| 868 | example = [-1, 0, 1, 42, 0x7f] |
| 869 | smallerexample = [-1, 0, 1, 42, 0x7e] |
| 870 | biggerexample = [-1, 0, 1, 43, 0x7f] |
| 871 | outside = 23 |
| 872 | |
| 873 | def test_overflow(self): |
| 874 | a = array.array(self.typecode) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 875 | lower = -1 * int(pow(2, a.itemsize * 8 - 1)) |
| 876 | upper = int(pow(2, a.itemsize * 8 - 1)) - 1 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 877 | self.check_overflow(lower, upper) |
| 878 | |
| 879 | class UnsignedNumberTest(NumberTest): |
| 880 | example = [0, 1, 17, 23, 42, 0xff] |
| 881 | smallerexample = [0, 1, 17, 23, 42, 0xfe] |
| 882 | biggerexample = [0, 1, 17, 23, 43, 0xff] |
| 883 | outside = 0xaa |
| 884 | |
| 885 | def test_overflow(self): |
| 886 | a = array.array(self.typecode) |
| 887 | lower = 0 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 888 | upper = int(pow(2, a.itemsize * 8)) - 1 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 889 | self.check_overflow(lower, upper) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 890 | |
| 891 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 892 | class ByteTest(SignedNumberTest): |
| 893 | typecode = 'b' |
| 894 | minitemsize = 1 |
| 895 | tests.append(ByteTest) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 896 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 897 | class UnsignedByteTest(UnsignedNumberTest): |
| 898 | typecode = 'B' |
| 899 | minitemsize = 1 |
| 900 | tests.append(UnsignedByteTest) |
| 901 | |
| 902 | class ShortTest(SignedNumberTest): |
| 903 | typecode = 'h' |
| 904 | minitemsize = 2 |
| 905 | tests.append(ShortTest) |
| 906 | |
| 907 | class UnsignedShortTest(UnsignedNumberTest): |
| 908 | typecode = 'H' |
| 909 | minitemsize = 2 |
| 910 | tests.append(UnsignedShortTest) |
| 911 | |
| 912 | class IntTest(SignedNumberTest): |
| 913 | typecode = 'i' |
| 914 | minitemsize = 2 |
| 915 | tests.append(IntTest) |
| 916 | |
| 917 | class UnsignedIntTest(UnsignedNumberTest): |
| 918 | typecode = 'I' |
| 919 | minitemsize = 2 |
| 920 | tests.append(UnsignedIntTest) |
| 921 | |
| 922 | class LongTest(SignedNumberTest): |
| 923 | typecode = 'l' |
| 924 | minitemsize = 4 |
| 925 | tests.append(LongTest) |
| 926 | |
| 927 | class UnsignedLongTest(UnsignedNumberTest): |
| 928 | typecode = 'L' |
| 929 | minitemsize = 4 |
| 930 | tests.append(UnsignedLongTest) |
| 931 | |
| 932 | class FPTest(NumberTest): |
| 933 | example = [-42.0, 0, 42, 1e5, -1e10] |
| 934 | smallerexample = [-42.0, 0, 42, 1e5, -2e10] |
| 935 | biggerexample = [-42.0, 0, 42, 1e5, 1e10] |
| 936 | outside = 23 |
| 937 | |
| 938 | def assertEntryEqual(self, entry1, entry2): |
| 939 | self.assertAlmostEqual(entry1, entry2) |
| 940 | |
Walter Dörwald | cf99b0a | 2003-05-22 13:15:31 +0000 | [diff] [blame] | 941 | def test_byteswap(self): |
| 942 | a = array.array(self.typecode, self.example) |
| 943 | self.assertRaises(TypeError, a.byteswap, 42) |
| 944 | if a.itemsize in (1, 2, 4, 8): |
| 945 | b = array.array(self.typecode, self.example) |
| 946 | b.byteswap() |
| 947 | if a.itemsize==1: |
| 948 | self.assertEqual(a, b) |
| 949 | else: |
| 950 | # On alphas treating the byte swapped bit patters as |
| 951 | # floats/doubles results in floating point exceptions |
| 952 | # => compare the 8bit string values instead |
| 953 | self.assertNotEqual(a.tostring(), b.tostring()) |
| 954 | b.byteswap() |
| 955 | self.assertEqual(a, b) |
| 956 | |
Neal Norwitz | 35c6cd0 | 2003-05-22 13:29:15 +0000 | [diff] [blame] | 957 | class FloatTest(FPTest): |
| 958 | typecode = 'f' |
| 959 | minitemsize = 4 |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 960 | tests.append(FloatTest) |
| 961 | |
| 962 | class DoubleTest(FPTest): |
| 963 | typecode = 'd' |
| 964 | minitemsize = 8 |
| 965 | tests.append(DoubleTest) |
| 966 | |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 967 | def test_main(verbose=None): |
| 968 | import sys |
| 969 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 970 | support.run_unittest(*tests) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 971 | |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 972 | # verify reference counting |
| 973 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 974 | import gc |
| 975 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 976 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 977 | support.run_unittest(*tests) |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 978 | gc.collect() |
| 979 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 980 | print(counts) |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 981 | |
| 982 | if __name__ == "__main__": |
| 983 | test_main(verbose=True) |