xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tfits.c (revision 6cd39ddb8550f6fa1bff3fed32053d7f19fd0453)
1 /* Test file for:
2  mpfr_fits_sint_p, mpfr_fits_slong_p, mpfr_fits_sshort_p,
3  mpfr_fits_uint_p, mpfr_fits_ulong_p, mpfr_fits_ushort_p
4 
5 Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
6 Contributed by the AriC and Caramel projects, INRIA.
7 
8 This file is part of the GNU MPFR Library.
9 
10 The GNU MPFR Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
14 
15 The GNU MPFR Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18 License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
22 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24 
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"       /* for a build within gmp */
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <limits.h>
32 
33 #include "mpfr-intmax.h"
34 #include "mpfr-test.h"
35 
36 #define ERROR1 { printf("Initial error for x="); mpfr_dump(x); exit(1); }
37 #define ERROR2 { printf("Error for x="); mpfr_dump(x); exit(1); }
38 
39 static void check_intmax (void);
40 
41 int
42 main (void)
43 {
44   mpfr_t x;
45 
46   tests_start_mpfr ();
47 
48   mpfr_init2 (x, 256);
49 
50   /* Check NAN */
51   mpfr_set_nan (x);
52   if (mpfr_fits_ulong_p (x, MPFR_RNDN))
53     ERROR1;
54   if (mpfr_fits_slong_p (x, MPFR_RNDN))
55     ERROR1;
56   if (mpfr_fits_uint_p (x, MPFR_RNDN))
57     ERROR1;
58   if (mpfr_fits_sint_p (x, MPFR_RNDN))
59     ERROR1;
60   if (mpfr_fits_ushort_p (x, MPFR_RNDN))
61     ERROR1;
62   if (mpfr_fits_sshort_p (x, MPFR_RNDN))
63     ERROR1;
64 
65   /* Check INF */
66   mpfr_set_inf (x, 1);
67   if (mpfr_fits_ulong_p (x, MPFR_RNDN))
68     ERROR1;
69   if (mpfr_fits_slong_p (x, MPFR_RNDN))
70     ERROR1;
71   if (mpfr_fits_uint_p (x, MPFR_RNDN))
72     ERROR1;
73   if (mpfr_fits_sint_p (x, MPFR_RNDN))
74     ERROR1;
75   if (mpfr_fits_ushort_p (x, MPFR_RNDN))
76     ERROR1;
77   if (mpfr_fits_sshort_p (x, MPFR_RNDN))
78     ERROR1;
79 
80   /* Check Zero */
81   MPFR_SET_ZERO (x);
82   if (!mpfr_fits_ulong_p (x, MPFR_RNDN))
83     ERROR2;
84   if (!mpfr_fits_slong_p (x, MPFR_RNDN))
85     ERROR2;
86   if (!mpfr_fits_uint_p (x, MPFR_RNDN))
87     ERROR2;
88   if (!mpfr_fits_sint_p (x, MPFR_RNDN))
89     ERROR2;
90   if (!mpfr_fits_ushort_p (x, MPFR_RNDN))
91     ERROR2;
92   if (!mpfr_fits_sshort_p (x, MPFR_RNDN))
93     ERROR2;
94 
95   /* Check small op */
96   mpfr_set_str1 (x, "1@-1");
97   if (!mpfr_fits_ulong_p (x, MPFR_RNDN))
98     ERROR2;
99   if (!mpfr_fits_slong_p (x, MPFR_RNDN))
100     ERROR2;
101   if (!mpfr_fits_uint_p (x, MPFR_RNDN))
102     ERROR2;
103   if (!mpfr_fits_sint_p (x, MPFR_RNDN))
104     ERROR2;
105   if (!mpfr_fits_ushort_p (x, MPFR_RNDN))
106     ERROR2;
107   if (!mpfr_fits_sshort_p (x, MPFR_RNDN))
108     ERROR2;
109 
110   /* Check 17 */
111   mpfr_set_ui (x, 17, MPFR_RNDN);
112   if (!mpfr_fits_ulong_p (x, MPFR_RNDN))
113     ERROR2;
114   if (!mpfr_fits_slong_p (x, MPFR_RNDN))
115     ERROR2;
116   if (!mpfr_fits_uint_p (x, MPFR_RNDN))
117     ERROR2;
118   if (!mpfr_fits_sint_p (x, MPFR_RNDN))
119     ERROR2;
120   if (!mpfr_fits_ushort_p (x, MPFR_RNDN))
121     ERROR2;
122   if (!mpfr_fits_sshort_p (x, MPFR_RNDN))
123     ERROR2;
124 
125   /* Check all other values */
126   mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN);
127   mpfr_mul_2exp (x, x, 1, MPFR_RNDN);
128   if (mpfr_fits_ulong_p (x, MPFR_RNDN))
129     ERROR1;
130   if (mpfr_fits_slong_p (x, MPFR_RNDN))
131     ERROR1;
132   mpfr_mul_2exp (x, x, 40, MPFR_RNDN);
133   if (mpfr_fits_ulong_p (x, MPFR_RNDN))
134     ERROR1;
135   if (mpfr_fits_uint_p (x, MPFR_RNDN))
136     ERROR1;
137   if (mpfr_fits_sint_p (x, MPFR_RNDN))
138     ERROR1;
139   if (mpfr_fits_ushort_p (x, MPFR_RNDN))
140     ERROR1;
141   if (mpfr_fits_sshort_p (x, MPFR_RNDN))
142     ERROR1;
143 
144   mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN);
145   if (!mpfr_fits_ulong_p (x, MPFR_RNDN))
146     ERROR2;
147   mpfr_set_ui (x, LONG_MAX, MPFR_RNDN);
148   if (!mpfr_fits_slong_p (x, MPFR_RNDN))
149     ERROR2;
150   mpfr_set_ui (x, UINT_MAX, MPFR_RNDN);
151   if (!mpfr_fits_uint_p (x, MPFR_RNDN))
152     ERROR2;
153   mpfr_set_ui (x, INT_MAX, MPFR_RNDN);
154   if (!mpfr_fits_sint_p (x, MPFR_RNDN))
155     ERROR2;
156   mpfr_set_ui (x, USHRT_MAX, MPFR_RNDN);
157   if (!mpfr_fits_ushort_p (x, MPFR_RNDN))
158     ERROR2;
159   mpfr_set_ui (x, SHRT_MAX, MPFR_RNDN);
160   if (!mpfr_fits_sshort_p (x, MPFR_RNDN))
161     ERROR2;
162 
163   mpfr_set_si (x, 1, MPFR_RNDN);
164   if (!mpfr_fits_sint_p (x, MPFR_RNDN))
165     ERROR2;
166   if (!mpfr_fits_sshort_p (x, MPFR_RNDN))
167     ERROR2;
168 
169   /* Check negative value */
170   mpfr_set_si (x, -1, MPFR_RNDN);
171   if (!mpfr_fits_sint_p (x, MPFR_RNDN))
172     ERROR2;
173   if (!mpfr_fits_sshort_p (x, MPFR_RNDN))
174     ERROR2;
175   if (!mpfr_fits_slong_p (x, MPFR_RNDN))
176     ERROR2;
177   if (mpfr_fits_uint_p (x, MPFR_RNDN))
178     ERROR1;
179   if (mpfr_fits_ushort_p (x, MPFR_RNDN))
180     ERROR1;
181   if (mpfr_fits_ulong_p (x, MPFR_RNDN))
182     ERROR1;
183 
184   mpfr_clear (x);
185 
186   check_intmax ();
187 
188   tests_end_mpfr ();
189   return 0;
190 }
191 
192 static void check_intmax (void)
193 {
194 #ifdef _MPFR_H_HAVE_INTMAX_T
195   mpfr_t x;
196 
197   mpfr_init2 (x, sizeof (uintmax_t)*CHAR_BIT);
198 
199   /* Check NAN */
200   mpfr_set_nan (x);
201   if (mpfr_fits_uintmax_p (x, MPFR_RNDN))
202     ERROR1;
203   if (mpfr_fits_intmax_p (x, MPFR_RNDN))
204     ERROR1;
205 
206   /* Check INF */
207   mpfr_set_inf (x, 1);
208   if (mpfr_fits_uintmax_p (x, MPFR_RNDN))
209     ERROR1;
210   if (mpfr_fits_intmax_p (x, MPFR_RNDN))
211     ERROR1;
212 
213   /* Check Zero */
214   MPFR_SET_ZERO (x);
215   if (!mpfr_fits_uintmax_p (x, MPFR_RNDN))
216     ERROR2;
217   if (!mpfr_fits_intmax_p (x, MPFR_RNDN))
218     ERROR2;
219 
220   /* Check small op */
221   mpfr_set_str1 (x, "1@-1");
222   if (!mpfr_fits_uintmax_p (x, MPFR_RNDN))
223     ERROR2;
224   if (!mpfr_fits_intmax_p (x, MPFR_RNDN))
225     ERROR2;
226 
227   /* Check 17 */
228   mpfr_set_ui (x, 17, MPFR_RNDN);
229   if (!mpfr_fits_uintmax_p (x, MPFR_RNDN))
230     ERROR2;
231   if (!mpfr_fits_intmax_p (x, MPFR_RNDN))
232     ERROR2;
233 
234   /* Check hugest */
235   mpfr_set_ui_2exp (x, 42, sizeof (uintmax_t) * 32, MPFR_RNDN);
236   if (mpfr_fits_uintmax_p (x, MPFR_RNDN))
237     ERROR1;
238   if (mpfr_fits_intmax_p (x, MPFR_RNDN))
239     ERROR1;
240 
241   /* Check all other values */
242   mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN);
243   mpfr_add_ui (x, x, 1, MPFR_RNDN);
244   if (mpfr_fits_uintmax_p (x, MPFR_RNDN))
245     ERROR1;
246   mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN);
247   if (!mpfr_fits_uintmax_p (x, MPFR_RNDN))
248     ERROR2;
249   mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN);
250   mpfr_add_ui (x, x, 1, MPFR_RNDN);
251   if (mpfr_fits_intmax_p (x, MPFR_RNDN))
252     ERROR1;
253   mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN);
254   if (!mpfr_fits_intmax_p (x, MPFR_RNDN))
255     ERROR2;
256   mpfr_set_sj (x, MPFR_INTMAX_MIN, MPFR_RNDN);
257   if (!mpfr_fits_intmax_p (x, MPFR_RNDN))
258     ERROR2;
259   mpfr_sub_ui (x, x, 1, MPFR_RNDN);
260   if (mpfr_fits_intmax_p (x, MPFR_RNDN))
261     ERROR1;
262 
263   /* Check negative value */
264   mpfr_set_si (x, -1, MPFR_RNDN);
265   if (!mpfr_fits_intmax_p (x, MPFR_RNDN))
266     ERROR2;
267   if (mpfr_fits_uintmax_p (x, MPFR_RNDN))
268     ERROR1;
269 
270   mpfr_clear (x);
271 #endif
272 }
273 
274