[Gmp-commit] /var/hg/gmp-proj/mini-gmp: Added tests for mpz_scan0 and mpz_scan1.

mercurial at gmplib.org mercurial at gmplib.org
Fri Jan 13 23:37:42 CET 2012


details:   /var/hg/gmp-proj/mini-gmp/rev/1567c30cce3e
changeset: 78:1567c30cce3e
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Fri Jan 13 23:37:40 2012 +0100
description:
Added tests for mpz_scan0 and mpz_scan1.

diffstat:

 .hgignore           |   1 +
 tests/Makefile      |   4 +-
 tests/hex-random.c  |  37 +++++++++++++++++++++++++++++++++++
 tests/hex-random.h  |   5 +++-
 tests/mini-random.c |  12 +++++++++++
 tests/mini-random.h |   2 +
 tests/t-scan.c      |  55 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 113 insertions(+), 3 deletions(-)

diffs (180 lines):

diff -r b327ec321322 -r 1567c30cce3e .hgignore
--- a/.hgignore	Fri Jan 13 22:02:38 2012 +0100
+++ b/.hgignore	Fri Jan 13 23:37:40 2012 +0100
@@ -16,4 +16,5 @@
 ^tests/t-powm$
 ^tests/t-logops$
 ^tests/t-bitops$
+^tests/t-scan$
 ^tests/t-str$
diff -r b327ec321322 -r 1567c30cce3e tests/Makefile
--- a/tests/Makefile	Fri Jan 13 22:02:38 2012 +0100
+++ b/tests/Makefile	Fri Jan 13 23:37:40 2012 +0100
@@ -8,8 +8,8 @@
 LIBS = -lgmp -lm -lmcheck
 
 CHECK_PROGRAMS = t-add t-sub t-mul t-invert t-div t-div_2exp \
-	t-double t-gcd \
-	t-sqrt t-powm t-logops t-bitops t-str t-lcm 
+	t-double t-gcd t-lcm \
+	t-sqrt t-powm t-logops t-bitops t-scan t-str
 
 MISC_OBJS = hex-random.o mini-random.o mini-gmp.o
 
diff -r b327ec321322 -r 1567c30cce3e tests/hex-random.c
--- a/tests/hex-random.c	Fri Jan 13 22:02:38 2012 +0100
+++ b/tests/hex-random.c	Fri Jan 13 23:37:40 2012 +0100
@@ -325,6 +325,43 @@
 }
 
 void
+hex_random_scan_op (enum hex_random_op op, unsigned long maxbits,
+		    char **ap, unsigned long *b, unsigned long *r)
+{
+  mpz_t a;
+  unsigned long abits, bbits;
+  unsigned signs;
+
+  mpz_init (a);
+
+  abits = gmp_urandomb_ui (state, 32) % maxbits;
+  bbits = gmp_urandomb_ui (state, 32) % (maxbits + 100);
+  
+  mpz_rrandomb (a, state, abits);
+
+  signs = gmp_urandomb_ui (state, 1);
+  if (signs & 1)
+    mpz_neg (a, a);
+
+  switch (op)
+    {
+    default:
+      abort ();
+
+    case OP_SCAN0:
+      *r = mpz_scan0 (a, bbits);
+      break;
+    case OP_SCAN1:
+      *r = mpz_scan1 (a, bbits);
+      break;
+    }
+  gmp_asprintf (ap, "%Zx", a);
+  *b = bbits;
+
+  mpz_clear (a);
+}
+
+void
 hex_random_str_op (unsigned long maxbits,
 		   int base, char **ap, char **rp)
 {
diff -r b327ec321322 -r 1567c30cce3e tests/hex-random.h
--- a/tests/hex-random.h	Fri Jan 13 22:02:38 2012 +0100
+++ b/tests/hex-random.h	Fri Jan 13 23:37:40 2012 +0100
@@ -24,7 +24,8 @@
     OP_FDIV_Q_2, OP_FDIV_R_2,
     OP_TDIV_Q_2,  OP_TDIV_R_2,
     OP_GCD, OP_LCM, OP_POWM, OP_AND, OP_IOR, OP_XOR,
-    OP_SETBIT, OP_CLRBIT, OP_COMBIT
+    OP_SETBIT, OP_CLRBIT, OP_COMBIT,
+    OP_SCAN0, OP_SCAN1,
   };
 
 void hex_random_init (void);
