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