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)
53 def test_subclasses(self
):
55 self
.assertEqual(r
[TrapInt(5):TrapInt(10)], r
[5:10])
56 self
.assertEqual(r
[TrapLong(5):TrapLong(10)], r
[5:10])
57 self
.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
58 self
.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
63 self
.assertRaises(TypeError, operator
.index
, self
.o
)
64 self
.assertRaises(TypeError, operator
.index
, self
.n
)
65 self
.assertRaises(TypeError, slice(self
.o
).indices
, 0)
66 self
.assertRaises(TypeError, slice(self
.n
).indices
, 0)
69 class SeqTestCase(unittest
.TestCase
):
70 # This test case isn't run directly. It just defines common tests
71 # to the different sequence types below
81 self
.assertEqual(self
.seq
[self
.n
], self
.seq
[2])
82 self
.assertEqual(self
.seq
[self
.o
], self
.seq
[-2])
89 self
.assertEqual(self
.seq
[self
.o
:self
.o2
], self
.seq
[1:3])
90 self
.assertEqual(self
.seq
[self
.n
:self
.n2
], self
.seq
[2:4])
92 def test_repeat(self
):
95 self
.assertEqual(self
.seq
* self
.o
, self
.seq
* 3)
96 self
.assertEqual(self
.seq
* self
.n
, self
.seq
* 2)
97 self
.assertEqual(self
.o
* self
.seq
, self
.seq
* 3)
98 self
.assertEqual(self
.n
* self
.seq
, self
.seq
* 2)
100 def test_wrappers(self
):
103 self
.assertEqual(self
.seq
.__getitem
__(self
.o
), self
.seq
[4])
104 self
.assertEqual(self
.seq
.__mul
__(self
.o
), self
.seq
* 4)
105 self
.assertEqual(self
.seq
.__rmul
__(self
.o
), self
.seq
* 4)
106 self
.assertEqual(self
.seq
.__getitem
__(self
.n
), self
.seq
[5])
107 self
.assertEqual(self
.seq
.__mul
__(self
.n
), self
.seq
* 5)
108 self
.assertEqual(self
.seq
.__rmul
__(self
.n
), self
.seq
* 5)
110 def test_subclasses(self
):
111 self
.assertEqual(self
.seq
[TrapInt()], self
.seq
[0])
112 self
.assertEqual(self
.seq
[TrapLong()], self
.seq
[0])
114 def test_error(self
):
117 indexobj
= lambda x
, obj
: obj
.seq
[x
]
118 self
.assertRaises(TypeError, indexobj
, self
.o
, self
)
119 self
.assertRaises(TypeError, indexobj
, self
.n
, self
)
120 sliceobj
= lambda x
, obj
: obj
.seq
[x
:]
121 self
.assertRaises(TypeError, sliceobj
, self
.o
, self
)
122 self
.assertRaises(TypeError, sliceobj
, self
.n
, self
)
125 class ListTestCase(SeqTestCase
):
126 seq
= [0,10,20,30,40,50]
128 def test_setdelitem(self
):
131 lst
= list('ab!cdefghi!j')
136 self
.assertEqual(lst
, list('abYdefghXj'))
138 lst
= [5, 6, 7, 8, 9, 10, 11]
139 lst
.__setitem
__(self
.n
, "here")
140 self
.assertEqual(lst
, [5, 6, "here", 8, 9, 10, 11])
141 lst
.__delitem
__(self
.n
)
142 self
.assertEqual(lst
, [5, 6, 8, 9, 10, 11])
144 def test_inplace_repeat(self
):
149 self
.assertEqual(lst
, [6, 4, 6, 4])
151 self
.assertEqual(lst
, [6, 4, 6, 4] * 3)
153 lst
= [5, 6, 7, 8, 9, 11]
154 l2
= lst
.__imul
__(self
.n
)
155 self
.assertTrue(l2
is lst
)
156 self
.assertEqual(lst
, [5, 6, 7, 8, 9, 11] * 3)
159 class TupleTestCase(SeqTestCase
):
160 seq
= (0,10,20,30,40,50)
162 class StringTestCase(SeqTestCase
):
163 seq
= "this is a test"
165 class UnicodeTestCase(SeqTestCase
):
166 seq
= u
"this is a test"
169 class XRangeTestCase(unittest
.TestCase
):
171 def test_xrange(self
):
174 self
.assertEqual(xrange(1, 20)[n
], 6)
175 self
.assertEqual(xrange(1, 20).__getitem
__(n
), 6)
177 class OverflowTestCase(unittest
.TestCase
):
183 def test_large_longs(self
):
184 self
.assertEqual(self
.pos
.__index
__(), self
.pos
)
185 self
.assertEqual(self
.neg
.__index
__(), self
.neg
)
187 def _getitem_helper(self
, base
):
190 return maxint
#cannot return long here
191 def __getitem__(self
, key
):
193 def __getslice__(self
, i
, j
):
196 self
.assertEqual(x
[self
.pos
], self
.pos
)
197 self
.assertEqual(x
[self
.neg
], self
.neg
)
198 self
.assertEqual(x
[self
.neg
:self
.pos
], (maxint
+minsize
, maxsize
))
199 self
.assertEqual(x
[self
.neg
:self
.pos
:1].indices(maxsize
), (0, maxsize
, 1))
201 def test_getitem(self
):
202 self
._getitem
_helper
(object)
204 def test_getitem_classic(self
):
206 self
._getitem
_helper
(Empty
)
208 def test_sequence_repeat(self
):
209 self
.assertRaises(OverflowError, lambda: "a" * self
.pos
)
210 self
.assertRaises(OverflowError, lambda: "a" * self
.neg
)
214 test_support
.run_unittest(
224 if __name__
== "__main__":