@@ -36,5 +37,7 @@
 		     char **ap, char **bp, char **rp, char **qp);
 void hex_random_bit_op (enum hex_random_op op, unsigned long maxbits,
 			char **ap, unsigned long *b, char **rp);
+void hex_random_scan_op (enum hex_random_op op, unsigned long maxbits,
+			char **ap, unsigned long *b, unsigned long *r);
 void hex_random_str_op (unsigned long maxbits,
 			int base, char **ap, char **rp);
diff -r b327ec321322 -r 1567c30cce3e tests/mini-random.c
--- a/tests/mini-random.c	Fri Jan 13 22:02:38 2012 +0100
+++ b/tests/mini-random.c	Fri Jan 13 23:37:40 2012 +0100
@@ -103,3 +103,15 @@
   free (ap);
   free (rp);
 }
+
+void
+mini_random_scan_op (enum hex_random_op op, unsigned long maxbits,
+		     mpz_t a, mp_bitcnt_t *b, mp_bitcnt_t *r)
+{
+  char *ap;
+
+  hex_random_scan_op (op, maxbits, &ap, b, r);
+  set_str (a, ap);
+
+  free (ap);
+}
diff -r b327ec321322 -r 1567c30cce3e tests/mini-random.h
--- a/tests/mini-random.h	Fri Jan 13 22:02:38 2012 +0100
+++ b/tests/mini-random.h	Fri Jan 13 23:37:40 2012 +0100
@@ -26,5 +26,7 @@
 		     mpz_t a, mpz_t b, mpz_t r);
 void mini_random_op4 (enum hex_random_op op, unsigned long maxbits,
 		      mpz_t a, mpz_t b, mpz_t c, mpz_t e);
+void mini_random_scan_op (enum hex_random_op op, unsigned long maxbits,
+			  mpz_t a, mp_bitcnt_t *b, mp_bitcnt_t *r);
 void mini_random_bit_op (enum hex_random_op op, unsigned long maxbits,
 			 mpz_t a, mp_bitcnt_t *b, mpz_t r);
diff -r b327ec321322 -r 1567c30cce3e tests/t-scan.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/t-scan.c	Fri Jan 13 23:37:40 2012 +0100
@@ -0,0 +1,55 @@
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "mini-random.h"
+
+#define MAXBITS 400
+#define COUNT 10000
+
+static void
+dump (const char *label, const mpz_t x)
+{
+  char *buf = mpz_get_str (NULL, 16, x);
+  fprintf (stderr, "%s: %s\n", label, buf);
+  free (buf);
+}
+
+int
+main (int argc, char **argv)
+{
+  unsigned i;
+  mpz_t a;
+  mp_bitcnt_t b, res, ref;
+
+  hex_random_init ();
+
+  mpz_init (a);
+  
+  for (i = 0; i < COUNT; i++)
+    {
+      mini_random_scan_op (OP_SCAN0, MAXBITS, a, &b, &ref);
+      res = mpz_scan0 (a, b);
+      if (res != ref)
+	{
+	  fprintf (stderr, "mpz_scan0 failed:\n");
+	  dump ("a", a);
+	  fprintf (stderr, "b: %lu\n", b);
+	  fprintf (stderr, "r: %lu\n", res);
+	  fprintf (stderr, "ref: %lu\n", ref);
+	}
+      mini_random_scan_op (OP_SCAN1, MAXBITS, a, &b, &ref);
+      res = mpz_scan1 (a, b);
+      if (res != ref)
+	{
+	  fprintf (stderr, "mpz_scan1 failed:\n");
+	  dump ("a", a);
+	  fprintf (stderr, "b: %lu\n", b);
+	  fprintf (stderr, "r: %lu\n", res);
+	  fprintf (stderr, "ref: %lu\n", ref);
+	}
+    }
+  mpz_clear (a);
+
+  return 0;
+}


More information about the gmp-commit mailing list