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