[Python-checkins] CVS: python/dist/src/Lib/test test_generators.py,1.2,1.3
Tim Peters
tim_one@users.sourceforge.net
Sat, 23 Jun 2001 14:01:49 -0700
Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv21480/python/dist/src/Lib/test
Modified Files:
test_generators.py
Log Message:
Add a recursive Sieve of Eratosthenes prime generator. Not practical,
but it's a heck of a good generator exerciser (think about it <wink>).
Index: test_generators.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_generators.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** test_generators.py 2001/06/23 20:45:43 1.2
--- test_generators.py 2001/06/23 21:01:47 1.3
***************
*** 304,310 ****
"""
__test__ = {"tut": tutorial_tests,
"pep": pep_tests,
! "zemail": email_tests}
# Magic test name that regrtest.py invokes *after* importing this module.
--- 304,348 ----
"""
+ # Fun tests (for sufficiently warped notions of "fun").
+
+ fun_tests = """
+
+ Build up to a recursive Sieve of Eratosthenes generator.
+
+ >>> def firstn(g, n):
+ ... return [g.next() for i in range(n)]
+
+ >>> def intsfrom(i):
+ ... while 1:
+ ... yield i
+ ... i += 1
+
+ >>> firstn(intsfrom(5), 7)
+ [5, 6, 7, 8, 9, 10, 11]
+
+ >>> def exclude_multiples(n, ints):
+ ... for i in ints:
+ ... if i % n:
+ ... yield i
+
+ >>> firstn(exclude_multiples(3, intsfrom(1)), 6)
+ [1, 2, 4, 5, 7, 8]
+
+ >>> def sieve(ints):
+ ... prime = ints.next()
+ ... yield prime
+ ... not_divisible_by_prime = exclude_multiples(prime, ints)
+ ... for p in sieve(not_divisible_by_prime):
+ ... yield p
+
+ >>> primes = sieve(intsfrom(2))
+ >>> firstn(primes, 20)
+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
+ """
+
__test__ = {"tut": tutorial_tests,
"pep": pep_tests,
! "email": email_tests,
! "fun": fun_tests}
# Magic test name that regrtest.py invokes *after* importing this module.