1/* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem. 2 3Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation, 4Inc. 5 6This file is part of the GNU MP Library. 7 8The GNU MP Library is free software; you can redistribute it and/or modify 9it under the terms of the GNU Lesser General Public License as published by 10the Free Software Foundation; either version 3 of the License, or (at your 11option) any later version. 12 13The GNU MP Library is distributed in the hope that it will be useful, but 14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16License for more details. 17 18You should have received a copy of the GNU Lesser General Public License 19along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 20 21#include <stdio.h> 22#include <stdlib.h> 23 24#include "gmp.h" 25#include "gmp-impl.h" 26#include "tests.h" 27 28void dump_abort __GMP_PROTO ((mpz_t, mpz_t, mpz_t)); 29void debug_mp __GMP_PROTO ((mpz_t, int)); 30 31int 32main (int argc, char **argv) 33{ 34 mpz_t x2; 35 mpz_t x, rem; 36 mpz_t temp, temp2; 37 mp_size_t x2_size; 38 int i; 39 int reps = 1000; 40 gmp_randstate_ptr rands; 41 mpz_t bs; 42 unsigned long size_range; 43 44 tests_start (); 45 TESTS_REPS (reps, argv, argc); 46 47 rands = RANDS; 48 49 mpz_init (bs); 50 51 mpz_init (x2); 52 mpz_init (x); 53 mpz_init (rem); 54 mpz_init (temp); 55 mpz_init (temp2); 56 57 for (i = 0; i < reps; i++) 58 { 59 mpz_urandomb (bs, rands, 32); 60 size_range = mpz_get_ui (bs) % 17 + 2; /* 0..262144 bit operands */ 61 62 mpz_urandomb (bs, rands, size_range); 63 x2_size = mpz_get_ui (bs); 64 mpz_rrandomb (x2, rands, x2_size); 65 66 /* printf ("%ld\n", SIZ (x2)); */ 67 68 mpz_sqrtrem (x, rem, x2); 69 MPZ_CHECK_FORMAT (x); 70 MPZ_CHECK_FORMAT (rem); 71 72 mpz_mul (temp, x, x); 73 74 /* Is square of result > argument? */ 75 if (mpz_cmp (temp, x2) > 0) 76 dump_abort (x2, x, rem); 77 78 mpz_add_ui (temp2, x, 1); 79 mpz_mul (temp2, temp2, temp2); 80 81 /* Is square of (result + 1) <= argument? */ 82 if (mpz_cmp (temp2, x2) <= 0) 83 dump_abort (x2, x, rem); 84 85 mpz_add (temp2, temp, rem); 86 87 /* Is the remainder wrong? */ 88 if (mpz_cmp (x2, temp2) != 0) 89 dump_abort (x2, x, rem); 90 } 91 92 mpz_clear (bs); 93 mpz_clear (x2); 94 mpz_clear (x); 95 mpz_clear (rem); 96 mpz_clear (temp); 97 mpz_clear (temp2); 98 99 tests_end (); 100 exit (0); 101} 102 103void 104dump_abort (mpz_t x2, mpz_t x, mpz_t rem) 105{ 106 fprintf (stderr, "ERROR\n"); 107 fprintf (stderr, "x2 = "); debug_mp (x2, -16); 108 fprintf (stderr, "x = "); debug_mp (x, -16); 109 fprintf (stderr, "remainder = "); debug_mp (rem, -16); 110 abort(); 111} 112 113void 114debug_mp (mpz_t x, int base) 115{ 116 mpz_out_str (stderr, base, x); fputc ('\n', stderr); 117} 118