[libmath-prime-util-perl] 14/16: Many more tests

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:44:27 UTC 2015


This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.06
in repository libmath-prime-util-perl.

commit be45b0858591db2db04ede01aa0eb4bfc2f36798
Author: Dana Jacobsen <dana at acm.org>
Date:   Mon Jun 11 23:19:05 2012 -0600

    Many more tests
---
 Changes                |  1 +
 XS.xs                  |  4 ++--
 lib/Math/Prime/Util.pm |  2 +-
 t/11-primes.t          | 36 +++++++++++++++++++++++++++++++++++-
 t/13-primecount.t      |  6 +++---
 t/16-randomprime.t     | 19 ++++++++++++++++++-
 t/17-pseudoprime.t     |  7 ++++++-
 t/50-factoring.t       | 34 ++++++++++++++++++++++++++++++++--
 util.c                 |  4 ++--
 9 files changed, 100 insertions(+), 13 deletions(-)

diff --git a/Changes b/Changes
index 8fb3bc0..f845e32 100644
--- a/Changes
+++ b/Changes
@@ -11,6 +11,7 @@ Revision history for Perl extension Math::Prime::Util.
     - Add Ei(x), li(x), and R(x) functions.
     - prime_count_approx uses R(x), making it vastly more accurate.
     - Let user override rand for random_prime.
+    - Add many more tests with the help of Devel::Cover.
 
 0.04  7 June 2012
     - Didn't do tests on 32-bit machine before release.  Test suite caught
diff --git a/XS.xs b/XS.xs
index eac8c57..f1e072d 100644
--- a/XS.xs
+++ b/XS.xs
@@ -374,8 +374,8 @@ miller_rabin(IN UV n, ...)
   CODE:
     if (items < 2)
       croak("No bases given to miller_rabin");
