Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1 | doctests = """ |
| 2 | ########### Tests mostly copied from test_listcomps.py ############ |
| 3 | |
| 4 | Test simple loop with conditional |
| 5 | |
| 6 | >>> sum({i*i for i in range(100) if i&1 == 1}) |
| 7 | 166650 |
| 8 | |
| 9 | Test simple case |
| 10 | |
| 11 | >>> {2*y + x + 1 for x in (0,) for y in (1,)} |
| 12 | {3} |
| 13 | |
| 14 | Test simple nesting |
| 15 | |
| 16 | >>> list(sorted({(i,j) for i in range(3) for j in range(4)})) |
| 17 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 18 | |
| 19 | Test nesting with the inner expression dependent on the outer |
| 20 | |
| 21 | >>> list(sorted({(i,j) for i in range(4) for j in range(i)})) |
| 22 | [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] |
| 23 | |
| 24 | Make sure the induction variable is not exposed |
| 25 | |
| 26 | >>> i = 20 |
| 27 | >>> sum({i*i for i in range(100)}) |
| 28 | 328350 |
| 29 | |
| 30 | >>> i |
| 31 | 20 |
| 32 | |
| 33 | Verify that syntax error's are raised for setcomps used as lvalues |
| 34 | |
| 35 | >>> {y for y in (1,2)} = 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
| 36 | Traceback (most recent call last): |
| 37 | ... |
| 38 | SyntaxError: ... |
| 39 | |
| 40 | >>> {y for y in (1,2)} += 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
| 41 | Traceback (most recent call last): |
| 42 | ... |
| 43 | SyntaxError: ... |
| 44 | |
| 45 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 46 | Make a nested set comprehension that acts like set(range()) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 47 | |
| 48 | >>> def srange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 49 | ... return {i for i in range(n)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 50 | >>> list(sorted(srange(10))) |
| 51 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 52 | |
| 53 | Same again, only as a lambda expression instead of a function definition |
| 54 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 55 | >>> lrange = lambda n: {i for i in range(n)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 56 | >>> list(sorted(lrange(10))) |
| 57 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 58 | |
| 59 | Generators can call other generators: |
| 60 | |
| 61 | >>> def grange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 62 | ... for x in {i for i in range(n)}: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 63 | ... yield x |
| 64 | >>> list(sorted(grange(5))) |
| 65 | [0, 1, 2, 3, 4] |
| 66 | |
| 67 | |
| 68 | Make sure that None is a valid return value |
| 69 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 70 | >>> {None for i in range(10)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 71 | {None} |
| 72 | |
| 73 | ########### Tests for various scoping corner cases ############ |
| 74 | |
| 75 | Return lambdas that use the iteration variable as a default argument |
| 76 | |
| 77 | >>> items = {(lambda i=i: i) for i in range(5)} |
| 78 | >>> {x() for x in items} == set(range(5)) |
| 79 | True |
| 80 | |
| 81 | Same again, only this time as a closure variable |
| 82 | |
| 83 | >>> items = {(lambda: i) for i in range(5)} |
| 84 | >>> {x() for x in items} |
| 85 | {4} |
| 86 | |
| 87 | Another way to test that the iteration variable is local to the list comp |
| 88 | |
| 89 | >>> items = {(lambda: i) for i in range(5)} |
| 90 | >>> i = 20 |
| 91 | >>> {x() for x in items} |
| 92 | {4} |
| 93 | |
| 94 | And confirm that a closure can jump over the list comp scope |
| 95 | |
| 96 | >>> items = {(lambda: y) for i in range(5)} |
| 97 | >>> y = 2 |
| 98 | >>> {x() for x in items} |
| 99 | {2} |
| 100 | |
| 101 | We also repeat each of the above scoping tests inside a function |
| 102 | |
| 103 | >>> def test_func(): |
| 104 | ... items = {(lambda i=i: i) for i in range(5)} |
| 105 | ... return {x() for x in items} |
| 106 | >>> test_func() == set(range(5)) |
| 107 | True |
| 108 | |
| 109 | >>> def test_func(): |
| 110 | ... items = {(lambda: i) for i in range(5)} |
| 111 | ... return {x() for x in items} |
| 112 | >>> test_func() |
| 113 | {4} |
| 114 | |
| 115 | >>> def test_func(): |
| 116 | ... items = {(lambda: i) for i in range(5)} |
| 117 | ... i = 20 |
| 118 | ... return {x() for x in items} |
| 119 | >>> test_func() |
| 120 | {4} |
| 121 | |
| 122 | >>> def test_func(): |
| 123 | ... items = {(lambda: y) for i in range(5)} |
| 124 | ... y = 2 |
| 125 | ... return {x() for x in items} |
| 126 | >>> test_func() |
| 127 | {2} |
| 128 | |
| 129 | """ |
| 130 | |
| 131 | |
| 132 | __test__ = {'doctests' : doctests} |
| 133 | |
| 134 | def test_main(verbose=None): |
| 135 | import sys |
| 136 | from test import test_support |
| 137 | from test import test_listcomps |
| 138 | test_support.run_doctest(test_listcomps, verbose) |
| 139 | |
| 140 | # verify reference counting |
| 141 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 142 | import gc |
| 143 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 144 | for i in range(len(counts)): |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 145 | test_support.run_doctest(test_genexps, verbose) |
| 146 | gc.collect() |
| 147 | counts[i] = sys.gettotalrefcount() |
| 148 | print(counts) |
| 149 | |
| 150 | if __name__ == "__main__": |
| 151 | test_main(verbose=True) |
| 152 | doctests = """ |
| 153 | ########### Tests mostly copied from test_listcomps.py ############ |
| 154 | |
| 155 | Test simple loop with conditional |
| 156 | |
| 157 | >>> sum({i*i for i in range(100) if i&1 == 1}) |
| 158 | 166650 |
| 159 | |
| 160 | Test simple case |
| 161 | |
| 162 | >>> {2*y + x + 1 for x in (0,) for y in (1,)} |
| 163 | {3} |
| 164 | |
| 165 | Test simple nesting |
| 166 | |
| 167 | >>> list(sorted({(i,j) for i in range(3) for j in range(4)})) |
| 168 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 169 | |
| 170 | Test nesting with the inner expression dependent on the outer |
| 171 | |
| 172 | >>> list(sorted({(i,j) for i in range(4) for j in range(i)})) |
| 173 | [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] |
| 174 | |
| 175 | Make sure the induction variable is not exposed |
| 176 | |
| 177 | >>> i = 20 |
| 178 | >>> sum({i*i for i in range(100)}) |
| 179 | 328350 |
| 180 | |
| 181 | >>> i |
| 182 | 20 |
| 183 | |
| 184 | Verify that syntax error's are raised for setcomps used as lvalues |
| 185 | |
| 186 | >>> {y for y in (1,2)} = 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
| 187 | Traceback (most recent call last): |
| 188 | ... |
| 189 | SyntaxError: ... |
| 190 | |
| 191 | >>> {y for y in (1,2)} += 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
| 192 | Traceback (most recent call last): |
| 193 | ... |
| 194 | SyntaxError: ... |
| 195 | |
| 196 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 197 | Make a nested set comprehension that acts like set(range()) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 198 | |
| 199 | >>> def srange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 200 | ... return {i for i in range(n)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 201 | >>> list(sorted(srange(10))) |
| 202 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 203 | |
| 204 | Same again, only as a lambda expression instead of a function definition |
| 205 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 206 | >>> lrange = lambda n: {i for i in range(n)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 207 | >>> list(sorted(lrange(10))) |
| 208 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 209 | |
| 210 | Generators can call other generators: |
| 211 | |
| 212 | >>> def grange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 213 | ... for x in {i for i in range(n)}: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 214 | ... yield x |
| 215 | >>> list(sorted(grange(5))) |
| 216 | [0, 1, 2, 3, 4] |
| 217 | |
| 218 | |
| 219 | Make sure that None is a valid return value |
| 220 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 221 | >>> {None for i in range(10)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 222 | {None} |
| 223 | |
| 224 | ########### Tests for various scoping corner cases ############ |
| 225 | |
| 226 | Return lambdas that use the iteration variable as a default argument |
| 227 | |
| 228 | >>> items = {(lambda i=i: i) for i in range(5)} |
| 229 | >>> {x() for x in items} == set(range(5)) |
| 230 | True |
| 231 | |
| 232 | Same again, only this time as a closure variable |
| 233 | |
| 234 | >>> items = {(lambda: i) for i in range(5)} |
| 235 | >>> {x() for x in items} |
| 236 | {4} |
| 237 | |
| 238 | Another way to test that the iteration variable is local to the list comp |
| 239 | |
| 240 | >>> items = {(lambda: i) for i in range(5)} |
| 241 | >>> i = 20 |
| 242 | >>> {x() for x in items} |
| 243 | {4} |
| 244 | |
| 245 | And confirm that a closure can jump over the list comp scope |
| 246 | |
| 247 | >>> items = {(lambda: y) for i in range(5)} |
| 248 | >>> y = 2 |
| 249 | >>> {x() for x in items} |
| 250 | {2} |
| 251 | |
| 252 | We also repeat each of the above scoping tests inside a function |
| 253 | |
| 254 | >>> def test_func(): |
| 255 | ... items = {(lambda i=i: i) for i in range(5)} |
| 256 | ... return {x() for x in items} |
| 257 | >>> test_func() == set(range(5)) |
| 258 | True |
| 259 | |
| 260 | >>> def test_func(): |
| 261 | ... items = {(lambda: i) for i in range(5)} |
| 262 | ... return {x() for x in items} |
| 263 | >>> test_func() |
| 264 | {4} |
| 265 | |
| 266 | >>> def test_func(): |
| 267 | ... items = {(lambda: i) for i in range(5)} |
| 268 | ... i = 20 |
| 269 | ... return {x() for x in items} |
| 270 | >>> test_func() |
| 271 | {4} |
| 272 | |
| 273 | >>> def test_func(): |
| 274 | ... items = {(lambda: y) for i in range(5)} |
| 275 | ... y = 2 |
| 276 | ... return {x() for x in items} |
| 277 | >>> test_func() |
| 278 | {2} |
| 279 | |
| 280 | """ |
| 281 | |
| 282 | |
| 283 | __test__ = {'doctests' : doctests} |
| 284 | |
| 285 | def test_main(verbose=None): |
| 286 | import sys |
| 287 | from test import test_support |
| 288 | from test import test_listcomps |
| 289 | test_support.run_doctest(test_listcomps, verbose) |
| 290 | |
| 291 | # verify reference counting |
| 292 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 293 | import gc |
| 294 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 295 | for i in range(len(counts)): |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 296 | test_support.run_doctest(test_genexps, verbose) |
| 297 | gc.collect() |
| 298 | counts[i] = sys.gettotalrefcount() |
| 299 | print(counts) |
| 300 | |
| 301 | if __name__ == "__main__": |
| 302 | test_main(verbose=True) |
| 303 | doctests = """ |
| 304 | ########### Tests mostly copied from test_listcomps.py ############ |
| 305 | |
| 306 | Test simple loop with conditional |
| 307 | |
| 308 | >>> sum({i*i for i in range(100) if i&1 == 1}) |
| 309 | 166650 |
| 310 | |
| 311 | Test simple case |
| 312 | |
| 313 | >>> {2*y + x + 1 for x in (0,) for y in (1,)} |
| 314 | {3} |
| 315 | |
| 316 | Test simple nesting |
| 317 | |
| 318 | >>> list(sorted({(i,j) for i in range(3) for j in range(4)})) |
| 319 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 320 | |
| 321 | Test nesting with the inner expression dependent on the outer |
| 322 | |
| 323 | >>> list(sorted({(i,j) for i in range(4) for j in range(i)})) |
| 324 | [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] |
| 325 | |
| 326 | Make sure the induction variable is not exposed |
| 327 | |
| 328 | >>> i = 20 |
| 329 | >>> sum({i*i for i in range(100)}) |
| 330 | 328350 |
| 331 | |
| 332 | >>> i |
| 333 | 20 |
| 334 | |
| 335 | Verify that syntax error's are raised for setcomps used as lvalues |
| 336 | |
| 337 | >>> {y for y in (1,2)} = 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
| 338 | Traceback (most recent call last): |
| 339 | ... |
| 340 | SyntaxError: ... |
| 341 | |
| 342 | >>> {y for y in (1,2)} += 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
| 343 | Traceback (most recent call last): |
| 344 | ... |
| 345 | SyntaxError: ... |
| 346 | |
| 347 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 348 | Make a nested set comprehension that acts like set(range()) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 349 | |
| 350 | >>> def srange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 351 | ... return {i for i in range(n)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 352 | >>> list(sorted(srange(10))) |
| 353 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 354 | |
| 355 | Same again, only as a lambda expression instead of a function definition |
| 356 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 357 | >>> lrange = lambda n: {i for i in range(n)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 358 | >>> list(sorted(lrange(10))) |
| 359 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 360 | |
| 361 | Generators can call other generators: |
| 362 | |
| 363 | >>> def grange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 364 | ... for x in {i for i in range(n)}: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 365 | ... yield x |
| 366 | >>> list(sorted(grange(5))) |
| 367 | [0, 1, 2, 3, 4] |
| 368 | |
| 369 | |
| 370 | Make sure that None is a valid return value |
| 371 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 372 | >>> {None for i in range(10)} |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 373 | {None} |
| 374 | |
| 375 | ########### Tests for various scoping corner cases ############ |
| 376 | |
| 377 | Return lambdas that use the iteration variable as a default argument |
| 378 | |
| 379 | >>> items = {(lambda i=i: i) for i in range(5)} |
| 380 | >>> {x() for x in items} == set(range(5)) |
| 381 | True |
| 382 | |
| 383 | Same again, only this time as a closure variable |
| 384 | |
| 385 | >>> items = {(lambda: i) for i in range(5)} |
| 386 | >>> {x() for x in items} |
| 387 | {4} |
| 388 | |
| 389 | Another way to test that the iteration variable is local to the list comp |
| 390 | |
| 391 | >>> items = {(lambda: i) for i in range(5)} |
| 392 | >>> i = 20 |
| 393 | >>> {x() for x in items} |
| 394 | {4} |
| 395 | |
| 396 | And confirm that a closure can jump over the list comp scope |
| 397 | |
| 398 | >>> items = {(lambda: y) for i in range(5)} |
| 399 | >>> y = 2 |
| 400 | >>> {x() for x in items} |
| 401 | {2} |
| 402 | |
| 403 | We also repeat each of the above scoping tests inside a function |
| 404 | |
| 405 | >>> def test_func(): |
| 406 | ... items = {(lambda i=i: i) for i in range(5)} |
| 407 | ... return {x() for x in items} |
| 408 | >>> test_func() == set(range(5)) |
| 409 | True |
| 410 | |
| 411 | >>> def test_func(): |
| 412 | ... items = {(lambda: i) for i in range(5)} |
| 413 | ... return {x() for x in items} |
| 414 | >>> test_func() |
| 415 | {4} |
| 416 | |
| 417 | >>> def test_func(): |
| 418 | ... items = {(lambda: i) for i in range(5)} |
| 419 | ... i = 20 |
| 420 | ... return {x() for x in items} |
| 421 | >>> test_func() |
| 422 | {4} |
| 423 | |
| 424 | >>> def test_func(): |
| 425 | ... items = {(lambda: y) for i in range(5)} |
| 426 | ... y = 2 |
| 427 | ... return {x() for x in items} |
| 428 | >>> test_func() |
| 429 | {2} |
| 430 | |
| 431 | """ |
| 432 | |
| 433 | |
| 434 | __test__ = {'doctests' : doctests} |
| 435 | |
| 436 | def test_main(verbose=None): |
| 437 | import sys |
| 438 | from test import test_support |
| 439 | from test import test_listcomps |
| 440 | test_support.run_doctest(test_listcomps, verbose) |
| 441 | |
| 442 | # verify reference counting |
| 443 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 444 | import gc |
| 445 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame^] | 446 | for i in range(len(counts)): |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 447 | test_support.run_doctest(test_genexps, verbose) |
| 448 | gc.collect() |
| 449 | counts[i] = sys.gettotalrefcount() |
| 450 | print(counts) |
| 451 | |
| 452 | if __name__ == "__main__": |
| 453 | test_main(verbose=True) |