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