~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/tests/tset_si.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test file for mpfr_set_si and mpfr_set_ui.
 
2
 
 
3
Copyright 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
7
The MPFR 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 MPFR 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 MPFR Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
 
20
MA 02110-1301, USA. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <time.h>
 
25
#include <limits.h>
 
26
 
 
27
#include "mpfr-test.h"
 
28
 
 
29
#define ERROR(str) {printf("Error for "str"\n"); exit(1);}
 
30
 
 
31
static void
 
32
test_2exp (void)
 
33
{
 
34
  mpfr_t x;
 
35
  int res;
 
36
 
 
37
  mpfr_init2 (x, 32);
 
38
 
 
39
  mpfr_set_ui_2exp (x, 1, 0, GMP_RNDN);
 
40
  if (mpfr_cmp_ui(x, 1))
 
41
    ERROR("(1U,0)");
 
42
 
 
43
  mpfr_set_ui_2exp (x, 1024, -10, GMP_RNDN);
 
44
  if (mpfr_cmp_ui(x, 1))
 
45
    ERROR("(1024U,-10)");
 
46
 
 
47
  mpfr_set_ui_2exp (x, 1024, 10, GMP_RNDN);
 
48
  if (mpfr_cmp_ui(x, 1024*1024))
 
49
    ERROR("(1024U,+10)");
 
50
 
 
51
  mpfr_set_si_2exp (x, -1024*1024, -10, GMP_RNDN);
 
52
  if (mpfr_cmp_si(x, -1024))
 
53
    ERROR("(1M,-10)");
 
54
 
 
55
  mpfr_set_ui_2exp (x, 0x92345678, 16, GMP_RNDN);
 
56
  if (mpfr_cmp_str (x, "92345678@4", 16, GMP_RNDN))
 
57
    ERROR("(x92345678U,+16)");
 
58
 
 
59
  mpfr_set_si_2exp (x, -0x1ABCDEF0, -256, GMP_RNDN);
 
60
  if (mpfr_cmp_str (x, "-1ABCDEF0@-64", 16, GMP_RNDN))
 
61
    ERROR("(-x1ABCDEF0,-256)");
 
62
 
 
63
  mpfr_set_prec (x, 2);
 
64
  res = mpfr_set_si_2exp (x, 7, 10, GMP_RNDU);
 
65
  if (mpfr_cmp_ui (x, 1<<13) || res <= 0)
 
66
    ERROR ("Prec 2 + si_2exp");
 
67
 
 
68
  res = mpfr_set_ui_2exp (x, 7, 10, GMP_RNDU);
 
69
  if (mpfr_cmp_ui (x, 1<<13) || res <= 0)
 
70
    ERROR ("Prec 2 + ui_2exp");
 
71
 
 
72
  mpfr_clear (x);
 
73
}
 
74
 
 
75
static void
 
76
test_macros (void)
 
77
{
 
78
  mpfr_t x[3];
 
79
  mpfr_ptr p;
 
80
  mpfr_rnd_t r;
 
81
 
 
82
  mpfr_inits (x[0], x[1], x[2], NULL);
 
83
  p = x[0];
 
84
  r = 0;
 
85
  mpfr_set_ui (p++, 0, r++);
 
86
  if (p != x[1] || r != 1)
 
87
    {
 
88
      printf ("Error in mpfr_set_ui macro: p - x[0] = %d (expecting 1), "
 
89
              "r = %d (expecting 1)\n", (int) (p - x[0]), r);
 
90
      exit (1);
 
91
    }
 
92
  p = x[0];
 
93
  r = 0;
 
94
  mpfr_set_si (p++, 0, r++);
 
95
  if (p != x[1] || r != 1)
 
96
    {
 
97
      printf ("Error in mpfr_set_si macro: p - x[0] = %d (expecting 1), "
 
98
              "r = %d (expecting 1)\n", (int) (p - x[0]), r);
 
99
      exit (1);
 
100
    }
 
101
  mpfr_clears (x[0], x[1], x[2], NULL);
 
102
}
 
103
 
 
104
/* FIXME: Comparing against mpfr_get_si/ui is not ideal, it'd be better to
 
105
   have all tests examine the bits in mpfr_t for what should come out.  */
 
