11from decimal import Decimal
22from fractions import Fraction
3- import itertools
43import random
54import unittest
65from test import support
@@ -449,8 +448,8 @@ class MathTests(IntMathTests):
449448 import math as module
450449
451450
452- # isprime() and primes() exist only in math.integer, not in math, so their
453- # tests are not in IntMathTests (which is re-run against math above).
451+ # isprime() exists only in math.integer, not in math, so its tests
452+ # are not in IntMathTests (which is re-run against math above).
454453
455454class IsPrimeTests (unittest .TestCase ):
456455 import math .integer as module
@@ -580,115 +579,6 @@ def test_isprime_non_integers(self):
580579 self .assertRaises (TypeError , isprime , 7 , 11 )
581580
582581
583- class PrimesIterTests (unittest .TestCase ):
584- import math .integer as module
585-
586- def test_primes (self ):
587- primes = self .module .primes
588- expected = primes_below (10 ** 4 )
589- self .assertEqual (list (primes (stop = 10 ** 4 )), expected )
590- self .assertEqual (len (expected ), 1229 )
591- self .assertEqual (list (itertools .islice (primes (), 25 )), expected [:25 ])
592-
593- def test_primes_start (self ):
594- primes = self .module .primes
595- for start in [- 10 ** 100 , - 100 , - 1 , 0 , 1 , 2 ]:
596- with self .subTest (start = start ):
597- self .assertEqual (list (primes (start , 10 )), [2 , 3 , 5 , 7 ])
598- self .assertEqual (list (primes (3 , 10 )), [3 , 5 , 7 ])
599- self .assertEqual (list (primes (4 , 10 )), [5 , 7 ])
600- self .assertEqual (list (primes (8 , 12 )), [11 ])
601- self .assertEqual (list (primes (9 , 12 )), [11 ])
602- self .assertEqual (list (primes (7 , 8 )), [7 ])
603-
604- def test_primes_stop (self ):
605- primes = self .module .primes
606- # The range is half-open.
607- self .assertEqual (list (primes (2 , 2 )), [])
608- self .assertEqual (list (primes (2 , 3 )), [2 ])
609- self .assertEqual (list (primes (3 , 3 )), [])
610- self .assertEqual (list (primes (7 , 7 )), [])
611- self .assertEqual (list (primes (2 , - 10 )), [])
612- self .assertEqual (list (primes (10 , 5 )), [])
613- self .assertEqual (list (primes (stop = 0 )), [])
614-
615- def test_primes_unbounded (self ):
616- primes = self .module .primes
617- it = primes ()
618- self .assertEqual ([next (it ) for _ in range (5 )], [2 , 3 , 5 , 7 , 11 ])
619- it = primes (10 ** 6 )
620- self .assertEqual (next (it ), 1000003 )
621-
622- def test_primes_huge (self ):
623- primes = self .module .primes
624- boundary = 10 ** 18
625- expected = [n for n in range (boundary - 200 , boundary + 200 )
626- if py_isprime (n )]
627- self .assertEqual (list (primes (boundary - 200 , boundary + 200 )),
628- expected )
629- # The top of the supported range.
630- expected = [n for n in range (2 ** 64 - 200 , 2 ** 64 ) if py_isprime (n )]
631- self .assertEqual (list (primes (2 ** 64 - 200 , 2 ** 64 - 1 )), expected )
632-
633- def test_primes_overflow (self ):
634- primes = self .module .primes
635- # The bounds must be less than 2**64.
636- self .assertRaises (OverflowError , primes , 2 ** 64 )
637- self .assertRaises (OverflowError , primes , 2 ** 64 + 100 , 2 ** 64 + 200 )
638- self .assertRaises (OverflowError , primes , 0 , 2 ** 64 )
639- self .assertRaises (OverflowError , primes , 10 ** 100 )
640- # An unbounded iterator raises when it runs out of the
641- # supported range.
642- it = primes (2 ** 64 - 60 )
643- self .assertEqual (next (it ), 2 ** 64 - 59 )
644- self .assertRaises (OverflowError , next , it )
645- self .assertRaises (StopIteration , next , it )
646-
647- def test_primes_iterator_protocol (self ):
648- primes = self .module .primes
649- it = primes (2 , 10 )
650- self .assertIs (iter (it ), it )
651- self .assertEqual (list (it ), [2 , 3 , 5 , 7 ])
652- # An exhausted iterator stays exhausted.
653- self .assertEqual (list (it ), [])
654- self .assertRaises (StopIteration , next , it )
655- # The iterator type cannot be instantiated directly.
656- self .assertRaises (TypeError , type (primes ()))
657-
658- def test_primes_keywords (self ):
659- primes = self .module .primes
660- self .assertEqual (list (primes (start = 10 , stop = 30 )), [11 , 13 , 17 , 19 , 23 , 29 ])
661- self .assertEqual (list (primes (10 , stop = 30 )), [11 , 13 , 17 , 19 , 23 , 29 ])
662-
663- def test_primes_integer_like (self ):
664- primes = self .module .primes
665- self .assertEqual (list (primes (True , 10 )), [2 , 3 , 5 , 7 ])
666- self .assertEqual (list (primes (IntSubclass (3 ), IntSubclass (10 ))), [3 , 5 , 7 ])
667- self .assertEqual (list (primes (MyIndexable (3 ), MyIndexable (10 ))), [3 , 5 , 7 ])
668- # The yielded values are exact ints.
669- for p in primes (IntSubclass (3 ), IntSubclass (10 )):
670- self .assertIs (type (p ), int )
671-
672- def test_primes_int_subclass_operators (self ):
673- # Overridden operators of an int subclass must not affect the
674- # iteration.
675- primes = self .module .primes
676- self .assertEqual (list (primes (BadIntSubclass (3 ), BadIntSubclass (10 ))),
677- [3 , 5 , 7 ])
678- big = 10 ** 18
679- self .assertEqual (list (primes (BadIntSubclass (big ), big + 100 )),
680- [n for n in range (big , big + 100 ) if py_isprime (n )])
681-
682- def test_primes_non_integers (self ):
683- primes = self .module .primes
684- self .assertRaises (TypeError , primes , 2.5 )
685- self .assertRaises (TypeError , primes , 2.5 , 10 )
686- self .assertRaises (TypeError , primes , 2 , 10.5 )
687- self .assertRaises (TypeError , primes , '2' )
688- self .assertRaises (TypeError , primes , 2 , '10' )
689- self .assertRaises (TypeError , primes , 2 , 10 , 3 )
690-
691-
692582class MiscTests (unittest .TestCase ):
693583
694584 def test_module_name (self ):
@@ -700,11 +590,10 @@ def test_module_name(self):
700590 self .assertEqual (obj .__module__ , 'math.integer' )
701591
702592 def test_math_namespace (self ):
703- # New functions are added only to math.integer, not to math
593+ # The new function is added only to math.integer, not to math
704594 # (PEP 791).
705595 import math
706596 self .assertFalse (hasattr (math , 'isprime' ))
707- self .assertFalse (hasattr (math , 'primes' ))
708597
709598
710599if __name__ == '__main__' :
0 commit comments