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