diff options
Diffstat (limited to 'Lib/test/test_array.py')
| -rwxr-xr-x | Lib/test/test_array.py | 79 |
1 files changed, 73 insertions, 6 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 597f3b2..01c2209 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -6,8 +6,8 @@ import unittest from test import test_support from weakref import proxy -import array, cStringIO, math -from cPickle import loads, dumps +import array, cStringIO +from cPickle import loads, dumps, HIGHEST_PROTOCOL class ArraySubclass(array.array): pass @@ -97,7 +97,7 @@ class BaseTest(unittest.TestCase): self.assertEqual(a, b) def test_pickle(self): - for protocol in (0, 1, 2): + for protocol in range(HIGHEST_PROTOCOL + 1): a = array.array(self.typecode, self.example) b = loads(dumps(a, protocol)) self.assertNotEqual(id(a), id(b)) @@ -112,7 +112,7 @@ class BaseTest(unittest.TestCase): self.assertEqual(type(a), type(b)) def test_pickle_for_empty_array(self): - for protocol in (0, 1, 2): + for protocol in range(HIGHEST_PROTOCOL + 1): a = array.array(self.typecode) b = loads(dumps(a, protocol)) self.assertNotEqual(id(a), id(b)) @@ -163,6 +163,7 @@ class BaseTest(unittest.TestCase): a = array.array(self.typecode, 2*self.example) self.assertRaises(TypeError, a.tofile) self.assertRaises(TypeError, a.tofile, cStringIO.StringIO()) + test_support.unlink(test_support.TESTFN) f = open(test_support.TESTFN, 'wb') try: a.tofile(f) @@ -187,6 +188,17 @@ class BaseTest(unittest.TestCase): f.close() test_support.unlink(test_support.TESTFN) + def test_fromfile_ioerror(self): + # Issue #5395: Check if fromfile raises a proper IOError + # instead of EOFError. + a = array.array(self.typecode) + f = open(test_support.TESTFN, 'wb') + try: + self.assertRaises(IOError, a.fromfile, f, len(self.example)) + finally: + f.close() + test_support.unlink(test_support.TESTFN) + def test_tofromlist(self): a = array.array(self.typecode, 2*self.example) b = array.array(self.typecode) @@ -474,6 +486,18 @@ class BaseTest(unittest.TestCase): array.array(self.typecode) ) + def test_extended_getslice(self): + # Test extended slicing by comparing with list slicing + # (Assumes list conversion works correctly, too) + a = array.array(self.typecode, self.example) + indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) + for start in indices: + for stop in indices: + # Everything except the initial 0 (invalid step) + for step in indices[1:]: + self.assertEqual(list(a[start:stop:step]), + list(a)[start:stop:step]) + def test_setslice(self): a = array.array(self.typecode, self.example) a[:1] = a @@ -557,12 +581,34 @@ class BaseTest(unittest.TestCase): a = array.array(self.typecode, self.example) self.assertRaises(TypeError, a.__setslice__, 0, 0, None) + self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None) self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None) b = array.array(self.badtypecode()) self.assertRaises(TypeError, a.__setslice__, 0, 0, b) + self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b) self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b) + def test_extended_set_del_slice(self): + indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) + for start in indices: + for stop in indices: + # Everything except the initial 0 (invalid step) + for step in indices[1:]: + a = array.array(self.typecode, self.example) + L = list(a) + # Make sure we have a slice of exactly the right length, + # but with (hopefully) different data. + data = L[start:stop:step] + data.reverse() + L[start:stop:step] = data + a[start:stop:step] = array.array(self.typecode, data) + self.assertEquals(a, array.array(self.typecode, L)) + + del L[start:stop:step] + del a[start:stop:step] + self.assertEquals(a, array.array(self.typecode, L)) + def test_index(self): example = 2*self.example a = array.array(self.typecode, example) @@ -682,7 +728,8 @@ class BaseTest(unittest.TestCase): def test_buffer(self): a = array.array(self.typecode, self.example) - b = buffer(a) + with test_support._check_py3k_warnings(): + b = buffer(a) self.assertEqual(b[0], a.tostring()[0]) def test_weakref(self): @@ -728,7 +775,6 @@ class CharacterTest(StringTest): return array.array.__new__(cls, 'c', s) def __init__(self, s, color='blue'): - array.array.__init__(self, 'c', s) self.color = color def strip(self): @@ -816,6 +862,9 @@ class NumberTest(BaseTest): a = array.array(self.typecode, range(10)) del a[::1000] self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) + # test issue7788 + a = array.array(self.typecode, range(10)) + del a[9::1<<333] def test_assignment(self): a = array.array(self.typecode, range(10)) @@ -975,6 +1024,24 @@ tests.append(FloatTest) class DoubleTest(FPTest): typecode = 'd' minitemsize = 8 + + def test_alloc_overflow(self): + from sys import maxsize + a = array.array('d', [-1]*65536) + try: + a *= maxsize//65536 + 1 + except MemoryError: + pass + else: + self.fail("Array of size > maxsize created - MemoryError expected") + b = array.array('d', [ 2.71828183, 3.14159265, -1]) + try: + b * (maxsize//3 + 1) + except MemoryError: + pass + else: + self.fail("Array of size > maxsize created - MemoryError expected") + tests.append(DoubleTest) def test_main(verbose=None): |
