Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 1 | import unittest |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 2 | import sys |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 3 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 6 | class G: |
| 7 | 'Sequence using __getitem__' |
| 8 | def __init__(self, seqn): |
| 9 | self.seqn = seqn |
| 10 | def __getitem__(self, i): |
| 11 | return self.seqn[i] |
| 12 | |
| 13 | class I: |
| 14 | 'Sequence using iterator protocol' |
| 15 | def __init__(self, seqn): |
| 16 | self.seqn = seqn |
| 17 | self.i = 0 |
| 18 | def __iter__(self): |
| 19 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 20 | def __next__(self): |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 21 | if self.i >= len(self.seqn): raise StopIteration |
| 22 | v = self.seqn[self.i] |
| 23 | self.i += 1 |
| 24 | return v |
| 25 | |
| 26 | class Ig: |
| 27 | 'Sequence using iterator protocol defined with a generator' |
| 28 | def __init__(self, seqn): |
| 29 | self.seqn = seqn |
| 30 | self.i = 0 |
| 31 | def __iter__(self): |
| 32 | for val in self.seqn: |
| 33 | yield val |
| 34 | |
| 35 | class X: |
| 36 | 'Missing __getitem__ and __iter__' |
| 37 | def __init__(self, seqn): |
| 38 | self.seqn = seqn |
| 39 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 40 | def __next__(self): |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 41 | if self.i >= len(self.seqn): raise StopIteration |
| 42 | v = self.seqn[self.i] |
| 43 | self.i += 1 |
| 44 | return v |
| 45 | |
| 46 | class E: |
| 47 | 'Test propagation of exceptions' |
| 48 | def __init__(self, seqn): |
| 49 | self.seqn = seqn |
| 50 | self.i = 0 |
| 51 | def __iter__(self): |
| 52 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 53 | def __next__(self): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 54 | 3 // 0 |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 55 | |
| 56 | class N: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 57 | 'Iterator missing __next__()' |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +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 EnumerateTestCase(unittest.TestCase): |
| 65 | |
| 66 | enum = enumerate |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 67 | seq, res = 'abc', [(0,'a'), (1,'b'), (2,'c')] |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 68 | |
| 69 | def test_basicfunction(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 70 | self.assertEqual(type(self.enum(self.seq)), self.enum) |
| 71 | e = self.enum(self.seq) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 72 | self.assertEqual(iter(e), e) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 73 | self.assertEqual(list(self.enum(self.seq)), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 74 | self.enum.__doc__ |
| 75 | |
| 76 | def test_getitemseqn(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 77 | self.assertEqual(list(self.enum(G(self.seq))), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 78 | e = self.enum(G('')) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 79 | self.assertRaises(StopIteration, next, e) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 80 | |
| 81 | def test_iteratorseqn(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 82 | self.assertEqual(list(self.enum(I(self.seq))), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 83 | e = self.enum(I('')) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 84 | self.assertRaises(StopIteration, next, e) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 85 | |
| 86 | def test_iteratorgenerator(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 87 | self.assertEqual(list(self.enum(Ig(self.seq))), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 88 | e = self.enum(Ig('')) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 89 | self.assertRaises(StopIteration, next, e) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 90 | |
| 91 | def test_noniterable(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 92 | self.assertRaises(TypeError, self.enum, X(self.seq)) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 93 | |
| 94 | def test_illformediterable(self): |
Thomas Wouters | 1034dad | 2006-04-15 09:16:16 +0000 | [diff] [blame] | 95 | self.assertRaises(TypeError, self.enum, N(self.seq)) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 96 | |
| 97 | def test_exception_propagation(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 98 | self.assertRaises(ZeroDivisionError, list, self.enum(E(self.seq))) |
| 99 | |
| 100 | def test_argumentcheck(self): |
| 101 | self.assertRaises(TypeError, self.enum) # no arguments |
| 102 | self.assertRaises(TypeError, self.enum, 1) # wrong type (not iterable) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 103 | self.assertRaises(TypeError, self.enum, 'abc', 'a') # wrong type |
| 104 | self.assertRaises(TypeError, self.enum, 'abc', 2, 3) # too many arguments |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 105 | |
| 106 | def test_tuple_reuse(self): |
| 107 | # Tests an implementation detail where tuple is reused |
| 108 | # whenever nothing else holds a reference to it |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 109 | self.assertEqual(len(set(map(id, list(enumerate(self.seq))))), len(self.seq)) |
| 110 | self.assertEqual(len(set(map(id, enumerate(self.seq)))), min(1,len(self.seq))) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 111 | |
| 112 | class MyEnum(enumerate): |
| 113 | pass |
| 114 | |
| 115 | class SubclassTestCase(EnumerateTestCase): |
| 116 | |
| 117 | enum = MyEnum |
| 118 | |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 119 | class TestEmpty(EnumerateTestCase): |
| 120 | |
| 121 | seq, res = '', [] |
| 122 | |
| 123 | class TestBig(EnumerateTestCase): |
| 124 | |
| 125 | seq = range(10,20000,2) |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 126 | res = list(zip(range(20000), seq)) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 127 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 128 | class TestReversed(unittest.TestCase): |
| 129 | |
| 130 | def test_simple(self): |
| 131 | class A: |
| 132 | def __getitem__(self, i): |
| 133 | if i < 5: |
| 134 | return str(i) |
| 135 | raise StopIteration |
| 136 | def __len__(self): |
| 137 | return 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 138 | for data in 'abc', range(5), tuple(enumerate('abc')), A(), range(1,17,5): |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 139 | self.assertEqual(list(data)[::-1], list(reversed(data))) |
| 140 | self.assertRaises(TypeError, reversed, {}) |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 141 | # don't allow keyword arguments |
| 142 | self.assertRaises(TypeError, reversed, [], a=1) |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 144 | def test_range_optimization(self): |
| 145 | x = range(1) |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 146 | self.assertEqual(type(reversed(x)), type(iter(x))) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 147 | |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 148 | def test_len(self): |
Raymond Hettinger | d2c3626 | 2004-03-10 08:32:47 +0000 | [diff] [blame] | 149 | # This is an implementation detail, not an interface requirement |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 150 | from test.test_iterlen import len |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 151 | for s in ('hello', tuple('hello'), list('hello'), range(5)): |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 152 | self.assertEqual(len(reversed(s)), len(s)) |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 153 | r = reversed(s) |
| 154 | list(r) |
| 155 | self.assertEqual(len(r), 0) |
| 156 | class SeqWithWeirdLen: |
| 157 | called = False |
| 158 | def __len__(self): |
| 159 | if not self.called: |
| 160 | self.called = True |
| 161 | return 10 |
| 162 | raise ZeroDivisionError |
| 163 | def __getitem__(self, index): |
| 164 | return index |
| 165 | r = reversed(SeqWithWeirdLen()) |
| 166 | self.assertRaises(ZeroDivisionError, len, r) |
| 167 | |
| 168 | |
| 169 | def test_gc(self): |
| 170 | class Seq: |
| 171 | def __len__(self): |
| 172 | return 10 |
| 173 | def __getitem__(self, index): |
| 174 | return index |
| 175 | s = Seq() |
| 176 | r = reversed(s) |
| 177 | s.r = r |
| 178 | |
| 179 | def test_args(self): |
| 180 | self.assertRaises(TypeError, reversed) |
| 181 | self.assertRaises(TypeError, reversed, [], 'extra') |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 182 | |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 183 | def test_bug1229429(self): |
| 184 | # this bug was never in reversed, it was in |
| 185 | # PyObject_CallMethod, and reversed_new calls that sometimes. |
| 186 | if not hasattr(sys, "getrefcount"): |
| 187 | return |
| 188 | def f(): |
| 189 | pass |
| 190 | r = f.__reversed__ = object() |
| 191 | rc = sys.getrefcount(r) |
| 192 | for i in range(10): |
| 193 | try: |
| 194 | reversed(f) |
| 195 | except TypeError: |
| 196 | pass |
| 197 | else: |
| 198 | self.fail("non-callable __reversed__ didn't raise!") |
| 199 | self.assertEqual(rc, sys.getrefcount(r)) |
Tim Peters | f5f32b4 | 2005-07-17 23:16:17 +0000 | [diff] [blame] | 200 | |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 201 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 202 | class TestStart(EnumerateTestCase): |
| 203 | |
| 204 | enum = lambda i: enumerate(i, start=11) |
| 205 | seq, res = 'abc', [(1, 'a'), (2, 'b'), (3, 'c')] |
| 206 | |
| 207 | |
| 208 | class TestLongStart(EnumerateTestCase): |
| 209 | |
| 210 | enum = lambda i: enumerate(i, start=sys.maxsize+1) |
| 211 | seq, res = 'abc', [(sys.maxsize+1,'a'), (sys.maxsize+2,'b'), |
| 212 | (sys.maxsize+3,'c')] |
| 213 | |
| 214 | |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 215 | def test_main(verbose=None): |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 216 | testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig, |
| 217 | TestReversed) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 218 | support.run_unittest(*testclasses) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 219 | |
| 220 | # verify reference counting |
| 221 | import sys |
| 222 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 223 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 224 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 225 | support.run_unittest(*testclasses) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 226 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 227 | print(counts) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 228 | |
| 229 | if __name__ == "__main__": |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 230 | test_main(verbose=True) |