1/* Test file for mpfr_nextabove, mpfr_nextbelow, mpfr_nexttoward.
2
3Copyright 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#include <stdio.h>
24#include <stdlib.h>
25
26#include "mpfr-test.h"
27
28/* Generic tests for mpfr_nextabove and mpfr_nextbelow */
29static void
30generic_abovebelow (void)
31{
32  int i;
33
34  for (i = 0; i < 20000; i++)
35    {
36      mpfr_t x, y, z, t;
37      mpfr_prec_t prec;
38      int neg, below;
39
40      prec = (randlimb () % 300) + MPFR_PREC_MIN;
41      mpfr_inits2 (prec, x, y, z, (mpfr_ptr) 0);
42      mpfr_init2 (t, 3);
43
44      /* special tests (executed once is enough) */
45      if (i == 0)
46        {
47          mpfr_set_nan (x);
48          mpfr_nextabove (x);
49          MPFR_ASSERTN(mpfr_nan_p (x));
50          mpfr_nextbelow (x);
51          MPFR_ASSERTN(mpfr_nan_p (x));
52          mpfr_nexttoward (x, y);
53          MPFR_ASSERTN(mpfr_nan_p (x));
54          mpfr_set_ui (y, 1, MPFR_RNDN);
55          mpfr_nexttoward (y, x);
56          MPFR_ASSERTN(mpfr_nan_p (y));
57          mpfr_set_ui (x, 1, MPFR_RNDN);
58          mpfr_set_ui (y, 1, MPFR_RNDN);
59          mpfr_nexttoward (x, y);
60          MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0);
61        }
62
63      do
64        mpfr_urandomb (x, RANDS);
65      while (mpfr_cmp_ui (x, 0) == 0);
66      neg = randlimb () & 1;
67      if (neg)
68        mpfr_neg (x, x, MPFR_RNDN);
69      mpfr_set (y, x, MPFR_RNDN);
70      below = randlimb () & 1;
71      if (below)
72        mpfr_nextbelow (y);
73      else
74        mpfr_nextabove (y);
75      mpfr_set_si (t, below ? -5 : 5, MPFR_RNDN);
76      mpfr_mul_2si (t, t, (mpfr_get_exp) (x) - prec - 3, MPFR_RNDN);
77      /* t = (1/2 + 1/8) ulp(x) */
78      mpfr_add (z, x, t, MPFR_RNDN);
79      if (!mpfr_number_p (y) || mpfr_cmp (y, z) != 0)
80        {
81          printf ("Error in mpfr_next%s for\n",
82                  below ? "below" : "above");
83          mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
84          printf (", got\n");
85          mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
86          printf (" instead of\n");
87          mpfr_out_str (stdout, 2, 0, z, MPFR_RNDN);
88          printf ("\n");
89          exit (1);
90        }
91      mpfr_clears (x, y, z, t, (mpfr_ptr) 0);
92    }
93}
94
95static void
96inverse_test (void)
97{
98  static const char *tests[] = { "0", "1", "2", "3.1", "Inf" };
99  int i, neg, below;
100  mpfr_prec_t prec;
101
102  for (i = 0; i < (int) (sizeof(tests) / sizeof(tests[0])); i++)
103    for (neg = 0; neg <= 1; neg++)
104      for (below = 0; below <= 1; below++)
105        for (prec = MPFR_PREC_MIN; prec < 200; prec += 3)
106          {
107            mpfr_t x, y;
108            int sign;
109
110            mpfr_inits2 (prec, x, y, (mpfr_ptr) 0);
111            mpfr_set_str (x, tests[i], 10, MPFR_RNDN);
112            if (neg)
113              mpfr_neg (x, x, MPFR_RNDN);
114            mpfr_set (y, x, MPFR_RNDN);
115            if (below)
116              mpfr_nextbelow (y);
117            else
118              mpfr_nextabove (y);
119            sign = MPFR_SIGN (y);
120            if (!(neg == below && mpfr_inf_p (x)))  /* then x = y */
121              {
122                if (mpfr_cmp (x, y) == 0)
123                  {
124                    printf ("Error in inverse_test for %s, neg = %d,"
125                            " below = %d, prec = %d: x = y", tests[i],
126                            neg, below, (int) prec);
127                    printf ("\nx = ");
128                    mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
129                    printf ("\ny = ");
130                    mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
131                    printf ("\n");
132                    exit (1);
133                 }
134                mpfr_nexttoward (y, x);
135                if (mpfr_cmp_ui (y, 0) == 0 && MPFR_SIGN (y) != sign)
136                  {
137                    printf ("Sign error in inverse_test for %s, neg = %d,"
138                            " below = %d, prec = %d\n", tests[i], neg,
139                            below, (int) prec);
140                    mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
141                    printf ("\n");
142                    exit (1);
143                  }
144              }
145            if (mpfr_cmp (x, y) != 0)
146              {
147                printf ("Error in inverse_test for %s, neg = %d, below = %d,"
148                        " prec = %d", tests[i], neg, below, (int) prec);
149                printf ("\nx = ");
150                mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
151                printf ("\ny = ");
152                mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
153                printf ("\n");
154                exit (1);
155              }
156            mpfr_clears (x, y, (mpfr_ptr) 0);
157          }
158}
159
160int
161main (void)
162{
163  tests_start_mpfr ();
164  generic_abovebelow ();
165  inverse_test ();
166  tests_end_mpfr ();
167  return 0;
168}
169