Home | History | Annotate | Line # | Download | only in tests
tset.c revision 1.1.1.3
      1      1.1  mrg /* Test file for mpfr_set.
      2      1.1  mrg 
      3  1.1.1.3  mrg Copyright 2001-2016 Free Software Foundation, Inc.
      4  1.1.1.3  mrg Contributed by the AriC and Caramba projects, INRIA.
      5      1.1  mrg 
      6      1.1  mrg This file is part of the GNU MPFR Library.
      7      1.1  mrg 
      8      1.1  mrg The GNU MPFR Library is free software; you can redistribute it and/or modify
      9      1.1  mrg it under the terms of the GNU Lesser General Public License as published by
     10      1.1  mrg the Free Software Foundation; either version 3 of the License, or (at your
     11      1.1  mrg option) any later version.
     12      1.1  mrg 
     13      1.1  mrg The GNU MPFR Library is distributed in the hope that it will be useful, but
     14      1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15      1.1  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16      1.1  mrg License for more details.
     17      1.1  mrg 
     18      1.1  mrg You should have received a copy of the GNU Lesser General Public License
     19      1.1  mrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20      1.1  mrg http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21      1.1  mrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22      1.1  mrg 
     23      1.1  mrg #include <stdio.h>
     24      1.1  mrg #include <stdlib.h>
     25      1.1  mrg 
     26      1.1  mrg #include "mpfr-test.h"
     27      1.1  mrg 
     28      1.1  mrg int error;
     29      1.1  mrg 
     30      1.1  mrg #define PRINT_ERROR_IF(condition, text)         \
     31      1.1  mrg   do {                                          \
     32      1.1  mrg     if (condition)                              \
     33      1.1  mrg       {                                         \
     34      1.1  mrg         printf ("%s", text);                    \
     35      1.1  mrg         error = 1;                              \
     36      1.1  mrg       }                                         \
     37      1.1  mrg   } while (0)
     38      1.1  mrg 
     39      1.1  mrg 
     40      1.1  mrg /* Maybe better create its own test file ? */
     41      1.1  mrg static void
     42      1.1  mrg check_neg_special (void)
     43      1.1  mrg {
     44      1.1  mrg   mpfr_t x;
     45      1.1  mrg   mpfr_init (x);
     46      1.1  mrg   MPFR_SET_NAN (x);
     47      1.1  mrg   mpfr_clear_nanflag ();
     48      1.1  mrg   mpfr_neg (x, x, MPFR_RNDN);
     49      1.1  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
     50      1.1  mrg                   "ERROR: neg (NaN) doesn't set Nan flag.\n");
     51      1.1  mrg   mpfr_clear (x);
     52      1.1  mrg }
     53      1.1  mrg 
     54      1.1  mrg static void
     55      1.1  mrg check_special (void)
     56      1.1  mrg {
     57      1.1  mrg   mpfr_t x, y;
     58      1.1  mrg   int inexact;
     59      1.1  mrg 
     60      1.1  mrg   mpfr_init (x);
     61      1.1  mrg   mpfr_init (y);
     62      1.1  mrg 
     63      1.1  mrg   mpfr_set_inf (x, 1);
     64      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
     65      1.1  mrg                   "ERROR: mpfr_set_inf failed to set variable to +infinity.\n");
     66      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
     67      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
     68      1.1  mrg                   "ERROR: mpfr_set failed to set variable to +infinity.\n");
     69      1.1  mrg 
     70      1.1  mrg   inexact = mpfr_set_ui (y, 0, MPFR_RNDN);
     71      1.1  mrg   PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
     72      1.1  mrg                   "ERROR: mpfr_set_ui failed to set variable to +0.\n");
     73      1.1  mrg 
     74      1.1  mrg   mpfr_set_inf (x, -1);
     75      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) > 0,
     76      1.1  mrg                   "ERROR: mpfr_set_inf failed to set variable to -infinity.\n");
     77      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
     78      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) > 0 || inexact != 0,
     79      1.1  mrg                   "ERROR: mpfr_set failed to set variable to -infinity.\n");
     80      1.1  mrg 
     81      1.1  mrg   mpfr_set_zero (x, 1);
     82      1.1  mrg   PRINT_ERROR_IF (!mpfr_zero_p (x) || mpfr_sgn (x) < 0,
     83      1.1  mrg                   "ERROR: mpfr_set_zero failed to set variable to +0.\n");
     84      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
     85      1.1  mrg   PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
     86      1.1  mrg                   "ERROR: mpfr_set failed to set variable to +0.\n");
     87      1.1  mrg 
     88      1.1  mrg   mpfr_set_zero (x, -1);
     89      1.1  mrg   PRINT_ERROR_IF (!mpfr_zero_p (x) || mpfr_sgn (x) > 0,
     90      1.1  mrg                   "ERROR: mpfr_set_zero failed to set variable to -0.\n");
     91      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
     92      1.1  mrg   PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) > 0 || inexact != 0,
     93      1.1  mrg                   "ERROR: mpfr_set failed to set variable to -0.\n");
     94      1.1  mrg 
     95      1.1  mrg   mpfr_set_nan (x);
     96      1.1  mrg   PRINT_ERROR_IF (!mpfr_nan_p (x),
     97      1.1  mrg                   "ERROR: mpfr_set_nan failed to set variable to NaN.\n");
     98      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
     99      1.1  mrg   PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
    100      1.1  mrg                   "ERROR: mpfr_set failed to set variable to NaN.\n");
    101      1.1  mrg 
    102      1.1  mrg   mpfr_clear (x);
    103      1.1  mrg   mpfr_clear (y);
    104      1.1  mrg }
    105      1.1  mrg 
    106      1.1  mrg static void
    107      1.1  mrg check_ternary_value (void)
    108      1.1  mrg {
    109      1.1  mrg   int p, q, rnd;
    110      1.1  mrg   int inexact, cmp;
    111      1.1  mrg   mpfr_t x, y;
    112      1.1  mrg 
    113      1.1  mrg   mpfr_init (x);
    114      1.1  mrg   mpfr_init (y);
    115      1.1  mrg   for (p=2; p<500; p++)
    116      1.1  mrg     {
    117      1.1  mrg       mpfr_set_prec (x, p);
    118      1.1  mrg       mpfr_urandomb (x, RANDS);
    119      1.1  mrg       if (randlimb () % 2)
    120      1.1  mrg         mpfr_neg (x, x, MPFR_RNDN);
    121      1.1  mrg       for (q=2; q<2*p; q++)
    122      1.1  mrg         {
    123      1.1  mrg           mpfr_set_prec (y, q);
    124      1.1  mrg           for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
    125      1.1  mrg             {
    126      1.1  mrg               inexact = mpfr_set (y, x, (mpfr_rnd_t) rnd);
    127      1.1  mrg               cmp = mpfr_cmp (y, x);
    128      1.1  mrg               if (((inexact == 0) && (cmp != 0)) ||
    129      1.1  mrg                   ((inexact > 0) && (cmp <= 0)) ||
    130      1.1  mrg                   ((inexact < 0) && (cmp >= 0)))
    131      1.1  mrg                 {
    132      1.1  mrg                   printf ("Wrong ternary value in mpfr_set: expected %d,"
    133      1.1  mrg                           " got %d\n", cmp, inexact);
    134      1.1  mrg                   exit (1);
    135      1.1  mrg                 }
    136      1.1  mrg             }
    137      1.1  mrg         }
    138      1.1  mrg     }
    139      1.1  mrg   mpfr_clear (x);
    140      1.1  mrg   mpfr_clear (y);
    141      1.1  mrg }
    142      1.1  mrg 
    143      1.1  mrg #define TEST_FUNCTION mpfr_set
    144      1.1  mrg #include "tgeneric.c"
    145      1.1  mrg 
    146      1.1  mrg int
    147      1.1  mrg main (void)
    148      1.1  mrg {
    149      1.1  mrg   mpfr_t x, y, z, u;
    150      1.1  mrg   int inexact;
    151      1.1  mrg   mpfr_exp_t emax;
    152      1.1  mrg 
    153      1.1  mrg   tests_start_mpfr ();
    154      1.1  mrg 
    155      1.1  mrg   /* Default : no error */
    156      1.1  mrg   error = 0;
    157      1.1  mrg 
    158      1.1  mrg   /* check prototypes of mpfr_init_set_* */
    159      1.1  mrg   inexact = mpfr_init_set_si (x, -1, MPFR_RNDN);
    160  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    161      1.1  mrg   inexact = mpfr_init_set (y, x, MPFR_RNDN);
    162  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    163      1.1  mrg   inexact = mpfr_init_set_ui (z, 1, MPFR_RNDN);
    164  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    165      1.1  mrg   inexact = mpfr_init_set_d (u, 1.0, MPFR_RNDN);
    166  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    167      1.1  mrg 
    168      1.1  mrg   emax = mpfr_get_emax ();
    169      1.1  mrg   set_emax (0);
    170      1.1  mrg   mpfr_set_prec (x, 3);
    171      1.1  mrg   mpfr_set_str_binary (x, "0.111");
    172      1.1  mrg   mpfr_set_prec (y, 2);
    173      1.1  mrg   mpfr_set (y, x, MPFR_RNDU);
    174      1.1  mrg   if (!(MPFR_IS_INF (y) && MPFR_SIGN (y) > 0))
    175      1.1  mrg     {
    176      1.1  mrg       printf ("Error for y=x=0.111 with px=3, py=2 and emax=0\nx=");
    177      1.1  mrg       mpfr_dump (x);
    178      1.1  mrg       printf ("y=");
    179      1.1  mrg       mpfr_dump (y);
    180      1.1  mrg       exit (1);
    181      1.1  mrg     }
    182      1.1  mrg 
    183      1.1  mrg   set_emax (emax);
    184      1.1  mrg 
    185      1.1  mrg   mpfr_set_prec (y, 11);
    186      1.1  mrg   mpfr_set_str_binary (y, "0.11111111100E-8");
    187      1.1  mrg   mpfr_set_prec (x, 2);
    188      1.1  mrg   mpfr_set (x, y, MPFR_RNDN);
    189      1.1  mrg   mpfr_set_str_binary (y, "1.0E-8");
    190      1.1  mrg   if (mpfr_cmp (x, y))
    191      1.1  mrg     {
    192      1.1  mrg       printf ("Error for y=0.11111111100E-8, prec=2, rnd=MPFR_RNDN\n");
    193      1.1  mrg       exit (1);
    194      1.1  mrg     }
    195      1.1  mrg 
    196      1.1  mrg   mpfr_clear (x);
    197      1.1  mrg   mpfr_clear (y);
    198      1.1  mrg   mpfr_clear (z);
    199      1.1  mrg   mpfr_clear (u);
    200      1.1  mrg 
    201      1.1  mrg   check_ternary_value ();
    202      1.1  mrg   check_special ();
    203      1.1  mrg   check_neg_special ();
    204      1.1  mrg 
    205      1.1  mrg   test_generic (2, 1000, 10);
    206      1.1  mrg 
    207      1.1  mrg   tests_end_mpfr ();
    208      1.1  mrg   return error;
    209      1.1  mrg }
    210