1 /* Test file for mpfr_check. 2 3 Copyright 2003-2004, 2006-2020 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramba projects, INRIA. 5 6 This file is part of the GNU MPFR Library. 7 8 The GNU MPFR Library is free software; you can redistribute it and/or modify 9 it under the terms of the GNU Lesser General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or (at your 11 option) any later version. 12 13 The GNU MPFR Library is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16 License for more details. 17 18 You should have received a copy of the GNU Lesser General Public License 19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23 #include "mpfr-test.h" 24 25 #define PRINT_ERROR(s) \ 26 (printf ("mpfr_check failed " s " Prec=%lu\n", (unsigned long) pr), \ 27 exit(1)) 28 29 int 30 main (void) 31 { 32 mpfr_t a; 33 mp_limb_t *p, tmp; 34 mp_size_t s; 35 mpfr_prec_t pr; 36 int max; 37 38 tests_start_mpfr (); 39 for (pr = MPFR_PREC_MIN ; pr < 500 ; pr++) 40 { 41 mpfr_init2 (a, pr); 42 if (!mpfr_check (a)) 43 PRINT_ERROR ("for init"); 44 /* Check special cases */ 45 MPFR_SET_NAN (a); 46 if (!mpfr_check (a)) 47 PRINT_ERROR ("for nan"); 48 MPFR_SET_POS (a); 49 MPFR_SET_INF (a); 50 if (!mpfr_check (a)) 51 PRINT_ERROR ("for inf"); 52 MPFR_SET_ZERO (a); 53 if (!mpfr_check (a)) 54 PRINT_ERROR ("for zero"); 55 MPFR_EXP (a) = MPFR_EXP_MIN; 56 if (mpfr_check (a)) 57 PRINT_ERROR ("for EXP = MPFR_EXP_MIN"); 58 /* Check var */ 59 mpfr_set_ui (a, 2, MPFR_RNDN); 60 if (!mpfr_check (a)) 61 PRINT_ERROR ("for set_ui"); 62 mpfr_clear_overflow (); 63 max = 1000; /* Allows max 2^1000 bits for the exponent */ 64 while (!mpfr_overflow_p () && max > 0) 65 { 66 mpfr_mul (a, a, a, MPFR_RNDN); 67 if (!mpfr_check (a)) 68 PRINT_ERROR ("for mul"); 69 max--; 70 } 71 if (max == 0) 72 PRINT_ERROR ("for overflow"); 73 mpfr_set_ui (a, 2137, MPFR_RNDN); 74 /* Corrupt a and check for it */ 75 MPFR_SIGN (a) = 2; 76 if (mpfr_check (a)) 77 PRINT_ERROR ("sgn"); 78 MPFR_SET_POS (a); 79 /* Check prec */ 80 MPFR_PREC (a) = MPFR_PREC_MIN - 1; 81 if (mpfr_check (a)) PRINT_ERROR ("precmin"); 82 #if MPFR_VERSION_MAJOR < 3 83 /* Disable the test with MPFR >= 3 since mpfr_prec_t is now signed. 84 The "if" below is sufficient, but the MPFR_PREC_MAX+1 generates 85 a warning with GCC 4.4.4 even though the test is always false. */ 86 if ((mpfr_prec_t) 0 - 1 > 0) 87 { 88 MPFR_PREC (a) = MPFR_PREC_MAX + 1; 89 if (mpfr_check (a)) 90 PRINT_ERROR ("precmax"); 91 } 92 #endif 93 MPFR_PREC (a) = pr; 94 if (!mpfr_check (a)) 95 PRINT_ERROR ("prec"); 96 /* Check exponent */ 97 MPFR_EXP (a) = MPFR_EXP_INVALID; 98 if (mpfr_check(a)) 99 PRINT_ERROR ("exp invalid"); 100 MPFR_EXP (a) = -MPFR_EXP_INVALID; 101 if (mpfr_check(a)) 102 PRINT_ERROR ("-exp invalid"); 103 MPFR_EXP(a) = 0; 104 if (!mpfr_check(a)) PRINT_ERROR ("exp 0"); 105 /* Check Mantissa */ 106 p = MPFR_MANT(a); 107 MPFR_MANT (a) = NULL; 108 if (mpfr_check (a)) 109 PRINT_ERROR ("Mantissa Null Ptr"); 110 MPFR_MANT (a) = p; 111 /* Check size */ 112 s = MPFR_GET_ALLOC_SIZE (a); 113 MPFR_SET_ALLOC_SIZE (a, 0); 114 if (mpfr_check (a)) 115 PRINT_ERROR ("0 size"); 116 MPFR_SET_ALLOC_SIZE (a, MP_SIZE_T_MIN); 117 if (mpfr_check (a)) PRINT_ERROR ("min size"); 118 MPFR_SET_ALLOC_SIZE (a, MPFR_LIMB_SIZE (a) - 1); 119 if (mpfr_check (a)) 120 PRINT_ERROR ("size < prec"); 121 MPFR_SET_ALLOC_SIZE (a, s); 122 /* Check normal form */ 123 tmp = MPFR_MANT (a)[0]; 124 if ((pr % GMP_NUMB_BITS) != 0) 125 { 126 MPFR_MANT(a)[0] = MPFR_LIMB_MAX; 127 if (mpfr_check (a)) 128 PRINT_ERROR ("last bits non 0"); 129 } 130 MPFR_MANT(a)[0] = tmp; 131 MPFR_MANT(a)[MPFR_LIMB_SIZE(a)-1] &= MPFR_LIMB_MASK (GMP_NUMB_BITS-1); 132 if (mpfr_check (a)) 133 PRINT_ERROR ("last bits non 0"); 134 /* Final */ 135 mpfr_set_ui (a, 2137, MPFR_RNDN); 136 if (!mpfr_check (a)) 137 PRINT_ERROR ("after last set"); 138 mpfr_clear (a); 139 if (mpfr_check (a)) 140 PRINT_ERROR ("after clear"); 141 } 142 tests_end_mpfr (); 143 return 0; 144 } 145