Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 1 | """ |
| 2 | Tests common to tuple, list and UserList.UserList |
| 3 | """ |
| 4 | |
| 5 | import unittest |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 6 | import sys |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 7 | |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 8 | # Various iterables |
| 9 | # This is used for checking the constructor (here and in test_deque.py) |
| 10 | def iterfunc(seqn): |
| 11 | 'Regular generator' |
| 12 | for i in seqn: |
| 13 | yield i |
| 14 | |
| 15 | class Sequence: |
| 16 | 'Sequence using __getitem__' |
| 17 | def __init__(self, seqn): |
| 18 | self.seqn = seqn |
| 19 | def __getitem__(self, i): |
| 20 | return self.seqn[i] |
| 21 | |
| 22 | class IterFunc: |
| 23 | 'Sequence using iterator protocol' |
| 24 | def __init__(self, seqn): |
| 25 | self.seqn = seqn |
| 26 | self.i = 0 |
| 27 | def __iter__(self): |
| 28 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 29 | def __next__(self): |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 30 | if self.i >= len(self.seqn): raise StopIteration |
| 31 | v = self.seqn[self.i] |
| 32 | self.i += 1 |
| 33 | return v |
| 34 | |
| 35 | class IterGen: |
| 36 | 'Sequence using iterator protocol defined with a generator' |
| 37 | def __init__(self, seqn): |
| 38 | self.seqn = seqn |
| 39 | self.i = 0 |
| 40 | def __iter__(self): |
| 41 | for val in self.seqn: |
| 42 | yield val |
| 43 | |
| 44 | class IterNextOnly: |
| 45 | 'Missing __getitem__ and __iter__' |
| 46 | def __init__(self, seqn): |
| 47 | self.seqn = seqn |
| 48 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 49 | def __next__(self): |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 50 | if self.i >= len(self.seqn): raise StopIteration |
| 51 | v = self.seqn[self.i] |
| 52 | self.i += 1 |
| 53 | return v |
| 54 | |
| 55 | class IterNoNext: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 56 | 'Iterator missing __next__()' |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 57 | def __init__(self, seqn): |
| 58 | self.seqn = seqn |
| 59 | self.i = 0 |
| 60 | def __iter__(self): |
| 61 | return self |
| 62 | |
| 63 | class IterGenExc: |
| 64 | 'Test propagation of exceptions' |
| 65 | def __init__(self, seqn): |
| 66 | self.seqn = seqn |
| 67 | self.i = 0 |
| 68 | def __iter__(self): |
| 69 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 70 | def __next__(self): |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 71 | 3 // 0 |
| 72 | |
| 73 | class IterFuncStop: |
| 74 | 'Test immediate stop' |
| 75 | def __init__(self, seqn): |
| 76 | pass |
| 77 | def __iter__(self): |
| 78 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 79 | def __next__(self): |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 80 | raise StopIteration |
| 81 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 82 | from itertools import chain |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 83 | def itermulti(seqn): |
| 84 | 'Test multiple tiers of iterators' |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 85 | return chain(map(lambda x:x, iterfunc(IterGen(Sequence(seqn))))) |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 86 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 87 | class CommonTest(unittest.TestCase): |
| 88 | # The type to be tested |
| 89 | type2test = None |
| 90 | |
| 91 | def test_constructors(self): |
| 92 | l0 = [] |
| 93 | l1 = [0] |
| 94 | l2 = [0, 1] |
| 95 | |
| 96 | u = self.type2test() |
| 97 | u0 = self.type2test(l0) |
| 98 | u1 = self.type2test(l1) |
| 99 | u2 = self.type2test(l2) |
| 100 | |
| 101 | uu = self.type2test(u) |
| 102 | uu0 = self.type2test(u0) |
| 103 | uu1 = self.type2test(u1) |
| 104 | uu2 = self.type2test(u2) |
| 105 | |
| 106 | v = self.type2test(tuple(u)) |
| 107 | class OtherSeq: |
| 108 | def __init__(self, initseq): |
| 109 | self.__data = initseq |
| 110 | def __len__(self): |
| 111 | return len(self.__data) |
| 112 | def __getitem__(self, i): |
| 113 | return self.__data[i] |
| 114 | s = OtherSeq(u0) |
| 115 | v0 = self.type2test(s) |
| 116 | self.assertEqual(len(v0), len(s)) |
| 117 | |
| 118 | s = "this is also a sequence" |
| 119 | vv = self.type2test(s) |
| 120 | self.assertEqual(len(vv), len(s)) |
| 121 | |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 122 | # Create from various iteratables |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 123 | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 124 | for g in (Sequence, IterFunc, IterGen, |
| 125 | itermulti, iterfunc): |
| 126 | self.assertEqual(self.type2test(g(s)), self.type2test(s)) |
| 127 | self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test()) |
| 128 | self.assertEqual(self.type2test(c for c in "123"), self.type2test("123")) |
| 129 | self.assertRaises(TypeError, self.type2test, IterNextOnly(s)) |
| 130 | self.assertRaises(TypeError, self.type2test, IterNoNext(s)) |
| 131 | self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s)) |
| 132 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 133 | def test_truth(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 134 | self.assertFalse(self.type2test()) |
| 135 | self.assertTrue(self.type2test([42])) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 136 | |
| 137 | def test_getitem(self): |
| 138 | u = self.type2test([0, 1, 2, 3, 4]) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 139 | for i in range(len(u)): |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 140 | self.assertEqual(u[i], i) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 141 | self.assertEqual(u[int(i)], i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 142 | for i in range(-len(u), -1): |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 143 | self.assertEqual(u[i], len(u)+i) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 144 | self.assertEqual(u[int(i)], len(u)+i) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 145 | self.assertRaises(IndexError, u.__getitem__, -len(u)-1) |
| 146 | self.assertRaises(IndexError, u.__getitem__, len(u)) |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 147 | self.assertRaises(ValueError, u.__getitem__, slice(0,10,0)) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 148 | |
| 149 | u = self.type2test() |
| 150 | self.assertRaises(IndexError, u.__getitem__, 0) |
| 151 | self.assertRaises(IndexError, u.__getitem__, -1) |
| 152 | |
| 153 | self.assertRaises(TypeError, u.__getitem__) |
| 154 | |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 155 | a = self.type2test([10, 11]) |
| 156 | self.assertEqual(a[0], 10) |
| 157 | self.assertEqual(a[1], 11) |
| 158 | self.assertEqual(a[-2], 10) |
| 159 | self.assertEqual(a[-1], 11) |
| 160 | self.assertRaises(IndexError, a.__getitem__, -3) |
| 161 | self.assertRaises(IndexError, a.__getitem__, 3) |
| 162 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 163 | def test_getslice(self): |
| 164 | l = [0, 1, 2, 3, 4] |
| 165 | u = self.type2test(l) |
| 166 | |
| 167 | self.assertEqual(u[0:0], self.type2test()) |
| 168 | self.assertEqual(u[1:2], self.type2test([1])) |
| 169 | self.assertEqual(u[-2:-1], self.type2test([3])) |
| 170 | self.assertEqual(u[-1000:1000], u) |
| 171 | self.assertEqual(u[1000:-1000], self.type2test([])) |
| 172 | self.assertEqual(u[:], u) |
| 173 | self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4])) |
| 174 | self.assertEqual(u[None:3], self.type2test([0, 1, 2])) |
| 175 | |
| 176 | # Extended slices |
| 177 | self.assertEqual(u[::], u) |
| 178 | self.assertEqual(u[::2], self.type2test([0, 2, 4])) |
| 179 | self.assertEqual(u[1::2], self.type2test([1, 3])) |
| 180 | self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0])) |
| 181 | self.assertEqual(u[::-2], self.type2test([4, 2, 0])) |
| 182 | self.assertEqual(u[3::-2], self.type2test([3, 1])) |
| 183 | self.assertEqual(u[3:3:-2], self.type2test([])) |
| 184 | self.assertEqual(u[3:2:-2], self.type2test([3])) |
| 185 | self.assertEqual(u[3:1:-2], self.type2test([3])) |
| 186 | self.assertEqual(u[3:0:-2], self.type2test([3, 1])) |
| 187 | self.assertEqual(u[::-100], self.type2test([4])) |
| 188 | self.assertEqual(u[100:-100:], self.type2test([])) |
| 189 | self.assertEqual(u[-100:100:], u) |
| 190 | self.assertEqual(u[100:-100:-1], u[::-1]) |
| 191 | self.assertEqual(u[-100:100:-1], self.type2test([])) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 192 | self.assertEqual(u[-100:100:2], self.type2test([0, 2, 4])) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 193 | |
| 194 | # Test extreme cases with long ints |
| 195 | a = self.type2test([0,1,2,3,4]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 196 | self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2])) |
| 197 | self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4])) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 198 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 199 | def test_contains(self): |
| 200 | u = self.type2test([0, 1, 2]) |
| 201 | for i in u: |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 202 | self.assertIn(i, u) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 203 | for i in min(u)-1, max(u)+1: |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 204 | self.assertNotIn(i, u) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 205 | |
| 206 | self.assertRaises(TypeError, u.__contains__) |
| 207 | |
Georg Brandl | be3856d | 2005-08-24 09:08:57 +0000 | [diff] [blame] | 208 | def test_contains_fake(self): |
| 209 | class AllEq: |
| 210 | # Sequences must use rich comparison against each item |
| 211 | # (unless "is" is true, or an earlier item answered) |
| 212 | # So instances of AllEq must be found in all non-empty sequences. |
| 213 | def __eq__(self, other): |
| 214 | return True |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 215 | __hash__ = None # Can't meet hash invariant requirements |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 216 | self.assertNotIn(AllEq(), self.type2test([])) |
| 217 | self.assertIn(AllEq(), self.type2test([1])) |
Georg Brandl | be3856d | 2005-08-24 09:08:57 +0000 | [diff] [blame] | 218 | |
| 219 | def test_contains_order(self): |
| 220 | # Sequences must test in-order. If a rich comparison has side |
| 221 | # effects, these will be visible to tests against later members. |
| 222 | # In this test, the "side effect" is a short-circuiting raise. |
| 223 | class DoNotTestEq(Exception): |
| 224 | pass |
| 225 | class StopCompares: |
| 226 | def __eq__(self, other): |
| 227 | raise DoNotTestEq |
Tim Peters | 9e34c04 | 2005-08-26 15:20:46 +0000 | [diff] [blame] | 228 | |
Georg Brandl | be3856d | 2005-08-24 09:08:57 +0000 | [diff] [blame] | 229 | checkfirst = self.type2test([1, StopCompares()]) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 230 | self.assertIn(1, checkfirst) |
Georg Brandl | be3856d | 2005-08-24 09:08:57 +0000 | [diff] [blame] | 231 | checklast = self.type2test([StopCompares(), 1]) |
| 232 | self.assertRaises(DoNotTestEq, checklast.__contains__, 1) |
| 233 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 234 | def test_len(self): |
| 235 | self.assertEqual(len(self.type2test()), 0) |
| 236 | self.assertEqual(len(self.type2test([])), 0) |
| 237 | self.assertEqual(len(self.type2test([0])), 1) |
| 238 | self.assertEqual(len(self.type2test([0, 1, 2])), 3) |
| 239 | |
| 240 | def test_minmax(self): |
| 241 | u = self.type2test([0, 1, 2]) |
| 242 | self.assertEqual(min(u), 0) |
| 243 | self.assertEqual(max(u), 2) |
| 244 | |
| 245 | def test_addmul(self): |
| 246 | u1 = self.type2test([0]) |
| 247 | u2 = self.type2test([0, 1]) |
| 248 | self.assertEqual(u1, u1 + self.type2test()) |
| 249 | self.assertEqual(u1, self.type2test() + u1) |
| 250 | self.assertEqual(u1 + self.type2test([1]), u2) |
| 251 | self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0])) |
| 252 | self.assertEqual(self.type2test(), u2*0) |
| 253 | self.assertEqual(self.type2test(), 0*u2) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 254 | self.assertEqual(self.type2test(), u2*0) |
| 255 | self.assertEqual(self.type2test(), 0*u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 256 | self.assertEqual(u2, u2*1) |
| 257 | self.assertEqual(u2, 1*u2) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 258 | self.assertEqual(u2, u2*1) |
| 259 | self.assertEqual(u2, 1*u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 260 | self.assertEqual(u2+u2, u2*2) |
| 261 | self.assertEqual(u2+u2, 2*u2) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 262 | self.assertEqual(u2+u2, u2*2) |
| 263 | self.assertEqual(u2+u2, 2*u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 264 | self.assertEqual(u2+u2+u2, u2*3) |
| 265 | self.assertEqual(u2+u2+u2, 3*u2) |
| 266 | |
| 267 | class subclass(self.type2test): |
| 268 | pass |
| 269 | u3 = subclass([0, 1]) |
| 270 | self.assertEqual(u3, u3*1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 271 | self.assertIsNot(u3, u3*1) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 272 | |
| 273 | def test_iadd(self): |
| 274 | u = self.type2test([0, 1]) |
| 275 | u += self.type2test() |
| 276 | self.assertEqual(u, self.type2test([0, 1])) |
| 277 | u += self.type2test([2, 3]) |
| 278 | self.assertEqual(u, self.type2test([0, 1, 2, 3])) |
| 279 | u += self.type2test([4, 5]) |
| 280 | self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5])) |
| 281 | |
| 282 | u = self.type2test("spam") |
| 283 | u += self.type2test("eggs") |
| 284 | self.assertEqual(u, self.type2test("spameggs")) |
| 285 | |
| 286 | def test_imul(self): |
| 287 | u = self.type2test([0, 1]) |
| 288 | u *= 3 |
| 289 | self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1])) |
| 290 | |
| 291 | def test_getitemoverwriteiter(self): |
| 292 | # Verify that __getitem__ overrides are not recognized by __iter__ |
| 293 | class T(self.type2test): |
| 294 | def __getitem__(self, key): |
| 295 | return str(key) + '!!!' |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 296 | self.assertEqual(next(iter(T((1,2)))), 1) |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 297 | |
| 298 | def test_repeat(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 299 | for m in range(4): |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 300 | s = tuple(range(m)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 301 | for n in range(-3, 5): |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 302 | self.assertEqual(self.type2test(s*n), self.type2test(s)*n) |
| 303 | self.assertEqual(self.type2test(s)*(-4), self.type2test([])) |
| 304 | self.assertEqual(id(s), id(s*1)) |
| 305 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 306 | def test_bigrepeat(self): |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 307 | import sys |
| 308 | if sys.maxsize <= 2147483647: |
| 309 | x = self.type2test([0]) |
| 310 | x *= 2**16 |
| 311 | self.assertRaises(MemoryError, x.__mul__, 2**16) |
| 312 | if hasattr(x, '__imul__'): |
| 313 | self.assertRaises(MemoryError, x.__imul__, 2**16) |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 314 | |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 315 | def test_subscript(self): |
| 316 | a = self.type2test([10, 11]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 317 | self.assertEqual(a.__getitem__(0), 10) |
| 318 | self.assertEqual(a.__getitem__(1), 11) |
| 319 | self.assertEqual(a.__getitem__(-2), 10) |
| 320 | self.assertEqual(a.__getitem__(-1), 11) |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 321 | self.assertRaises(IndexError, a.__getitem__, -3) |
| 322 | self.assertRaises(IndexError, a.__getitem__, 3) |
| 323 | self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10])) |
| 324 | self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11])) |
| 325 | self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11])) |
| 326 | self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11])) |
| 327 | self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([])) |
| 328 | self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0)) |
| 329 | self.assertRaises(TypeError, a.__getitem__, 'x') |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 330 | |
| 331 | def test_count(self): |
| 332 | a = self.type2test([0, 1, 2])*3 |
| 333 | self.assertEqual(a.count(0), 3) |
| 334 | self.assertEqual(a.count(1), 3) |
| 335 | self.assertEqual(a.count(3), 0) |
| 336 | |
| 337 | self.assertRaises(TypeError, a.count) |
| 338 | |
| 339 | class BadExc(Exception): |
| 340 | pass |
| 341 | |
| 342 | class BadCmp: |
| 343 | def __eq__(self, other): |
| 344 | if other == 2: |
| 345 | raise BadExc() |
| 346 | return False |
| 347 | |
| 348 | self.assertRaises(BadExc, a.count, BadCmp()) |
| 349 | |
| 350 | def test_index(self): |
| 351 | u = self.type2test([0, 1]) |
| 352 | self.assertEqual(u.index(0), 0) |
| 353 | self.assertEqual(u.index(1), 1) |
| 354 | self.assertRaises(ValueError, u.index, 2) |
| 355 | |
| 356 | u = self.type2test([-2, -1, 0, 0, 1, 2]) |
| 357 | self.assertEqual(u.count(0), 2) |
| 358 | self.assertEqual(u.index(0), 2) |
| 359 | self.assertEqual(u.index(0, 2), 2) |
| 360 | self.assertEqual(u.index(-2, -10), 0) |
| 361 | self.assertEqual(u.index(0, 3), 3) |
| 362 | self.assertEqual(u.index(0, 3, 4), 3) |
| 363 | self.assertRaises(ValueError, u.index, 2, 0, -10) |
| 364 | |
| 365 | self.assertRaises(TypeError, u.index) |
| 366 | |
| 367 | class BadExc(Exception): |
| 368 | pass |
| 369 | |
| 370 | class BadCmp: |
| 371 | def __eq__(self, other): |
| 372 | if other == 2: |
| 373 | raise BadExc() |
| 374 | return False |
| 375 | |
| 376 | a = self.type2test([0, 1, 2, 3]) |
| 377 | self.assertRaises(BadExc, a.index, BadCmp()) |
| 378 | |
| 379 | a = self.type2test([-2, -1, 0, 0, 1, 2]) |
| 380 | self.assertEqual(a.index(0), 2) |
| 381 | self.assertEqual(a.index(0, 2), 2) |
| 382 | self.assertEqual(a.index(0, -4), 2) |
| 383 | self.assertEqual(a.index(-2, -10), 0) |
| 384 | self.assertEqual(a.index(0, 3), 3) |
| 385 | self.assertEqual(a.index(0, -3), 3) |
| 386 | self.assertEqual(a.index(0, 3, 4), 3) |
| 387 | self.assertEqual(a.index(0, -3, -2), 3) |
| 388 | self.assertEqual(a.index(0, -4*sys.maxsize, 4*sys.maxsize), 2) |
| 389 | self.assertRaises(ValueError, a.index, 0, 4*sys.maxsize,-4*sys.maxsize) |
| 390 | self.assertRaises(ValueError, a.index, 2, 0, -10) |