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