-    if ( (n == 0) || (n == 1) ) XSRETURN(0);   /* 0 and 1 are composite */
-    if ( (n == 2) || (n == 3) ) XSRETURN(2);   /* 2 and 3 are prime */
+    if ( (n == 0) || (n == 1) ) XSRETURN_IV(0);   /* 0 and 1 are composite */
+    if ( (n == 2) || (n == 3) ) XSRETURN_IV(2);   /* 2 and 3 are prime */
     while (c < items) {
       int b = 0;
       while (c < items) {
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index 57122bd..fc738a8 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -395,7 +395,7 @@ A common place these would be used is sizing an array to hold the first C<$n>
 primes.  It may be desirable to use a bit more memory than is necessary, to
 avoid calling C<prime_count>.
 
-These routines use hand-verified tight limits below a range at least C<2^33>,
+These routines use hand-verified tight limits below a range at least C<2^35>,
 and fall back to the Dusart bounds of
 
     x/logx * (1 + 1/logx + 1.80/log^2x) <= Pi(x)
diff --git a/t/11-primes.t b/t/11-primes.t
index 8fe1ac8..5d36d63 100644
--- a/t/11-primes.t
+++ b/t/11-primes.t
@@ -8,7 +8,24 @@ use Math::Prime::Util qw/primes prime_count/;
 my $use64 = Math::Prime::Util::_maxbits > 32;
 my $extra = defined $ENV{RELEASE_TESTING} && $ENV{RELEASE_TESTING};
 
-plan tests => 12 + 1 + 19 + ($use64 ? 1 : 0) + 1;
+plan tests => 12+3 + 12 + 1 + 19 + ($use64 ? 1 : 0) + 1 + 13*6;
+
+ok(!eval { primes(undef); },   "primes(undef)");
+ok(!eval { primes("a"); },     "primes(a)");
+ok(!eval { primes(-4); },      "primes(-4)");
+ok(!eval { primes(2,undef); }, "primes(2,undef)");
+ok(!eval { primes(2,'x'); },   "primes(2,x)");
+ok(!eval { primes(2,-4); },    "primes(2,-4)");
+ok(!eval { primes(undef,7); }, "primes(undef,7)");
+ok(!eval { primes('x',7); },   "primes(x,7)");
+ok(!eval { primes(-10,7); },   "primes(-10,7)");
+ok(!eval { primes(undef,undef); },  "primes(undef,undef)");
+ok(!eval { primes('x','x'); }, "primes(x,x)");
+ok(!eval { primes(-10,-4); },  "primes(-10,-4)");
+
+ok(!eval { primes(50000000000000000000); },  "primes(inf)");
+ok(!eval { primes(2,50000000000000000000); },  "primes(2,inf)");
+ok(!eval { primes(50000000000000000000,50000000000000000001); },  "primes(inf,inf)");
 
 my @small_primes = qw/
 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
@@ -94,3 +111,20 @@ if ($use64) {
 }
 
 is( scalar @{primes(474973,838390)}, prime_count(838390) - prime_count(474973), "count primes within a range" );
+
+
+foreach my $method (qw/trial erat simple segment sieve dynamic/) {
+  is_deeply( primes({method=>$method}, 0, 3572), \@small_primes, "Primes between 0 and 3572" );
+  is_deeply( primes({method=>$method}, 2, 20), [2,3,5,7,11,13,17,19], "Primes between 2 and 20" );
+  is_deeply( primes({method=>$method}, 30, 70), [31,37,41,43,47,53,59,61,67], "Primes between 30 and 70" );
+  is_deeply( primes({method=>$method}, 30, 70), [31,37,41,43,47,53,59,61,67], "Primes between 30 and 70" );
+  is_deeply( primes({method=>$method}, 20, 2), [], "Primes between 20 and 2" );
+  is_deeply( primes({method=>$method}, 1, 1), [], "Primes ($method) between 1 and 1" );
+  is_deeply( primes({method=>$method}, 2, 2), [2], "Primes ($method) between 2 and 2" );
+  is_deeply( primes({method=>$method}, 3, 3), [3], "Primes ($method) between 3 and 3" );
+  is_deeply( primes({method=>$method}, 2010733, 2010733+148), [2010733,2010733+148], "Primegap 21 inclusive" );
+  is_deeply( primes({method=>$method}, 2010733+1, 2010733+148-2), [], "Primegap 21 exclusive" );
+  is_deeply( primes({method=>$method}, 3088, 3164), [3089,3109,3119,3121,3137,3163], "Primes between 3088 and 3164" );
+  is_deeply( primes({method=>$method}, 3089, 3163), [3089,3109,3119,3121,3137,3163], "Primes between 3089 and 3163" );
+  is_deeply( primes({method=>$method}, 3090, 3162), [3109,3119,3121,3137], "Primes between 3090 and 3162" );
+}
diff --git a/t/13-primecount.t b/t/13-primecount.t
index c23cb0e..659d209 100644
--- a/t/13-primecount.t
+++ b/t/13-primecount.t
@@ -8,7 +8,9 @@ use Math::Prime::Util qw/prime_count prime_count_lower prime_count_upper prime_c
 my $use64 = Math::Prime::Util::_maxbits > 32;
 my $extra = defined $ENV{RELEASE_TESTING} && $ENV{RELEASE_TESTING};
 
-plan tests => 14*3 + 8 + 6*$extra + 3*18*$use64 + 11 + 6*$use64;
+plan tests => 1 + 14*3 + 8 + 6*$extra + 3*18*$use64 + 11 + 6*$use64;
+
+ok( eval { prime_count(13); 1; }, "prime_count in void context");
 
 #  Powers of 2:  http://oeis.org/A007053/b007053.txt
 #  Powers of 10: http://oeis.org/A006880/b006880.txt
@@ -124,5 +126,3 @@ sub fixnum {
 #    109726486, // prime count 2^32 interval starting at 10^17
 #    103626726, // prime count 2^32 interval starting at 10^18
 #    98169972}; // prime count 2^32 interval starting at 10^19
-
-
diff --git a/t/16-randomprime.t b/t/16-randomprime.t
index 19c3b44..277b09e 100644
--- a/t/16-randomprime.t
+++ b/t/16-randomprime.t
@@ -11,7 +11,7 @@ use Math::Prime::Util qw/random_prime random_ndigit_prime is_prime/;
 my $use64 = Math::Prime::Util::_maxbits > 32;
 my $extra = defined $ENV{RELEASE_TESTING} && $ENV{RELEASE_TESTING};
 
-plan tests => 13 + 6*1 + 11*3 + 10*2 + 12*2;
+plan tests => 13+4 + 6*1 + 11*3 + 10*2 + 12*2 + 2*$use64 + 10*2;
 
 my $infinity = ~0 * ~0;
 
@@ -29,6 +29,11 @@ ok(!eval { random_prime(2,$infinity); }, "random_prime(2,+infinity)");
 ok(!eval { random_prime($infinity); }, "random_prime(+infinity)");
 ok(!eval { random_prime(-$infinity); }, "random_prime(-infinity)");
 
+ok(!eval { random_ndigit_prime(undef); }, "random_ndigit_prime(undef)");
+ok(!eval { random_ndigit_prime(0); }, "random_ndigit_prime(0)");
+ok(!eval { random_ndigit_prime(-5); }, "random_ndigit_prime(-5)");
+ok(!eval { random_ndigit_prime(50); }, "random_ndigit_prime(50)");
+
 my %range_edge_empty = (
   "0 to 0" => [],
   "0 to 1" => [],
@@ -100,3 +105,15 @@ foreach my $high (@to) {
   ok($isprime, "All returned values for $high were prime" );
   ok($inrange, "All returned values for $high were in the range" );
 }
+
+if ($use64) {
+  my $got = random_prime(2**34);
+  cmp_ok( $got, '<=', 2**34, "34-bit random_prime is in range");
+  ok( is_prime($got), "32-bit prime is indeed prime");
+}
+
+foreach my $digits (1 .. 10) {
+  my $got = random_ndigit_prime($digits);
+  is( length($got), $digits, "$digits-digit random prime is $digits digits");
+  ok( is_prime($got), "$digits-digit random prime is indeed prime");
+}
diff --git a/t/17-pseudoprime.t b/t/17-pseudoprime.t
index 1c7b18c..e78d15e 100644
--- a/t/17-pseudoprime.t
+++ b/t/17-pseudoprime.t
@@ -8,12 +8,17 @@ use Math::Prime::Util qw/is_prime miller_rabin/;
 my $use64 = Math::Prime::Util::_maxbits > 32;
 my $extra = defined $ENV{RELEASE_TESTING} && $ENV{RELEASE_TESTING};
 
-plan tests => 3 + 295 + 4 + 4*$use64 + 1 + 1*$extra + 161;
+plan tests => 3 + 4 + 295 + 4 + 4*$use64 + 1 + 1*$extra + 161;
 
 ok(!eval { miller_rabin(2047); }, "MR with no base fails");
 ok(!eval { miller_rabin(2047,0); }, "MR base 0 fails");
 ok(!eval { miller_rabin(2047,1); }, "MR base 1 fails");
 
+is( miller_rabin(0, 2), 0, "MR with 0 shortcut composite");
+is( miller_rabin(1, 2), 0, "MR with 0 shortcut composite");
+is( miller_rabin(2, 2), 2, "MR with 2 shortcut prime");
+is( miller_rabin(3, 2), 2, "MR with 3 shortcut prime");
+
 # small primes
 my @sp = qw/2 3 5 7 11 13 17 19 23 29 31 37/;
 # strong pseudoprimes for all prime bases 2 .. pn
diff --git a/t/50-factoring.t b/t/50-factoring.t
index d7f9de7..1868ede 100644
--- a/t/50-factoring.t
+++ b/t/50-factoring.t
@@ -10,13 +10,22 @@ my $extra = defined $ENV{RELEASE_TESTING} && $ENV{RELEASE_TESTING};
 
 my @testn = qw/0 1 2 3 4 5 6 7 8 16 57 64 377 9592 30107 78498 664579 5761455
                114256942 2214143 999999929 50847534 455052511 2147483647
-               4118054813/;
+               4118054813
+               30 210 2310 30030 510510 9699690 223092870
+               1363 989 779 629 403
+               547308031
+               808 2727 12625 34643 134431 221897 496213 692759 1228867
+               2463289 3008891 5115953 6961021 8030207 10486123
+               10893343 12327779 701737021
+              /;
 
 my @testn64 = qw/37607912018 346065536839 600851475143
                  3204941750802 29844570422669
                  279238341033925 2623557157654233 24739954287740860
                  3369738766071892021 10023859281455311421
                  9007199254740991 9007199254740992 9007199254740993
+                 6469693230 200560490130 7420738134810 304250263527210
+                 13082761331670030 614889782588491410
                 /;
 
 
@@ -25,7 +34,7 @@ push @testn, @testn64 if $use64;
 push @testn, qw/9999986200004761 99999989237606677 999999866000004473/
       if $use64 && $extra;
 
-plan tests =>  (2 * scalar @testn) + ($use64 ? 1 : 0);
+plan tests =>  (2 * scalar @testn) + 1*$use64 + 6*7;
 
 if ($use64) {
   # Simple test:  perl -e 'die if 18446744073709550592 == ~0'
@@ -56,3 +65,24 @@ foreach my $n (@testn) {
     ok( $isprime, "All factors [ $facstring ] of $n are prime" );
   }
 };
+
+extra_factor_test("trial_factor",  sub {Math::Prime::Util::trial_factor(shift)});
+extra_factor_test("fermat_factor", sub {Math::Prime::Util::fermat_factor(shift)});
+extra_factor_test("holf_factor",   sub {Math::Prime::Util::holf_factor(shift)});
+extra_factor_test("squfof_factor", sub {Math::Prime::Util::squfof_factor(shift)});
+extra_factor_test("pbrent_factor", sub {Math::Prime::Util::pbrent_factor(shift)});
+extra_factor_test("prho_factor",   sub {Math::Prime::Util::prho_factor(shift)});
+extra_factor_test("pminus1_factor",sub {Math::Prime::Util::pminus1_factor(shift)});
+
+sub extra_factor_test {
+  my $fname = shift;
+  my $fsub = shift;
+
+  is_deeply( [ sort {$a<=>$b} $fsub->(1)   ], [1],       "$fname(1)" );
+  is_deeply( [ sort {$a<=>$b} $fsub->(4)   ], [2, 2],    "$fname(4)" );
+  is_deeply( [ sort {$a<=>$b} $fsub->(9)   ], [3, 3],    "$fname(9)" );
+  is_deeply( [ sort {$a<=>$b} $fsub->(25)  ], [5, 5],    "$fname(9)" );
+  is_deeply( [ sort {$a<=>$b} $fsub->(175) ], [5, 5, 7], "$fname(175)" );
+  is_deeply( [ sort {$a<=>$b} $fsub->(403) ], [13, 31],  "$fname(403)" );
+}
+
diff --git a/util.c b/util.c
index c7a704e..5ef0459 100644
--- a/util.c
+++ b/util.c
@@ -416,7 +416,7 @@ UV prime_count_lower(UV x)
   else a = 2.32;
 #else
   else if (x < UVCONST( 5433800000)) { a = 2.32; }
-  else if (x < UVCONST(50000000000)) { a = 2.15; }
+  else if (x < UVCONST(60000000000)) { a = 2.15; }
 #endif
 
   return (UV) ( (fx/flogx) * (F1 + F1/flogx + a/(flogx*flogx)) );
@@ -466,7 +466,7 @@ UV prime_count_upper(UV x)
 #if BITS_PER_WORD == 32
   else a = 2.362;
 #else
-  else if (x < UVCONST(50000000000)) { a = 2.362; }
+  else if (x < UVCONST(60000000000)) { a = 2.362; }
 #endif
 
   /*

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git



More information about the Pkg-perl-cvs-commits mailing list