2 from test
import test_support
5 maxsize
= test_support
.MAX_Py_ssize_t
12 class newstyle(object):
24 class BaseTestCase(unittest
.TestCase
):
32 self
.assertEqual(operator
.index(self
.o
), -2)
33 self
.assertEqual(operator
.index(self
.n
), 2)
38 slc
= slice(self
.o
, self
.o
, self
.o
)
39 check_slc
= slice(1, 1, 1)
40 self
.assertEqual(slc
.indices(self
.o
), check_slc
.indices(1))
41 slc
= slice(self
.n
, self
.n
, self
.n
)
42 check_slc
= slice(2, 2, 2)
43 self
.assertEqual(slc
.indices(self
.n
), check_slc
.indices(2))
45 def test_wrappers(self
):
48 self
.assertEqual(6 .__index
__(), 6)
49 self
.assertEqual(-7L.__index
__(), -7)
50 self
.assertEqual(self
.o
.__index
__(), 4)
51 self
.assertEqual(self
.n
.__index
__(), 5)
52 self
.assertEqual(True.__index
__(), 1)
53 self
.assertEqual(False.__index
__(), 0)
55 def test_subclasses(self
):
57 self
.assertEqual(r
[TrapInt(5):TrapInt(10)], r
[5:10])
58 self
.assertEqual(r
[TrapLong(5):TrapLong(10)], r
[5:10])
59 self
.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
60 self
.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
65 self
.assertRaises(TypeError, operator
.index
, self
.o
)
66 self
.assertRaises(TypeError, operator
.index
, self
.n
)
67 self
.assertRaises(TypeError, slice(self
.o
).indices
, 0)
68 self
.assertRaises(TypeError, slice(self
.n
).indices
, 0)
71 class SeqTestCase(unittest
.TestCase
):
72 # This test case isn't run directly. It just defines common tests
73 # to the different sequence types below
83 self
.assertEqual(self
.seq
[self
.n
], self
.seq
[2])
84 self
.assertEqual(self
.seq
[self
.o
], self
.seq
[-2])
91 self
.assertEqual(self
.seq
[self
.o
:self
.o2
], self
.seq
[1:3])
92 self
.assertEqual(self
.seq
[self
.n
:self
.n2
], self
.seq
[2:4])
94 def test_slice_bug7532(self
):
95 seqlen
= len(self
.seq
)
96 self
.o
.ind
= int(seqlen
* 1.5)
97 self
.n
.ind
= seqlen
+ 2
98 self
.assertEqual(self
.seq
[self
.o
:], self
.seq
[0:0])
99 self
.assertEqual(self
.seq
[:self
.o
], self
.seq
)
100 self
.assertEqual(self
.seq
[self
.n
:], self
.seq
[0:0])
101 self
.assertEqual(self
.seq
[:self
.n
], self
.seq
)
102 if isinstance(self
.seq
, ClassicSeq
):
104 # These tests fail for ClassicSeq (see bug #7532)
105 self
.o2
.ind
= -seqlen
- 2
106 self
.n2
.ind
= -int(seqlen
* 1.5)
107 self
.assertEqual(self
.seq
[self
.o2
:], self
.seq
)
108 self
.assertEqual(self
.seq
[:self
.o2
], self
.seq
[0:0])
109 self
.assertEqual(self
.seq
[self
.n2
:], self
.seq
)
110 self
.assertEqual(self
.seq
[:self
.n2
], self
.seq
[0:0])
112 def test_repeat(self
):
115 self
.assertEqual(self
.seq
* self
.o
, self
.seq
* 3)
116 self
.assertEqual(self
.seq
* self
.n
, self
.seq
* 2)
117 self
.assertEqual(self
.o
* self
.seq
, self
.seq
* 3)
118 self
.assertEqual(self
.n
* self
.seq
, self
.seq
* 2)
120 def test_wrappers(self
):
123 self
.assertEqual(self
.seq
.__getitem
__(self
.o
), self
.seq
[4])
124 self
.assertEqual(self
.seq
.__mul
__(self
.o
), self
.seq
* 4)
125 self
.assertEqual(self
.seq
.__rmul
__(self
.o
), self
.seq
* 4)
126 self
.assertEqual(self
.seq
.__getitem
__(self
.n
), self
.seq
[5])
127 self
.assertEqual(self
.seq
.__mul
__(self
.n
), self
.seq
* 5)
128 self
.assertEqual(self
.seq
.__rmul
__(self
.n
), self
.seq
* 5)
130 def test_subclasses(self
):
131 self
.assertEqual(self
.seq
[TrapInt()], self
.seq
[0])
132 self
.assertEqual(self
.seq
[TrapLong()], self
.seq
[0])
134 def test_error(self
):
137 indexobj
= lambda x
, obj
: obj
.seq
[x
]
138 self
.assertRaises(TypeError, indexobj
, self
.o
, self
)
139 self
.assertRaises(TypeError, indexobj
, self
.n
, self
)
140 sliceobj
= lambda x
, obj
: obj
.seq
[x
:]
141 self
.assertRaises(TypeError, sliceobj
, self
.o
, self
)
142 self
.assertRaises(TypeError, sliceobj
, self
.n
, self
)
145 class ListTestCase(SeqTestCase
):
146 seq
= [0,10,20,30,40,50]
148 def test_setdelitem(self
):
151 lst
= list('ab!cdefghi!j')
156 self
.assertEqual(lst
, list('abYdefghXj'))
158 lst
= [5, 6, 7, 8, 9, 10, 11]
159 lst
.__setitem
__(self
.n
, "here")
160 self
.assertEqual(lst
, [5, 6, "here", 8, 9, 10, 11])
161 lst
.__delitem
__(self
.n
)
162 self
.assertEqual(lst
, [5, 6, 8, 9, 10, 11])
164 def test_inplace_repeat(self
):
169 self
.assertEqual(lst
, [6, 4, 6, 4])
171 self
.assertEqual(lst
, [6, 4, 6, 4] * 3)
173 lst
= [5, 6, 7, 8, 9, 11]
174 l2
= lst
.__imul
__(self
.n
)
175 self
.assertIs(l2
, lst
)
176 self
.assertEqual(lst
, [5, 6, 7, 8, 9, 11] * 3)
181 def __init__(self
, iterable
):
182 self
._list
= list(iterable
)
185 return repr(self
._list
)
187 def __eq__(self
, other
):
188 return self
._list
== other
191 return len(self
._list
)
193 def __mul__(self
, n
):
194 return self
.__class
__(self
._list
*n
)
197 def __getitem__(self
, index
):
198 return self
._list
[index
]
201 class _GetSliceMixin
:
203 def __getslice__(self
, i
, j
):
204 return self
._list
.__getslice
__(i
, j
)
207 class ClassicSeq(_BaseSeq
): pass
208 class NewSeq(_BaseSeq
, object): pass
209 class ClassicSeqDeprecated(_GetSliceMixin
, ClassicSeq
): pass
210 class NewSeqDeprecated(_GetSliceMixin
, NewSeq
): pass
213 class TupleTestCase(SeqTestCase
):
214 seq
= (0,10,20,30,40,50)
216 class StringTestCase(SeqTestCase
):
217 seq
= "this is a test"
219 class ByteArrayTestCase(SeqTestCase
):
220 seq
= bytearray("this is a test")
222 class UnicodeTestCase(SeqTestCase
):
223 seq
= u
"this is a test"
225 class ClassicSeqTestCase(SeqTestCase
):
226 seq
= ClassicSeq((0,10,20,30,40,50))
228 class NewSeqTestCase(SeqTestCase
):
229 seq
= NewSeq((0,10,20,30,40,50))
231 class ClassicSeqDeprecatedTestCase(SeqTestCase
):
232 seq
= ClassicSeqDeprecated((0,10,20,30,40,50))
234 class NewSeqDeprecatedTestCase(SeqTestCase
):
235 seq
= NewSeqDeprecated((0,10,20,30,40,50))
238 class XRangeTestCase(unittest
.TestCase
):
240 def test_xrange(self
):
243 self
.assertEqual(xrange(1, 20)[n
], 6)
244 self
.assertEqual(xrange(1, 20).__getitem
__(n
), 6)
246 class OverflowTestCase(unittest
.TestCase
):
252 def test_large_longs(self
):
253 self
.assertEqual(self
.pos
.__index
__(), self
.pos
)
254 self
.assertEqual(self
.neg
.__index
__(), self
.neg
)
256 def _getitem_helper(self
, base
):
259 return maxint
# cannot return long here
260 def __getitem__(self
, key
):
263 self
.assertEqual(x
[self
.pos
], self
.pos
)
264 self
.assertEqual(x
[self
.neg
], self
.neg
)
265 self
.assertEqual(x
[self
.neg
:self
.pos
].indices(maxsize
),
267 self
.assertEqual(x
[self
.neg
:self
.pos
:1].indices(maxsize
),
270 def _getslice_helper_deprecated(self
, base
):
273 return maxint
# cannot return long here
274 def __getitem__(self
, key
):
276 def __getslice__(self
, i
, j
):
279 self
.assertEqual(x
[self
.pos
], self
.pos
)
280 self
.assertEqual(x
[self
.neg
], self
.neg
)
281 self
.assertEqual(x
[self
.neg
:self
.pos
], (maxint
+minsize
, maxsize
))
282 self
.assertEqual(x
[self
.neg
:self
.pos
:1].indices(maxsize
),
285 def test_getitem(self
):
286 self
._getitem
_helper
(object)
287 with test_support
.check_py3k_warnings():
288 self
._getslice
_helper
_deprecated
(object)
290 def test_getitem_classic(self
):
292 # XXX This test fails (see bug #7532)
293 #self._getitem_helper(Empty)
294 with test_support
.check_py3k_warnings():
295 self
._getslice
_helper
_deprecated
(Empty
)
297 def test_sequence_repeat(self
):
298 self
.assertRaises(OverflowError, lambda: "a" * self
.pos
)
299 self
.assertRaises(OverflowError, lambda: "a" * self
.neg
)
303 test_support
.run_unittest(
315 with test_support
.check_py3k_warnings():
316 test_support
.run_unittest(
317 ClassicSeqDeprecatedTestCase
,
318 NewSeqDeprecatedTestCase
,
322 if __name__
== "__main__":