0001 #! /usr/bin/env python 0002 """Test the arraymodule. 0003 Roger E. Masse 0004 """ 0005 0006 import unittest 0007 from test import test_support 0008 from weakref import proxy 0009 import array, cStringIO, math 0010 0011 tests = [] # list to accumulate all tests 0012 typecodes = "cubBhHiIlLfd" 0013 0014 class BadConstructorTest(unittest.TestCase): 0015 0016 def test_constructor(self): 0017 self.assertRaises(TypeError, array.array) 0018 self.assertRaises(TypeError, array.array, spam=42) 0019 self.assertRaises(TypeError, array.array, 'xx') 0020 self.assertRaises(ValueError, array.array, 'x') 0021 0022 tests.append(BadConstructorTest) 0023 0024 class BaseTest(unittest.TestCase): 0025 # Required class attributes (provided by subclasses 0026 # typecode: the typecode to test 0027 # example: an initializer usable in the constructor for this type 0028 # smallerexample: the same length as example, but smaller 0029 # biggerexample: the same length as example, but bigger 0030 # outside: An entry that is not in example 0031 # minitemsize: the minimum guaranteed itemsize 0032 0033 def assertEntryEqual(self, entry1, entry2): 0034 self.assertEqual(entry1, entry2) 0035 0036 def badtypecode(self): 0037 # Return a typecode that is different from our own 0038 return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] 0039 0040 def test_constructor(self): 0041 a = array.array(self.typecode) 0042 self.assertEqual(a.typecode, self.typecode) 0043 self.assert_(a.itemsize>=self.minitemsize) 0044 self.assertRaises(TypeError, array.array, self.typecode, None) 0045 0046 def test_len(self): 0047 a = array.array(self.typecode) 0048 a.append(self.example[0]) 0049 self.assertEqual(len(a), 1) 0050 0051 a = array.array(self.typecode, self.example) 0052 self.assertEqual(len(a), len(self.example)) 0053 0054 def test_buffer_info(self): 0055 a = array.array(self.typecode, self.example) 0056 self.assertRaises(TypeError, a.buffer_info, 42) 0057 bi = a.buffer_info() 0058 self.assert_(isinstance(bi, tuple)) 0059 self.assertEqual(len(bi), 2) 0060 self.assert_(isinstance(bi[0], int)) 0061 self.assert_(isinstance(bi[1], int)) 0062 self.assertEqual(bi[1], len(a)) 0063 0064 def test_byteswap(self): 0065 a = array.array(self.typecode, self.example) 0066 self.assertRaises(TypeError, a.byteswap, 42) 0067 if a.itemsize in (1, 2, 4, 8): 0068 b = array.array(self.typecode, self.example) 0069 b.byteswap() 0070 if a.itemsize==1: 0071 self.assertEqual(a, b) 0072 else: 0073 self.assertNotEqual(a, b) 0074 b.byteswap() 0075 self.assertEqual(a, b) 0076 0077 def test_copy(self): 0078 import copy 0079 a = array.array(self.typecode, self.example) 0080 b = copy.copy(a) 0081 self.assertNotEqual(id(a), id(b)) 0082 self.assertEqual(a, b) 0083 0084 def test_insert(self): 0085 a = array.array(self.typecode, self.example) 0086 a.insert(0, self.example[0]) 0087 self.assertEqual(len(a), 1+len(self.example)) 0088 self.assertEqual(a[0], a[1]) 0089 self.assertRaises(TypeError, a.insert) 0090 self.assertRaises(TypeError, a.insert, None) 0091 self.assertRaises(TypeError, a.insert, 0, None) 0092 0093 a = array.array(self.typecode, self.example) 0094 a.insert(-1, self.example[0]) 0095 self.assertEqual( 0096 a, 0097 array.array( 0098 self.typecode, 0099 self.example[:-1] + self.example[:1] + self.example[-1:] 0100 ) 0101 ) 0102 0103 a = array.array(self.typecode, self.example) 0104 a.insert(-1000, self.example[0]) 0105 self.assertEqual( 0106 a, 0107 array.array(self.typecode, self.example[:1] + self.example) 0108 ) 0109 0110 a = array.array(self.typecode, self.example) 0111 a.insert(1000, self.example[0]) 0112 self.assertEqual( 0113 a, 0114 array.array(self.typecode, self.example + self.example[:1]) 0115 ) 0116 0117 def test_tofromfile(self): 0118 a = array.array(self.typecode, 2*self.example) 0119 self.assertRaises(TypeError, a.tofile) 0120 self.assertRaises(TypeError, a.tofile, cStringIO.StringIO()) 0121 f = open(test_support.TESTFN, 'wb') 0122 try: 0123 a.tofile(f) 0124 f.close() 0125 b = array.array(self.typecode) 0126 f = open(test_support.TESTFN, 'rb') 0127 self.assertRaises(TypeError, b.fromfile) 0128 self.assertRaises( 0129 TypeError, 0130 b.fromfile, 0131 cStringIO.StringIO(), len(self.example) 0132 ) 0133 b.fromfile(f, len(self.example)) 0134 self.assertEqual(b, array.array(self.typecode, self.example)) 0135 self.assertNotEqual(a, b) 0136 b.fromfile(f, len(self.example)) 0137 self.assertEqual(a, b) 0138 self.assertRaises(EOFError, b.fromfile, f, 1) 0139 f.close() 0140 finally: 0141 if not f.closed: 0142 f.close() 0143 test_support.unlink(test_support.TESTFN) 0144 0145 def test_tofromlist(self): 0146 a = array.array(self.typecode, 2*self.example) 0147 b = array.array(self.typecode) 0148 self.assertRaises(TypeError, a.tolist, 42) 0149 self.assertRaises(TypeError, b.fromlist) 0150 self.assertRaises(TypeError, b.fromlist, 42) 0151 self.assertRaises(TypeError, b.fromlist, [None]) 0152 b.fromlist(a.tolist()) 0153 self.assertEqual(a, b) 0154 0155 def test_tofromstring(self): 0156 a = array.array(self.typecode, 2*self.example) 0157 b = array.array(self.typecode) 0158 self.assertRaises(TypeError, a.tostring, 42) 0159 self.assertRaises(TypeError, b.fromstring) 0160 self.assertRaises(TypeError, b.fromstring, 42) 0161 b.fromstring(a.tostring()) 0162 self.assertEqual(a, b) 0163 if a.itemsize>1: 0164 self.assertRaises(ValueError, b.fromstring, "x") 0165 0166 def test_repr(self): 0167 a = array.array(self.typecode, 2*self.example) 0168 self.assertEqual(a, eval(repr(a), {"array": array.array})) 0169 0170 a = array.array(self.typecode) 0171 self.assertEqual(repr(a), "array('%s')" % self.typecode) 0172 0173 def test_str(self): 0174 a = array.array(self.typecode, 2*self.example) 0175 str(a) 0176 0177 def test_cmp(self): 0178 a = array.array(self.typecode, self.example) 0179 self.assert_((a == 42) is False) 0180 self.assert_((a != 42) is True) 0181 0182 self.assert_((a == a) is True) 0183 self.assert_((a != a) is False) 0184 self.assert_((a < a) is False) 0185 self.assert_((a <= a) is True) 0186 self.assert_((a > a) is False) 0187 self.assert_((a >= a) is True) 0188 0189 as = array.array(self.typecode, self.smallerexample) 0190 ab = array.array(self.typecode, self.biggerexample) 0191 0192 self.assert_((a == 2*a) is False) 0193 self.assert_((a != 2*a) is True) 0194 self.assert_((a < 2*a) is True) 0195 self.assert_((a <= 2*a) is True) 0196 self.assert_((a > 2*a) is False) 0197 self.assert_((a >= 2*a) is False) 0198 0199 self.assert_((a == as) is False) 0200 self.assert_((a != as) is True) 0201 self.assert_((a < as) is False) 0202 self.assert_((a <= as) is False) 0203 self.assert_((a > as) is True) 0204 self.assert_((a >= as) is True) 0205 0206 self.assert_((a == ab) is False) 0207 self.assert_((a != ab) is True) 0208 self.assert_((a < ab) is True) 0209 self.assert_((a <= ab) is True) 0210 self.assert_((a > ab) is False) 0211 self.assert_((a >= ab) is False) 0212 0213 def test_add(self): 0214 a = array.array(self.typecode, self.example) \ 0215 + array.array(self.typecode, self.example[::-1]) 0216 self.assertEqual( 0217 a, 0218 array.array(self.typecode, self.example + self.example[::-1]) 0219 ) 0220 0221 b = array.array(self.badtypecode()) 0222 self.assertRaises(TypeError, a.__add__, b) 0223 0224 self.assertRaises(TypeError, a.__add__, "bad") 0225 0226 def test_iadd(self): 0227 a = array.array(self.typecode, self.example[::-1]) 0228 b = a 0229 a += array.array(self.typecode, 2*self.example) 0230 self.assert_(a is b) 0231 self.assertEqual( 0232 a, 0233 array.array(self.typecode, self.example[::-1]+2*self.example) 0234 ) 0235 0236 b = array.array(self.badtypecode()) 0237 self.assertRaises(TypeError, a.__add__, b) 0238 0239 self.assertRaises(TypeError, a.__iadd__, "bad") 0240 0241 def test_mul(self): 0242 a = 5*array.array(self.typecode, self.example) 0243 self.assertEqual( 0244 a, 0245 array.array(self.typecode, 5*self.example) 0246 ) 0247 0248 a = array.array(self.typecode, self.example)*5 0249 self.assertEqual( 0250 a, 0251 array.array(self.typecode, self.example*5) 0252 ) 0253 0254 a = 0*array.array(self.typecode, self.example) 0255 self.assertEqual( 0256 a, 0257 array.array(self.typecode) 0258 ) 0259 0260 a = (-1)*array.array(self.typecode, self.example) 0261 self.assertEqual( 0262 a, 0263 array.array(self.typecode) 0264 ) 0265 0266 self.assertRaises(TypeError, a.__mul__, "bad") 0267 0268 def test_imul(self): 0269 a = array.array(self.typecode, self.example) 0270 b = a 0271 0272 a *= 5 0273 self.assert_(a is b) 0274 self.assertEqual( 0275 a, 0276 array.array(self.typecode, 5*self.example) 0277 ) 0278 0279 a *= 0 0280 self.assert_(a is b) 0281 self.assertEqual(a, array.array(self.typecode)) 0282 0283 a *= 1000 0284 self.assert_(a is b) 0285 self.assertEqual(a, array.array(self.typecode)) 0286 0287 a *= -1 0288 self.assert_(a is b) 0289 self.assertEqual(a, array.array(self.typecode)) 0290 0291 a = array.array(self.typecode, self.example) 0292 a *= -1 0293 self.assertEqual(a, array.array(self.typecode)) 0294 0295 self.assertRaises(TypeError, a.__imul__, "bad") 0296 0297 def test_getitem(self): 0298 a = array.array(self.typecode, self.example) 0299 self.assertEntryEqual(a[0], self.example[0]) 0300 self.assertEntryEqual(a[0L], self.example[0]) 0301 self.assertEntryEqual(a[-1], self.example[-1]) 0302 self.assertEntryEqual(a[-1L], self.example[-1]) 0303 self.assertEntryEqual(a[len(self.example)-1], self.example[-1]) 0304 self.assertEntryEqual(a[-len(self.example)], self.example[0]) 0305 self.assertRaises(TypeError, a.__getitem__) 0306 self.assertRaises(IndexError, a.__getitem__, len(self.example)) 0307 self.assertRaises(IndexError, a.__getitem__, -len(self.example)-1) 0308 0309 def test_setitem(self): 0310 a = array.array(self.typecode, self.example) 0311 a[0] = a[-1] 0312 self.assertEntryEqual(a[0], a[-1]) 0313 0314 a = array.array(self.typecode, self.example) 0315 a[0L] = a[-1] 0316 self.assertEntryEqual(a[0], a[-1]) 0317 0318 a = array.array(self.typecode, self.example) 0319 a[-1] = a[0] 0320 self.assertEntryEqual(a[0], a[-1]) 0321 0322 a = array.array(self.typecode, self.example) 0323 a[-1L] = a[0] 0324 self.assertEntryEqual(a[0], a[-1]) 0325 0326 a = array.array(self.typecode, self.example) 0327 a[len(self.example)-1] = a[0] 0328 self.assertEntryEqual(a[0], a[-1]) 0329 0330 a = array.array(self.typecode, self.example) 0331 a[-len(self.example)] = a[-1] 0332 self.assertEntryEqual(a[0], a[-1]) 0333 0334 self.assertRaises(TypeError, a.__setitem__) 0335 self.assertRaises(TypeError, a.__setitem__, None) 0336 self.assertRaises(TypeError, a.__setitem__, 0, None) 0337 self.assertRaises( 0338 IndexError, 0339 a.__setitem__, 0340 len(self.example), self.example[0] 0341 ) 0342 self.assertRaises( 0343 IndexError, 0344 a.__setitem__, 0345 -len(self.example)-1, self.example[0] 0346 ) 0347 0348 def test_delitem(self): 0349 a = array.array(self.typecode, self.example) 0350 del a[0] 0351 self.assertEqual( 0352 a, 0353 array.array(self.typecode, self.example[1:]) 0354 ) 0355 0356 a = array.array(self.typecode, self.example) 0357 del a[-1] 0358 self.assertEqual( 0359 a, 0360 array.array(self.typecode, self.example[:-1]) 0361 ) 0362 0363 a = array.array(self.typecode, self.example) 0364 del a[len(self.example)-1] 0365 self.assertEqual( 0366 a, 0367 array.array(self.typecode, self.example[:-1]) 0368 ) 0369 0370 a = array.array(self.typecode, self.example) 0371 del a[-len(self.example)] 0372 self.assertEqual( 0373 a, 0374 array.array(self.typecode, self.example[1:]) 0375 ) 0376 0377 self.assertRaises(TypeError, a.__delitem__) 0378 self.assertRaises(TypeError, a.__delitem__, None) 0379 self.assertRaises(IndexError, a.__delitem__, len(self.example)) 0380 self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1) 0381 0382 def test_getslice(self): 0383 a = array.array(self.typecode, self.example) 0384 self.assertEqual(a[:], a) 0385 0386 self.assertEqual( 0387 a[1:], 0388 array.array(self.typecode, self.example[1:]) 0389 ) 0390 0391 self.assertEqual( 0392 a[:1], 0393 array.array(self.typecode, self.example[:1]) 0394 ) 0395 0396 self.assertEqual( 0397 a[:-1], 0398 array.array(self.typecode, self.example[:-1]) 0399 ) 0400 0401 self.assertEqual( 0402 a[-1:], 0403 array.array(self.typecode, self.example[-1:]) 0404 ) 0405 0406 self.assertEqual( 0407 a[-1:-1], 0408 array.array(self.typecode) 0409 ) 0410 0411 self.assertEqual( 0412 a[1000:], 0413 array.array(self.typecode) 0414 ) 0415 self.assertEqual(a[-1000:], a) 0416 self.assertEqual(a[:1000], a) 0417 self.assertEqual( 0418 a[:-1000], 0419 array.array(self.typecode) 0420 ) 0421 self.assertEqual(a[-1000:1000], a) 0422 self.assertEqual( 0423 a[2000:1000], 0424 array.array(self.typecode) 0425 ) 0426 0427 def test_setslice(self): 0428 a = array.array(self.typecode, self.example) 0429 a[:1] = a 0430 self.assertEqual( 0431 a, 0432 array.array(self.typecode, self.example + self.example[1:]) 0433 ) 0434 0435 a = array.array(self.typecode, self.example) 0436 a[:-1] = a 0437 self.assertEqual( 0438 a, 0439 array.array(self.typecode, self.example + self.example[-1:]) 0440 ) 0441 0442 a = array.array(self.typecode, self.example) 0443 a[-1:] = a 0444 self.assertEqual( 0445 a, 0446 array.array(self.typecode, self.example[:-1] + self.example) 0447 ) 0448 0449 a = array.array(self.typecode, self.example) 0450 a[1:] = a 0451 self.assertEqual( 0452 a, 0453 array.array(self.typecode, self.example[:1] + self.example) 0454 ) 0455 0456 a = array.array(self.typecode, self.example) 0457 a[1:-1] = a 0458 self.assertEqual( 0459 a, 0460 array.array( 0461 self.typecode, 0462 self.example[:1] + self.example + self.example[-1:] 0463 ) 0464 ) 0465 0466 a = array.array(self.typecode, self.example) 0467 a[1000:] = a 0468 self.assertEqual( 0469 a, 0470 array.array(self.typecode, 2*self.example) 0471 ) 0472 0473 a = array.array(self.typecode, self.example) 0474 a[-1000:] = a 0475 self.assertEqual( 0476 a, 0477 array.array(self.typecode, self.example) 0478 ) 0479 0480 a = array.array(self.typecode, self.example) 0481 a[:1000] = a 0482 self.assertEqual( 0483 a, 0484 array.array(self.typecode, self.example) 0485 ) 0486 0487 a = array.array(self.typecode, self.example) 0488 a[:-1000] = a 0489 self.assertEqual( 0490 a, 0491 array.array(self.typecode, 2*self.example) 0492 ) 0493 0494 a = array.array(self.typecode, self.example) 0495 a[1:0] = a 0496 self.assertEqual( 0497 a, 0498 array.array(self.typecode, self.example[:1] + self.example + self.example[1:]) 0499 ) 0500 0501 a = array.array(self.typecode, self.example) 0502 a[2000:1000] = a 0503 self.assertEqual( 0504 a, 0505 array.array(self.typecode, 2*self.example) 0506 ) 0507 0508 a = array.array(self.typecode, self.example) 0509 self.assertRaises(TypeError, a.__setslice__, 0, 0, None) 0510 self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None) 0511 0512 b = array.array(self.badtypecode()) 0513 self.assertRaises(TypeError, a.__setslice__, 0, 0, b) 0514 self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b) 0515 0516 def test_index(self): 0517 example = 2*self.example 0518 a = array.array(self.typecode, example) 0519 self.assertRaises(TypeError, a.index) 0520 for x in example: 0521 self.assertEqual(a.index(x), example.index(x)) 0522 self.assertRaises(ValueError, a.index, None) 0523 self.assertRaises(ValueError, a.index, self.outside) 0524 0525 def test_count(self): 0526 example = 2*self.example 0527 a = array.array(self.typecode, example) 0528 self.assertRaises(TypeError, a.count) 0529 for x in example: 0530 self.assertEqual(a.count(x), example.count(x)) 0531 self.assertEqual(a.count(self.outside), 0) 0532 self.assertEqual(a.count(None), 0) 0533 0534 def test_remove(self): 0535 for x in self.example: 0536 example = 2*self.example 0537 a = array.array(self.typecode, example) 0538 pos = example.index(x) 0539 example2 = example[:pos] + example[pos+1:] 0540 a.remove(x) 0541 self.assertEqual(a, array.array(self.typecode, example2)) 0542 0543 a = array.array(self.typecode, self.example) 0544 self.assertRaises(ValueError, a.remove, self.outside) 0545 0546 self.assertRaises(ValueError, a.remove, None) 0547 0548 def test_pop(self): 0549 a = array.array(self.typecode) 0550 self.assertRaises(IndexError, a.pop) 0551 0552 a = array.array(self.typecode, 2*self.example) 0553 self.assertRaises(TypeError, a.pop, 42, 42) 0554 self.assertRaises(TypeError, a.pop, None) 0555 self.assertRaises(IndexError, a.pop, len(a)) 0556 self.assertRaises(IndexError, a.pop, -len(a)-1) 0557 0558 self.assertEntryEqual(a.pop(0), self.example[0]) 0559 self.assertEqual( 0560 a, 0561 array.array(self.typecode, self.example[1:]+self.example) 0562 ) 0563 self.assertEntryEqual(a.pop(1), self.example[2]) 0564 self.assertEqual( 0565 a, 0566 array.array(self.typecode, self.example[1:2]+self.example[3:]+self.example) 0567 ) 0568 self.assertEntryEqual(a.pop(0), self.example[1]) 0569 self.assertEntryEqual(a.pop(), self.example[-1]) 0570 self.assertEqual( 0571 a, 0572 array.array(self.typecode, self.example[3:]+self.example[:-1]) 0573 ) 0574 0575 def test_reverse(self): 0576 a = array.array(self.typecode, self.example) 0577 self.assertRaises(TypeError, a.reverse, 42) 0578 a.reverse() 0579 self.assertEqual( 0580 a, 0581 array.array(self.typecode, self.example[::-1]) 0582 ) 0583 0584 def test_extend(self): 0585 a = array.array(self.typecode, self.example) 0586 self.assertRaises(TypeError, a.extend) 0587 a.extend(array.array(self.typecode, self.example[::-1])) 0588 self.assertEqual( 0589 a, 0590 array.array(self.typecode, self.example+self.example[::-1]) 0591 ) 0592 0593 b = array.array(self.badtypecode()) 0594 self.assertRaises(TypeError, a.extend, b) 0595 0596 a = array.array(self.typecode, self.example) 0597 a.extend(self.example[::-1]) 0598 self.assertEqual( 0599 a, 0600 array.array(self.typecode, self.example+self.example[::-1]) 0601 ) 0602 0603 def test_constructor_with_iterable_argument(self): 0604 a = array.array(self.typecode, iter(self.example)) 0605 b = array.array(self.typecode, self.example) 0606 self.assertEqual(a, b) 0607 0608 # non-iterable argument 0609 self.assertRaises(TypeError, array.array, self.typecode, 10) 0610 0611 # pass through errors raised in __iter__ 0612 class A: 0613 def __iter__(self): 0614 raise UnicodeError 0615 self.assertRaises(UnicodeError, array.array, self.typecode, A()) 0616 0617 # pass through errors raised in next() 0618 def B(): 0619 raise UnicodeError 0620 yield None 0621 self.assertRaises(UnicodeError, array.array, self.typecode, B()) 0622 0623 def test_coveritertraverse(self): 0624 try: 0625 import gc 0626 except ImportError: 0627 return 0628 a = array.array(self.typecode) 0629 l = [iter(a)] 0630 l.append(l) 0631 gc.collect() 0632 0633 def test_buffer(self): 0634 a = array.array(self.typecode, self.example) 0635 b = buffer(a) 0636 self.assertEqual(b[0], a.tostring()[0]) 0637 0638 def test_weakref(self): 0639 s = array.array(self.typecode, self.example) 0640 p = proxy(s) 0641 self.assertEqual(p.tostring(), s.tostring()) 0642 s = None 0643 self.assertRaises(ReferenceError, len, p) 0644 0645 def test_bug_782369(self): 0646 import sys 0647 if hasattr(sys, "getrefcount"): 0648 for i in range(10): 0649 b = array.array('B', range(64)) 0650 rc = sys.getrefcount(10) 0651 for i in range(10): 0652 b = array.array('B', range(64)) 0653 self.assertEqual(rc, sys.getrefcount(10)) 0654 0655 0656 0657 class StringTest(BaseTest): 0658 0659 def test_setitem(self): 0660 super(StringTest, self).test_setitem() 0661 a = array.array(self.typecode, self.example) 0662 self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) 0663 0664 class CharacterTest(StringTest): 0665 typecode = 'c' 0666 example = '\x01azAZ\x00\xfe' 0667 smallerexample = '\x01azAY\x00\xfe' 0668 biggerexample = '\x01azAZ\x00\xff' 0669 outside = '\x33' 0670 minitemsize = 1 0671 0672 def test_subbclassing(self): 0673 class EditableString(array.array): 0674 def __new__(cls, s, *args, **kwargs): 0675 return array.array.__new__(cls, 'c', s) 0676 0677 def __init__(self, s, color='blue'): 0678 array.array.__init__(self, 'c', s) 0679 self.color = color 0680 0681 def strip(self): 0682 self[:] = array.array('c', self.tostring().strip()) 0683 0684 def __repr__(self): 0685 return 'EditableString(%r)' % self.tostring() 0686 0687 s = EditableString("\ttest\r\n") 0688 s.strip() 0689 self.assertEqual(s.tostring(), "test") 0690 0691 self.assertEqual(s.color, "blue") 0692 s.color = "red" 0693 self.assertEqual(s.color, "red") 0694 self.assertEqual(s.__dict__.keys(), ["color"]) 0695 0696 def test_nounicode(self): 0697 a = array.array(self.typecode, self.example) 0698 self.assertRaises(ValueError, a.fromunicode, unicode('')) 0699 self.assertRaises(ValueError, a.tounicode) 0700 0701 tests.append(CharacterTest) 0702 0703 if test_support.have_unicode: 0704 class UnicodeTest(StringTest): 0705 typecode = 'u' 0706 example = unicode(r'\x01\u263a\x00\ufeff', 'unicode-escape') 0707 smallerexample = unicode(r'\x01\u263a\x00\ufefe', 'unicode-escape') 0708 biggerexample = unicode(r'\x01\u263a\x01\ufeff', 'unicode-escape') 0709 outside = unicode('\x33') 0710 minitemsize = 2 0711 0712 def test_unicode(self): 0713 self.assertRaises(TypeError, array.array, 'b', unicode('foo', 'ascii')) 0714 0715 a = array.array('u', unicode(r'\xa0\xc2\u1234', 'unicode-escape')) 0716 a.fromunicode(unicode(' ', 'ascii')) 0717 a.fromunicode(unicode('', 'ascii')) 0718 a.fromunicode(unicode('', 'ascii')) 0719 a.fromunicode(unicode(r'\x11abc\xff\u1234', 'unicode-escape')) 0720 s = a.tounicode() 0721 self.assertEqual( 0722 s, 0723 unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape') 0724 ) 0725 0726 s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape') 0727 a = array.array('u', s) 0728 self.assertEqual( 0729 repr(a), 0730 r"""array('u', u'\x00="\'a\\b\x80\xff\x00\x01\u1234')""" 0731 ) 0732 0733 self.assertRaises(TypeError, a.fromunicode) 0734 0735 tests.append(UnicodeTest) 0736 0737 class NumberTest(BaseTest): 0738 0739 def test_extslice(self): 0740 a = array.array(self.typecode, range(5)) 0741 self.assertEqual(a[::], a) 0742 self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) 0743 self.assertEqual(a[1::2], array.array(self.typecode, [1,3])) 0744 self.assertEqual(a[::-1], array.array(self.typecode, [4,3,2,1,0])) 0745 self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0])) 0746 self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) 0747 self.assertEqual(a[-100:100:], a) 0748 self.assertEqual(a[100:-100:-1], a[::-1]) 0749 self.assertEqual(a[-100L:100L:2L], array.array(self.typecode, [0,2,4])) 0750 self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) 0751 self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) 0752 0753 def test_delslice(self): 0754 a = array.array(self.typecode, range(5)) 0755 del a[::2] 0756 self.assertEqual(a, array.array(self.typecode, [1,3])) 0757 a = array.array(self.typecode, range(5)) 0758 del a[1::2] 0759 self.assertEqual(a, array.array(self.typecode, [0,2,4])) 0760 a = array.array(self.typecode, range(5)) 0761 del a[1::-2] 0762 self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) 0763 a = array.array(self.typecode, range(10)) 0764 del a[::1000] 0765 self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) 0766 0767 def test_assignment(self): 0768 a = array.array(self.typecode, range(10)) 0769 a[::2] = array.array(self.typecode, [42]*5) 0770 self.assertEqual(a, array.array(self.typecode, [42, 1, 42, 3, 42, 5, 42, 7, 42, 9])) 0771 a = array.array(self.typecode, range(10)) 0772 a[::-4] = array.array(self.typecode, [10]*3) 0773 self.assertEqual(a, array.array(self.typecode, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) 0774 a = array.array(self.typecode, range(4)) 0775 a[::-1] = a 0776 self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) 0777 a = array.array(self.typecode, range(10)) 0778 b = a[:] 0779 c = a[:] 0780 ins = array.array(self.typecode, range(2)) 0781 a[2:3] = ins 0782 b[slice(2,3)] = ins 0783 c[2:3:] = ins 0784 0785 def test_iterationcontains(self): 0786 a = array.array(self.typecode, range(10)) 0787 self.assertEqual(list(a), range(10)) 0788 b = array.array(self.typecode, [20]) 0789 self.assertEqual(a[-1] in a, True) 0790 self.assertEqual(b[0] not in a, True) 0791 0792 def check_overflow(self, lower, upper): 0793 # method to be used by subclasses 0794 0795 # should not overflow assigning lower limit 0796 a = array.array(self.typecode, [lower]) 0797 a[0] = lower 0798 # should overflow assigning less than lower limit 0799 self.assertRaises(OverflowError, array.array, self.typecode, [lower-1]) 0800 self.assertRaises(OverflowError, a.__setitem__, 0, lower-1) 0801 # should not overflow assigning upper limit 0802 a = array.array(self.typecode, [upper]) 0803 a[0] = upper 0804 # should overflow assigning more than upper limit 0805 self.assertRaises(OverflowError, array.array, self.typecode, [upper+1]) 0806 self.assertRaises(OverflowError, a.__setitem__, 0, upper+1) 0807 0808 def test_subclassing(self): 0809 typecode = self.typecode 0810 class ExaggeratingArray(array.array): 0811 __slots__ = ['offset'] 0812 0813 def __new__(cls, typecode, data, offset): 0814 return array.array.__new__(cls, typecode, data) 0815 0816 def __init__(self, typecode, data, offset): 0817 self.offset = offset 0818 0819 def __getitem__(self, i): 0820 return array.array.__getitem__(self, i) + self.offset 0821 0822 a = ExaggeratingArray(self.typecode, [3, 6, 7, 11], 4) 0823 self.assertEntryEqual(a[0], 7) 0824 0825 self.assertRaises(AttributeError, setattr, a, "color", "blue") 0826 0827 class SignedNumberTest(NumberTest): 0828 example = [-1, 0, 1, 42, 0x7f] 0829 smallerexample = [-1, 0, 1, 42, 0x7e] 0830 biggerexample = [-1, 0, 1, 43, 0x7f] 0831 outside = 23 0832 0833 def test_overflow(self): 0834 a = array.array(self.typecode) 0835 lower = -1 * long(pow(2, a.itemsize * 8 - 1)) 0836 upper = long(pow(2, a.itemsize * 8 - 1)) - 1L 0837 self.check_overflow(lower, upper) 0838 0839 class UnsignedNumberTest(NumberTest): 0840 example = [0, 1, 17, 23, 42, 0xff] 0841 smallerexample = [0, 1, 17, 23, 42, 0xfe] 0842 biggerexample = [0, 1, 17, 23, 43, 0xff] 0843 outside = 0xaa 0844 0845 def test_overflow(self): 0846 a = array.array(self.typecode) 0847 lower = 0 0848 upper = long(pow(2, a.itemsize * 8)) - 1L 0849 self.check_overflow(lower, upper) 0850 0851 0852 class ByteTest(SignedNumberTest): 0853 typecode = 'b' 0854 minitemsize = 1 0855 tests.append(ByteTest) 0856 0857 class UnsignedByteTest(UnsignedNumberTest): 0858 typecode = 'B' 0859 minitemsize = 1 0860 tests.append(UnsignedByteTest) 0861 0862 class ShortTest(SignedNumberTest): 0863 typecode = 'h' 0864 minitemsize = 2 0865 tests.append(ShortTest) 0866 0867 class UnsignedShortTest(UnsignedNumberTest): 0868 typecode = 'H' 0869 minitemsize = 2 0870 tests.append(UnsignedShortTest) 0871 0872 class IntTest(SignedNumberTest): 0873 typecode = 'i' 0874 minitemsize = 2 0875 tests.append(IntTest) 0876 0877 class UnsignedIntTest(UnsignedNumberTest): 0878 typecode = 'I' 0879 minitemsize = 2 0880 tests.append(UnsignedIntTest) 0881 0882 class LongTest(SignedNumberTest): 0883 typecode = 'l' 0884 minitemsize = 4 0885 tests.append(LongTest) 0886 0887 class UnsignedLongTest(UnsignedNumberTest): 0888 typecode = 'L' 0889 minitemsize = 4 0890 tests.append(UnsignedLongTest) 0891 0892 class FPTest(NumberTest): 0893 example = [-42.0, 0, 42, 1e5, -1e10] 0894 smallerexample = [-42.0, 0, 42, 1e5, -2e10] 0895 biggerexample = [-42.0, 0, 42, 1e5, 1e10] 0896 outside = 23 0897 0898 def assertEntryEqual(self, entry1, entry2): 0899 self.assertAlmostEqual(entry1, entry2) 0900 0901 def test_byteswap(self): 0902 a = array.array(self.typecode, self.example) 0903 self.assertRaises(TypeError, a.byteswap, 42) 0904 if a.itemsize in (1, 2, 4, 8): 0905 b = array.array(self.typecode, self.example) 0906 b.byteswap() 0907 if a.itemsize==1: 0908 self.assertEqual(a, b) 0909 else: 0910 # On alphas treating the byte swapped bit patters as 0911 # floats/doubles results in floating point exceptions 0912 # => compare the 8bit string values instead 0913 self.assertNotEqual(a.tostring(), b.tostring()) 0914 b.byteswap() 0915 self.assertEqual(a, b) 0916 0917 class FloatTest(FPTest): 0918 typecode = 'f' 0919 minitemsize = 4 0920 tests.append(FloatTest) 0921 0922 class DoubleTest(FPTest): 0923 typecode = 'd' 0924 minitemsize = 8 0925 tests.append(DoubleTest) 0926 0927 def test_main(verbose=None): 0928 import sys 0929 0930 test_support.run_unittest(*tests) 0931 0932 # verify reference counting 0933 if verbose and hasattr(sys, "gettotalrefcount"): 0934 import gc 0935 counts = [None] * 5 0936 for i in xrange(len(counts)): 0937 test_support.run_unittest(*tests) 0938 gc.collect() 0939 counts[i] = sys.gettotalrefcount() 0940 print counts 0941 0942 if __name__ == "__main__": 0943 test_main(verbose=True) 0944
Generated by PyXR 0.9.4