summaryrefslogtreecommitdiff
path: root/tests/tcmp_ui.c
blob: 83c6803ff8a8dfc0b047331c5fa3af1a0f84f17f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* Test file for mpfr_cmp_ui and mpfr_cmp_si.

Copyright 1999, 2001-2021 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#ifdef TCMP_UI_CHECK_NAN

mpfr_clear_erangeflag ();
c = mpfr_cmp_si (x, TCMP_UI_CHECK_NAN);
if (c != 0 || !mpfr_erangeflag_p ())
  {
    printf ("NaN error on %d (1)\n", TCMP_UI_CHECK_NAN);
    exit (1);
  }
mpfr_clear_erangeflag ();
c = (mpfr_cmp_si) (x, TCMP_UI_CHECK_NAN);
if (c != 0 || !mpfr_erangeflag_p ())
  {
    printf ("NaN error on %d (2)\n", TCMP_UI_CHECK_NAN);
    exit (1);
  }
if (TCMP_UI_CHECK_NAN >= 0)
  {
    mpfr_clear_erangeflag ();
    c = mpfr_cmp_ui (x, TCMP_UI_CHECK_NAN);
    if (c != 0 || !mpfr_erangeflag_p ())
      {
        printf ("NaN error on %d (3)\n", TCMP_UI_CHECK_NAN);
        exit (1);
      }
    mpfr_clear_erangeflag ();
    c = (mpfr_cmp_ui) (x, TCMP_UI_CHECK_NAN);
    if (c != 0 || !mpfr_erangeflag_p ())
      {
        printf ("NaN error on %d (4)\n", TCMP_UI_CHECK_NAN);
        exit (1);
      }
  }

#else

#include "mpfr-test.h"

static void
check_nan (void)
{
  mpfr_t x;
  int c, i;

  mpfr_init (x);
  mpfr_set_nan (x);
  /* We need constants to completely test the macros. */
#undef TCMP_UI_CHECK_NAN
#define TCMP_UI_CHECK_NAN -17
#include "tcmp_ui.c"
#undef TCMP_UI_CHECK_NAN
#define TCMP_UI_CHECK_NAN 0
#include "tcmp_ui.c"
#undef TCMP_UI_CHECK_NAN
#define TCMP_UI_CHECK_NAN 17
#include "tcmp_ui.c"
  for (i = -17; i <= 17; i += 17)
    {
#undef TCMP_UI_CHECK_NAN
#define TCMP_UI_CHECK_NAN i
#include "tcmp_ui.c"
    }
  mpfr_clear (x);
}

/* Since mpfr_cmp_ui and mpfr_cmp_si are also implemented by a macro
   with __builtin_constant_p for GCC, check that side effects are
   handled correctly. */
static void
check_macros (void)
{
  mpfr_t x;
  int c;

  mpfr_init2 (x, 32);

  c = 0;
  mpfr_set_ui (x, 17, MPFR_RNDN);
  if (mpfr_cmp_ui (x, 17) != 0)
    {
      printf ("Error 1 on mpfr_cmp_ui(x,17) in check_macros\n");
      exit (1);
    }
  if (mpfr_cmp_ui (x, (c++, 17)) != 0)
    {
      printf ("Error 2 on mpfr_cmp_ui(x,17) in check_macros\n");
      exit (1);
    }
  if (c != 1)
    {
      /* Failure in r13626 on x86_64 with the clang-9 1:9-1 Debian package,
         with any optimization level: c = 2 instead of 1
         Bug report: https://bugs.llvm.org/show_bug.cgi?id=43557 */
      /* [2020-06-17]
         If one adds tcc support for macros using __builtin_constant_p
         in mpfr.h by testing __TINYC__, one also gets a failure.
         Bug report: https://savannah.nongnu.org/bugs/?58606
         "__builtin_constant_p is buggy on argument with side effect and
         constant value" */
      printf ("Error 3 on mpfr_cmp_ui(x,17) in check_macros\n"
              "(c = %d instead of 1)\n", c);
      exit (1);
    }
  if (mpfr_cmp_si (x, 17) != 0)
    {
      printf ("Error 1 on mpfr_cmp_si(x,17) in check_macros\n");
      exit (1);
    }
  if (mpfr_cmp_si (x, (c++, 17)) != 0)
    {
      printf ("Error 2 on mpfr_cmp_si(x,17) in check_macros\n");
      exit (1);
    }
  if (c != 2)
    {
      printf ("Error 3 on mpfr_cmp_si(x,17) in check_macros\n"
              "(c = %d instead of 2)\n", c);
      exit (1);
    }

  c = 0;
  mpfr_set_ui (x, 0, MPFR_RNDN);
  if (mpfr_cmp_ui (x, 0) != 0)
    {
      printf ("Error 1 on mpfr_cmp_ui(x,0) in check_macros\n");
      exit (1);
    }
  if (mpfr_cmp_ui (x, (c++, 0)) != 0)
    {
      printf ("Error 2 on mpfr_cmp_ui(x,0) in check_macros\n");
      exit (1);
    }
  if (c != 1)
    {
      printf ("Error 3 on mpfr_cmp_ui(x,0) in check_macros\n"
              "(c = %d instead of 1)\n", c);
      exit (1);
    }
  if (mpfr_cmp_si (x, 0) != 0)
    {
      printf ("Error 1 on mpfr_cmp_si(x,0) in check_macros\n");
      exit (1);
    }
  if (mpfr_cmp_si (x, (c++, 0)) != 0)
    {
      printf ("Error 2 on mpfr_cmp_si(x,0) in check_macros\n");
      exit (1);
    }
  if (c != 2)
    {
      printf ("Error 3 on mpfr_cmp_si(x,0) in check_macros\n"
              "(c = %d instead of 2)\n", c);
      exit (1);
    }

  mpfr_clear (x);
}

