[Gmp-commit] /var/hg/gmp: Add unit test for mpz_invert.

mercurial at gmplib.org mercurial at gmplib.org
Fri Feb 24 13:59:55 CET 2012


details:   /var/hg/gmp/rev/6e97e3d23b38
changeset: 14672:6e97e3d23b38
user:      Torbjorn Granlund <tege at gmplib.org>
date:      Fri Feb 24 13:59:52 2012 +0100
description:
Add unit test for mpz_invert.

diffstat:

 ChangeLog             |   5 ++
 tests/mpz/Makefile.am |   6 +-
 tests/mpz/t-invert.c  |  95 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 3 deletions(-)

diffs (133 lines):

diff -r 142b66e668b7 -r 6e97e3d23b38 ChangeLog
--- a/ChangeLog	Fri Feb 24 11:27:02 2012 +0100
+++ b/ChangeLog	Fri Feb 24 13:59:52 2012 +0100
@@ -1,3 +1,8 @@
+2012-02-24  Torbjorn Granlund  <tege at gmplib.org>
+
+	* tests/mpz/t-invert.c: New file.
+	* tests/mpz/Makefile.am (check_PROGRAMS): Add t-invert.
+
 2012-02-23  Marc Glisse  <marc.glisse at inria.fr>
 
 	* tests/mpq/t-cmp.c: Move NUM and DEN macros...
diff -r 142b66e668b7 -r 6e97e3d23b38 tests/mpz/Makefile.am
--- a/tests/mpz/Makefile.am	Fri Feb 24 11:27:02 2012 +0100
+++ b/tests/mpz/Makefile.am	Fri Feb 24 13:59:52 2012 +0100
@@ -1,6 +1,6 @@
 ## Process this file with automake to generate Makefile.in
 
-# Copyright 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2009 Free Software
+# Copyright 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2009, 2012 Free Software
 # Foundation, Inc.
 #
 # This file is part of the GNU MP Library.
@@ -23,8 +23,8 @@
 LDADD = $(top_builddir)/tests/libtests.la $(top_builddir)/libgmp.la
 
 check_PROGRAMS = t-addsub t-cmp t-mul t-mul_i t-tdiv t-tdiv_ui t-fdiv   \
-  t-fdiv_ui t-cdiv_ui t-gcd t-gcd_ui t-lcm dive dive_ui t-sqrtrem convert io \
-  t-inp_str logic bit t-powm t-powm_ui t-pow t-div_2exp reuse           \
+  t-fdiv_ui t-cdiv_ui t-gcd t-gcd_ui t-lcm t-invert dive dive_ui t-sqrtrem \
+  convert io t-inp_str logic bit t-powm t-powm_ui t-pow t-div_2exp reuse   \
   t-root t-perfsqr t-perfpow t-jac t-bin t-get_d t-get_d_2exp t-get_si	\
   t-set_d t-set_si							\
   t-fac_ui t-fib_ui t-lucnum_ui t-scan t-fits                           \
diff -r 142b66e668b7 -r 6e97e3d23b38 tests/mpz/t-invert.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mpz/t-invert.c	Fri Feb 24 13:59:52 2012 +0100
@@ -0,0 +1,95 @@
+/* Test mpz_invert.
+
+Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005,
+2008, 2009, 2012 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+The GNU MP Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+The GNU MP Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "tests.h"
+
+int
+main (int argc, char **argv)
+{
+  mpz_t a, m, ainv, t;
+  int test, r;
+  gmp_randstate_ptr rands;
+  mpz_t bs;
+  unsigned long bsi, size_range;
+  int reps = 1000;
+
+  tests_start ();
+  TESTS_REPS (reps, argv, argc);
+
+  rands = RANDS;
+
+  mpz_init (bs);
+  mpz_init (a);
+  mpz_init (m);
+  mpz_init (ainv);
+  mpz_init (t);
+
+  for (test = 0; test < reps; test++)
+    {
+      mpz_urandomb (bs, rands, 32);
+      size_range = mpz_get_ui (bs) % 16 + 2;
+
+      mpz_urandomb (bs, rands, size_range);
+      mpz_rrandomb (a, rands, mpz_get_ui (bs));
+      mpz_urandomb (bs, rands, size_range);
+      mpz_rrandomb (m, rands, mpz_get_ui (bs));
+
+      if (mpz_sgn (m) == 0)
+	continue;
+
+      mpz_urandomb (bs, rands, 8);
+      bsi = mpz_get_ui (bs);
+
+      if ((bsi & 1) != 0)
+	mpz_neg (a, a);
+      if ((bsi & 2) != 0)
+	mpz_neg (m, m);
+
+      r = mpz_invert (ainv, a, m);
+      if (r != 0)
+	{
+	  mpz_mul (t, ainv, a);
+	  mpz_mod (t, t, m);
+
+	  if (mpz_cmp_ui (t, 1) != 0)
+	    {
+	      fprintf (stderr, "ERROR in test %d\n", test);
+	      gmp_fprintf (stderr, "a^(-1)*a != 1 (mod m)\n");
+	      gmp_fprintf (stderr, "a = %Zx\n", a);
+	      gmp_fprintf (stderr, "m = %Zx\n", m);
+	      abort ();
+	    }
+	}
+    }
+
+  mpz_clear (bs);
+  mpz_clear (a);
+  mpz_clear (m);
+  mpz_clear (ainv);
+  mpz_clear (t);
+
+  tests_end ();
+  exit (0);
+}


More information about the gmp-commit mailing list