106
 
 
107
int
 
108
main (int argc, char *argv[])
 
109
{
 
110
  mpfr_t x;
 
111
  long k, z, d, N;
 
112
  unsigned long zl, dl;
 
113
  int inex;
 
114
  int r;
 
115
  mp_exp_t emax;
 
116
 
 
117
  tests_start_mpfr ();
 
118
 
 
119
  mpfr_init2 (x, 100);
 
120
 
 
121
  N = (argc==1) ? 100000 : atol (argv[1]);
 
122
 
 
123
  for (k = 1; k <= N; k++)
 
124
    {
 
125
      z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2;
 
126
      inex = mpfr_set_si (x, z, GMP_RNDZ);
 
127
      d = mpfr_get_si (x, GMP_RNDZ);
 
128
      if (d != z)
 
129
        {
 
130
          printf ("Error in mpfr_set_si: expected %ld got %ld\n", z, d);
 
131
          exit (1);
 
132
        }
 
133
      if (inex)
 
134
        {
 
135
          printf ("Error in mpfr_set_si: inex value incorrect for %ld: %d\n",
 
136
                  z, inex);
 
137
          exit (1);
 
138
        }
 
139
    }
 
140
 
 
141
  for (k = 1; k <= N; k++)
 
142
    {
 
143
      zl = randlimb ();
 
144
      inex = mpfr_set_ui (x, zl, GMP_RNDZ);
 
145
      dl = mpfr_get_ui (x, GMP_RNDZ);
 
146
      if (dl != zl)
 
147
        {
 
148
          printf ("Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl);
 
149
          exit (1);
 
150
        }
 
151
      if (inex)
 
152
        {
 
153
          printf ("Error in mpfr_set_ui: inex value incorrect for %lu: %d\n",
 
154
                  zl, inex);
 
155
          exit (1);
 
156
        }
 
157
    }
 
158
 
 
159
  mpfr_set_prec (x, 2);
 
160
  if (mpfr_set_si (x, 5, GMP_RNDZ) >= 0)
 
161
    {
 
162
      printf ("Wrong inexact flag for x=5, rnd=GMP_RNDZ\n");
 
163
      exit (1);
 
164
    }
 
165
 
 
166
  mpfr_set_prec (x, 2);
 
167
  if (mpfr_set_si (x, -5, GMP_RNDZ) <= 0)
 
168
    {
 
169
      printf ("Wrong inexact flag for x=-5, rnd=GMP_RNDZ\n");
 
170
      exit (1);
 
171
    }
 
172
 
 
173
  mpfr_set_prec (x, 3);
 
174
  inex = mpfr_set_si (x, 77617, GMP_RNDD); /* should be 65536 */
 
175
  if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
 
176
      || inex >= 0)
 
177
    {
 
178
      printf ("Error in mpfr_set_si(x:3, 77617, GMP_RNDD)\n");
 
179
      mpfr_print_binary (x);
 
180
      puts ("");
 
181
      exit (1);
 
182
    }
 
183
  inex = mpfr_set_ui (x, 77617, GMP_RNDD); /* should be 65536 */
 
184
  if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
 
185
      || inex >= 0)
 
186
    {
 
187
      printf ("Error in mpfr_set_ui(x:3, 77617, GMP_RNDD)\n");
 
188
      mpfr_print_binary (x);
 
189
      puts ("");
 
190
      exit (1);
 
191
    }
 
192
 
 
193
  mpfr_set_prec (x, 2);
 
194
  inex = mpfr_set_si (x, 33096, GMP_RNDU);
 
195
  if (mpfr_get_si (x, GMP_RNDZ) != 49152 || inex <= 0)
 
196
    {
 
197
      printf ("Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n",
 
198
              mpfr_get_si (x, GMP_RNDZ), inex);
 
199
      exit (1);
 
200
    }
 
201
  inex = mpfr_set_ui (x, 33096, GMP_RNDU);
 
202
  if (mpfr_get_si (x, GMP_RNDZ) != 49152)
 
