From aca40aa0b9e7b6107a7d5117ce4d341b1a11b81c Mon Sep 17 00:00:00 2001 From: Marco Bodrato Date: Sun, 13 Jan 2013 19:18:55 +0100 Subject: mini-gmp/tests/t-signed.c: New test program. --- mini-gmp/tests/Makefile | 2 +- mini-gmp/tests/t-signed.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 mini-gmp/tests/t-signed.c (limited to 'mini-gmp') diff --git a/mini-gmp/tests/Makefile b/mini-gmp/tests/Makefile index b9b61ecf8..565693e13 100644 --- a/mini-gmp/tests/Makefile +++ b/mini-gmp/tests/Makefile @@ -28,7 +28,7 @@ LDFLAGS = LIBS = -lgmp -lm -lmcheck CHECK_PROGRAMS = t-add t-sub t-mul t-invert t-div t-div_2exp \ - t-double t-gcd t-lcm t-import t-comb \ + t-double t-gcd t-lcm t-import t-comb t-signed \ t-sqrt t-root t-powm t-logops t-bitops t-scan t-str \ t-reuse diff --git a/mini-gmp/tests/t-signed.c b/mini-gmp/tests/t-signed.c new file mode 100644 index 000000000..5f7a27229 --- /dev/null +++ b/mini-gmp/tests/t-signed.c @@ -0,0 +1,129 @@ +/* Exercise some mpz_..._si functions. + +Copyright 2013 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 +#include + +#include "mini-gmp.h" + +void +check_si (mpz_t sz, mpz_t oz, long si, int c) +{ + mpz_t t; + int fail; + + mpz_init_set_si (t, si); + + if ((fail = mpz_cmp_si (sz, si)) != 0) + printf ("mpz_cmp_si (sz, %ld) != 0.\n", si); + if (mpz_cmp_si (oz, si) != -c) + printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, -c), fail = 1; + if (! mpz_fits_slong_p (sz)) + printf ("mpz_fits_slong_p (sz) != 1.\n"), fail = 1; + if (mpz_get_si (sz) != si) + printf ("mpz_get_si (sz) != %ld.\n", si), fail = 1; + if (mpz_cmp (t, sz) != 0) + { + printf ("mpz_init_set_si (%ld) failed.\n", si); + printf (" got="); mpz_out_str (stdout, 10, t); printf ("\n"); + fail = 1; + } + + mpz_clear (t); + + if (fail) + { + printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n"); + printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n"); + printf (" si=%ld\n", si); + abort (); + } +} + +void +try_op_si (int c) +{ + long si, oi; + mpz_t sz, oz; + + si = c; + mpz_init_set_si (sz, si); + + oi = si; + mpz_init_set (oz, sz); + + do { + si *= 2; + mpz_mul_2exp (sz, sz, 1); + + if (mpz_cmp_si (sz, oi) != c) + { + printf ("mpz_cmp_si (sz, %ld) != %i.\n", oi, c); + printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n"); + abort (); + } + + if ((si < oi ? -1 : si > oi) != c) + break; + + check_si (sz, oz, si, c); + + oi = (si - c) * 2 + c; + mpz_mul_si (oz, sz, 2*c); + if (c == -1) + mpz_ui_sub (oz, 1, oz); /* oz = sz * 2 + 1 */ + else + mpz_sub_ui (oz, oz, 1); /* oz = sz * 2 - 1 */ + + if (mpz_cmp_si (oz, si) != c) + { + printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, c); + printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n"); + abort (); + } + + if ((oi < si ? -1 : oi > si) != c) + { + mpz_set (sz, oz); + break; + } + + check_si (oz, sz, oi, c); + } while (1); + + mpz_clear (oz); + + if (mpz_fits_slong_p (sz)) + { + printf ("Should not fit a signed long any more.\n"); + printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n"); + abort (); + } + + mpz_clear (sz); +} + +int +main (int argc, char *argv[]) +{ + try_op_si (-1); + try_op_si (1); + + return 0; +} -- cgit v1.2.1