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