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