1 | n/a | import unittest |
---|
2 | n/a | from test import support |
---|
3 | n/a | import operator |
---|
4 | n/a | maxsize = support.MAX_Py_ssize_t |
---|
5 | n/a | |
---|
6 | n/a | class newstyle: |
---|
7 | n/a | def __index__(self): |
---|
8 | n/a | return self.ind |
---|
9 | n/a | |
---|
10 | n/a | class TrapInt(int): |
---|
11 | n/a | def __index__(self): |
---|
12 | n/a | return int(self) |
---|
13 | n/a | |
---|
14 | n/a | class BaseTestCase(unittest.TestCase): |
---|
15 | n/a | def setUp(self): |
---|
16 | n/a | self.o = newstyle() |
---|
17 | n/a | self.n = newstyle() |
---|
18 | n/a | |
---|
19 | n/a | def test_basic(self): |
---|
20 | n/a | self.o.ind = -2 |
---|
21 | n/a | self.n.ind = 2 |
---|
22 | n/a | self.assertEqual(operator.index(self.o), -2) |
---|
23 | n/a | self.assertEqual(operator.index(self.n), 2) |
---|
24 | n/a | |
---|
25 | n/a | def test_slice(self): |
---|
26 | n/a | self.o.ind = 1 |
---|
27 | n/a | self.n.ind = 2 |
---|
28 | n/a | slc = slice(self.o, self.o, self.o) |
---|
29 | n/a | check_slc = slice(1, 1, 1) |
---|
30 | n/a | self.assertEqual(slc.indices(self.o), check_slc.indices(1)) |
---|
31 | n/a | slc = slice(self.n, self.n, self.n) |
---|
32 | n/a | check_slc = slice(2, 2, 2) |
---|
33 | n/a | self.assertEqual(slc.indices(self.n), check_slc.indices(2)) |
---|
34 | n/a | |
---|
35 | n/a | def test_wrappers(self): |
---|
36 | n/a | self.o.ind = 4 |
---|
37 | n/a | self.n.ind = 5 |
---|
38 | n/a | self.assertEqual(6 .__index__(), 6) |
---|
39 | n/a | self.assertEqual(-7 .__index__(), -7) |
---|
40 | n/a | self.assertEqual(self.o.__index__(), 4) |
---|
41 | n/a | self.assertEqual(self.n.__index__(), 5) |
---|
42 | n/a | self.assertEqual(True.__index__(), 1) |
---|
43 | n/a | self.assertEqual(False.__index__(), 0) |
---|
44 | n/a | |
---|
45 | n/a | def test_subclasses(self): |
---|
46 | n/a | r = list(range(10)) |
---|
47 | n/a | self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10]) |
---|
48 | n/a | self.assertEqual(slice(TrapInt()).indices(0), (0,0,1)) |
---|
49 | n/a | |
---|
50 | n/a | def test_error(self): |
---|
51 | n/a | self.o.ind = 'dumb' |
---|
52 | n/a | self.n.ind = 'bad' |
---|
53 | n/a | self.assertRaises(TypeError, operator.index, self.o) |
---|
54 | n/a | self.assertRaises(TypeError, operator.index, self.n) |
---|
55 | n/a | self.assertRaises(TypeError, slice(self.o).indices, 0) |
---|
56 | n/a | self.assertRaises(TypeError, slice(self.n).indices, 0) |
---|
57 | n/a | |
---|
58 | n/a | def test_int_subclass_with_index(self): |
---|
59 | n/a | # __index__ should be used when computing indices, even for int |
---|
60 | n/a | # subclasses. See issue #17576. |
---|
61 | n/a | class MyInt(int): |
---|
62 | n/a | def __index__(self): |
---|
63 | n/a | return int(self) + 1 |
---|
64 | n/a | |
---|
65 | n/a | my_int = MyInt(7) |
---|
66 | n/a | direct_index = my_int.__index__() |
---|
67 | n/a | operator_index = operator.index(my_int) |
---|
68 | n/a | self.assertEqual(direct_index, 8) |
---|
69 | n/a | self.assertEqual(operator_index, 7) |
---|
70 | n/a | # Both results should be of exact type int. |
---|
71 | n/a | self.assertIs(type(direct_index), int) |
---|
72 | n/a | #self.assertIs(type(operator_index), int) |
---|
73 | n/a | |
---|
74 | n/a | def test_index_returns_int_subclass(self): |
---|
75 | n/a | class BadInt: |
---|
76 | n/a | def __index__(self): |
---|
77 | n/a | return True |
---|
78 | n/a | |
---|
79 | n/a | class BadInt2(int): |
---|
80 | n/a | def __index__(self): |
---|
81 | n/a | return True |
---|
82 | n/a | |
---|
83 | n/a | bad_int = BadInt() |
---|
84 | n/a | with self.assertWarns(DeprecationWarning): |
---|
85 | n/a | n = operator.index(bad_int) |
---|
86 | n/a | self.assertEqual(n, 1) |
---|
87 | n/a | |
---|
88 | n/a | bad_int = BadInt2() |
---|
89 | n/a | n = operator.index(bad_int) |
---|
90 | n/a | self.assertEqual(n, 0) |
---|
91 | n/a | |
---|
92 | n/a | |
---|
93 | n/a | class SeqTestCase: |
---|
94 | n/a | # This test case isn't run directly. It just defines common tests |
---|
95 | n/a | # to the different sequence types below |
---|
96 | n/a | def setUp(self): |
---|
97 | n/a | self.o = newstyle() |
---|
98 | n/a | self.n = newstyle() |
---|
99 | n/a | self.o2 = newstyle() |
---|
100 | n/a | self.n2 = newstyle() |
---|
101 | n/a | |
---|
102 | n/a | def test_index(self): |
---|
103 | n/a | self.o.ind = -2 |
---|
104 | n/a | self.n.ind = 2 |
---|
105 | n/a | self.assertEqual(self.seq[self.n], self.seq[2]) |
---|
106 | n/a | self.assertEqual(self.seq[self.o], self.seq[-2]) |
---|
107 | n/a | |
---|
108 | n/a | def test_slice(self): |
---|
109 | n/a | self.o.ind = 1 |
---|
110 | n/a | self.o2.ind = 3 |
---|
111 | n/a | self.n.ind = 2 |
---|
112 | n/a | self.n2.ind = 4 |
---|
113 | n/a | self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3]) |
---|
114 | n/a | self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4]) |
---|
115 | n/a | |
---|
116 | n/a | def test_slice_bug7532(self): |
---|
117 | n/a | seqlen = len(self.seq) |
---|
118 | n/a | self.o.ind = int(seqlen * 1.5) |
---|
119 | n/a | self.n.ind = seqlen + 2 |
---|
120 | n/a | self.assertEqual(self.seq[self.o:], self.seq[0:0]) |
---|
121 | n/a | self.assertEqual(self.seq[:self.o], self.seq) |
---|
122 | n/a | self.assertEqual(self.seq[self.n:], self.seq[0:0]) |
---|
123 | n/a | self.assertEqual(self.seq[:self.n], self.seq) |
---|
124 | n/a | self.o2.ind = -seqlen - 2 |
---|
125 | n/a | self.n2.ind = -int(seqlen * 1.5) |
---|
126 | n/a | self.assertEqual(self.seq[self.o2:], self.seq) |
---|
127 | n/a | self.assertEqual(self.seq[:self.o2], self.seq[0:0]) |
---|
128 | n/a | self.assertEqual(self.seq[self.n2:], self.seq) |
---|
129 | n/a | self.assertEqual(self.seq[:self.n2], self.seq[0:0]) |
---|
130 | n/a | |
---|
131 | n/a | def test_repeat(self): |
---|
132 | n/a | self.o.ind = 3 |
---|
133 | n/a | self.n.ind = 2 |
---|
134 | n/a | self.assertEqual(self.seq * self.o, self.seq * 3) |
---|
135 | n/a | self.assertEqual(self.seq * self.n, self.seq * 2) |
---|
136 | n/a | self.assertEqual(self.o * self.seq, self.seq * 3) |
---|
137 | n/a | self.assertEqual(self.n * self.seq, self.seq * 2) |
---|
138 | n/a | |
---|
139 | n/a | def test_wrappers(self): |
---|
140 | n/a | self.o.ind = 4 |
---|
141 | n/a | self.n.ind = 5 |
---|
142 | n/a | self.assertEqual(self.seq.__getitem__(self.o), self.seq[4]) |
---|
143 | n/a | self.assertEqual(self.seq.__mul__(self.o), self.seq * 4) |
---|
144 | n/a | self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4) |
---|
145 | n/a | self.assertEqual(self.seq.__getitem__(self.n), self.seq[5]) |
---|
146 | n/a | self.assertEqual(self.seq.__mul__(self.n), self.seq * 5) |
---|
147 | n/a | self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5) |
---|
148 | n/a | |
---|
149 | n/a | def test_subclasses(self): |
---|
150 | n/a | self.assertEqual(self.seq[TrapInt()], self.seq[0]) |
---|
151 | n/a | |
---|
152 | n/a | def test_error(self): |
---|
153 | n/a | self.o.ind = 'dumb' |
---|
154 | n/a | self.n.ind = 'bad' |
---|
155 | n/a | indexobj = lambda x, obj: obj.seq[x] |
---|
156 | n/a | self.assertRaises(TypeError, indexobj, self.o, self) |
---|
157 | n/a | self.assertRaises(TypeError, indexobj, self.n, self) |
---|
158 | n/a | sliceobj = lambda x, obj: obj.seq[x:] |
---|
159 | n/a | self.assertRaises(TypeError, sliceobj, self.o, self) |
---|
160 | n/a | self.assertRaises(TypeError, sliceobj, self.n, self) |
---|
161 | n/a | |
---|
162 | n/a | |
---|
163 | n/a | class ListTestCase(SeqTestCase, unittest.TestCase): |
---|
164 | n/a | seq = [0,10,20,30,40,50] |
---|
165 | n/a | |
---|
166 | n/a | def test_setdelitem(self): |
---|
167 | n/a | self.o.ind = -2 |
---|
168 | n/a | self.n.ind = 2 |
---|
169 | n/a | lst = list('ab!cdefghi!j') |
---|
170 | n/a | del lst[self.o] |
---|
171 | n/a | del lst[self.n] |
---|
172 | n/a | lst[self.o] = 'X' |
---|
173 | n/a | lst[self.n] = 'Y' |
---|
174 | n/a | self.assertEqual(lst, list('abYdefghXj')) |
---|
175 | n/a | |
---|
176 | n/a | lst = [5, 6, 7, 8, 9, 10, 11] |
---|
177 | n/a | lst.__setitem__(self.n, "here") |
---|
178 | n/a | self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11]) |
---|
179 | n/a | lst.__delitem__(self.n) |
---|
180 | n/a | self.assertEqual(lst, [5, 6, 8, 9, 10, 11]) |
---|
181 | n/a | |
---|
182 | n/a | def test_inplace_repeat(self): |
---|
183 | n/a | self.o.ind = 2 |
---|
184 | n/a | self.n.ind = 3 |
---|
185 | n/a | lst = [6, 4] |
---|
186 | n/a | lst *= self.o |
---|
187 | n/a | self.assertEqual(lst, [6, 4, 6, 4]) |
---|
188 | n/a | lst *= self.n |
---|
189 | n/a | self.assertEqual(lst, [6, 4, 6, 4] * 3) |
---|
190 | n/a | |
---|
191 | n/a | lst = [5, 6, 7, 8, 9, 11] |
---|
192 | n/a | l2 = lst.__imul__(self.n) |
---|
193 | n/a | self.assertIs(l2, lst) |
---|
194 | n/a | self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3) |
---|
195 | n/a | |
---|
196 | n/a | |
---|
197 | n/a | class NewSeq: |
---|
198 | n/a | |
---|
199 | n/a | def __init__(self, iterable): |
---|
200 | n/a | self._list = list(iterable) |
---|
201 | n/a | |
---|
202 | n/a | def __repr__(self): |
---|
203 | n/a | return repr(self._list) |
---|
204 | n/a | |
---|
205 | n/a | def __eq__(self, other): |
---|
206 | n/a | return self._list == other |
---|
207 | n/a | |
---|
208 | n/a | def __len__(self): |
---|
209 | n/a | return len(self._list) |
---|
210 | n/a | |
---|
211 | n/a | def __mul__(self, n): |
---|
212 | n/a | return self.__class__(self._list*n) |
---|
213 | n/a | __rmul__ = __mul__ |
---|
214 | n/a | |
---|
215 | n/a | def __getitem__(self, index): |
---|
216 | n/a | return self._list[index] |
---|
217 | n/a | |
---|
218 | n/a | |
---|
219 | n/a | class TupleTestCase(SeqTestCase, unittest.TestCase): |
---|
220 | n/a | seq = (0,10,20,30,40,50) |
---|
221 | n/a | |
---|
222 | n/a | class ByteArrayTestCase(SeqTestCase, unittest.TestCase): |
---|
223 | n/a | seq = bytearray(b"this is a test") |
---|
224 | n/a | |
---|
225 | n/a | class BytesTestCase(SeqTestCase, unittest.TestCase): |
---|
226 | n/a | seq = b"this is a test" |
---|
227 | n/a | |
---|
228 | n/a | class StringTestCase(SeqTestCase, unittest.TestCase): |
---|
229 | n/a | seq = "this is a test" |
---|
230 | n/a | |
---|
231 | n/a | class NewSeqTestCase(SeqTestCase, unittest.TestCase): |
---|
232 | n/a | seq = NewSeq((0,10,20,30,40,50)) |
---|
233 | n/a | |
---|
234 | n/a | |
---|
235 | n/a | |
---|
236 | n/a | class RangeTestCase(unittest.TestCase): |
---|
237 | n/a | |
---|
238 | n/a | def test_range(self): |
---|
239 | n/a | n = newstyle() |
---|
240 | n/a | n.ind = 5 |
---|
241 | n/a | self.assertEqual(range(1, 20)[n], 6) |
---|
242 | n/a | self.assertEqual(range(1, 20).__getitem__(n), 6) |
---|
243 | n/a | |
---|
244 | n/a | |
---|
245 | n/a | class OverflowTestCase(unittest.TestCase): |
---|
246 | n/a | |
---|
247 | n/a | def setUp(self): |
---|
248 | n/a | self.pos = 2**100 |
---|
249 | n/a | self.neg = -self.pos |
---|
250 | n/a | |
---|
251 | n/a | def test_large_longs(self): |
---|
252 | n/a | self.assertEqual(self.pos.__index__(), self.pos) |
---|
253 | n/a | self.assertEqual(self.neg.__index__(), self.neg) |
---|
254 | n/a | |
---|
255 | n/a | def test_getitem(self): |
---|
256 | n/a | class GetItem: |
---|
257 | n/a | def __len__(self): |
---|
258 | n/a | assert False, "__len__ should not be invoked" |
---|
259 | n/a | def __getitem__(self, key): |
---|
260 | n/a | return key |
---|
261 | n/a | x = GetItem() |
---|
262 | n/a | self.assertEqual(x[self.pos], self.pos) |
---|
263 | n/a | self.assertEqual(x[self.neg], self.neg) |
---|
264 | n/a | self.assertEqual(x[self.neg:self.pos].indices(maxsize), |
---|
265 | n/a | (0, maxsize, 1)) |
---|
266 | n/a | self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), |
---|
267 | n/a | (0, maxsize, 1)) |
---|
268 | n/a | |
---|
269 | n/a | def test_sequence_repeat(self): |
---|
270 | n/a | self.assertRaises(OverflowError, lambda: "a" * self.pos) |
---|
271 | n/a | self.assertRaises(OverflowError, lambda: "a" * self.neg) |
---|
272 | n/a | |
---|
273 | n/a | |
---|
274 | n/a | if __name__ == "__main__": |
---|
275 | n/a | unittest.main() |
---|