203
    {
 
204
      printf ("Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n",
 
205
              mpfr_get_si (x, GMP_RNDZ), inex);
 
206
      exit (1);
 
207
    }
 
208
 
 
209
  for (r = 0 ; r < GMP_RND_MAX ; r++)
 
210
    {
 
211
      mpfr_set_si (x, -1, (mp_rnd_t) r);
 
212
      mpfr_set_ui (x, 0, (mp_rnd_t) r);
 
213
      if (MPFR_IS_NEG (x) || mpfr_get_ui (x, (mp_rnd_t) r) != 0)
 
214
        {
 
215
          printf ("mpfr_set_ui (x, 0) gives -0 for %s\n",
 
216
                  mpfr_print_rnd_mode ((mp_rnd_t) r));
 
217
          exit (1);
 
218
        }
 
219
 
 
220
      mpfr_set_si (x, -1, (mp_rnd_t) r);
 
221
      mpfr_set_si (x, 0, (mp_rnd_t) r);
 
222
      if (MPFR_IS_NEG (x) || mpfr_get_si (x, (mp_rnd_t) r) != 0)
 
223
        {
 
224
          printf ("mpfr_set_si (x, 0) gives -0 for %s\n",
 
225
                  mpfr_print_rnd_mode ((mp_rnd_t) r));
 
226
          exit (1);
 
227
        }
 
228
    }
 
229
 
 
230
  /* check potential bug in case mp_limb_t is unsigned */
 
231
  emax = mpfr_get_emax ();
 
232
  set_emax (0);
 
233
  mpfr_set_si (x, -1, GMP_RNDN);
 
234
  if (mpfr_sgn (x) >= 0)
 
235
    {
 
236
      printf ("mpfr_set_si (x, -1) fails\n");
 
237
      exit (1);
 
238
    }
 
239
  set_emax (emax);
 
240
 
 
241
  emax = mpfr_get_emax ();
 
242
  set_emax (5);
 
243
  mpfr_set_prec (x, 2);
 
244
  mpfr_set_si (x, -31, GMP_RNDN);
 
245
  if (mpfr_sgn (x) >= 0)
 
246
    {
 
247
      printf ("mpfr_set_si (x, -31) fails\n");
 
248
      exit (1);
 
249
    }
 
250
  set_emax (emax);
 
251
 
 
252
  /* test for get_ui */
 
253
  mpfr_set_ui (x, 0, GMP_RNDN);
 
254
  MPFR_ASSERTN(mpfr_get_ui (x, GMP_RNDN) == 0);
 
255
  mpfr_set_ui (x, ULONG_MAX, GMP_RNDU);
 
256
  mpfr_nextabove (x);
 
257
  mpfr_get_ui (x, GMP_RNDU);
 
258
 
 
259
  /* another test for get_ui */
 
260
  mpfr_set_prec (x, 10);
 
261
  mpfr_set_str_binary (x, "10.101");
 
262
  dl = mpfr_get_ui (x, GMP_RNDN);
 
263
  MPFR_ASSERTN (dl == 3);
 
264
 
 
265
  mpfr_set_str_binary (x, "-1.0");
 
266
  mpfr_get_ui (x, GMP_RNDN);
 
267
 
 
268
  mpfr_set_str_binary (x, "0.1");
 
269
  dl = mpfr_get_ui (x, GMP_RNDN);
 
270
  MPFR_ASSERTN (dl == 0);
 
271
  dl = mpfr_get_ui (x, GMP_RNDZ);
 
272
  MPFR_ASSERTN (dl == 0);
 
273
  dl = mpfr_get_ui (x, GMP_RNDD);
 
274
  MPFR_ASSERTN (dl == 0);
 
275
  dl = mpfr_get_ui (x, GMP_RNDU);
 
276
  MPFR_ASSERTN (dl == 1);
 
277
 
 
278
  /* coverage tests */
 
279
  mpfr_set_prec (x, 2);
 
280
  mpfr_set_si (x, -7, GMP_RNDD);
 
281
  MPFR_ASSERTN(mpfr_cmp_si (x, -8) == 0);
 
282
  mpfr_set_prec (x, 2);
 
283
  mpfr_set_ui (x, 7, GMP_RNDU);
 
