Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1 | doctests = """ |
| 2 | |
| 3 | Test simple loop with conditional |
| 4 | |
| 5 | >>> sum(i*i for i in range(100) if i&1 == 1) |
| 6 | 166650 |
| 7 | |
| 8 | Test simple nesting |
| 9 | |
| 10 | >>> list((i,j) for i in range(3) for j in range(4) ) |
| 11 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 12 | |
| 13 | Test nesting with the inner expression dependent on the outer |
| 14 | |
| 15 | >>> list((i,j) for i in range(4) for j in range(i) ) |
| 16 | [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] |
| 17 | |
| 18 | Make sure the induction variable is not exposed |
| 19 | |
| 20 | >>> i = 20 |
| 21 | >>> sum(i*i for i in range(100)) |
| 22 | 328350 |
| 23 | >>> i |
| 24 | 20 |
| 25 | |
| 26 | Test first class |
| 27 | |
| 28 | >>> g = (i*i for i in range(4)) |
| 29 | >>> type(g) |
| 30 | <type 'generator'> |
| 31 | >>> list(g) |
| 32 | [0, 1, 4, 9] |
| 33 | |
| 34 | Test direct calls to next() |
| 35 | |
| 36 | >>> g = (i*i for i in range(3)) |
| 37 | >>> g.next() |
| 38 | 0 |
| 39 | >>> g.next() |
| 40 | 1 |
| 41 | >>> g.next() |
| 42 | 4 |
| 43 | >>> g.next() |
| 44 | Traceback (most recent call last): |
| 45 | File "<pyshell#21>", line 1, in -toplevel- |
| 46 | g.next() |
| 47 | StopIteration |
| 48 | |
| 49 | Does it stay stopped? |
| 50 | |
| 51 | >>> g.next() |
| 52 | Traceback (most recent call last): |
| 53 | File "<pyshell#21>", line 1, in -toplevel- |
| 54 | g.next() |
| 55 | StopIteration |
| 56 | >>> list(g) |
| 57 | [] |
| 58 | |
| 59 | Test running gen when defining function is out of scope |
| 60 | |
| 61 | >>> def f(n): |
| 62 | ... return (i*i for i in xrange(n)) |
| 63 | ... |
| 64 | >>> list(f(10)) |
| 65 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 66 | |
| 67 | >>> def f(n): |
| 68 | ... return ((i,j) for i in xrange(3) for j in xrange(n)) |
| 69 | ... |
| 70 | >>> list(f(4)) |
| 71 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 72 | >>> def f(n): |
| 73 | ... return ((i,j) for i in xrange(3) for j in xrange(4) if j in xrange(n)) |
| 74 | ... |
| 75 | >>> list(f(4)) |
| 76 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 77 | >>> list(f(2)) |
| 78 | [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] |
| 79 | |
| 80 | #Verify that parenthesis are required in a statement |
| 81 | #>>> def f(n): |
| 82 | #... return i*i for i in xrange(n) |
| 83 | #... |
| 84 | #SyntaxError: invalid syntax |
| 85 | |
| 86 | Verify early binding for the outermost for-expression |
| 87 | |
| 88 | >>> x=10 |
| 89 | >>> g = (i*i for i in range(x)) |
| 90 | >>> x = 5 |
| 91 | >>> list(g) |
| 92 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 93 | |
Raymond Hettinger | 83ee795 | 2004-05-20 23:04:13 +0000 | [diff] [blame^] | 94 | Verify that the outermost for-expression makes an immediate check |
| 95 | for iterability |
| 96 | |
| 97 | >>> (i for i in 6) |
| 98 | Traceback (most recent call last): |
| 99 | File "<pyshell#4>", line 1, in -toplevel- |
| 100 | (i for i in 6) |
| 101 | TypeError: iteration over non-sequence |
| 102 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 103 | Verify late binding for the outermost if-expression |
| 104 | |
| 105 | >>> include = (2,4,6,8) |
| 106 | >>> g = (i*i for i in range(10) if i in include) |
| 107 | >>> include = (1,3,5,7,9) |
| 108 | >>> list(g) |
| 109 | [1, 9, 25, 49, 81] |
| 110 | |
| 111 | Verify late binding for the innermost for-expression |
| 112 | |
| 113 | >>> g = ((i,j) for i in range(3) for j in range(x)) |
| 114 | >>> x = 4 |
| 115 | >>> list(g) |
| 116 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 117 | |
| 118 | Verify re-use of tuples (a side benefit of using genexps over listcomps) |
| 119 | |
| 120 | >>> tupleids = map(id, ((i,i) for i in xrange(10))) |
| 121 | >>> max(tupleids) - min(tupleids) |
| 122 | 0 |
| 123 | |
| 124 | |
| 125 | |
| 126 | ########### Tests borrowed from or inspired by test_generators.py ############ |
| 127 | |
| 128 | Make a generator that acts like range() |
| 129 | |
| 130 | >>> yrange = lambda n: (i for i in xrange(n)) |
| 131 | >>> list(yrange(10)) |
| 132 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 133 | |
| 134 | Generators always return to the most recent caller: |
| 135 | |
| 136 | >>> def creator(): |
| 137 | ... r = yrange(5) |
| 138 | ... print "creator", r.next() |
| 139 | ... return r |
| 140 | ... |
| 141 | >>> def caller(): |
| 142 | ... r = creator() |
| 143 | ... for i in r: |
| 144 | ... print "caller", i |
| 145 | ... |
| 146 | >>> caller() |
| 147 | creator 0 |
| 148 | caller 1 |
| 149 | caller 2 |
| 150 | caller 3 |
| 151 | caller 4 |
| 152 | |
| 153 | Generators can call other generators: |
| 154 | |
| 155 | >>> def zrange(n): |
| 156 | ... for i in yrange(n): |
| 157 | ... yield i |
| 158 | ... |
| 159 | >>> list(zrange(5)) |
| 160 | [0, 1, 2, 3, 4] |
| 161 | |
| 162 | |
| 163 | Verify that a gen exp cannot be resumed while it is actively running: |
| 164 | |
| 165 | >>> g = (me.next() for i in xrange(10)) |
| 166 | >>> me = g |
| 167 | >>> me.next() |
| 168 | Traceback (most recent call last): |
| 169 | File "<pyshell#30>", line 1, in -toplevel- |
| 170 | me.next() |
| 171 | File "<pyshell#28>", line 1, in <generator expression> |
| 172 | g = (me.next() for i in xrange(10)) |
| 173 | ValueError: generator already executing |
| 174 | |
| 175 | Verify exception propagation |
| 176 | |
| 177 | >>> g = (10 // i for i in (5, 0, 2)) |
| 178 | >>> g.next() |
| 179 | 2 |
| 180 | >>> g.next() |
| 181 | Traceback (most recent call last): |
| 182 | File "<pyshell#37>", line 1, in -toplevel- |
| 183 | g.next() |
| 184 | File "<pyshell#35>", line 1, in <generator expression> |
| 185 | g = (10 // i for i in (5, 0, 2)) |
| 186 | ZeroDivisionError: integer division or modulo by zero |
| 187 | >>> g.next() |
| 188 | Traceback (most recent call last): |
| 189 | File "<pyshell#38>", line 1, in -toplevel- |
| 190 | g.next() |
| 191 | StopIteration |
| 192 | |
| 193 | Make sure that None is a valid return value |
| 194 | |
| 195 | >>> list(None for i in xrange(10)) |
| 196 | [None, None, None, None, None, None, None, None, None, None] |
| 197 | |
| 198 | Check that generator attributes are present |
| 199 | |
| 200 | >>> g = (i*i for i in range(3)) |
| 201 | >>> expected = set(['gi_frame', 'gi_running', 'next']) |
| 202 | >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected |
| 203 | True |
| 204 | |
| 205 | >>> print g.next.__doc__ |
| 206 | x.next() -> the next value, or raise StopIteration |
| 207 | >>> import types |
| 208 | >>> isinstance(g, types.GeneratorType) |
| 209 | True |
| 210 | |
| 211 | Check the __iter__ slot is defined to return self |
| 212 | |
| 213 | >>> iter(g) is g |
| 214 | True |
| 215 | |
| 216 | Verify that the running flag is set properly |
| 217 | |
| 218 | >>> g = (me.gi_running for i in (0,1)) |
| 219 | >>> me = g |
| 220 | >>> me.gi_running |
| 221 | 0 |
| 222 | >>> me.next() |
| 223 | 1 |
| 224 | >>> me.gi_running |
| 225 | 0 |
| 226 | |
| 227 | Verify that genexps are weakly referencable |
| 228 | |
| 229 | >>> import weakref |
| 230 | >>> g = (i*i for i in range(4)) |
| 231 | >>> wr = weakref.ref(g) |
| 232 | >>> wr() is g |
| 233 | True |
| 234 | >>> p = weakref.proxy(g) |
| 235 | >>> list(p) |
| 236 | [0, 1, 4, 9] |
| 237 | |
| 238 | |
| 239 | """ |
| 240 | |
| 241 | |
| 242 | __test__ = {'doctests' : doctests} |
| 243 | |
| 244 | def test_main(verbose=None): |
| 245 | import sys |
| 246 | from test import test_support |
| 247 | from test import test_genexps |
| 248 | test_support.run_doctest(test_genexps, verbose) |
| 249 | |
| 250 | # verify reference counting |
| 251 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 252 | import gc |
| 253 | counts = [None] * 5 |
| 254 | for i in xrange(len(counts)): |
| 255 | test_support.run_doctest(test_genexps, verbose) |
| 256 | gc.collect() |
| 257 | counts[i] = sys.gettotalrefcount() |
| 258 | print counts |
| 259 | |
| 260 | if __name__ == "__main__": |
| 261 | test_main(verbose=True) |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |