test_setcomps.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. doctests = """
  2. ########### Tests mostly copied from test_listcomps.py ############
  3. Test simple loop with conditional
  4. >>> sum({i*i for i in range(100) if i&1 == 1})
  5. 166650
  6. Test simple case
  7. >>> {2*y + x + 1 for x in (0,) for y in (1,)}
  8. set([3])
  9. Test simple nesting
  10. >>> list(sorted({(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. Test nesting with the inner expression dependent on the outer
  13. >>> list(sorted({(i,j) for i in range(4) for j in range(i)}))
  14. [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
  15. Make sure the induction variable is not exposed
  16. >>> i = 20
  17. >>> sum({i*i for i in range(100)})
  18. 328350
  19. >>> i
  20. 20
  21. Verify that syntax error's are raised for setcomps used as lvalues
  22. >>> {y for y in (1,2)} = 10 # doctest: +IGNORE_EXCEPTION_DETAIL
  23. Traceback (most recent call last):
  24. ...
  25. SyntaxError: ...
  26. >>> {y for y in (1,2)} += 10 # doctest: +IGNORE_EXCEPTION_DETAIL
  27. Traceback (most recent call last):
  28. ...
  29. SyntaxError: ...
  30. Make a nested set comprehension that acts like set(range())
  31. >>> def srange(n):
  32. ... return {i for i in range(n)}
  33. >>> list(sorted(srange(10)))
  34. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  35. Same again, only as a lambda expression instead of a function definition
  36. >>> lrange = lambda n: {i for i in range(n)}
  37. >>> list(sorted(lrange(10)))
  38. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  39. Generators can call other generators:
  40. >>> def grange(n):
  41. ... for x in {i for i in range(n)}:
  42. ... yield x
  43. >>> list(sorted(grange(5)))
  44. [0, 1, 2, 3, 4]
  45. Make sure that None is a valid return value
  46. >>> {None for i in range(10)}
  47. set([None])
  48. ########### Tests for various scoping corner cases ############
  49. Return lambdas that use the iteration variable as a default argument
  50. >>> items = {(lambda i=i: i) for i in range(5)}
  51. >>> {x() for x in items} == set(range(5))
  52. True
  53. Same again, only this time as a closure variable
  54. >>> items = {(lambda: i) for i in range(5)}
  55. >>> {x() for x in items}
  56. set([4])
  57. Another way to test that the iteration variable is local to the list comp
  58. >>> items = {(lambda: i) for i in range(5)}
  59. >>> i = 20
  60. >>> {x() for x in items}
  61. set([4])
  62. And confirm that a closure can jump over the list comp scope
  63. >>> items = {(lambda: y) for i in range(5)}
  64. >>> y = 2
  65. >>> {x() for x in items}
  66. set([2])
  67. We also repeat each of the above scoping tests inside a function
  68. >>> def test_func():
  69. ... items = {(lambda i=i: i) for i in range(5)}
  70. ... return {x() for x in items}
  71. >>> test_func() == set(range(5))
  72. True
  73. >>> def test_func():
  74. ... items = {(lambda: i) for i in range(5)}
  75. ... return {x() for x in items}
  76. >>> test_func()
  77. set([4])
  78. >>> def test_func():
  79. ... items = {(lambda: i) for i in range(5)}
  80. ... i = 20
  81. ... return {x() for x in items}
  82. >>> test_func()
  83. set([4])
  84. >>> def test_func():
  85. ... items = {(lambda: y) for i in range(5)}
  86. ... y = 2
  87. ... return {x() for x in items}
  88. >>> test_func()
  89. set([2])
  90. """
  91. __test__ = {'doctests' : doctests}
  92. def test_main(verbose=None):
  93. import sys
  94. from test import test_support
  95. from test import test_setcomps
  96. test_support.run_doctest(test_setcomps, verbose)
  97. # verify reference counting
  98. if verbose and hasattr(sys, "gettotalrefcount"):
  99. import gc
  100. counts = [None] * 5
  101. for i in range(len(counts)):
  102. test_support.run_doctest(test_setcomps, verbose)
  103. gc.collect()
  104. counts[i] = sys.gettotalrefcount()
  105. print(counts)
  106. if __name__ == "__main__":
  107. test_main(verbose=True)