1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport operator
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom sys import maxint
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmaxsize = test_support.MAX_Py_ssize_t
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepminsize = -maxsize-1
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass oldstyle:
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __index__(self):
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.ind
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass newstyle(object):
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __index__(self):
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.ind
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TrapInt(int):
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __index__(self):
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TrapLong(long):
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __index__(self):
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass BaseTestCase(unittest.TestCase):
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o = oldstyle()
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n = newstyle()
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_basic(self):
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = -2
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 2
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(operator.index(self.o), -2)
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(operator.index(self.n), 2)
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slice(self):
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 1
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 2
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        slc = slice(self.o, self.o, self.o)
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check_slc = slice(1, 1, 1)
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(slc.indices(self.o), check_slc.indices(1))
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        slc = slice(self.n, self.n, self.n)
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check_slc = slice(2, 2, 2)
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(slc.indices(self.n), check_slc.indices(2))
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_wrappers(self):
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 4
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 5
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(6 .__index__(), 6)
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(-7L.__index__(), -7)
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.o.__index__(), 4)
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.n.__index__(), 5)
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(True.__index__(), 1)
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(False.__index__(), 0)
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_subclasses(self):
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        r = range(10)
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_error(self):
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 'dumb'
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 'bad'
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, operator.index, self.o)
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, operator.index, self.n)
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, slice(self.o).indices, 0)
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, slice(self.n).indices, 0)
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SeqTestCase(unittest.TestCase):
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # This test case isn't run directly. It just defines common tests
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # to the different sequence types below
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o = oldstyle()
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n = newstyle()
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o2 = oldstyle()
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n2 = newstyle()
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_index(self):
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = -2
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 2
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.n], self.seq[2])
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.o], self.seq[-2])
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slice(self):
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 1
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o2.ind = 3
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 2
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n2.ind = 4
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slice_bug7532(self):
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        seqlen = len(self.seq)
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = int(seqlen * 1.5)
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = seqlen + 2
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.o:], self.seq[0:0])
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[:self.o], self.seq)
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.n:], self.seq[0:0])
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[:self.n], self.seq)
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if isinstance(self.seq, ClassicSeq):
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # These tests fail for ClassicSeq (see bug #7532)
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o2.ind = -seqlen - 2
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n2.ind = -int(seqlen * 1.5)
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.o2:], self.seq)
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[:self.o2], self.seq[0:0])
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[self.n2:], self.seq)
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[:self.n2], self.seq[0:0])
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_repeat(self):
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 3
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 2
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq * self.o, self.seq * 3)
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq * self.n, self.seq * 2)
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.o * self.seq, self.seq * 3)
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.n * self.seq, self.seq * 2)
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_wrappers(self):
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 4
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 5
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_subclasses(self):
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[TrapInt()], self.seq[0])
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.seq[TrapLong()], self.seq[0])
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_error(self):
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 'dumb'
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 'bad'
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        indexobj = lambda x, obj: obj.seq[x]
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, indexobj, self.o, self)
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, indexobj, self.n, self)
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sliceobj = lambda x, obj: obj.seq[x:]
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sliceobj, self.o, self)
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, sliceobj, self.n, self)
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ListTestCase(SeqTestCase):
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = [0,10,20,30,40,50]
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_setdelitem(self):
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = -2
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 2
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst = list('ab!cdefghi!j')
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del lst[self.o]
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del lst[self.n]
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst[self.o] = 'X'
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst[self.n] = 'Y'
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst, list('abYdefghXj'))
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst = [5, 6, 7, 8, 9, 10, 11]
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst.__setitem__(self.n, "here")
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst.__delitem__(self.n)
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_inplace_repeat(self):
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.o.ind = 2
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.n.ind = 3
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst = [6, 4]
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst *= self.o
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst, [6, 4, 6, 4])
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst *= self.n
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst, [6, 4, 6, 4] * 3)
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lst = [5, 6, 7, 8, 9, 11]
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l2 = lst.__imul__(self.n)
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIs(l2, lst)
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _BaseSeq:
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, iterable):
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._list = list(iterable)
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __repr__(self):
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return repr(self._list)
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __eq__(self, other):
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self._list == other
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __len__(self):
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return len(self._list)
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __mul__(self, n):
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.__class__(self._list*n)
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __rmul__ = __mul__
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __getitem__(self, index):
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self._list[index]
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass _GetSliceMixin:
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __getslice__(self, i, j):
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self._list.__getslice__(i, j)
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ClassicSeq(_BaseSeq): pass
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass NewSeq(_BaseSeq, object): pass
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ClassicSeqDeprecated(_GetSliceMixin, ClassicSeq): pass
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass NewSeqDeprecated(_GetSliceMixin, NewSeq): pass
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TupleTestCase(SeqTestCase):
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = (0,10,20,30,40,50)
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass StringTestCase(SeqTestCase):
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = "this is a test"
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ByteArrayTestCase(SeqTestCase):
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = bytearray("this is a test")
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass UnicodeTestCase(SeqTestCase):
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = u"this is a test"
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ClassicSeqTestCase(SeqTestCase):
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = ClassicSeq((0,10,20,30,40,50))
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass NewSeqTestCase(SeqTestCase):
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = NewSeq((0,10,20,30,40,50))
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ClassicSeqDeprecatedTestCase(SeqTestCase):
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = ClassicSeqDeprecated((0,10,20,30,40,50))
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass NewSeqDeprecatedTestCase(SeqTestCase):
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    seq = NewSeqDeprecated((0,10,20,30,40,50))
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass XRangeTestCase(unittest.TestCase):
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_xrange(self):
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        n = newstyle()
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        n.ind = 5
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(xrange(1, 20)[n], 6)
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(xrange(1, 20).__getitem__(n), 6)
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass OverflowTestCase(unittest.TestCase):
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.pos = 2**100
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.neg = -self.pos
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_large_longs(self):
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.pos.__index__(), self.pos)
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.neg.__index__(), self.neg)
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _getitem_helper(self, base):
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class GetItem(base):
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __len__(self):
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return maxint # cannot return long here
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, key):
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return key
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = GetItem()
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.pos], self.pos)
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.neg], self.neg)
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.neg:self.pos].indices(maxsize),
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         (0, maxsize, 1))
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         (0, maxsize, 1))
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _getslice_helper_deprecated(self, base):
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class GetItem(base):
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __len__(self):
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return maxint # cannot return long here
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, key):
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return key
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getslice__(self, i, j):
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return i, j
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = GetItem()
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.pos], self.pos)
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.neg], self.neg)
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         (0, maxsize, 1))
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_getitem(self):
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._getitem_helper(object)
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with test_support.check_py3k_warnings():
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._getslice_helper_deprecated(object)
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_getitem_classic(self):
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Empty: pass
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX This test fails (see bug #7532)
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        #self._getitem_helper(Empty)
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with test_support.check_py3k_warnings():
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._getslice_helper_deprecated(Empty)
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_sequence_repeat(self):
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, lambda: "a" * self.pos)
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, lambda: "a" * self.neg)
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main():
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_support.run_unittest(
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        BaseTestCase,
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ListTestCase,
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        TupleTestCase,
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ByteArrayTestCase,
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        StringTestCase,
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        UnicodeTestCase,
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ClassicSeqTestCase,
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        NewSeqTestCase,
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        XRangeTestCase,
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        OverflowTestCase,
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    )
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with test_support.check_py3k_warnings():
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.run_unittest(
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ClassicSeqDeprecatedTestCase,
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            NewSeqDeprecatedTestCase,
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        )
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
324