|
Packit |
5c3484 |
/* Test mpz_add, mpz_sub, mpz_add_ui, mpz_sub_ui, and mpz_ui_sub.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Copyright 2002 Free Software Foundation, Inc.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
This file is part of the GNU MP Library test suite.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is free software; you can redistribute it
|
|
Packit |
5c3484 |
and/or modify it under the terms of the GNU General Public License as
|
|
Packit |
5c3484 |
published by the Free Software Foundation; either version 3 of the License,
|
|
Packit |
5c3484 |
or (at your option) any later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is distributed in the hope that it will be
|
|
Packit |
5c3484 |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5c3484 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
Packit |
5c3484 |
Public License for more details.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
You should have received a copy of the GNU General Public License along with
|
|
Packit |
5c3484 |
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include <stdio.h>
|
|
Packit |
5c3484 |
#include <stdlib.h>
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include "gmp.h"
|
|
Packit |
5c3484 |
#include "gmp-impl.h"
|
|
Packit |
5c3484 |
#include "longlong.h"
|
|
Packit |
5c3484 |
#include "tests.h"
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void debug_mp (mpz_t, int);
|
|
Packit |
5c3484 |
void dump_abort (int, const char *, mpz_t, mpz_t);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
int
|
|
Packit |
5c3484 |
main (int argc, char **argv)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpz_t op1, op2, r1, r2;
|
|
Packit |
5c3484 |
mp_size_t op1n, op2n;
|
|
Packit |
5c3484 |
unsigned long int op2long;
|
|
Packit |
5c3484 |
int i;
|
|
Packit |
5c3484 |
int reps = 100000;
|
|
Packit |
5c3484 |
gmp_randstate_ptr rands;
|
|
Packit |
5c3484 |
mpz_t bs;
|
|
Packit |
5c3484 |
unsigned long bsi, size_range;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
tests_start ();
|
|
Packit |
5c3484 |
rands = RANDS;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_init (bs);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (argc == 2)
|
|
Packit |
5c3484 |
reps = atoi (argv[1]);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_init (op1);
|
|
Packit |
5c3484 |
mpz_init (op2);
|
|
Packit |
5c3484 |
mpz_init (r1);
|
|
Packit |
5c3484 |
mpz_init (r2);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
for (i = 0; i < reps; i++)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpz_urandomb (bs, rands, 32);
|
|
Packit |
5c3484 |
size_range = mpz_get_ui (bs) % 10 + 2;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_urandomb (bs, rands, size_range);
|
|
Packit |
5c3484 |
op1n = mpz_get_ui (bs);
|
|
Packit |
5c3484 |
mpz_rrandomb (op1, rands, op1n);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_urandomb (bs, rands, size_range);
|
|
Packit |
5c3484 |
op2n = mpz_get_ui (bs);
|
|
Packit |
5c3484 |
mpz_rrandomb (op2, rands, op2n);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_urandomb (bs, rands, 2);
|
|
Packit |
5c3484 |
bsi = mpz_get_ui (bs);
|
|
Packit |
5c3484 |
if ((bsi & 1) != 0)
|
|
Packit |
5c3484 |
mpz_neg (op1, op1);
|
|
Packit |
5c3484 |
if ((bsi & 2) != 0)
|
|
Packit |
5c3484 |
mpz_neg (op2, op2);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
/* printf ("%ld %ld\n", SIZ (multiplier), SIZ (multiplicand)); */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_add (r1, op1, op2);
|
|
Packit |
5c3484 |
mpz_sub (r2, r1, op2);
|
|
Packit |
5c3484 |
if (mpz_cmp (r2, op1) != 0)
|
|
Packit |
5c3484 |
dump_abort (i, "mpz_add or mpz_sub incorrect", op1, op2);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (mpz_fits_ulong_p (op2))
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
op2long = mpz_get_ui (op2);
|
|
Packit |
5c3484 |
mpz_add_ui (r1, op1, op2long);
|
|
Packit |
5c3484 |
mpz_sub_ui (r2, r1, op2long);
|
|
Packit |
5c3484 |
if (mpz_cmp (r2, op1) != 0)
|
|
Packit |
5c3484 |
dump_abort (i, "mpz_add_ui or mpz_sub_ui incorrect", op1, op2);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_ui_sub (r1, op2long, op1);
|
|
Packit |
5c3484 |
mpz_sub_ui (r2, op1, op2long);
|
|
Packit |
5c3484 |
mpz_neg (r2, r2);
|
|
Packit |
5c3484 |
if (mpz_cmp (r1, r2) != 0)
|
|
Packit |
5c3484 |
dump_abort (i, "mpz_add_ui or mpz_ui_sub incorrect", op1, op2);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_clear (bs);
|
|
Packit |
5c3484 |
mpz_clear (op1);
|
|
Packit |
5c3484 |
mpz_clear (op2);
|
|
Packit |
5c3484 |
mpz_clear (r1);
|
|
Packit |
5c3484 |
mpz_clear (r2);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
tests_end ();
|
|
Packit |
5c3484 |
exit (0);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
dump_abort (int i, const char *s, mpz_t op1, mpz_t op2)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
fprintf (stderr, "ERROR: %s in test %d\n", s, i);
|
|
Packit |
5c3484 |
fprintf (stderr, "op1 = "); debug_mp (op1, -16);
|
|
Packit |
5c3484 |
fprintf (stderr, "op2 = "); debug_mp (op2, -16);
|
|
Packit |
5c3484 |
abort();
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
debug_mp (mpz_t x, int base)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpz_out_str (stderr, base, x); fputc ('\n', stderr);
|
|
Packit |
5c3484 |
}
|