xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tset_z.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /* Test file for mpfr_set_z.
2 
3 Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 http://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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 
27 #include "mpfr-test.h"
28 
29 static void check0(void)
30 {
31   mpz_t y;
32   mpfr_t x;
33   int inexact, r;
34 
35   /* Check for +0 */
36   mpfr_init (x);
37   mpz_init (y);
38   mpz_set_si (y, 0);
39   for(r = 0; r < MPFR_RND_MAX; r++)
40     {
41       inexact = mpfr_set_z (x, y, (mpfr_rnd_t) r);
42       if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact)
43         {
44           printf("mpfr_set_z(x,0) failed for %s\n",
45                  mpfr_print_rnd_mode ((mpfr_rnd_t) r));
46           exit(1);
47         }
48     }
49   mpfr_clear(x);
50   mpz_clear(y);
51 }
52 
53 /* FIXME: It'd be better to examine the actual data in an mpfr_t to see that
54    it's as expected.  Comparing mpfr_set_z with mpfr_cmp or against
55    mpfr_get_si is a rather indirect test of a low level routine.  */
56 
57 static void
58 check (long i, mpfr_rnd_t rnd)
59 {
60   mpfr_t f;
61   mpz_t z;
62 
63   mpfr_init2 (f, 8 * sizeof(long));
64   mpz_init (z);
65   mpz_set_ui (z, i);
66   mpfr_set_z (f, z, rnd);
67   if (mpfr_get_si (f, MPFR_RNDZ) != i)
68     {
69       printf ("Error in mpfr_set_z for i=%ld rnd_mode=%d\n", i, rnd);
70       exit (1);
71     }
72   mpfr_clear (f);
73   mpz_clear (z);
74 }
75 
76 static void
77 check_large (void)
78 {
79   mpz_t z;
80   mpfr_t x, y;
81   mpfr_exp_t emax, emin;
82 
83   mpz_init (z);
84   mpfr_init2 (x, 160);
85   mpfr_init2 (y, 160);
86 
87   mpz_set_str (z, "77031627725494291259359895954016675357279104942148788042", 10);
88   mpfr_set_z (x, z, MPFR_RNDN);
89   mpfr_set_str_binary (y, "0.1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111000001001E186");
90   if (mpfr_cmp (x, y))
91     {
92       printf ("Error in mpfr_set_z on large input\n");
93       exit (1);
94     }
95 
96   /* check overflow */
97   emax = mpfr_get_emax ();
98   set_emax (2);
99   mpz_set_str (z, "7", 10);
100   mpfr_set_z (x, z, MPFR_RNDU);
101   MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
102   set_emax (3);
103   mpfr_set_prec (x, 2);
104   mpz_set_str (z, "7", 10);
105   mpfr_set_z (x, z, MPFR_RNDU);
106   MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
107   set_emax (emax);
108 
109   /* check underflow */
110   emin = mpfr_get_emin ();
111   set_emin (3);
112   mpz_set_str (z, "1", 10);
113   mpfr_set_z (x, z, MPFR_RNDZ);
114   MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
115   set_emin (2);
116   mpfr_set_z (x, z, MPFR_RNDN);
117   MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
118   set_emin (emin);
119 
120   mpz_clear (z);
121 
122   mpfr_clear (x);
123   mpfr_clear (y);
124 }
125 
126 int
127 main (int argc, char *argv[])
128 {
129   long j;
130 
131   tests_start_mpfr ();
132 
133   check_large ();
134   check (0, MPFR_RNDN);
135   for (j = 0; j < 200000; j++)
136     check (randlimb () & LONG_MAX, RND_RAND ());
137   check0 ();
138 
139   tests_end_mpfr ();
140 
141   return 0;
142 }
143