summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libgmp/tests
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/lib/libgmp/tests')
-rw-r--r--gnu/lib/libgmp/tests/Makefile64
-rw-r--r--gnu/lib/libgmp/tests/tst-convert.c80
-rw-r--r--gnu/lib/libgmp/tests/tst-dm.c117
-rw-r--r--gnu/lib/libgmp/tests/tst-dm_ui.c116
-rw-r--r--gnu/lib/libgmp/tests/tst-gcd.c131
-rw-r--r--gnu/lib/libgmp/tests/tst-mdm.c117
-rw-r--r--gnu/lib/libgmp/tests/tst-mdm_ui.c116
-rw-r--r--gnu/lib/libgmp/tests/tst-mul.c261
-rw-r--r--gnu/lib/libgmp/tests/tst-pow_ui.c120
-rw-r--r--gnu/lib/libgmp/tests/tst-sqrtrem.c97
-rw-r--r--gnu/lib/libgmp/tests/urandom.h17
11 files changed, 1236 insertions, 0 deletions
diff --git a/gnu/lib/libgmp/tests/Makefile b/gnu/lib/libgmp/tests/Makefile
new file mode 100644
index 0000000..aa18015
--- /dev/null
+++ b/gnu/lib/libgmp/tests/Makefile
@@ -0,0 +1,64 @@
+# Makefile for tests for GNU MP
+
+# Copyright (C) 1991, 1993 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 General Public License as published by
+# the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with the GNU MP Library; see the file COPYING. If not, write to
+# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+
+CC = gcc
+
+TEST_LIBS = ../libgmp.a
+OPT = -O -g
+CFLAGS = -I. -I.. $(OPT)
+
+TEST_SRCS = tst-mul.c tst-dm.c tst-dm_ui.c tst-mdm.c tst-mdm_ui.c tst-gcd.c \
+ tst-sqrtrem.c tst-convert.c
+TEST_OBJS = tst-mul.o tst-dm.o tst-dm_ui.o tst-mdm.o tst-mdm_ui.o tst-gcd.o \
+ tst-sqrtrem.o tst-convert.o
+TESTS = tst-mul tst-dm tst-dm_ui tst-mdm tst-mdm_ui tst-gcd \
+ tst-sqrtrem tst-convert
+
+tests: $(TESTS)
+ for i in $(TESTS); do echo $$i; ./$$i; done
+
+tst-mul: tst-mul.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+tst-dm: tst-dm.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+tst-dm_ui: tst-dm_ui.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+tst-mdm: tst-mdm.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+tst-mdm_ui: tst-mdm_ui.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+tst-gcd: tst-gcd.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+tst-sqrtrem: tst-sqrtrem.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+tst-convert: tst-convert.o $(TEST_LIBS)
+ $(CC) $(CFLAGS) -o $@ $@.o $(TEST_LIBS)
+
+$(TEST_PREFIX)clean:
+ rm -f $(TESTS) $(TEST_OBJS) core
+
+tst-convert.o : tst-convert.c ../gmp.h urandom.h
+tst-dm.o : tst-dm.c ../gmp.h urandom.h
+tst-dm_ui.o : tst-dm_ui.c ../gmp.h urandom.h
+tst-gcd.o : tst-gcd.c ../gmp.h urandom.h
+tst-mdm.o : tst-mdm.c ../gmp.h urandom.h
+tst-mdm_ui.o : tst-mdm_ui.c ../gmp.h urandom.h
+tst-mul.o : tst-mul.c ../gmp.h ../gmp-impl.h ../gmp-mparam.h ../longlong.h urandom.h
+tst-sqrtrem.o : tst-sqrtrem.c ../gmp.h urandom.h
diff --git a/gnu/lib/libgmp/tests/tst-convert.c b/gnu/lib/libgmp/tests/tst-convert.c
new file mode 100644
index 0000000..bbc0dab
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-convert.c
@@ -0,0 +1,80 @@
+/* Test mpz_add, mpz_cmp, mpz_cmp_ui, mpz_gcd, mpz_gcdext, mpz_get_str,
+ mpz_mod, mpz_mul, mpz_set_str.
+
+Copyright (C) 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT op1, op2;
+ mp_size size;
+ int i;
+ int reps = 100000;
+ char *str;
+ int base;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&op1);
+ mpz_init (&op2);
+
+ for (i = 0; i < reps; i++)
+ {
+ size = urandom () % SIZE - SIZE/2;
+
+ mpz_random2 (&op1, size);
+ base = urandom () % 36 + 1;
+ if (base == 1)
+ base = 0;
+
+ str = mpz_get_str ((char *) 0, base, &op1);
+ mpz_set_str (&op2, str, base);
+ free (str);
+
+ if (mpz_cmp (&op1, &op2))
+ {
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "op1 = "); debug_mp (&op1, -16);
+ fprintf (stderr, "base = %d\n", base);
+ abort ();
+ }
+ }
+
+ exit (0);
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-dm.c b/gnu/lib/libgmp/tests/tst-dm.c
new file mode 100644
index 0000000..92c4194
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-dm.c
@@ -0,0 +1,117 @@
+/* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_div, mpz_divmod, mpz_mod,
+ mpz_mul.
+
+Copyright (C) 1991, 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT dividend, divisor;
+ MP_INT quotient, remainder;
+ MP_INT quotient2, remainder2;
+ MP_INT temp;
+ mp_size dividend_size, divisor_size;
+ int i;
+ int reps = 100000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&dividend);
+ mpz_init (&divisor);
+ mpz_init (&quotient);
+ mpz_init (&remainder);
+ mpz_init (&quotient2);
+ mpz_init (&remainder2);
+ mpz_init (&temp);
+
+ for (i = 0; i < reps; i++)
+ {
+ dividend_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&dividend, dividend_size);
+
+ divisor_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&divisor, divisor_size);
+ if (mpz_cmp_ui (&divisor, 0) == 0)
+ continue;
+
+ mpz_divmod (&quotient, &remainder, &dividend, &divisor);
+ mpz_div (&quotient2, &dividend, &divisor);
+ mpz_mod (&remainder2, &dividend, &divisor);
+
+ /* First determine that the quotients and remainders computed
+ with different functions are equal. */
+ if (mpz_cmp (&quotient, &quotient2) != 0)
+ dump_abort (&dividend, &divisor);
+ if (mpz_cmp (&remainder, &remainder2) != 0)
+ dump_abort (&dividend, &divisor);
+
+ /* Check if the sign of the quotient is correct. */
+ if (mpz_cmp_ui (&quotient, 0) != 0)
+ if ((mpz_cmp_ui (&quotient, 0) < 0)
+ != ((mpz_cmp_ui (&dividend, 0) ^ mpz_cmp_ui (&divisor, 0)) < 0))
+ dump_abort (&dividend, &divisor);
+
+ /* Check if the remainder has the same sign as the dividend
+ (quotient rounded towards 0). */
+ if (mpz_cmp_ui (&remainder, 0) != 0)
+ if ((mpz_cmp_ui (&remainder, 0) < 0) != (mpz_cmp_ui (&dividend, 0) < 0))
+ dump_abort (&dividend, &divisor);
+
+ mpz_mul (&temp, &quotient, &divisor);
+ mpz_add (&temp, &temp, &remainder);
+ if (mpz_cmp (&temp, &dividend) != 0)
+ dump_abort (&dividend, &divisor);
+
+ mpz_abs (&temp, &divisor);
+ mpz_abs (&remainder, &remainder);
+ if (mpz_cmp (&remainder, &temp) >= 0)
+ dump_abort (&dividend, &divisor);
+ }
+
+ exit (0);
+}
+
+dump_abort (dividend, divisor)
+ MP_INT *dividend, *divisor;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
+ fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-dm_ui.c b/gnu/lib/libgmp/tests/tst-dm_ui.c
new file mode 100644
index 0000000..9f69841
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-dm_ui.c
@@ -0,0 +1,116 @@
+/* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_div, mpz_div_ui,
+ mpz_divmod, mpz_divmod_ui, mpz_mod, mpz_mod_ui, mpz_mul, mpz_mul_ui.
+
+Copyright (C) 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT dividend;
+ MP_INT quotient, remainder;
+ MP_INT quotient2, remainder2;
+ MP_INT temp;
+ mp_size dividend_size;
+ mp_limb divisor;
+ int i;
+ int reps = 100000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&dividend);
+ mpz_init (&quotient);
+ mpz_init (&remainder);
+ mpz_init (&quotient2);
+ mpz_init (&remainder2);
+ mpz_init (&temp);
+
+ for (i = 0; i < reps; i++)
+ {
+ dividend_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&dividend, dividend_size);
+
+ divisor = urandom ();
+ if (divisor == 0)
+ continue;
+
+ mpz_divmod_ui (&quotient, &remainder, &dividend, divisor);
+ mpz_div_ui (&quotient2, &dividend, divisor);
+ mpz_mod_ui (&remainder2, &dividend, divisor);
+
+ /* First determine that the quotients and remainders computed
+ with different functions are equal. */
+ if (mpz_cmp (&quotient, &quotient2) != 0)
+ dump_abort (&dividend, divisor);
+ if (mpz_cmp (&remainder, &remainder2) != 0)
+ dump_abort (&dividend, divisor);
+
+ /* Check if the sign of the quotient is correct. */
+ if (mpz_cmp_ui (&quotient, 0) != 0)
+ if ((mpz_cmp_ui (&quotient, 0) < 0)
+ != (mpz_cmp_ui (&dividend, 0) < 0))
+ dump_abort (&dividend, divisor);
+
+ /* Check if the remainder has the same sign as the dividend
+ (quotient rounded towards 0). */
+ if (mpz_cmp_ui (&remainder, 0) != 0)
+ if ((mpz_cmp_ui (&remainder, 0) < 0) != (mpz_cmp_ui (&dividend, 0) < 0))
+ dump_abort (&dividend, divisor);
+
+ mpz_mul_ui (&temp, &quotient, divisor);
+ mpz_add (&temp, &temp, &remainder);
+ if (mpz_cmp (&temp, &dividend) != 0)
+ dump_abort (&dividend, divisor);
+
+ mpz_abs (&remainder, &remainder);
+ if (mpz_cmp_ui (&remainder, divisor) >= 0)
+ dump_abort (&dividend, divisor);
+ }
+
+ exit (0);
+}
+
+dump_abort (dividend, divisor)
+ MP_INT *dividend;
+ mp_limb divisor;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
+ fprintf (stderr, "divisor = %lX\n", divisor);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-gcd.c b/gnu/lib/libgmp/tests/tst-gcd.c
new file mode 100644
index 0000000..166aa56
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-gcd.c
@@ -0,0 +1,131 @@
+/* Test mpz_gcd, mpz_gcdext, mpz_mul, mpz_mod, mpz_add, mpz_cmp,
+ mpz_cmp_ui. mpz_init_set, mpz_set, mpz_clear.
+
+Copyright (C) 1991, 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void mpz_refgcd (), debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT op1, op2;
+ MP_INT refgcd, gcd, s, t;
+ MP_INT temp1, temp2;
+ mp_size op1_size, op2_size;
+ int i;
+ int reps = 10000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&op1);
+ mpz_init (&op2);
+ mpz_init (&refgcd);
+ mpz_init (&gcd);
+ mpz_init (&temp1);
+ mpz_init (&temp2);
+ mpz_init (&s);
+ mpz_init (&t);
+
+ for (i = 0; i < reps; i++)
+ {
+ op1_size = urandom () % SIZE;
+ op2_size = urandom () % SIZE;
+
+ mpz_random2 (&op1, op1_size);
+ mpz_random2 (&op2, op2_size);
+
+ mpz_refgcd (&refgcd, &op1, &op2);
+
+ mpz_gcd (&gcd, &op1, &op2);
+ if (mpz_cmp (&refgcd, &gcd))
+ dump_abort (&op1, &op2);
+
+ mpz_gcdext (&gcd, &s, &t, &op1, &op2);
+ if (mpz_cmp (&refgcd, &gcd))
+ dump_abort (&op1, &op2);
+
+ mpz_mul (&temp1, &s, &op1);
+ mpz_mul (&temp2, &t, &op2);
+ mpz_add (&gcd, &temp1, &temp2);
+ if (mpz_cmp (&refgcd, &gcd))
+ dump_abort (&op1, &op2);
+ }
+
+ exit (0);
+}
+
+void
+mpz_refgcd (g, x, y)
+ MP_INT *g;
+ MP_INT *x, *y;
+{
+ MP_INT xx, yy;
+
+ mpz_init (&xx);
+ mpz_init (&yy);
+
+ mpz_abs (&xx, x);
+ mpz_abs (&yy, y);
+
+ for (;;)
+ {
+ if (mpz_cmp_ui (&yy, 0) == 0)
+ {
+ mpz_set (g, &xx);
+ break;
+ }
+ mpz_mod (&xx, &xx, &yy);
+ if (mpz_cmp_ui (&xx, 0) == 0)
+ {
+ mpz_set (g, &yy);
+ break;
+ }
+ mpz_mod (&yy, &yy, &xx);
+ }
+
+ mpz_clear (&xx);
+ mpz_clear (&yy);
+}
+
+dump_abort (op1, op2)
+ MP_INT *op1, *op2;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "op1 = "); debug_mp (op1, -16);
+ fprintf (stderr, "op2 = "); debug_mp (op2, -16);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-mdm.c b/gnu/lib/libgmp/tests/tst-mdm.c
new file mode 100644
index 0000000..a515ddf
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-mdm.c
@@ -0,0 +1,117 @@
+/* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_divmod, mpz_mdiv,
+ mpz_mdivmod, mpz_mmod, mpz_mul.
+
+Copyright (C) 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT dividend, divisor;
+ MP_INT quotient, remainder;
+ MP_INT quotient2, remainder2;
+ MP_INT temp;
+ mp_size dividend_size, divisor_size;
+ int i;
+ int reps = 100000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&dividend);
+ mpz_init (&divisor);
+ mpz_init (&quotient);
+ mpz_init (&remainder);
+ mpz_init (&quotient2);
+ mpz_init (&remainder2);
+ mpz_init (&temp);
+
+ for (i = 0; i < reps; i++)
+ {
+ dividend_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&dividend, dividend_size);
+
+ divisor_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&divisor, divisor_size);
+ if (mpz_cmp_ui (&divisor, 0) == 0)
+ continue;
+
+ mpz_mdivmod (&quotient, &remainder, &dividend, &divisor);
+ mpz_mdiv (&quotient2, &dividend, &divisor);
+ mpz_mmod (&remainder2, &dividend, &divisor);
+
+ /* First determine that the quotients and remainders computed
+ with different functions are equal. */
+ if (mpz_cmp (&quotient, &quotient2) != 0)
+ dump_abort (&dividend, &divisor);
+ if (mpz_cmp (&remainder, &remainder2) != 0)
+ dump_abort (&dividend, &divisor);
+
+ /* Check if the sign of the quotient is correct. */
+ if (mpz_cmp_ui (&quotient, 0) != 0)
+ if ((mpz_cmp_ui (&quotient, 0) < 0)
+ != ((mpz_cmp_ui (&dividend, 0) ^ mpz_cmp_ui (&divisor, 0)) < 0))
+ dump_abort (&dividend, &divisor);
+
+ /* Check if the remainder has the same sign as the divisor
+ (quotient rounded towards minus infinity). */
+ if (mpz_cmp_ui (&remainder, 0) != 0)
+ if ((mpz_cmp_ui (&remainder, 0) < 0) != (mpz_cmp_ui (&divisor, 0) < 0))
+ dump_abort (&dividend, &divisor);
+
+ mpz_mul (&temp, &quotient, &divisor);
+ mpz_add (&temp, &temp, &remainder);
+ if (mpz_cmp (&temp, &dividend) != 0)
+ dump_abort (&dividend, &divisor);
+
+ mpz_abs (&temp, &divisor);
+ mpz_abs (&remainder, &remainder);
+ if (mpz_cmp (&remainder, &temp) >= 0)
+ dump_abort (&dividend, &divisor);
+ }
+
+ exit (0);
+}
+
+dump_abort (dividend, divisor)
+ MP_INT *dividend, *divisor;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
+ fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-mdm_ui.c b/gnu/lib/libgmp/tests/tst-mdm_ui.c
new file mode 100644
index 0000000..26befbe
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-mdm_ui.c
@@ -0,0 +1,116 @@
+/* Test mpz_abs, mpz_add, mpz_cmp. mpz_cmp_si, mpz_cmp_ui, mpz_divmod,
+ mpz_mdiv_ui, mpz_mdivmod_ui, mpz_mmod_ui, mpz_mul, mpz_mul_ui.
+
+Copyright (C) 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT dividend;
+ MP_INT quotient, remainder;
+ MP_INT quotient2, remainder2;
+ MP_INT temp;
+ mp_size dividend_size;
+ mp_limb divisor;
+ int i;
+ int reps = 100000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&dividend);
+ mpz_init (&quotient);
+ mpz_init (&remainder);
+ mpz_init (&quotient2);
+ mpz_init (&remainder2);
+ mpz_init (&temp);
+
+ for (i = 0; i < reps; i++)
+ {
+ dividend_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&dividend, dividend_size);
+
+ divisor = urandom ();
+ if (divisor == 0)
+ continue;
+
+ mpz_mdivmod_ui (&quotient, &remainder, &dividend, divisor);
+ mpz_mdiv_ui (&quotient2, &dividend, divisor);
+ mpz_mmod_ui (&remainder2, &dividend, divisor);
+
+ /* First determine that the quotients and remainders computed
+ with different functions are equal. */
+ if (mpz_cmp (&quotient, &quotient2) != 0)
+ dump_abort (&dividend, divisor);
+ if (mpz_cmp (&remainder, &remainder2) != 0)
+ dump_abort (&dividend, divisor);
+
+ /* Check if the sign of the quotient is correct. */
+ if (mpz_cmp_ui (&quotient, 0) != 0)
+ if ((mpz_cmp_ui (&quotient, 0) < 0)
+ != (mpz_cmp_ui (&dividend, 0) < 0))
+ dump_abort (&dividend, divisor);
+
+ /* Check if the remainder has the same sign as the divisor
+ (quotient rounded towards minus infinity). */
+ if (mpz_cmp_ui (&remainder, 0) != 0)
+ if (mpz_cmp_ui (&remainder, 0) < 0)
+ dump_abort (&dividend, divisor);
+
+ mpz_mul_ui (&temp, &quotient, divisor);
+ mpz_add (&temp, &temp, &remainder);
+ if (mpz_cmp (&temp, &dividend) != 0)
+ dump_abort (&dividend, divisor);
+
+ mpz_abs (&remainder, &remainder);
+ if (mpz_cmp_ui (&remainder, divisor) >= 0)
+ dump_abort (&dividend, divisor);
+ }
+
+ exit (0);
+}
+
+dump_abort (dividend, divisor)
+ MP_INT *dividend;
+ mp_limb divisor;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
+ fprintf (stderr, "divisor = %lX\n", divisor);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-mul.c b/gnu/lib/libgmp/tests/tst-mul.c
new file mode 100644
index 0000000..0eab422
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-mul.c
@@ -0,0 +1,261 @@
+/* Test mpz_add, mpz_cmp, mpz_cmp_ui, mpz_divmod, mpz_mul.
+
+Copyright (C) 1991, 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "longlong.h"
+#include "urandom.h"
+
+void debug_mp ();
+mp_size _mpn_mul_classic ();
+void mpz_refmul ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT multiplier, multiplicand;
+ MP_INT product, ref_product;
+ MP_INT quotient, remainder;
+ mp_size multiplier_size, multiplicand_size;
+ int i;
+ int reps = 100000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&multiplier);
+ mpz_init (&multiplicand);
+ mpz_init (&product);
+ mpz_init (&ref_product);
+ mpz_init (&quotient);
+ mpz_init (&remainder);
+
+ for (i = 0; i < reps; i++)
+ {
+ multiplier_size = urandom () % SIZE - SIZE/2;
+ multiplicand_size = urandom () % SIZE - SIZE/2;
+
+ mpz_random2 (&multiplier, multiplier_size);
+ mpz_random2 (&multiplicand, multiplicand_size);
+
+ mpz_mul (&product, &multiplier, &multiplicand);
+ mpz_refmul (&ref_product, &multiplier, &multiplicand);
+ if (mpz_cmp_ui (&multiplicand, 0) != 0)
+ mpz_divmod (&quotient, &remainder, &product, &multiplicand);
+
+ if (mpz_cmp (&product, &ref_product))
+ dump_abort (&multiplier, &multiplicand);
+
+ if (mpz_cmp_ui (&multiplicand, 0) != 0)
+ if (mpz_cmp_ui (&remainder, 0) || mpz_cmp (&quotient, &multiplier))
+ dump_abort (&multiplier, &multiplicand);
+ }
+
+ exit (0);
+}
+
+void
+mpz_refmul (w, u, v)
+ MP_INT *w;
+ const MP_INT *u;
+ const MP_INT *v;
+{
+ mp_size usize = u->size;
+ mp_size vsize = v->size;
+ mp_size wsize;
+ mp_size sign_product;
+ mp_ptr up, vp;
+ mp_ptr wp;
+ mp_ptr free_me = NULL;
+ size_t free_me_size;
+
+ sign_product = usize ^ vsize;
+ usize = ABS (usize);
+ vsize = ABS (vsize);
+
+ if (usize < vsize)
+ {
+ /* Swap U and V. */
+ {const MP_INT *t = u; u = v; v = t;}
+ {mp_size t = usize; usize = vsize; vsize = t;}
+ }
+
+ up = u->d;
+ vp = v->d;
+ wp = w->d;
+
+ /* Ensure W has space enough to store the result. */
+ wsize = usize + vsize;
+ if (w->alloc < wsize)
+ {
+ if (wp == up || wp == vp)
+ {
+ free_me = wp;
+ free_me_size = w->alloc;
+ }
+ else
+ (*_mp_free_func) (wp, w->alloc * BYTES_PER_MP_LIMB);
+
+ w->alloc = wsize;
+ wp = (mp_ptr) (*_mp_allocate_func) (wsize * BYTES_PER_MP_LIMB);
+ w->d = wp;
+ }
+ else
+ {
+ /* Make U and V not overlap with W. */
+ if (wp == up)
+ {
+ /* W and U are identical. Allocate temporary space for U. */
+ up = (mp_ptr) alloca (usize * BYTES_PER_MP_LIMB);
+ /* Is V identical too? Keep it identical with U. */
+ if (wp == vp)
+ vp = up;
+ /* Copy to the temporary space. */
+ MPN_COPY (up, wp, usize);
+ }
+ else if (wp == vp)
+ {
+ /* W and V are identical. Allocate temporary space for V. */
+ vp = (mp_ptr) alloca (vsize * BYTES_PER_MP_LIMB);
+ /* Copy to the temporary space. */
+ MPN_COPY (vp, wp, vsize);
+ }
+ }
+
+ wsize = _mpn_mul_classic (wp, up, usize, vp, vsize);
+ w->size = sign_product < 0 ? -wsize : wsize;
+ if (free_me != NULL)
+ (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);
+
+ alloca (0);
+}
+
+mp_size
+_mpn_mul_classic (prodp, up, usize, vp, vsize)
+ mp_ptr prodp;
+ mp_srcptr up;
+ mp_size usize;
+ mp_srcptr vp;
+ mp_size vsize;
+{
+ mp_size n;
+ mp_size prod_size;
+ mp_limb cy;
+ mp_size i, j;
+ mp_limb prod_low, prod_high;
+ mp_limb cy_dig;
+ mp_limb v_limb, c;
+
+ if (vsize == 0)
+ return 0;
+
+ /* Offset UP and PRODP so that the inner loop can be faster. */
+ up += usize;
+ prodp += usize;
+
+ /* Multiply by the first limb in V separately, as the result can
+ be stored (not added) to PROD. We also avoid a loop for zeroing. */
+ v_limb = vp[0];
+ cy_dig = 0;
+ j = -usize;
+ do
+ {
+ umul_ppmm (prod_high, prod_low, up[j], v_limb);
+ add_ssaaaa (cy_dig, prodp[j], prod_high, prod_low, 0, cy_dig);
+ j++;
+ }
+ while (j < 0);
+
+ prodp[j] = cy_dig;
+ prodp++;
+
+ /* For each iteration in the outer loop, multiply one limb from
+ U with one limb from V, and add it to PROD. */
+ for (i = 1; i < vsize; i++)
+ {
+ v_limb = vp[i];
+ cy_dig = 0;
+ j = -usize;
+
+ /* Inner loops. Simulate the carry flag by jumping between
+ these loops. The first is used when there was no carry
+ in the previois iteration; the second when there was carry. */
+
+ do
+ {
+ umul_ppmm (prod_high, prod_low, up[j], v_limb);
+ add_ssaaaa (cy_dig, prod_low, prod_high, prod_low, 0, cy_dig);
+ c = prodp[j];
+ prod_low += c;
+ prodp[j] = prod_low;
+ if (prod_low < c)
+ goto cy_loop;
+ ncy_loop:
+ j++;
+ }
+ while (j < 0);
+
+ prodp[j] = cy_dig;
+ prodp++;
+ continue;
+
+ do
+ {
+ umul_ppmm (prod_high, prod_low, up[j], v_limb);
+ add_ssaaaa (cy_dig, prod_low, prod_high, prod_low, 0, cy_dig);
+ c = prodp[j];
+ prod_low += c + 1;
+ prodp[j] = prod_low;
+ if (prod_low > c)
+ goto ncy_loop;
+ cy_loop:
+ j++;
+ }
+ while (j < 0);
+
+ cy_dig += 1;
+ prodp[j] = cy_dig;
+ prodp++;
+ }
+
+ return usize + vsize - (cy_dig == 0);
+}
+
+dump_abort (multiplier, multiplicand)
+ MP_INT *multiplier, *multiplicand;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "multiplier = "); debug_mp (multiplier, -16);
+ fprintf (stderr, "multiplicand = "); debug_mp (multiplicand, -16);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-pow_ui.c b/gnu/lib/libgmp/tests/tst-pow_ui.c
new file mode 100644
index 0000000..c7d929a
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-pow_ui.c
@@ -0,0 +1,120 @@
+/* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_div, mpz_divmod, mpz_mod,
+ mpz_mul.
+
+Copyright (C) 1991, 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT dividend, divisor;
+ MP_INT quotient, remainder;
+ MP_INT quotient2, remainder2;
+ MP_INT temp;
+ mp_size dividend_size, divisor_size;
+ int i;
+ int reps = 100000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&dividend);
+ mpz_init (&divisor);
+ mpz_init (&quotient);
+ mpz_init (&remainder);
+ mpz_init (&quotient2);
+ mpz_init (&remainder2);
+ mpz_init (&temp);
+
+ for (i = 0; i < reps; i++)
+ {
+ base_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&base, base_size);
+
+ divisor_size = urandom () % SIZE - SIZE/2;
+ mpz_random2 (&divisor, divisor_size);
+ if (mpz_cmp_ui (&divisor, 0) == 0)
+ continue;
+
+ exp_size = urandom () % SIZE/2;
+ mpz_random2 (&exp, exp_size);
+
+ mpz_powm (&result1, &base, &exp, &divisor);
+ mpz_div (&quotient2, &dividend, &divisor);
+ mpz_mod (&remainder2, &dividend, &divisor);
+
+ /* First determine that the quotients and remainders computed
+ with different functions are equal. */
+ if (mpz_cmp (&quotient, &quotient2) != 0)
+ dump_abort (&dividend, &divisor);
+ if (mpz_cmp (&remainder, &remainder2) != 0)
+ dump_abort (&dividend, &divisor);
+
+ /* Check if the sign of the quotient is correct. */
+ if (mpz_cmp_ui (&quotient, 0) != 0)
+ if ((mpz_cmp_ui (&quotient, 0) < 0)
+ != ((mpz_cmp_ui (&dividend, 0) ^ mpz_cmp_ui (&divisor, 0)) < 0))
+ dump_abort (&dividend, &divisor);
+
+ /* Check if the remainder has the same sign as the dividend
+ (quotient rounded towards 0). */
+ if (mpz_cmp_ui (&remainder, 0) != 0)
+ if ((mpz_cmp_ui (&remainder, 0) < 0) != (mpz_cmp_ui (&dividend, 0) < 0))
+ dump_abort (&dividend, &divisor);
+
+ mpz_mul (&temp, &quotient, &divisor);
+ mpz_add (&temp, &temp, &remainder);
+ if (mpz_cmp (&temp, &dividend) != 0)
+ dump_abort (&dividend, &divisor);
+
+ mpz_abs (&temp, &divisor);
+ mpz_abs (&remainder, &remainder);
+ if (mpz_cmp (&remainder, &temp) >= 0)
+ dump_abort (&dividend, &divisor);
+ }
+
+ exit (0);
+}
+
+dump_abort (dividend, divisor)
+ MP_INT *dividend, *divisor;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
+ fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/tst-sqrtrem.c b/gnu/lib/libgmp/tests/tst-sqrtrem.c
new file mode 100644
index 0000000..b62350a
--- /dev/null
+++ b/gnu/lib/libgmp/tests/tst-sqrtrem.c
@@ -0,0 +1,97 @@
+/* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem.
+
+Copyright (C) 1991, 1993 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 General Public License as published by
+the Free Software Foundation; either version 2, 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU MP Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "urandom.h"
+
+void debug_mp ();
+
+#ifndef SIZE
+#define SIZE 8
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ MP_INT x2;
+ MP_INT x, rem;
+ MP_INT temp, temp2;
+ mp_size x2_size;
+ int i;
+ int reps = 100000;
+
+ if (argc == 2)
+ reps = atoi (argv[1]);
+
+ mpz_init (&x2);
+ mpz_init (&x);
+ mpz_init (&rem);
+ mpz_init (&temp);
+ mpz_init (&temp2);
+
+ for (i = 0; i < reps; i++)
+ {
+ x2_size = urandom () % SIZE;
+
+ mpz_random2 (&x2, x2_size);
+
+ mpz_sqrtrem (&x, &rem, &x2);
+ mpz_mul (&temp, &x, &x);
+
+ /* Is square of result > argument? */
+ if (mpz_cmp (&temp, &x2) > 0)
+ dump_abort (&x2, &x, &rem);
+
+ mpz_add_ui (&temp2, &x, 1);
+ mpz_mul (&temp2, &temp2, &temp2);
+
+ /* Is square of (result + 1) <= argument? */
+ if (mpz_cmp (&temp2, &x2) <= 0)
+ dump_abort (&x2, &x, &rem);
+
+ mpz_add (&temp2, &temp, &rem);
+
+ /* Is the remainder wrong? */
+ if (mpz_cmp (&x2, &temp2) != 0)
+ dump_abort (&x2, &x, &rem);
+ }
+
+ exit (0);
+}
+
+dump_abort (x2, x, rem)
+ MP_INT *x2, *x, *rem;
+{
+ fprintf (stderr, "ERROR\n");
+ fprintf (stderr, "x2 = "); debug_mp (x2, -16);
+ fprintf (stderr, "x = "); debug_mp (x, -16);
+ fprintf (stderr, "remainder = "); debug_mp (rem, -16);
+ abort();
+}
+
+void
+debug_mp (x, base)
+ MP_INT *x;
+{
+ mpz_out_str (stderr, base, x); fputc ('\n', stderr);
+}
diff --git a/gnu/lib/libgmp/tests/urandom.h b/gnu/lib/libgmp/tests/urandom.h
new file mode 100644
index 0000000..e018580
--- /dev/null
+++ b/gnu/lib/libgmp/tests/urandom.h
@@ -0,0 +1,17 @@
+#if defined (hpux) || defined (__alpha__)
+/* HPUX lacks random(). DEC Alpha's random() returns a double. */
+static inline unsigned long
+urandom ()
+{
+ return mrand48 ();
+}
+#else
+long random ();
+
+static inline unsigned long
+urandom ()
+{
+ /* random() returns 31 bits, we want 32. */
+ return random() ^ (random() << 1);
+}
+#endif
OpenPOWER on IntegriCloud