diff options
Diffstat (limited to 'Lib/test/test_array.py')
-rwxr-xr-x | Lib/test/test_array.py | 114 |
1 files changed, 65 insertions, 49 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index e26e9add055..a532a9fd2bd 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -16,6 +16,13 @@ import warnings import array from array import _array_reconstructor as array_reconstructor +try: + # Try to determine availability of long long independently + # of the array module under test + struct.calcsize('@q') + have_long_long = True +except struct.error: + have_long_long = False class ArraySubclass(array.array): pass @@ -24,8 +31,9 @@ class ArraySubclassWithKwargs(array.array): def __init__(self, typecode, newarg=None): array.array.__init__(self) -tests = [] # list to accumulate all tests typecodes = "ubBhHiIlLfd" +if have_long_long: + typecodes += 'qQ' class BadConstructorTest(unittest.TestCase): @@ -35,7 +43,6 @@ class BadConstructorTest(unittest.TestCase): self.assertRaises(TypeError, array.array, 'xx') self.assertRaises(ValueError, array.array, 'x') -tests.append(BadConstructorTest) # Machine format codes. # @@ -165,10 +172,7 @@ class ArrayReconstructorTest(unittest.TestCase): msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) -tests.append(ArrayReconstructorTest) - - -class BaseTest(unittest.TestCase): +class BaseTest: # Required class attributes (provided by subclasses # typecode: the typecode to test # example: an initializer usable in the constructor for this type @@ -209,10 +213,14 @@ class BaseTest(unittest.TestCase): self.assertEqual(bi[1], len(a)) def test_byteswap(self): - a = array.array(self.typecode, self.example) + if self.typecode == 'u': + example = '\U00100100' + else: + example = self.example + a = array.array(self.typecode, example) self.assertRaises(TypeError, a.byteswap, 42) if a.itemsize in (1, 2, 4, 8): - b = array.array(self.typecode, self.example) + b = array.array(self.typecode, example) b.byteswap() if a.itemsize==1: self.assertEqual(a, b) @@ -272,6 +280,20 @@ class BaseTest(unittest.TestCase): self.assertEqual(a.x, b.x) self.assertEqual(type(a), type(b)) + def test_iterator_pickle(self): + data = array.array(self.typecode, self.example) + orgit = iter(data) + d = pickle.dumps(orgit) + it = pickle.loads(d) + self.assertEqual(type(orgit), type(it)) + self.assertEqual(list(it), list(data)) + + if len(data): + it = pickle.loads(d) + next(it) + d = pickle.dumps(it) + self.assertEqual(list(it), list(data)[1:]) + def test_insert(self): a = array.array(self.typecode, self.example) a.insert(0, self.example[0]) @@ -991,14 +1013,14 @@ class BaseTest(unittest.TestCase): @support.cpython_only def test_sizeof_with_buffer(self): a = array.array(self.typecode, self.example) - basesize = support.calcvobjsize('4Pi') + basesize = support.calcvobjsize('Pn2Pi') buffer_size = a.buffer_info()[1] * a.itemsize support.check_sizeof(self, a, basesize + buffer_size) @support.cpython_only def test_sizeof_without_buffer(self): a = array.array(self.typecode) - basesize = support.calcvobjsize('4Pi') + basesize = support.calcvobjsize('Pn2Pi') support.check_sizeof(self, a, basesize) @@ -1009,7 +1031,7 @@ class StringTest(BaseTest): a = array.array(self.typecode, self.example) self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) -class UnicodeTest(StringTest): +class UnicodeTest(StringTest, unittest.TestCase): typecode = 'u' example = '\x01\u263a\x00\ufeff' smallerexample = '\x01\u263a\x00\ufefe' @@ -1018,6 +1040,16 @@ class UnicodeTest(StringTest): minitemsize = 2 def test_unicode(self): + try: + import ctypes + sizeof_wchar = ctypes.sizeof(ctypes.c_wchar) + except ImportError: + import sys + if sys.platform == 'win32': + sizeof_wchar = 2 + else: + sizeof_wchar = 4 + self.assertRaises(TypeError, array.array, 'b', 'foo') a = array.array('u', '\xa0\xc2\u1234') @@ -1027,6 +1059,7 @@ class UnicodeTest(StringTest): a.fromunicode('\x11abc\xff\u1234') s = a.tounicode() self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234') + self.assertEqual(a.itemsize, sizeof_wchar) s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234' a = array.array('u', s) @@ -1036,8 +1069,6 @@ class UnicodeTest(StringTest): self.assertRaises(TypeError, a.fromunicode) -tests.append(UnicodeTest) - class NumberTest(BaseTest): def test_extslice(self): @@ -1178,45 +1209,47 @@ class UnsignedNumberTest(NumberTest): ) -class ByteTest(SignedNumberTest): +class ByteTest(SignedNumberTest, unittest.TestCase): typecode = 'b' minitemsize = 1 -tests.append(ByteTest) -class UnsignedByteTest(UnsignedNumberTest): +class UnsignedByteTest(UnsignedNumberTest, unittest.TestCase): typecode = 'B' minitemsize = 1 -tests.append(UnsignedByteTest) -class ShortTest(SignedNumberTest): +class ShortTest(SignedNumberTest, unittest.TestCase): typecode = 'h' minitemsize = 2 -tests.append(ShortTest) -class UnsignedShortTest(UnsignedNumberTest): +class UnsignedShortTest(UnsignedNumberTest, unittest.TestCase): typecode = 'H' minitemsize = 2 -tests.append(UnsignedShortTest) -class IntTest(SignedNumberTest): +class IntTest(SignedNumberTest, unittest.TestCase): typecode = 'i' minitemsize = 2 -tests.append(IntTest) -class UnsignedIntTest(UnsignedNumberTest): +class UnsignedIntTest(UnsignedNumberTest, unittest.TestCase): typecode = 'I' minitemsize = 2 -tests.append(UnsignedIntTest) -class LongTest(SignedNumberTest): +class LongTest(SignedNumberTest, unittest.TestCase): typecode = 'l' minitemsize = 4 -tests.append(LongTest) -class UnsignedLongTest(UnsignedNumberTest): +class UnsignedLongTest(UnsignedNumberTest, unittest.TestCase): typecode = 'L' minitemsize = 4 -tests.append(UnsignedLongTest) + +@unittest.skipIf(not have_long_long, 'need long long support') +class LongLongTest(SignedNumberTest, unittest.TestCase): + typecode = 'q' + minitemsize = 8 + +@unittest.skipIf(not have_long_long, 'need long long support') +class UnsignedLongLongTest(UnsignedNumberTest, unittest.TestCase): + typecode = 'Q' + minitemsize = 8 class FPTest(NumberTest): example = [-42.0, 0, 42, 1e5, -1e10] @@ -1243,12 +1276,11 @@ class FPTest(NumberTest): b.byteswap() self.assertEqual(a, b) -class FloatTest(FPTest): +class FloatTest(FPTest, unittest.TestCase): typecode = 'f' minitemsize = 4 -tests.append(FloatTest) -class DoubleTest(FPTest): +class DoubleTest(FPTest, unittest.TestCase): typecode = 'd' minitemsize = 8 @@ -1269,22 +1301,6 @@ class DoubleTest(FPTest): else: self.fail("Array of size > maxsize created - MemoryError expected") -tests.append(DoubleTest) - -def test_main(verbose=None): - import sys - - support.run_unittest(*tests) - - # verify reference counting - if verbose and hasattr(sys, "gettotalrefcount"): - import gc - counts = [None] * 5 - for i in range(len(counts)): - support.run_unittest(*tests) - gc.collect() - counts[i] = sys.gettotalrefcount() - print(counts) if __name__ == "__main__": - test_main(verbose=True) + unittest.main() |