xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tcmp_ui.c (revision a04395531661c5e8d314125d5ae77d4cbedd5d73)
1 /* Test file for mpfr_cmp_ui and mpfr_cmp_si.
2 
3 Copyright 1999, 2001-2020 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #ifdef TCMP_UI_CHECK_NAN
24 
25 mpfr_clear_erangeflag ();
26 c = mpfr_cmp_si (x, TCMP_UI_CHECK_NAN);
27 if (c != 0 || !mpfr_erangeflag_p ())
28   {
29     printf ("NaN error on %d (1)\n", TCMP_UI_CHECK_NAN);
30     exit (1);
31   }
32 mpfr_clear_erangeflag ();
33 c = (mpfr_cmp_si) (x, TCMP_UI_CHECK_NAN);
34 if (c != 0 || !mpfr_erangeflag_p ())
35   {
36     printf ("NaN error on %d (2)\n", TCMP_UI_CHECK_NAN);
37     exit (1);
38   }
39 if (TCMP_UI_CHECK_NAN >= 0)
40   {
41     mpfr_clear_erangeflag ();
42     c = mpfr_cmp_ui (x, TCMP_UI_CHECK_NAN);
43     if (c != 0 || !mpfr_erangeflag_p ())
44       {
45         printf ("NaN error on %d (3)\n", TCMP_UI_CHECK_NAN);
46         exit (1);
47       }
48     mpfr_clear_erangeflag ();
49     c = (mpfr_cmp_ui) (x, TCMP_UI_CHECK_NAN);
50     if (c != 0 || !mpfr_erangeflag_p ())
51       {
52         printf ("NaN error on %d (4)\n", TCMP_UI_CHECK_NAN);
53         exit (1);
54       }
55   }
56 
57 #else
58 
59 #include "mpfr-test.h"
60 
61 static void
62 check_nan (void)
63 {
64   mpfr_t x;
65   int c, i;
66 
67   mpfr_init (x);
68   mpfr_set_nan (x);
69   /* We need constants to completely test the macros. */
70 #undef TCMP_UI_CHECK_NAN
71 #define TCMP_UI_CHECK_NAN -17
72 #include "tcmp_ui.c"
73 #undef TCMP_UI_CHECK_NAN
74 #define TCMP_UI_CHECK_NAN 0
75 #include "tcmp_ui.c"
76 #undef TCMP_UI_CHECK_NAN
77 #define TCMP_UI_CHECK_NAN 17
78 #include "tcmp_ui.c"
79   for (i = -17; i <= 17; i += 17)
80     {
81 #undef TCMP_UI_CHECK_NAN
82 #define TCMP_UI_CHECK_NAN i
83 #include "tcmp_ui.c"
84     }
85   mpfr_clear (x);
86 }
87 
88 /* Since mpfr_cmp_ui and mpfr_cmp_si are also implemented by a macro
89    with __builtin_constant_p for GCC, check that side effects are
90    handled correctly. */
91 static void
92 check_macros (void)
93 {
94   mpfr_t x;
95   int c;
96 
97   mpfr_init2 (x, 32);
98 
99   c = 0;
100   mpfr_set_ui (x, 17, MPFR_RNDN);
101   if (mpfr_cmp_ui (x, 17) != 0)
102     {
103       printf ("Error 1 on mpfr_cmp_ui(x,17) in check_macros\n");
104       exit (1);
105     }
106   if (mpfr_cmp_ui (x, (c++, 17)) != 0)
107     {
108       printf ("Error 2 on mpfr_cmp_ui(x,17) in check_macros\n");
109       exit (1);
110     }
111   if (c != 1)
112     {
113       /* Failure in r13626 on x86_64 with the clang-9 1:9-1 Debian package,
114          with any optimization level: c = 2 instead of 1
115          Bug report: https://bugs.llvm.org/show_bug.cgi?id=43557 */
116       /* [2020-06-17]
117          If one adds tcc support for macros using __builtin_constant_p
118          in mpfr.h by testing __TINYC__, one also gets a failure.
119          Bug report: https://savannah.nongnu.org/bugs/?58606
120          "__builtin_constant_p is buggy on argument with side effect and
121          constant value" */
122       printf ("Error 3 on mpfr_cmp_ui(x,17) in check_macros\n"
123               "(c = %d instead of 1)\n", c);
124       exit (1);
125     }
126   if (mpfr_cmp_si (x, 17) != 0)
127     {
128       printf ("Error 1 on mpfr_cmp_si(x,17) in check_macros\n");
129       exit (1);
130     }
131   if (mpfr_cmp_si (x, (c++, 17)) != 0)
132     {
133       printf ("Error 2 on mpfr_cmp_si(x,17) in check_macros\n");
134       exit (1);
135     }
136   if (c != 2)
137     {
138       printf ("Error 3 on mpfr_cmp_si(x,17) in check_macros\n"
139               "(c = %d instead of 2)\n", c);
140       exit (1);
141     }
142 
143   c = 0;
144   mpfr_set_ui (x, 0, MPFR_RNDN);
145   if (mpfr_cmp_ui (x, 0) != 0)
146     {
147       printf ("Error 1 on mpfr_cmp_ui(x,0) in check_macros\n");
148       exit (1);
149     }
150   if (mpfr_cmp_ui (x, (c++, 0)) != 0)
151     {
152       printf ("Error 2 on mpfr_cmp_ui(x,0) in check_macros\n");
153       exit (1);
154     }
155   if (c != 1)
156     {
157       printf ("Error 3 on mpfr_cmp_ui(x,0) in check_macros\n"
158               "(c = %d instead of 1)\n", c);
159       exit (1);
160     }
161   if (mpfr_cmp_si (x, 0) != 0)
162     {
163       printf ("Error 1 on mpfr_cmp_si(x,0) in check_macros\n");
164       exit (1);
165     }
166   if (mpfr_cmp_si (x, (c++, 0)) != 0)
167     {
168       printf ("Error 2 on mpfr_cmp_si(x,0) in check_macros\n");
169       exit (1);
170     }
171   if (c != 2)
172     {
173       printf ("Error 3 on mpfr_cmp_si(x,0) in check_macros\n"
174               "(c = %d instead of 2)\n", c);
175       exit (1);
176     }
177 
178   mpfr_clear (x);
179 }
180 
181 /* Bug in r7114 */
182 static void
183 test_macros (void)
184 {
185   mpfr_t x[3];
186   mpfr_ptr p;
187 
188   mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
189   mpfr_set_ui (x[0], 0, MPFR_RNDN);
190   p = x[0];
191   if (mpfr_cmp_ui (p++, 0) != 0)
192     {
193       printf ("Error in mpfr_cmp_ui macro: result should be 0.\n");
194       exit (1);
195     }
196   if (p != x[1])
197     {
198       printf ("Error in mpfr_cmp_ui macro: p - x[0] = %d (expecting 1)\n",
199               (int) (p - x[0]));
200       exit (1);
201     }
202   p = x[0];
203   if (mpfr_cmp_si (p++, 0) != 0)
204     {
205       printf ("Error in mpfr_cmp_si macro: result should be 0.\n");
206       exit (1);
207     }
208   if (p != x[1])
209     {
210       printf ("Error in mpfr_cmp_si macro: p - x[0] = %d (expecting 1)\n",
211               (int) (p - x[0]));
212       exit (1);
213     }
214   mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
215 }
216 
217 int
218 main (void)
219 {
220   mpfr_t x;
221   unsigned long i;
222   long s;
223 
224   tests_start_mpfr ();
225 
226   mpfr_init(x);
227 
228   /* tests for cmp_ui */
229   mpfr_set_ui (x, 3, MPFR_RNDZ);
230   if ((mpfr_cmp_ui) (x, i = 3) != 0)
231     {
232       printf ("Error in mpfr_cmp_ui(3.0, 3)\n");
233       exit (1);
234     }
235   if (mpfr_cmp_ui (x, i = 2) <= 0)
236     {
237       printf ("Error in mpfr_cmp_ui(3.0,2)\n");
238       exit (1);
239     }
240   if (mpfr_cmp_ui (x, i = 4) >= 0)
241     {
242       printf ("Error in mpfr_cmp_ui(3.0,4)\n");
243       exit (1);
244     }
245   mpfr_set_ui (x, 0, MPFR_RNDZ);
246   mpfr_neg (x, x, MPFR_RNDZ);
247   if (mpfr_cmp_ui (x, i = 0))
248     {
249       printf ("Error in mpfr_cmp_ui(0.0,0)\n");
250       exit (1);
251     }
252   mpfr_set_ui (x, 1, MPFR_RNDZ);
253   if (mpfr_cmp_ui (x, i = 0) == 0)
254     {
255       printf ("Error in mpfr_cmp_ui(1.0,0)\n");
256       exit (1);
257     }
258 
259   mpfr_set_inf (x, 1);
260   if (mpfr_cmp_ui (x, 1) <= 0)
261     {
262       printf ("Error in mpfr_cmp_ui (Inf, 0)\n");
263       exit (1);
264     }
265   mpfr_set_inf (x, -1);
266   if (mpfr_cmp_ui (x, 1) >= 0)
267     {
268       printf ("Error in mpfr_cmp_ui (-Inf, 0)\n");
269       exit (1);
270     }
271 
272   mpfr_set_si (x, -1, MPFR_RNDN);
273   MPFR_ASSERTN(mpfr_cmp_ui (x, 1) < 0);
274   MPFR_ASSERTN(mpfr_cmp_ui (x, 0) < 0);
275 
276   mpfr_set_ui (x, 1, MPFR_RNDN);
277   MPFR_ASSERTN(mpfr_cmp_ui (x, 0) > 0);
278 
279   /* tests for cmp_si */
280   (mpfr_set_si) (x, -3, MPFR_RNDZ);
281   if ((mpfr_cmp_si) (x, s = -3) != 0)
282     {
283       printf ("Error in mpfr_cmp_si(-3.0,-3)\n");
284       exit (1);
285     }
286   if (mpfr_cmp_si (x, s = -4) <= 0)
287     {
288       printf ("Error in mpfr_cmp_si(-3.0,-4)\n");
289       exit (1);
290     }
291   if (mpfr_cmp_si (x, s = 1) >= 0)
292     {
293       printf ("Error in mpfr_cmp_si(-3.0,1)\n");
294       exit (1);
295     }
296 
297   mpfr_set_inf (x, 1);
298   if (mpfr_cmp_si (x, -1) <= 0)
299     {
300       printf ("Error in mpfr_cmp_si (Inf, 0)\n");
301       exit (1);
302     }
303   mpfr_set_inf (x, -1);
304   if (mpfr_cmp_si (x, -1) >= 0)
305     {
306       printf ("Error in mpfr_cmp_si (-Inf, 0)\n");
307       exit (1);
308     }
309 
310   /* case b=0 */
311   mpfr_set_ui (x, 0, MPFR_RNDZ);
312   MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
313   MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
314   MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);
315 
316   /* case i=0 */
317   mpfr_set_ui (x, 1, MPFR_RNDZ);
318   MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
319   mpfr_set_ui (x, 0, MPFR_RNDZ);
320   MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
321   mpfr_neg (x, x, MPFR_RNDZ);
322   MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
323   mpfr_set_si (x, -1, MPFR_RNDZ);
324   MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);
325 
326   /* case large x */
327   mpfr_set_str_binary (x, "1E100");
328   MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
329   MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);
330   MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);
331   mpfr_set_str_binary (x, "-1E100");
332   MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);
333   MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
334   MPFR_ASSERTN(mpfr_cmp_si (x, -1) < 0);
335 
336   /* corner case */
337   mpfr_set_ui (x, 1, MPFR_RNDZ);
338   mpfr_mul_2ui (x, x, GMP_NUMB_BITS - 1, MPFR_RNDZ);
339   /* now EXP(x)=GMP_NUMB_BITS */
340   MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);
341 
342   mpfr_clear (x);
343 
344   check_nan ();
345   check_macros ();
346   test_macros ();
347 
348   tests_end_mpfr ();
349   return 0;
350 }
351 
352 #endif
353