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