[Gmp-commit] /var/hg/gmp-proj/mini-gmp: Tests for bit operations.
mercurial at gmplib.org
mercurial at gmplib.org
Fri Jan 13 17:43:33 CET 2012
details: /var/hg/gmp-proj/mini-gmp/rev/a0c947043620
changeset: 75:a0c947043620
user: Niels M?ller <nisse at lysator.liu.se>
date: Fri Jan 13 17:43:17 2012 +0100
description:
Tests for bit operations.
diffstat:
.hgignore | 2 +
tests/Makefile | 2 +-
tests/hex-random.c | 47 +++++++++++++++++++++++++
tests/hex-random.h | 5 ++-
tests/mini-random.c | 15 ++++++++
tests/mini-random.h | 2 +
tests/t-bitops.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 167 insertions(+), 2 deletions(-)
diffs (234 lines):
diff -r c2ee9441c83c -r a0c947043620 .hgignore
--- a/.hgignore Thu Jan 12 22:19:09 2012 +0100
+++ b/.hgignore Fri Jan 13 17:43:17 2012 +0100
@@ -10,7 +10,9 @@
^tests/t-div$
^tests/t-double$
^tests/t-gcd$
+^tests/t-lcm$
^tests/t-sqrt
^tests/t-powm$
^tests/t-logops$
+^tests/t-bitops$
^tests/t-str$
diff -r c2ee9441c83c -r a0c947043620 tests/Makefile
--- a/tests/Makefile Thu Jan 12 22:19:09 2012 +0100
+++ b/tests/Makefile Fri Jan 13 17:43:17 2012 +0100
@@ -8,7 +8,7 @@
LIBS = -lgmp -lm -lmcheck
CHECK_PROGRAMS = t-add t-sub t-mul t-invert t-div t-double t-gcd \
- t-sqrt t-powm t-logops t-str t-lcm
+ t-sqrt t-powm t-logops t-bitops t-str t-lcm
MISC_OBJS = hex-random.o mini-random.o mini-gmp.o
diff -r c2ee9441c83c -r a0c947043620 tests/hex-random.c
--- a/tests/hex-random.c Thu Jan 12 22:19:09 2012 +0100
+++ b/tests/hex-random.c Fri Jan 13 17:43:17 2012 +0100
@@ -260,6 +260,53 @@
}
void
+hex_random_bit_op (enum hex_random_op op, unsigned long maxbits,
+ char **ap, unsigned long *b, char **rp)
+{
+ mpz_t a, r;
+ unsigned long abits, bbits;
+ unsigned signs;
+
+ mpz_init (a);
+ mpz_init (r);
+
+ 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_SETBIT:
+ mpz_set (r, a);
+ mpz_setbit (r, bbits);
+ break;
+ case OP_CLRBIT:
+ mpz_set (r, a);
+ mpz_clrbit (r, bbits);
+ break;
+ case OP_COMBIT:
+ mpz_set (r, a);
+ mpz_combit (r, bbits);
+ break;
+ }
+
+ gmp_asprintf (ap, "%Zx", a);
+ *b = bbits;
+ gmp_asprintf (rp, "%Zx", r);
+
+ mpz_clear (a);
+ mpz_clear (r);
+}
+
+void
hex_random_str_op (unsigned long maxbits,
int base, char **ap, char **rp)
{
diff -r c2ee9441c83c -r a0c947043620 tests/hex-random.h
--- a/tests/hex-random.h Thu Jan 12 22:19:09 2012 +0100
+++ b/tests/hex-random.h Fri Jan 13 17:43:17 2012 +0100
@@ -18,7 +18,8 @@
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
enum hex_random_op { OP_ADD, OP_SUB, OP_MUL, OP_CDIV, OP_FDIV, OP_TDIV,
- OP_GCD, OP_LCM, OP_POWM, OP_AND, OP_IOR, OP_XOR };
+ OP_GCD, OP_LCM, OP_POWM, OP_AND, OP_IOR, OP_XOR,
+ OP_SETBIT, OP_CLRBIT, OP_COMBIT };
void hex_random_init (void);
char *hex_urandomb (unsigned long bits);
@@ -27,5 +28,7 @@
char **ap, char **bp, char **rp);
void hex_random_op4 (enum hex_random_op op, unsigned long maxbits,
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_str_op (unsigned long maxbits,
int base, char **ap, char **rp);
diff -r c2ee9441c83c -r a0c947043620 tests/mini-random.c
--- a/tests/mini-random.c Thu Jan 12 22:19:09 2012 +0100
+++ b/tests/mini-random.c Fri Jan 13 17:43:17 2012 +0100
@@ -88,3 +88,18 @@
free (cp);
free (dp);
}
+
+void
+mini_random_bit_op (enum hex_random_op op, unsigned long maxbits,
+ mpz_t a, mp_bitcnt_t *b, mpz_t r)
+{
+ char *ap;
+ char *rp;
+
+ hex_random_bit_op (op, maxbits, &ap, b, &rp);
+ set_str (a, ap);
+ set_str (r, rp);
+
+ free (ap);
+ free (rp);
+}
diff -r c2ee9441c83c -r a0c947043620 tests/mini-random.h
--- a/tests/mini-random.h Thu Jan 12 22:19:09 2012 +0100
+++ b/tests/mini-random.h Fri Jan 13 17:43:17 2012 +0100
@@ -26,3 +26,5 @@
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_bit_op (enum hex_random_op op, unsigned long maxbits,
+ mpz_t a, mp_bitcnt_t *b, mpz_t r);
diff -r c2ee9441c83c -r a0c947043620 tests/t-bitops.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/t-bitops.c Fri Jan 13 17:43:17 2012 +0100
@@ -0,0 +1,96 @@
+#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, res, ref;
+ mp_bitcnt_t b;
+
+ hex_random_init ();
+
+ mpz_init (a);
+ mpz_init (res);
+ mpz_init (ref);
+
+ for (i = 0; i < COUNT; i++)
+ {
+ mini_random_bit_op (OP_SETBIT, MAXBITS, a, &b, ref);
+ mpz_set (res, a);
+ mpz_setbit (res, b);
+ if (mpz_cmp (res, ref))
+ {
+ fprintf (stderr, "mpz_setbit failed:\n");
+ dump ("a", a);
+ fprintf (stderr, "b: %lu\n", b);
+ dump ("r", res);
+ dump ("ref", ref);
+ abort ();
+ }
+ if (!mpz_tstbit (res, b))
+ {
+ fprintf (stderr, "mpz_tstbit failed (after mpz_setbit):\n");
+ dump ("res", a);
+ fprintf (stderr, "b: %lu\n", b);
+ abort ();
+ }
+ mini_random_bit_op (OP_CLRBIT, MAXBITS, a, &b, ref);
+ mpz_set (res, a);
+ mpz_clrbit (res, b);
+ if (mpz_cmp (res, ref))
+ {
+ fprintf (stderr, "mpz_clrbit failed:\n");
+ dump ("a", a);
+ fprintf (stderr, "b: %lu\n", b);
+ dump ("r", res);
+ dump ("ref", ref);
+ abort ();
+ }
+ if (mpz_tstbit (res, b))
+ {
+ fprintf (stderr, "mpz_tstbit failed (after mpz_clrbit):\n");
+ dump ("res", a);
+ fprintf (stderr, "b: %lu\n", b);
+ abort ();
+ }
+ mini_random_bit_op (OP_COMBIT, MAXBITS, a, &b, ref);
+ mpz_set (res, a);
+ mpz_combit (res, b);
+ if (mpz_cmp (res, ref))
+ {
+ fprintf (stderr, "mpz_combit failed:\n");
+ dump ("a", a);
+ fprintf (stderr, "b: %lu\n", b);
+ dump ("r", res);
+ dump ("ref", ref);
+ abort ();
+ }
+ if (mpz_tstbit (res, b) == mpz_tstbit (a, b))
+ {
+ fprintf (stderr, "mpz_tstbit failed (after mpz_combit):\n");
+ dump ("res", a);
+ fprintf (stderr, "b: %lu\n", b);
+ abort ();
+ }
+ }
+ mpz_clear (a);
+ mpz_clear (res);
+ mpz_clear (ref);
+
+ return 0;
+}
More information about the gmp-commit
mailing list