[BACK]Return to t-addsub.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / mpz

Annotation of OpenXM_contrib/gmp/tests/mpz/t-addsub.c, Revision 1.1.1.1

1.1       ohara       1: /* Test mpz_add, mpz_sub, mpz_add_ui, mpz_sub_ui, and mpz_ui_sub.
                      2:
                      3: Copyright 2002 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24:
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27: #include "longlong.h"
                     28: #include "tests.h"
                     29:
                     30: void debug_mp _PROTO ((mpz_t, int));
                     31: void dump_abort _PROTO ((int, char *, mpz_t, mpz_t));
                     32:
                     33: int
                     34: main (int argc, char **argv)
                     35: {
                     36:   mpz_t op1, op2, r1, r2;
                     37:   mp_size_t op1n, op2n;
                     38:   unsigned long int op2long;
                     39:   int i;
                     40:   int reps = 100000;
                     41:   gmp_randstate_ptr rands;
                     42:   mpz_t bs;
                     43:   unsigned long bsi, size_range;
                     44:
                     45:   tests_start ();
                     46:   rands = RANDS;
                     47:
                     48:   mpz_init (bs);
                     49:
                     50:   if (argc == 2)
                     51:      reps = atoi (argv[1]);
                     52:
                     53:   mpz_init (op1);
                     54:   mpz_init (op2);
                     55:   mpz_init (r1);
                     56:   mpz_init (r2);
                     57:
                     58:   for (i = 0; i < reps; i++)
                     59:     {
                     60:       mpz_urandomb (bs, rands, 32);
                     61:       size_range = mpz_get_ui (bs) % 10 + 2;
                     62:
                     63:       mpz_urandomb (bs, rands, size_range);
                     64:       op1n = mpz_get_ui (bs);
                     65:       mpz_rrandomb (op1, rands, op1n);
                     66:
                     67:       mpz_urandomb (bs, rands, size_range);
                     68:       op2n = mpz_get_ui (bs);
                     69:       mpz_rrandomb (op2, rands, op2n);
                     70:
                     71:       mpz_urandomb (bs, rands, 2);
                     72:       bsi = mpz_get_ui (bs);
                     73:       if ((bsi & 1) != 0)
                     74:        mpz_neg (op1, op1);
                     75:       if ((bsi & 2) != 0)
                     76:        mpz_neg (op2, op2);
                     77:
                     78:       /* printf ("%ld %ld\n", SIZ (multiplier), SIZ (multiplicand)); */
                     79:
                     80:       mpz_add (r1, op1, op2);
                     81:       mpz_sub (r2, r1, op2);
                     82:       if (mpz_cmp (r2, op1) != 0)
                     83:        dump_abort (i, "mpz_add or mpz_sub incorrect", op1, op2);
                     84:
                     85:       if (mpz_fits_ulong_p (op2))
                     86:        {
                     87:          op2long = mpz_get_ui (op2);
                     88:          mpz_add_ui (r1, op1, op2long);
                     89:          mpz_sub_ui (r2, r1, op2long);
                     90:          if (mpz_cmp (r2, op1) != 0)
                     91:            dump_abort (i, "mpz_add_ui or mpz_sub_ui incorrect", op1, op2);
                     92:
                     93:          mpz_ui_sub (r1, op2long, op1);
                     94:          mpz_sub_ui (r2, op1, op2long);
                     95:          mpz_neg (r2, r2);
                     96:          if (mpz_cmp (r1, r2) != 0)
                     97:            dump_abort (i, "mpz_add_ui or mpz_ui_sub incorrect", op1, op2);
                     98:        }
                     99:     }
                    100:
                    101:   mpz_clear (bs);
                    102:   mpz_clear (op1);
                    103:   mpz_clear (op2);
                    104:   mpz_clear (r1);
                    105:   mpz_clear (r2);
                    106:
                    107:   tests_end ();
                    108:   exit (0);
                    109: }
                    110:
                    111: void
                    112: dump_abort (int i, char *s, mpz_t op1, mpz_t op2)
                    113: {
                    114:   fprintf (stderr, "ERROR: %s in test %d\n", s, i);
                    115:   fprintf (stderr, "op1 = "); debug_mp (op1, -16);
                    116:   fprintf (stderr, "op2 = "); debug_mp (op2, -16);
                    117:   abort();
                    118: }
                    119:
                    120: void
                    121: debug_mp (mpz_t x, int base)
                    122: {
                    123:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    124: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>