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