xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpf/t-fits.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /* Test mpf_fits_*_p
2 
3 Copyright 2001, 2002 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp.h"
23 #include "gmp-impl.h"
24 #include "tests.h"
25 
26 
27 /* Nothing sophisticated here, just exercise mpf_fits_*_p on a small amount
28    of data. */
29 
30 #define EXPECT_S(fun,name,answer)                                        \
31   got = fun (f);                                                         \
32   if (got != answer)                                                     \
33     {                                                                    \
34       printf ("%s (%s) got %d want %d\n", name, expr, got, answer);      \
35       printf (" f size %d exp %ld\n", SIZ(f), EXP(f));                   \
36       printf (" f dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n"); \
37       printf (" f hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n"); \
38       error = 1;                                                         \
39     }
40 
41 #if HAVE_STRINGIZE
42 #define EXPECT(fun,answer)  EXPECT_S(fun,#fun,answer)
43 #else
44 #define EXPECT(fun,answer)  EXPECT_S(fun,"fun",answer)
45 #endif
46 
47 int
48 main (void)
49 {
50   mpf_t       f, f0p5;
51   int         got;
52   const char  *expr;
53   int         error = 0;
54 
55   tests_start ();
56   mpf_init2 (f, 200L);
57   mpf_init2 (f0p5, 200L);
58 
59   /* 0.5 */
60   mpf_set_ui (f0p5, 1L);
61   mpf_div_2exp (f0p5, f0p5, 1L);
62 
63   mpf_set_ui (f, 0L);
64   expr = "0";
65   EXPECT (mpf_fits_ulong_p, 1);
66   EXPECT (mpf_fits_uint_p, 1);
67   EXPECT (mpf_fits_ushort_p, 1);
68   EXPECT (mpf_fits_slong_p, 1);
69   EXPECT (mpf_fits_sint_p, 1);
70   EXPECT (mpf_fits_sshort_p, 1);
71 
72   mpf_set_ui (f, 1L);
73   expr = "1";
74   EXPECT (mpf_fits_ulong_p, 1);
75   EXPECT (mpf_fits_uint_p, 1);
76   EXPECT (mpf_fits_ushort_p, 1);
77   EXPECT (mpf_fits_slong_p, 1);
78   EXPECT (mpf_fits_sint_p, 1);
79   EXPECT (mpf_fits_sshort_p, 1);
80 
81   mpf_set_si (f, -1L);
82   expr = "-1";
83   EXPECT (mpf_fits_ulong_p, 0);
84   EXPECT (mpf_fits_uint_p, 0);
85   EXPECT (mpf_fits_ushort_p, 0);
86   EXPECT (mpf_fits_slong_p, 1);
87   EXPECT (mpf_fits_sint_p, 1);
88   EXPECT (mpf_fits_sshort_p, 1);
89 
90 
91   mpf_set_ui (f, (unsigned long) USHRT_MAX);
92   expr = "USHRT_MAX";
93   EXPECT (mpf_fits_ulong_p, 1);
94   EXPECT (mpf_fits_uint_p, 1);
95   EXPECT (mpf_fits_ushort_p, 1);
96 
97   mpf_set_ui (f, (unsigned long) USHRT_MAX);
98   mpf_add (f, f, f0p5);
99   expr = "USHRT_MAX + 0.5";
100   EXPECT (mpf_fits_ulong_p, 1);
101   EXPECT (mpf_fits_uint_p, 1);
102   EXPECT (mpf_fits_ushort_p, 1);
103 
104   mpf_set_ui (f, (unsigned long) USHRT_MAX);
105   mpf_add_ui (f, f, 1L);
106   expr = "USHRT_MAX + 1";
107   EXPECT (mpf_fits_ushort_p, 0);
108 
109 
110   mpf_set_ui (f, (unsigned long) UINT_MAX);
111   expr = "UINT_MAX";
112   EXPECT (mpf_fits_ulong_p, 1);
113   EXPECT (mpf_fits_uint_p, 1);
114 
115   mpf_set_ui (f, (unsigned long) UINT_MAX);
116   mpf_add (f, f, f0p5);
117   expr = "UINT_MAX + 0.5";
118   EXPECT (mpf_fits_ulong_p, 1);
119   EXPECT (mpf_fits_uint_p, 1);
120 
121   mpf_set_ui (f, (unsigned long) UINT_MAX);
122   mpf_add_ui (f, f, 1L);
123   expr = "UINT_MAX + 1";
124   EXPECT (mpf_fits_uint_p, 0);
125 
126 
127   mpf_set_ui (f, ULONG_MAX);
128   expr = "ULONG_MAX";
129   EXPECT (mpf_fits_ulong_p, 1);
130 
131   mpf_set_ui (f, ULONG_MAX);
132   mpf_add (f, f, f0p5);
133   expr = "ULONG_MAX + 0.5";
134   EXPECT (mpf_fits_ulong_p, 1);
135 
136   mpf_set_ui (f, ULONG_MAX);
137   mpf_add_ui (f, f, 1L);
138   expr = "ULONG_MAX + 1";
139   EXPECT (mpf_fits_ulong_p, 0);
140 
141 
142   mpf_set_si (f, (long) SHRT_MAX);
143   expr = "SHRT_MAX";
144   EXPECT (mpf_fits_slong_p, 1);
145   EXPECT (mpf_fits_sint_p, 1);
146   EXPECT (mpf_fits_sshort_p, 1);
147 
148   mpf_set_si (f, (long) SHRT_MAX);
149   expr = "SHRT_MAX + 0.5";
150   mpf_add (f, f, f0p5);
151   EXPECT (mpf_fits_slong_p, 1);
152   EXPECT (mpf_fits_sint_p, 1);
153   EXPECT (mpf_fits_sshort_p, 1);
154 
155   mpf_set_si (f, (long) SHRT_MAX);
156   mpf_add_ui (f, f, 1L);
157   expr = "SHRT_MAX + 1";
158   EXPECT (mpf_fits_sshort_p, 0);
159 
160 
161   mpf_set_si (f, (long) INT_MAX);
162   expr = "INT_MAX";
163   EXPECT (mpf_fits_slong_p, 1);
164   EXPECT (mpf_fits_sint_p, 1);
165 
166   mpf_set_si (f, (long) INT_MAX);
167   mpf_add (f, f, f0p5);
168   expr = "INT_MAX + 0.5";
169   EXPECT (mpf_fits_slong_p, 1);
170   EXPECT (mpf_fits_sint_p, 1);
171 
172   mpf_set_si (f, (long) INT_MAX);
173   mpf_add_ui (f, f, 1L);
174   expr = "INT_MAX + 1";
175   EXPECT (mpf_fits_sint_p, 0);
176 
177 
178   mpf_set_si (f, LONG_MAX);
179   expr = "LONG_MAX";
180   EXPECT (mpf_fits_slong_p, 1);
181 
182   mpf_set_si (f, LONG_MAX);
183   mpf_add (f, f, f0p5);
184   expr = "LONG_MAX + 0.5";
185   EXPECT (mpf_fits_slong_p, 1);
186 
187   mpf_set_si (f, LONG_MAX);
188   mpf_add_ui (f, f, 1L);
189   expr = "LONG_MAX + 1";
190   EXPECT (mpf_fits_slong_p, 0);
191 
192 
193   mpf_set_si (f, (long) SHRT_MIN);
194   expr = "SHRT_MIN";
195   EXPECT (mpf_fits_slong_p, 1);
196   EXPECT (mpf_fits_sint_p, 1);
197   EXPECT (mpf_fits_sshort_p, 1);
198 
199   mpf_set_si (f, (long) SHRT_MIN);
200   mpf_sub (f, f, f0p5);
201   expr = "SHRT_MIN - 0.5";
202   EXPECT (mpf_fits_slong_p, 1);
203   EXPECT (mpf_fits_sint_p, 1);
204   EXPECT (mpf_fits_sshort_p, 1);
205 
206   mpf_set_si (f, (long) SHRT_MIN);
207   mpf_sub_ui (f, f, 1L);
208   expr = "SHRT_MIN + 1";
209   EXPECT (mpf_fits_sshort_p, 0);
210 
211 
212   mpf_set_si (f, (long) INT_MIN);
213   expr = "INT_MIN";
214   EXPECT (mpf_fits_slong_p, 1);
215   EXPECT (mpf_fits_sint_p, 1);
216 
217   mpf_set_si (f, (long) INT_MIN);
218   mpf_sub (f, f, f0p5);
219   expr = "INT_MIN - 0.5";
220   EXPECT (mpf_fits_slong_p, 1);
221   EXPECT (mpf_fits_sint_p, 1);
222 
223   mpf_set_si (f, (long) INT_MIN);
224   mpf_sub_ui (f, f, 1L);
225   expr = "INT_MIN + 1";
226   EXPECT (mpf_fits_sint_p, 0);
227 
228 
229   mpf_set_si (f, LONG_MIN);
230   expr = "LONG_MIN";
231   EXPECT (mpf_fits_slong_p, 1);
232 
233   mpf_set_si (f, LONG_MIN);
234   mpf_sub (f, f, f0p5);
235   expr = "LONG_MIN - 0.5";
236   EXPECT (mpf_fits_slong_p, 1);
237 
238   mpf_set_si (f, LONG_MIN);
239   mpf_sub_ui (f, f, 1L);
240   expr = "LONG_MIN + 1";
241   EXPECT (mpf_fits_slong_p, 0);
242 
243 
244   mpf_set_str_or_abort (f, "0.5", 10);
245   expr = "0.5";
246   EXPECT (mpf_fits_ulong_p, 1);
247   EXPECT (mpf_fits_uint_p, 1);
248   EXPECT (mpf_fits_ushort_p, 1);
249   EXPECT (mpf_fits_slong_p, 1);
250   EXPECT (mpf_fits_sint_p, 1);
251   EXPECT (mpf_fits_sshort_p, 1);
252 
253   mpf_set_str_or_abort (f, "-0.5", 10);
254   expr = "-0.5";
255   EXPECT (mpf_fits_ulong_p, 0);
256   EXPECT (mpf_fits_uint_p, 0);
257   EXPECT (mpf_fits_ushort_p, 0);
258   EXPECT (mpf_fits_slong_p, 1);
259   EXPECT (mpf_fits_sint_p, 1);
260   EXPECT (mpf_fits_sshort_p, 1);
261 
262 
263   mpf_set_str_or_abort (f, "1.000000000000000000000000000000000001", 16);
264   expr = "1.000000000000000000000000000000000001 base 16";
265   EXPECT (mpf_fits_ulong_p, 1);
266   EXPECT (mpf_fits_uint_p, 1);
267   EXPECT (mpf_fits_ushort_p, 1);
268   EXPECT (mpf_fits_slong_p, 1);
269   EXPECT (mpf_fits_sint_p, 1);
270   EXPECT (mpf_fits_sshort_p, 1);
271 
272   mpf_set_str_or_abort (f, "1@1000", 16);
273   expr = "1@1000 base 16";
274   EXPECT (mpf_fits_ulong_p, 0);
275   EXPECT (mpf_fits_uint_p, 0);
276   EXPECT (mpf_fits_ushort_p, 0);
277   EXPECT (mpf_fits_slong_p, 0);
278   EXPECT (mpf_fits_sint_p, 0);
279   EXPECT (mpf_fits_sshort_p, 0);
280 
281 
282   mpf_set_ui (f, 1L);
283   mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
284   mpf_sub_ui (f, f, 1L);
285   expr = "2^(BITS_PER_ULONG+1) - 1";
286   EXPECT (mpf_fits_ulong_p, 0);
287   EXPECT (mpf_fits_uint_p, 0);
288   EXPECT (mpf_fits_ushort_p, 0);
289   EXPECT (mpf_fits_slong_p, 0);
290   EXPECT (mpf_fits_sint_p, 0);
291   EXPECT (mpf_fits_sshort_p, 0);
292 
293   mpf_set_ui (f, 1L);
294   mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
295   mpf_sub_ui (f, f, 1L);
296   mpf_neg (f, f);
297   expr = "- (2^(BITS_PER_ULONG+1) - 1)";
298   EXPECT (mpf_fits_ulong_p, 0);
299   EXPECT (mpf_fits_uint_p, 0);
300   EXPECT (mpf_fits_ushort_p, 0);
301   EXPECT (mpf_fits_slong_p, 0);
302   EXPECT (mpf_fits_sint_p, 0);
303   EXPECT (mpf_fits_sshort_p, 0);
304 
305   mpf_set_ui (f, 1L);
306   mpf_mul_2exp (f, f, BITS_PER_ULONG + 5);
307   mpf_sub_ui (f, f, 1L);
308   expr = "2^(BITS_PER_ULONG+5) - 1";
309   EXPECT (mpf_fits_ulong_p, 0);
310   EXPECT (mpf_fits_uint_p, 0);
311   EXPECT (mpf_fits_ushort_p, 0);
312   EXPECT (mpf_fits_slong_p, 0);
313   EXPECT (mpf_fits_sint_p, 0);
314   EXPECT (mpf_fits_sshort_p, 0);
315 
316 
317   if (error)
318     abort ();
319 
320   mpf_clear (f);
321   mpf_clear (f0p5);
322   tests_end ();
323   exit (0);
324 }
325