| OLD | NEW |
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 """Test the arraymodule. | 2 """Test the arraymodule. |
| 3 Roger E. Masse | 3 Roger E. Masse |
| 4 """ | 4 """ |
| 5 | 5 |
| 6 import unittest | 6 import unittest |
| 7 from test import test_support | 7 from test import test_support |
| 8 from weakref import proxy | 8 from weakref import proxy |
| 9 import array, io, math | 9 import array, io, math |
| 10 from pickle import loads, dumps | 10 from pickle import loads, dumps |
| 11 | 11 |
| 12 class ArraySubclass(array.array): | 12 class ArraySubclass(array.array): |
| 13 pass | 13 pass |
| 14 | 14 |
| 15 class ArraySubclassWithKwargs(array.array): | 15 class ArraySubclassWithKwargs(array.array): |
| 16 def __init__(self, typecode, newarg=None): | 16 def __init__(self, typecode, newarg=None): |
| 17 array.array.__init__(typecode) | 17 array.array.__init__(typecode) |
| 18 | 18 |
| 19 tests = [] # list to accumulate all tests | 19 tests = [] # list to accumulate all tests |
| 20 typecodes = "ubBhHiIlLfd" | 20 typecodes = "ubBhHiIlLfd" |
| 21 | 21 |
| 22 class BadConstructorTest(unittest.TestCase): | 22 class BadConstructorTest(unittest.TestCase): |
| 23 | 23 |
| 24 def test_constructor(self): | 24 def test_constructor(self): |
| 25 self.assertRaises(TypeError, array.array) | 25 self.assertRaises(TypeError, array.array) |
| 26 self.assertRaises(TypeError, array.array, spam=42) | 26 self.assertRaises(TypeError, array.array, spam=42) |
| 27 self.assertRaises(TypeError, array.array, 'xx') | 27 self.assertRaises(TypeError, array.array, 'xx') |
| 28 self.assertRaises(ValueError, array.array, 'x') | 28 self.assertRaises(ValueError, array.array, 'x') |
| 29 | 29 |
| 30 tests.append(BadConstructorTest) | 30 tests.append(BadConstructorTest) |
| 31 | 31 |
| 32 class BaseTest(unittest.TestCase): | 32 class BaseTest(unittest.TestCase): |
| 33 # Required class attributes (provided by subclasses | 33 # Required class attributes (provided by subclasses |
| 34 # typecode: the typecode to test | 34 # typecode: the typecode to test |
| 35 # example: an initializer usable in the constructor for this type | 35 # example: an initializer usable in the constructor for this type |
| 36 # smallerexample: the same length as example, but smaller | 36 # smallerexample: the same length as example, but smaller |
| 37 # biggerexample: the same length as example, but bigger | 37 # biggerexample: the same length as example, but bigger |
| 38 # outside: An entry that is not in example | 38 # outside: An entry that is not in example |
| 39 # minitemsize: the minimum guaranteed itemsize | 39 # minitemsize: the minimum guaranteed itemsize |
| 40 | 40 |
| 41 def assertEntryEqual(self, entry1, entry2): | 41 def assertEntryEqual(self, entry1, entry2): |
| 42 self.assertEqual(entry1, entry2) | 42 self.assertEqual(entry1, entry2) |
| 43 | 43 |
| 44 def badtypecode(self): | 44 def badtypecode(self): |
| 45 # Return a typecode that is different from our own | 45 # Return a typecode that is different from our own |
| 46 return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] | 46 return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] |
| 47 | 47 |
| 48 def test_constructor(self): | 48 def test_constructor(self): |
| 49 a = array.array(self.typecode) | 49 a = array.array(self.typecode) |
| 50 self.assertEqual(a.typecode, self.typecode) | 50 self.assertEqual(a.typecode, self.typecode) |
| (...skipping 670 matching lines...) Show 10 above Show 10 below |
| 721 | 721 |
| 722 def test_bug_782369(self): | 722 def test_bug_782369(self): |
| 723 import sys | 723 import sys |
| 724 if hasattr(sys, "getrefcount"): | 724 if hasattr(sys, "getrefcount"): |
| 725 for i in range(10): | 725 for i in range(10): |
| 726 b = array.array('B', range(64)) | 726 b = array.array('B', range(64)) |
| 727 rc = sys.getrefcount(10) | 727 rc = sys.getrefcount(10) |
| 728 for i in range(10): | 728 for i in range(10): |
| 729 b = array.array('B', range(64)) | 729 b = array.array('B', range(64)) |
| 730 self.assertEqual(rc, sys.getrefcount(10)) | 730 self.assertEqual(rc, sys.getrefcount(10)) |
| 731 | 731 |
| 732 def test_subclass_with_kwargs(self): | 732 def test_subclass_with_kwargs(self): |
| 733 # SF bug #1486663 -- this used to erroneously raise a TypeError | 733 # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 734 ArraySubclassWithKwargs('b', newarg=1) | 734 ArraySubclassWithKwargs('b', newarg=1) |
| 735 | 735 |
| 736 def test_create_from_bytes(self): | 736 def test_create_from_bytes(self): |
| 737 a = array.array('H', b"1234") | 737 a = array.array('H', b"1234") |
| 738 self.assertEqual(len(a) * a.itemsize, 4) | 738 self.assertEqual(len(a) * a.itemsize, 4) |
| 739 | 739 |
| 740 | 740 |
| 741 class StringTest(BaseTest): | 741 class StringTest(BaseTest): |
| 742 | 742 |
| 743 def test_setitem(self): | 743 def test_setitem(self): |
| 744 super().test_setitem() | 744 super().test_setitem() |
| 745 a = array.array(self.typecode, self.example) | 745 a = array.array(self.typecode, self.example) |
| 746 self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) | 746 self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) |
| 747 | 747 |
| 748 class UnicodeTest(StringTest): | 748 class UnicodeTest(StringTest): |
| 749 typecode = 'u' | 749 typecode = 'u' |
| 750 example = '\x01\u263a\x00\ufeff' | 750 example = '\x01\u263a\x00\ufeff' |
| 751 smallerexample = '\x01\u263a\x00\ufefe' | 751 smallerexample = '\x01\u263a\x00\ufefe' |
| 752 biggerexample = '\x01\u263a\x01\ufeff' | 752 biggerexample = '\x01\u263a\x01\ufeff' |
| 753 outside = str('\x33') | 753 outside = str('\x33') |
| 754 minitemsize = 2 | 754 minitemsize = 2 |
| 755 | 755 |
| 756 def test_unicode(self): | 756 def test_unicode(self): |
| 757 self.assertRaises(TypeError, array.array, 'b', 'foo') | 757 self.assertRaises(TypeError, array.array, 'b', 'foo') |
| 758 | 758 |
| 759 a = array.array('u', '\xa0\xc2\u1234') | 759 a = array.array('u', '\xa0\xc2\u1234') |
| 760 a.fromunicode(' ') | 760 a.fromunicode(' ') |
| 761 a.fromunicode('') | 761 a.fromunicode('') |
| 762 a.fromunicode('') | 762 a.fromunicode('') |
| 763 a.fromunicode('\x11abc\xff\u1234') | 763 a.fromunicode('\x11abc\xff\u1234') |
| 764 s = a.tounicode() | 764 s = a.tounicode() |
| 765 self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234') | 765 self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234') |
| 766 | 766 |
| 767 s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234' | 767 s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234' |
| 768 a = array.array('u', s) | 768 a = array.array('u', s) |
| 769 self.assertEqual( | 769 self.assertEqual( |
| 770 repr(a), | 770 repr(a), |
| 771 "array('u', '\\x00=\"\\'a\\\\b\\x80\\xff\\x00\\x01\\u1234')") | 771 "array('u', '\\x00=\"\\'a\\\\b\\x80\xff\\x00\\x01\u1234')") |
| 772 | 772 |
| 773 self.assertRaises(TypeError, a.fromunicode) | 773 self.assertRaises(TypeError, a.fromunicode) |
| 774 | 774 |
| 775 tests.append(UnicodeTest) | 775 tests.append(UnicodeTest) |
| 776 | 776 |
| 777 class NumberTest(BaseTest): | 777 class NumberTest(BaseTest): |
| 778 | 778 |
| 779 def test_extslice(self): | 779 def test_extslice(self): |
| 780 a = array.array(self.typecode, range(5)) | 780 a = array.array(self.typecode, range(5)) |
| 781 self.assertEqual(a[::], a) | 781 self.assertEqual(a[::], a) |
| 782 self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) | 782 self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) |
| 783 self.assertEqual(a[1::2], array.array(self.typecode, [1,3])) | 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])) | 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])) | 785 self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0])) |
| 786 self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) | 786 self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) |
| 787 self.assertEqual(a[-100:100:], a) | 787 self.assertEqual(a[-100:100:], a) |
| 788 self.assertEqual(a[100:-100:-1], a[::-1]) | 788 self.assertEqual(a[100:-100:-1], a[::-1]) |
| 789 self.assertEqual(a[-100:100:2], array.array(self.typecode, [0,2,4])) | 789 self.assertEqual(a[-100:100:2], array.array(self.typecode, [0,2,4])) |
| 790 self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) | 790 self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) |
| 791 self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) | 791 self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) |
| 792 | 792 |
| 793 def test_delslice(self): | 793 def test_delslice(self): |
| 794 a = array.array(self.typecode, range(5)) | 794 a = array.array(self.typecode, range(5)) |
| 795 del a[::2] | 795 del a[::2] |
| 796 self.assertEqual(a, array.array(self.typecode, [1,3])) | 796 self.assertEqual(a, array.array(self.typecode, [1,3])) |
| 797 a = array.array(self.typecode, range(5)) | 797 a = array.array(self.typecode, range(5)) |
| 798 del a[1::2] | 798 del a[1::2] |
| 799 self.assertEqual(a, array.array(self.typecode, [0,2,4])) | 799 self.assertEqual(a, array.array(self.typecode, [0,2,4])) |
| 800 a = array.array(self.typecode, range(5)) | 800 a = array.array(self.typecode, range(5)) |
| 801 del a[1::-2] | 801 del a[1::-2] |
| 802 self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) | 802 self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) |
| 803 a = array.array(self.typecode, range(10)) | 803 a = array.array(self.typecode, range(10)) |
| 804 del a[::1000] | 804 del a[::1000] |
| 805 self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) | 805 self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) |
| 806 | 806 |
| 807 def test_assignment(self): | 807 def test_assignment(self): |
| 808 a = array.array(self.typecode, range(10)) | 808 a = array.array(self.typecode, range(10)) |
| 809 a[::2] = array.array(self.typecode, [42]*5) | 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])) | 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)) | 811 a = array.array(self.typecode, range(10)) |
| 812 a[::-4] = array.array(self.typecode, [10]*3) | 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])) | 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)) | 814 a = array.array(self.typecode, range(4)) |
| 815 a[::-1] = a | 815 a[::-1] = a |
| 816 self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) | 816 self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) |
| 817 a = array.array(self.typecode, range(10)) | 817 a = array.array(self.typecode, range(10)) |
| 818 b = a[:] | 818 b = a[:] |
| 819 c = a[:] | 819 c = a[:] |
| 820 ins = array.array(self.typecode, range(2)) | 820 ins = array.array(self.typecode, range(2)) |
| 821 a[2:3] = ins | 821 a[2:3] = ins |
| (...skipping 112 matching lines...) Show 10 above Show 10 below |
| 934 smallerexample = [-42.0, 0, 42, 1e5, -2e10] | 934 smallerexample = [-42.0, 0, 42, 1e5, -2e10] |
| 935 biggerexample = [-42.0, 0, 42, 1e5, 1e10] | 935 biggerexample = [-42.0, 0, 42, 1e5, 1e10] |
| 936 outside = 23 | 936 outside = 23 |
| 937 | 937 |
| 938 def assertEntryEqual(self, entry1, entry2): | 938 def assertEntryEqual(self, entry1, entry2): |
| 939 self.assertAlmostEqual(entry1, entry2) | 939 self.assertAlmostEqual(entry1, entry2) |
| 940 | 940 |
| 941 def test_byteswap(self): | 941 def test_byteswap(self): |
| 942 a = array.array(self.typecode, self.example) | 942 a = array.array(self.typecode, self.example) |
| 943 self.assertRaises(TypeError, a.byteswap, 42) | 943 self.assertRaises(TypeError, a.byteswap, 42) |
| 944 if a.itemsize in (1, 2, 4, 8): | 944 if a.itemsize in (1, 2, 4, 8): |
| 945 b = array.array(self.typecode, self.example) | 945 b = array.array(self.typecode, self.example) |
| 946 b.byteswap() | 946 b.byteswap() |
| 947 if a.itemsize==1: | 947 if a.itemsize==1: |
| 948 self.assertEqual(a, b) | 948 self.assertEqual(a, b) |
| 949 else: | 949 else: |
| 950 # On alphas treating the byte swapped bit patters as | 950 # On alphas treating the byte swapped bit patters as |
| 951 # floats/doubles results in floating point exceptions | 951 # floats/doubles results in floating point exceptions |
| 952 # => compare the 8bit string values instead | 952 # => compare the 8bit string values instead |
| 953 self.assertNotEqual(a.tostring(), b.tostring()) | 953 self.assertNotEqual(a.tostring(), b.tostring()) |
| 954 b.byteswap() | 954 b.byteswap() |
| 955 self.assertEqual(a, b) | 955 self.assertEqual(a, b) |
| 956 | 956 |
| 957 class FloatTest(FPTest): | 957 class FloatTest(FPTest): |
| 958 typecode = 'f' | 958 typecode = 'f' |
| 959 minitemsize = 4 | 959 minitemsize = 4 |
| 960 tests.append(FloatTest) | 960 tests.append(FloatTest) |
| 961 | 961 |
| 962 class DoubleTest(FPTest): | 962 class DoubleTest(FPTest): |
| 963 typecode = 'd' | 963 typecode = 'd' |
| 964 minitemsize = 8 | 964 minitemsize = 8 |
| 965 tests.append(DoubleTest) | 965 tests.append(DoubleTest) |
| 966 | 966 |
| 967 def test_main(verbose=None): | 967 def test_main(verbose=None): |
| 968 import sys | 968 import sys |
| 969 | 969 |
| 970 test_support.run_unittest(*tests) | 970 test_support.run_unittest(*tests) |
| 971 | 971 |
| 972 # verify reference counting | 972 # verify reference counting |
| 973 if verbose and hasattr(sys, "gettotalrefcount"): | 973 if verbose and hasattr(sys, "gettotalrefcount"): |
| 974 import gc | 974 import gc |
| 975 counts = [None] * 5 | 975 counts = [None] * 5 |
| 976 for i in range(len(counts)): | 976 for i in range(len(counts)): |
| 977 test_support.run_unittest(*tests) | 977 test_support.run_unittest(*tests) |
| 978 gc.collect() | 978 gc.collect() |
| 979 counts[i] = sys.gettotalrefcount() | 979 counts[i] = sys.gettotalrefcount() |
| 980 print(counts) | 980 print(counts) |
| 981 | 981 |
| 982 if __name__ == "__main__": | 982 if __name__ == "__main__": |
| 983 test_main(verbose=True) | 983 test_main(verbose=True) |
| OLD | NEW |