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