github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpf/t-set.c (about) 1 /* Test mpf_set, mpf_init_set. 2 3 Copyright 2004, 2012 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 #include "gmp.h" 23 #include "gmp-impl.h" 24 #include "tests.h" 25 26 void 27 check_reuse (void) 28 { 29 /* Try mpf_set(f,f) when f is bigger than prec. In the past this had 30 resulted in an MPN_COPY with invalid operand overlap. */ 31 mpf_t f; 32 mp_size_t limbs = 20; 33 unsigned long bits = limbs * GMP_NUMB_BITS; 34 mpf_init2 (f, bits); 35 refmpf_fill (f, limbs, GMP_NUMB_MAX); 36 mpf_set_prec_raw (f, bits / 2); 37 mpf_set (f, f); 38 MPF_CHECK_FORMAT (f); 39 mpf_set_prec_raw (f, bits); 40 mpf_clear (f); 41 } 42 43 void 44 check_random (long reps) 45 { 46 unsigned long test; 47 gmp_randstate_ptr rands; 48 mpf_t a, b; 49 mpz_t z; 50 int precbits; 51 52 #define PRECBITS 10 53 54 rands = RANDS; 55 56 mpz_init (z); 57 mpf_init2 (a, 1 << PRECBITS); 58 59 for (test = 0; test < reps; test++) 60 { 61 mpz_urandomb (z, rands, PRECBITS + 1); 62 precbits = mpz_get_ui (z) + 1; 63 mpz_urandomb (z, rands, precbits); 64 mpz_setbit (z, precbits - 1); /* make sure msb is set */ 65 mpf_set_z (a, z); 66 if (precbits & 1) 67 mpf_neg (a, a); 68 mpz_urandomb (z, rands, PRECBITS); 69 mpf_div_2exp (a, a, mpz_get_ui (z) + 1); 70 mpz_urandomb (z, rands, PRECBITS); 71 precbits -= mpz_get_ui (z); 72 if (precbits <= 0) 73 precbits = 1 - precbits; 74 mpf_set_default_prec (precbits); 75 76 mpf_init_set (b, a); 77 MPF_CHECK_FORMAT (b); 78 if (!mpf_eq (a, b, precbits)) 79 { 80 printf ("mpf_init_set wrong.\n"); 81 abort(); 82 } 83 84 mpf_set_ui (b, 0); 85 mpf_set (b, a); 86 MPF_CHECK_FORMAT (b); 87 if (!mpf_eq (a, b, precbits)) 88 { 89 printf ("mpf_set wrong.\n"); 90 abort(); 91 } 92 93 mpf_clear (b); 94 } 95 96 mpf_clear (a); 97 mpz_clear (z); 98 } 99 100 int 101 main (int argc, char *argv[]) 102 { 103 long reps = 10000; 104 105 tests_start (); 106 TESTS_REPS (reps, argv, argc); 107 108 check_reuse (); 109 check_random (reps); 110 111 tests_end (); 112 exit (0); 113 }