/* Bug in r7114 */
static void
test_macros (void)
{
  mpfr_t x[3];
  mpfr_ptr p;

  mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
  mpfr_set_ui (x[0], 0, MPFR_RNDN);
  p = x[0];
  if (mpfr_cmp_ui (p++, 0) != 0)
    {
      printf ("Error in mpfr_cmp_ui macro: result should be 0.\n");
      exit (1);
    }
  if (p != x[1])
    {
      printf ("Error in mpfr_cmp_ui macro: p - x[0] = %d (expecting 1)\n",
              (int) (p - x[0]));
      exit (1);
    }
  p = x[0];
  if (mpfr_cmp_si (p++, 0) != 0)
    {
      printf ("Error in mpfr_cmp_si macro: result should be 0.\n");
      exit (1);
    }
  if (p != x[1])
    {
      printf ("Error in mpfr_cmp_si macro: p - x[0] = %d (expecting 1)\n",
              (int) (p - x[0]));
      exit (1);
    }
  mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
}

int
main (void)
{
  mpfr_t x;
  unsigned long i;
  long s;

  tests_start_mpfr ();

  mpfr_init(x);

  /* tests for cmp_ui */
  mpfr_set_ui (x, 3, MPFR_RNDZ);
  if ((mpfr_cmp_ui) (x, i = 3) != 0)
    {
      printf ("Error in mpfr_cmp_ui(3.0, 3)\n");
      exit (1);
    }
  if (mpfr_cmp_ui (x, i = 2) <= 0)
    {
      printf ("Error in mpfr_cmp_ui(3.0,2)\n");
      exit (1);
    }
  if (mpfr_cmp_ui (x, i = 4) >= 0)
    {
      printf ("Error in mpfr_cmp_ui(3.0,4)\n");
      exit (1);
    }
  mpfr_set_ui (x, 0, MPFR_RNDZ);
  mpfr_neg (x, x, MPFR_RNDZ);
  if (mpfr_cmp_ui (x, i = 0))
    {
      printf ("Error in mpfr_cmp_ui(0.0,0)\n");
      exit (1);
    }
  mpfr_set_ui (x, 1, MPFR_RNDZ);
  if (mpfr_cmp_ui (x, i = 0) == 0)
    {
      printf ("Error in mpfr_cmp_ui(1.0,0)\n");
      exit (1);
    }

  mpfr_set_inf (x, 1);
  if (mpfr_cmp_ui (x, 1) <= 0)
    {
      printf ("Error in mpfr_cmp_ui (Inf, 0)\n");
      exit (1);
    }
  mpfr_set_inf (x, -1);
  if (mpfr_cmp_ui (x, 1) >= 0)
    {
      printf ("Error in mpfr_cmp_ui (-Inf, 0)\n");
      exit (1);
    }

  mpfr_set_si (x, -1, MPFR_RNDN);
  MPFR_ASSERTN(mpfr_cmp_ui (x, 1) < 0);
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) < 0);

  mpfr_set_ui (x, 1, MPFR_RNDN);
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) > 0);

  /* tests for cmp_si */
  (mpfr_set_si) (x, -3, MPFR_RNDZ);
  if ((mpfr_cmp_si) (x, s = -3) != 0)
    {
      printf ("Error in mpfr_cmp_si(-3.0,-3)\n");
      exit (1);
    }
  if (mpfr_cmp_si (x, s = -4) <= 0)
    {
      printf ("Error in mpfr_cmp_si(-3.0,-4)\n");
      exit (1);
    }
  if (mpfr_cmp_si (x, s = 1) >= 0)
    {
      printf ("Error in mpfr_cmp_si(-3.0,1)\n");
      exit (1);
    }

  mpfr_set_inf (x, 1);
  if (mpfr_cmp_si (x, -1) <= 0)
    {
      printf ("Error in mpfr_cmp_si (Inf, 0)\n");
      exit (1);
    }
  mpfr_set_inf (x, -1);
  if (mpfr_cmp_si (x, -1) >= 0)
    {
      printf ("Error in mpfr_cmp_si (-Inf, 0)\n");
      exit (1);
    }

  /* case b=0 */
  mpfr_set_ui (x, 0, MPFR_RNDZ);
  MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
  MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
  MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);

  /* case i=0 */
  mpfr_set_ui (x, 1, MPFR_RNDZ);
  MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
  mpfr_set_ui (x, 0, MPFR_RNDZ);
  MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
  mpfr_neg (x, x, MPFR_RNDZ);
  MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
  mpfr_set_si (x, -1, MPFR_RNDZ);
  MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);

  /* case large x */
  mpfr_set_str_binary (x, "1E100");
  MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
  MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);
  MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);
  mpfr_set_str_binary (x, "-1E100");
  MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);
  MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
  MPFR_ASSERTN(mpfr_cmp_si (x, -1) < 0);

  /* corner case */
  mpfr_set_ui (x, 1, MPFR_RNDZ);
  mpfr_mul_2ui (x, x, GMP_NUMB_BITS - 1, MPFR_RNDZ);
  /* now EXP(x)=GMP_NUMB_BITS */
  MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);

  mpfr_clear (x);

  check_nan ();
  check_macros ();
  test_macros ();

  tests_end_mpfr ();
  return 0;
}

#endif