diff options
author | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2012-12-06 09:34:11 +0100 |
---|---|---|
committer | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2012-12-06 09:34:11 +0100 |
commit | 226c3253c5e543d0d4d126b51842c72bf6c41a4b (patch) | |
tree | f343f45e9ac0d473d4842a5b4bf774e1f7d18f65 /tests | |
parent | 2489ddda83d2fe78b2058d5175d8a28817d7421b (diff) | |
download | gmp-226c3253c5e543d0d4d126b51842c72bf6c41a4b.tar.gz |
tests/mpq/reuse.c: New test (adapted from mpf/reuse.c)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/mpq/Makefile.am | 2 | ||||
-rw-r--r-- | tests/mpq/reuse.c | 198 |
2 files changed, 199 insertions, 1 deletions
diff --git a/tests/mpq/Makefile.am b/tests/mpq/Makefile.am index be1fce6f9..e84f685da 100644 --- a/tests/mpq/Makefile.am +++ b/tests/mpq/Makefile.am @@ -22,7 +22,7 @@ INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/tests LDADD = $(top_builddir)/tests/libtests.la $(top_builddir)/libgmp.la check_PROGRAMS = t-aors t-cmp t-cmp_ui t-cmp_si t-equal t-get_d t-get_str \ - t-inp_str t-inv t-md_2exp t-set_f t-set_str io + t-inp_str t-inv t-md_2exp t-set_f t-set_str io reuse TESTS = $(check_PROGRAMS) # Temporary files used by the tests. Removed automatically if the tests diff --git a/tests/mpq/reuse.c b/tests/mpq/reuse.c new file mode 100644 index 000000000..8a9eb493b --- /dev/null +++ b/tests/mpq/reuse.c @@ -0,0 +1,198 @@ +/* Test that routines allow reusing a source variable as destination. + +Copyright 1996, 2000, 2001, 2002, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library test suite. + +The GNU MP Library test suite 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 3 of the License, +or (at your option) any later version. + +The GNU MP Library test suite 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 test suite. If not, see http://www.gnu.org/licenses/. */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "gmp.h" +#include "gmp-impl.h" +#include "tests.h" + +#if __GMP_LIBGMP_DLL + +/* FIXME: When linking to a DLL libgmp, mpq_add etc can't be used as + initializers for global variables because they're effectively global + variables (function pointers) themselves. Perhaps calling a test + function successively with mpq_add etc would be better. */ + +int +main (void) +{ + printf ("Test suppressed for windows DLL\n"); + exit (0); +} + + +#else /* ! DLL_EXPORT */ + +#ifndef SIZE +#define SIZE 16 +#endif + +void dump_abort (const char *, mpq_t, mpq_t); + +typedef void (*dss_func) (mpq_ptr, mpq_srcptr, mpq_srcptr); + +dss_func dss_funcs[] = +{ + mpq_div, mpq_add, mpq_mul, mpq_sub, +}; + +const char *dss_func_names[] = +{ + "mpq_div", "mpq_add", "mpq_mul", "mpq_sub", +}; + +typedef void (*ds_func) (mpq_ptr, mpq_srcptr); + +ds_func ds_funcs[] = +{ + mpq_abs, mpq_neg, +}; + +const char *ds_func_names[] = +{ + "mpq_abs", "mpq_neg", +}; + +typedef void (*dsi_func) (mpq_ptr, mpq_srcptr, unsigned long int); + +dsi_func dsi_funcs[] = +{ + mpq_mul_2exp, mpq_div_2exp +}; + +const char *dsi_func_names[] = +{ + "mpq_mul_2exp", "mpq_div_2exp" +}; + +int +main (int argc, char **argv) +{ + int i; + int pass, reps = 100; + mpq_t in1, in2, out1; + unsigned long int in1i, in2i; + mpq_t res1, res2, res3; + gmp_randstate_ptr rands; + + tests_start (); + + if (argc > 1) + reps = strtol (argv[1], 0, 0); + + rands = RANDS; + + mpq_init (in1); + mpq_init (in2); + mpq_init (out1); + mpq_init (res1); + mpq_init (res2); + mpq_init (res3); + + for (pass = 1; pass <= reps; pass++) + { + in2i = urandom (); + + mpz_errandomb (mpq_numref(in1), rands, 512L); + mpz_errandomb_nonzero (mpq_denref(in1), rands, 512L); + if (in2i & 1) + mpz_neg (mpq_numref(in1),mpq_numref(in1)); + in2i >>= 1; + mpq_canonicalize (in1); + + mpz_errandomb (mpq_numref(in2), rands, 512L); + mpz_errandomb_nonzero (mpq_denref(in2), rands, 512L); + if (in2i & 1) + mpz_neg (mpq_numref(in2),mpq_numref(in2)); + in2i >>= 1; + mpq_canonicalize (in2); + + for (i = 0; i < sizeof (dss_funcs) / sizeof (dss_func); i++) + { + /* Don't divide by 0. */ + if (i == 0 && mpq_cmp_ui (in2, 0, 1) == 0) + continue; + + (dss_funcs[i]) (res1, in1, in2); + + mpq_set (out1, in1); + (dss_funcs[i]) (out1, out1, in2); + mpq_set (res2, out1); + + mpq_set (out1, in2); + (dss_funcs[i]) (out1, in1, out1); + mpq_set (res3, out1); + + if (mpq_cmp (res1, res2) != 0) + dump_abort (dss_func_names[i], res1, res2); + if (mpq_cmp (res1, res3) != 0) + dump_abort (dss_func_names[i], res1, res3); + } + + for (i = 0; i < sizeof (ds_funcs) / sizeof (ds_func); i++) + { + (ds_funcs[i]) (res1, in1); + + mpq_set (out1, in1); + (ds_funcs[i]) (out1, out1); + mpq_set (res2, out1); + + if (mpq_cmp (res1, res2) != 0) + dump_abort (ds_func_names[i], res1, res2); + } + + in2i %= 65536; + for (i = 0; i < sizeof (dsi_funcs) / sizeof (dsi_func); i++) + { + (dsi_funcs[i]) (res1, in1, in2i); + + mpq_set (out1, in1); + (dsi_funcs[i]) (out1, out1, in2i); + mpq_set (res2, out1); + + if (mpq_cmp (res1, res2) != 0) + dump_abort (dsi_func_names[i], res1, res2); + } + + } + + mpq_clear (in1); + mpq_clear (in2); + mpq_clear (out1); + mpq_clear (res1); + mpq_clear (res2); + mpq_clear (res3); + + tests_end (); + exit (0); +} + +void +dump_abort (const char *name, mpq_t res1, mpq_t res2) +{ + printf ("failure in %s:\n", name); + mpq_trace (" res1 ", res1); + mpq_trace (" res2 ", res2); + abort (); +} + +#endif /* ! DLL_EXPORT */ |