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