284
  MPFR_ASSERTN(mpfr_cmp_ui (x, 8) == 0);
 
285
  emax = mpfr_get_emax ();
 
286
  set_emax (3);
 
287
  mpfr_set_ui (x, 7, GMP_RNDU);
 
288
  MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
 
289
  set_emax (1);
 
290
  MPFR_ASSERTN( mpfr_set_ui (x, 7, GMP_RNDU) );
 
291
  MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
 
292
  set_emax (emax);
 
293
  mpfr_set_ui_2exp (x, 17, -50, GMP_RNDN);
 
294
  MPFR_ASSERTN (mpfr_get_ui (x, GMP_RNDD) == 0);
 
295
  MPFR_ASSERTN (mpfr_get_si (x, GMP_RNDD) == 0);
 
296
 
 
297
  /* Test for ERANGE flag + correct behaviour if overflow */
 
298
  mpfr_set_prec (x, 256);
 
299
  mpfr_set_ui (x, ULONG_MAX, GMP_RNDN);
 
300
  mpfr_clear_erangeflag ();
 
301
  dl = mpfr_get_ui (x, GMP_RNDN);
 
302
  if (dl != ULONG_MAX || mpfr_erangeflag_p ())
 
303
    {
 
304
      printf ("ERROR for get_ui + ERANGE + ULONG_MAX (1)\n");
 
305
      exit (1);
 
306
    }
 
307
  mpfr_add_ui (x, x, 1, GMP_RNDN);
 
308
  dl = mpfr_get_ui (x, GMP_RNDN);
 
309
  if (dl != ULONG_MAX || !mpfr_erangeflag_p ())
 
310
    {
 
311
      printf ("ERROR for get_ui + ERANGE + ULONG_MAX (2)\n");
 
312
      exit (1);
 
313
    }
 
314
  mpfr_set_si (x, -1, GMP_RNDN);
 
315
  mpfr_clear_erangeflag ();
 
316
  dl = mpfr_get_ui (x, GMP_RNDN);
 
317
  if (dl != 0 || !mpfr_erangeflag_p ())
 
318
    {
 
319
      printf ("ERROR for get_ui + ERANGE + -1 \n");
 
320
      exit (1);
 
321
    }
 
322
  mpfr_set_si (x, LONG_MAX, GMP_RNDN);
 
323
  mpfr_clear_erangeflag ();
 
324
  d = mpfr_get_si (x, GMP_RNDN);
 
325
  if (d != LONG_MAX || mpfr_erangeflag_p ())
 
326
    {
 
327
      printf ("ERROR for get_si + ERANGE + LONG_MAX (1): %ld\n", d);
 
328
      exit (1);
 
329
    }
 
330
  mpfr_add_ui (x, x, 1, GMP_RNDN);
 
331
  d = mpfr_get_si (x, GMP_RNDN);
 
332
  if (d != LONG_MAX || !mpfr_erangeflag_p ())
 
333
    {
 
334
      printf ("ERROR for get_si + ERANGE + LONG_MAX (2)\n");
 
335
      exit (1);
 
336
    }
 
337
  mpfr_set_si (x, LONG_MIN, GMP_RNDN);
 
338
  mpfr_clear_erangeflag ();
 
339
  d = mpfr_get_si (x, GMP_RNDN);
 
340
  if (d != LONG_MIN || mpfr_erangeflag_p ())
 
341
    {
 
342
      printf ("ERROR for get_si + ERANGE + LONG_MIN (1)\n");
 
343
      exit (1);
 
344
    }
 
345
  mpfr_sub_ui (x, x, 1, GMP_RNDN);
 
346
  d = mpfr_get_si (x, GMP_RNDN);
 
347
  if (d != LONG_MIN || !mpfr_erangeflag_p ())
 
348
    {
 
349
      printf ("ERROR for get_si + ERANGE + LONG_MIN (2)\n");
 
350
      exit (1);
 
351
    }
 
352
 
 
353
  mpfr_clear (x);
 
354
 
 
355
  test_2exp ();
 
356
  test_macros ();
 
357
  tests_end_mpfr ();
 
358
  return 0;
 
359
}