tcheck.c revision 1.1.1.5 1 1.1 mrg /* Test file for mpfr_check.
2 1.1 mrg
3 1.1.1.5 mrg Copyright 2003-2004, 2006-2020 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.4 mrg mpfr_mul (a, a, a, MPFR_RNDN);
67 1.1.1.4 mrg if (!mpfr_check (a))
68 1.1.1.4 mrg PRINT_ERROR ("for mul");
69 1.1 mrg max--;
70 1.1 mrg }
71 1.1.1.4 mrg if (max == 0)
72 1.1.1.5 mrg PRINT_ERROR ("for overflow");
73 1.1.1.4 mrg mpfr_set_ui (a, 2137, MPFR_RNDN);
74 1.1 mrg /* Corrupt a and check for it */
75 1.1.1.4 mrg MPFR_SIGN (a) = 2;
76 1.1.1.4 mrg if (mpfr_check (a))
77 1.1.1.4 mrg PRINT_ERROR ("sgn");
78 1.1.1.4 mrg MPFR_SET_POS (a);
79 1.1 mrg /* Check prec */
80 1.1.1.4 mrg MPFR_PREC (a) = MPFR_PREC_MIN - 1;
81 1.1.1.4 mrg if (mpfr_check (a)) PRINT_ERROR ("precmin");
82 1.1 mrg #if MPFR_VERSION_MAJOR < 3
83 1.1 mrg /* Disable the test with MPFR >= 3 since mpfr_prec_t is now signed.
84 1.1 mrg The "if" below is sufficient, but the MPFR_PREC_MAX+1 generates
85 1.1 mrg a warning with GCC 4.4.4 even though the test is always false. */
86 1.1 mrg if ((mpfr_prec_t) 0 - 1 > 0)
87 1.1 mrg {
88 1.1.1.4 mrg MPFR_PREC (a) = MPFR_PREC_MAX + 1;
89 1.1.1.4 mrg if (mpfr_check (a))
90 1.1.1.4 mrg PRINT_ERROR ("precmax");
91 1.1 mrg }
92 1.1 mrg #endif
93 1.1.1.4 mrg MPFR_PREC (a) = pr;
94 1.1.1.4 mrg if (!mpfr_check (a))
95 1.1.1.4 mrg PRINT_ERROR ("prec");
96 1.1 mrg /* Check exponent */
97 1.1.1.4 mrg MPFR_EXP (a) = MPFR_EXP_INVALID;
98 1.1.1.4 mrg if (mpfr_check(a))
99 1.1.1.4 mrg PRINT_ERROR ("exp invalid");
100 1.1.1.4 mrg MPFR_EXP (a) = -MPFR_EXP_INVALID;
101 1.1.1.4 mrg if (mpfr_check(a))
102 1.1.1.4 mrg PRINT_ERROR ("-exp invalid");
103 1.1 mrg MPFR_EXP(a) = 0;
104 1.1.1.4 mrg if (!mpfr_check(a)) PRINT_ERROR ("exp 0");
105 1.1 mrg /* Check Mantissa */
106 1.1 mrg p = MPFR_MANT(a);
107 1.1.1.4 mrg MPFR_MANT (a) = NULL;
108 1.1.1.4 mrg if (mpfr_check (a))
109 1.1.1.4 mrg PRINT_ERROR ("Mantissa Null Ptr");
110 1.1.1.4 mrg MPFR_MANT (a) = p;
111 1.1 mrg /* Check size */
112 1.1.1.4 mrg s = MPFR_GET_ALLOC_SIZE (a);
113 1.1.1.4 mrg MPFR_SET_ALLOC_SIZE (a, 0);
114 1.1.1.4 mrg if (mpfr_check (a))
115 1.1.1.4 mrg PRINT_ERROR ("0 size");
116 1.1.1.4 mrg MPFR_SET_ALLOC_SIZE (a, MP_SIZE_T_MIN);
117 1.1.1.4 mrg if (mpfr_check (a)) PRINT_ERROR ("min size");
118 1.1.1.4 mrg MPFR_SET_ALLOC_SIZE (a, MPFR_LIMB_SIZE (a) - 1);
119 1.1.1.4 mrg if (mpfr_check (a))
120 1.1.1.4 mrg PRINT_ERROR ("size < prec");
121 1.1.1.4 mrg MPFR_SET_ALLOC_SIZE (a, s);
122 1.1 mrg /* Check normal form */
123 1.1.1.4 mrg tmp = MPFR_MANT (a)[0];
124 1.1 mrg if ((pr % GMP_NUMB_BITS) != 0)
125 1.1 mrg {
126 1.1.1.4 mrg MPFR_MANT(a)[0] = MPFR_LIMB_MAX;
127 1.1.1.4 mrg if (mpfr_check (a))
128 1.1.1.4 mrg PRINT_ERROR ("last bits non 0");
129 1.1 mrg }
130 1.1 mrg MPFR_MANT(a)[0] = tmp;
131 1.1 mrg MPFR_MANT(a)[MPFR_LIMB_SIZE(a)-1] &= MPFR_LIMB_MASK (GMP_NUMB_BITS-1);
132 1.1.1.4 mrg if (mpfr_check (a))
133 1.1.1.4 mrg PRINT_ERROR ("last bits non 0");
134 1.1 mrg /* Final */
135 1.1.1.4 mrg mpfr_set_ui (a, 2137, MPFR_RNDN);
136 1.1.1.4 mrg if (!mpfr_check (a))
137 1.1.1.4 mrg PRINT_ERROR ("after last set");
138 1.1 mrg mpfr_clear (a);
139 1.1.1.4 mrg if (mpfr_check (a))
140 1.1.1.4 mrg PRINT_ERROR ("after clear");
141 1.1 mrg }
142 1.1 mrg tests_end_mpfr ();
143 1.1 mrg return 0;
144 1.1 mrg }
145