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