Home | History | Annotate | Line # | Download | only in tests
tcheck.c revision 1.1.1.3
      1      1.1  mrg /* Test file for mpfr_check.
      2      1.1  mrg 
      3  1.1.1.3  mrg Copyright 2003-2004, 2006-2016 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  mrg http://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 <stdlib.h>
     24      1.1  mrg #include <stdio.h>
     25      1.1  mrg 
     26      1.1  mrg #include "mpfr-test.h"
     27      1.1  mrg 
     28  1.1.1.2  mrg #define ERROR(s)                                                      \
     29  1.1.1.2  mrg   (printf ("mpfr_check failed " s " Prec=%lu\n", (unsigned long) pr), \
     30  1.1.1.2  mrg    exit(1))
     31      1.1  mrg 
     32      1.1  mrg int
     33      1.1  mrg main (void)
     34      1.1  mrg {
     35      1.1  mrg   mpfr_t a;
     36      1.1  mrg   mp_limb_t *p, tmp;
     37      1.1  mrg   mp_size_t s;
     38      1.1  mrg   mpfr_prec_t pr;
     39      1.1  mrg   int max;
     40      1.1  mrg 
     41      1.1  mrg   tests_start_mpfr ();
     42      1.1  mrg   for(pr = MPFR_PREC_MIN ; pr < 500 ; pr++)
     43      1.1  mrg     {
     44      1.1  mrg       mpfr_init2 (a, pr);
     45      1.1  mrg       if (!mpfr_check(a)) ERROR("for init");
     46      1.1  mrg       /* Check special cases */
     47      1.1  mrg       MPFR_SET_NAN(a);
     48      1.1  mrg       if (!mpfr_check(a)) ERROR("for nan");
     49      1.1  mrg       MPFR_SET_POS(a);
     50      1.1  mrg       MPFR_SET_INF(a);
     51      1.1  mrg       if (!mpfr_check(a)) ERROR("for inf");
     52      1.1  mrg       MPFR_SET_ZERO(a);
     53      1.1  mrg       if (!mpfr_check(a)) ERROR("for zero");
     54      1.1  mrg       /* Check var */
     55      1.1  mrg       mpfr_set_ui(a, 2, MPFR_RNDN);
     56      1.1  mrg       if (!mpfr_check(a)) ERROR("for set_ui");
     57      1.1  mrg       mpfr_clear_overflow();
     58      1.1  mrg       max = 1000; /* Allows max 2^1000 bits for the exponent */
     59      1.1  mrg       while ((!mpfr_overflow_p()) && (max>0))
     60      1.1  mrg         {
     61      1.1  mrg           mpfr_mul(a, a, a, MPFR_RNDN);
     62      1.1  mrg           if (!mpfr_check(a)) ERROR("for mul");
     63      1.1  mrg           max--;
     64      1.1  mrg         }
     65      1.1  mrg       if (max==0) ERROR("can't reach overflow");
     66      1.1  mrg       mpfr_set_ui(a, 2137, MPFR_RNDN);
     67      1.1  mrg       /* Corrupt a and check for it */
     68      1.1  mrg       MPFR_SIGN(a) = 2;
     69      1.1  mrg       if (mpfr_check(a))  ERROR("sgn");
     70      1.1  mrg       MPFR_SET_POS(a);
     71      1.1  mrg       /* Check prec */
     72      1.1  mrg       MPFR_PREC(a) = 1;
     73      1.1  mrg       if (mpfr_check(a))  ERROR("precmin");
     74      1.1  mrg #if MPFR_VERSION_MAJOR < 3
     75      1.1  mrg       /* Disable the test with MPFR >= 3 since mpfr_prec_t is now signed.
     76      1.1  mrg          The "if" below is sufficient, but the MPFR_PREC_MAX+1 generates
     77      1.1  mrg          a warning with GCC 4.4.4 even though the test is always false. */
     78      1.1  mrg       if ((mpfr_prec_t) 0 - 1 > 0)
     79      1.1  mrg         {
     80      1.1  mrg           MPFR_PREC(a) = MPFR_PREC_MAX+1;
     81      1.1  mrg           if (mpfr_check(a))  ERROR("precmax");
     82      1.1  mrg         }
     83      1.1  mrg #endif
     84      1.1  mrg       MPFR_PREC(a) = pr;
     85      1.1  mrg       if (!mpfr_check(a)) ERROR("prec");
     86      1.1  mrg       /* Check exponent */
     87      1.1  mrg       MPFR_EXP(a) = MPFR_EXP_INVALID;
     88      1.1  mrg       if (mpfr_check(a))  ERROR("exp invalid");
     89      1.1  mrg       MPFR_EXP(a) = -MPFR_EXP_INVALID;
     90      1.1  mrg       if (mpfr_check(a))  ERROR("-exp invalid");
     91      1.1  mrg       MPFR_EXP(a) = 0;
     92      1.1  mrg       if (!mpfr_check(a)) ERROR("exp 0");
     93      1.1  mrg       /* Check Mantissa */
     94      1.1  mrg       p = MPFR_MANT(a);
     95      1.1  mrg       MPFR_MANT(a) = NULL;
     96      1.1  mrg       if (mpfr_check(a))  ERROR("Mantissa Null Ptr");
     97      1.1  mrg       MPFR_MANT(a) = p;
     98      1.1  mrg       /* Check size */
     99      1.1  mrg       s = MPFR_GET_ALLOC_SIZE(a);
    100      1.1  mrg       MPFR_SET_ALLOC_SIZE(a, 0);
    101      1.1  mrg       if (mpfr_check(a))  ERROR("0 size");
    102      1.1  mrg       MPFR_SET_ALLOC_SIZE(a, MP_SIZE_T_MIN);
    103      1.1  mrg       if (mpfr_check(a))  ERROR("min size");
    104      1.1  mrg       MPFR_SET_ALLOC_SIZE(a, MPFR_LIMB_SIZE(a)-1 );
    105      1.1  mrg       if (mpfr_check(a))  ERROR("size < prec");
    106      1.1  mrg       MPFR_SET_ALLOC_SIZE(a, s);
    107      1.1  mrg       /* Check normal form */
    108      1.1  mrg       tmp = MPFR_MANT(a)[0];
    109      1.1  mrg       if ((pr % GMP_NUMB_BITS) != 0)
    110      1.1  mrg         {
    111      1.1  mrg           MPFR_MANT(a)[0] = ~0;
    112      1.1  mrg           if (mpfr_check(a))  ERROR("last bits non 0");
    113      1.1  mrg         }
    114      1.1  mrg       MPFR_MANT(a)[0] = tmp;
    115      1.1  mrg       MPFR_MANT(a)[MPFR_LIMB_SIZE(a)-1] &= MPFR_LIMB_MASK (GMP_NUMB_BITS-1);
    116      1.1  mrg       if (mpfr_check(a))  ERROR("last bits non 0");
    117      1.1  mrg       /* Final */
    118      1.1  mrg       mpfr_set_ui(a, 2137, MPFR_RNDN);
    119      1.1  mrg       if (!mpfr_check(a)) ERROR("after last set");
    120      1.1  mrg       mpfr_clear (a);
    121      1.1  mrg       if (mpfr_check(a))  ERROR("after clear");
    122      1.1  mrg     }
    123      1.1  mrg   tests_end_mpfr ();
    124      1.1  mrg   return 0;
    125      1.1  mrg }
    126