Home | History | Annotate | Line # | Download | only in tests
tset_sj.c revision 1.1.1.4
      1 /* Test file for
      2    mpfr_set_sj, mpfr_set_uj, mpfr_set_sj_2exp and mpfr_set_uj_2exp.
      3 
      4 Copyright 2004, 2006-2018 Free Software Foundation, Inc.
      5 Contributed by the AriC and Caramba projects, INRIA.
      6 
      7 This file is part of the GNU MPFR Library.
      8 
      9 The GNU MPFR Library is free software; you can redistribute it and/or modify
     10 it under the terms of the GNU Lesser General Public License as published by
     11 the Free Software Foundation; either version 3 of the License, or (at your
     12 option) any later version.
     13 
     14 The GNU MPFR Library is distributed in the hope that it will be useful, but
     15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     17 License for more details.
     18 
     19 You should have received a copy of the GNU Lesser General Public License
     20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     23 
     24 #ifdef HAVE_CONFIG_H
     25 # include "config.h"
     26 #endif
     27 
     28 
     29 #include "mpfr-intmax.h"
     30 #include "mpfr-test.h"
     31 
     32 #ifndef _MPFR_H_HAVE_INTMAX_T
     33 
     34 int
     35 main (void)
     36 {
     37   return 77;
     38 }
     39 
     40 #else
     41 
     42 #define PRINT_ERROR(str) \
     43   do { printf ("Error for %s\n", str); exit (1); } while (0)
     44 
     45 static int
     46 inexact_sign (int x)
     47 {
     48   return (x < 0) ? -1 : (x > 0);
     49 }
     50 
     51 static void
     52 check_set_uj (mpfr_prec_t pmin, mpfr_prec_t pmax, int N)
     53 {
     54   mpfr_t x, y;
     55   mpfr_prec_t p;
     56   int inex1, inex2, n;
     57   mp_limb_t limb;
     58 
     59   mpfr_inits2 (pmax, x, y, (mpfr_ptr) 0);
     60 
     61   for (p = pmin ; p < pmax ; p++)
     62     {
     63       mpfr_set_prec (x, p);
     64       mpfr_set_prec (y, p);
     65       for (n = 0 ; n < N ; n++)
     66         {
     67           /* mp_limb_t may be unsigned long long */
     68           limb = (unsigned long) randlimb ();
     69           inex1 = mpfr_set_uj (x, limb, MPFR_RNDN);
     70           inex2 = mpfr_set_ui (y, limb, MPFR_RNDN);
     71           if (mpfr_cmp (x, y))
     72             {
     73               printf ("ERROR for mpfr_set_uj and j=%lu and p=%lu\n",
     74                       (unsigned long) limb, (unsigned long) p);
     75               printf ("X="); mpfr_dump (x);
     76               printf ("Y="); mpfr_dump (y);
     77               exit (1);
     78             }
     79           if (inexact_sign (inex1) != inexact_sign (inex2))
     80             {
     81               printf ("ERROR for inexact(set_uj): j=%lu p=%lu\n"
     82                       "Inexact1= %d Inexact2= %d\n",
     83                       (unsigned long) limb, (unsigned long) p, inex1, inex2);
     84               exit (1);
     85             }
     86         }
     87     }
     88   /* Special case */
     89   mpfr_set_prec (x, sizeof(uintmax_t)*CHAR_BIT);
     90   inex1 = mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN);
     91   if (inex1 != 0 || mpfr_sgn(x) <= 0)
     92     PRINT_ERROR ("inexact / UINTMAX_MAX");
     93   inex1 = mpfr_add_ui (x, x, 1, MPFR_RNDN);
     94   if (inex1 != 0 || !mpfr_powerof2_raw (x)
     95       || MPFR_EXP (x) != sizeof(uintmax_t) * CHAR_BIT + 1)
     96     PRINT_ERROR ("power of 2");
     97   mpfr_set_uj (x, 0, MPFR_RNDN);
     98   if (!MPFR_IS_ZERO (x))
     99     PRINT_ERROR ("Setting 0");
    100 
    101   mpfr_clears (x, y, (mpfr_ptr) 0);
    102 }
    103 
    104 static void
    105 check_set_uj_2exp (void)
    106 {
    107   mpfr_t x;
    108   int inex;
    109 
    110   mpfr_init2 (x, sizeof(uintmax_t)*CHAR_BIT);
    111 
    112   inex = mpfr_set_uj_2exp (x, 1, 0, MPFR_RNDN);
    113   if (inex || mpfr_cmp_ui(x, 1))
    114     PRINT_ERROR ("(1U,0)");
    115 
    116   inex = mpfr_set_uj_2exp (x, 1024, -10, MPFR_RNDN);
    117   if (inex || mpfr_cmp_ui(x, 1))
    118     PRINT_ERROR ("(1024U,-10)");
    119 
    120   inex = mpfr_set_uj_2exp (x, 1024, 10, MPFR_RNDN);
    121   if (inex || mpfr_cmp_ui(x, 1024L * 1024L))
    122     PRINT_ERROR ("(1024U,+10)");
    123 
    124   inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, 1000, MPFR_RNDN);
    125   inex |= mpfr_div_2ui (x, x, 1000, MPFR_RNDN);
    126   inex |= mpfr_add_ui (x, x, 1, MPFR_RNDN);
    127   if (inex || !mpfr_powerof2_raw (x)
    128       || MPFR_EXP (x) != sizeof(uintmax_t) * CHAR_BIT + 1)
    129     PRINT_ERROR ("(UINTMAX_MAX)");
    130 
    131   inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, MPFR_EMAX_MAX-10, MPFR_RNDN);
    132   if (inex == 0 || !mpfr_inf_p (x))
    133     PRINT_ERROR ("Overflow");
    134 
    135   inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, MPFR_EMIN_MIN-1000, MPFR_RNDN);
    136   if (inex == 0 || !MPFR_IS_ZERO (x))
    137     PRINT_ERROR ("Underflow");
    138 
    139   mpfr_clear (x);
    140 }
    141 
    142 static void
    143 check_set_sj (void)
    144 {
    145   mpfr_t x;
    146   int inex;
    147 
    148   mpfr_init2 (x, sizeof(intmax_t)*CHAR_BIT-1);
    149 
    150   inex = mpfr_set_sj (x, -MPFR_INTMAX_MAX, MPFR_RNDN);
    151   inex |= mpfr_add_si (x, x, -1, MPFR_RNDN);
    152   if (inex || mpfr_sgn (x) >=0 || !mpfr_powerof2_raw (x)
    153       || MPFR_EXP (x) != sizeof(intmax_t) * CHAR_BIT)
    154     PRINT_ERROR ("set_sj (-INTMAX_MAX)");
    155 
    156   inex = mpfr_set_sj (x, 1742, MPFR_RNDN);
    157   if (inex || mpfr_cmp_ui (x, 1742))
    158     PRINT_ERROR ("set_sj (1742)");
    159 
    160   mpfr_clear (x);
    161 }
    162 
    163 static void
    164 check_set_sj_2exp (void)
    165 {
    166   mpfr_t x;
    167   int inex;
    168 
    169   mpfr_init2 (x, sizeof(intmax_t)*CHAR_BIT-1);
    170 
    171   inex = mpfr_set_sj_2exp (x, MPFR_INTMAX_MIN, 1000, MPFR_RNDN);
    172   if (inex || mpfr_sgn (x) >=0 || !mpfr_powerof2_raw (x)
    173       || MPFR_EXP (x) != sizeof(intmax_t) * CHAR_BIT + 1000)
    174     PRINT_ERROR ("set_sj_2exp (INTMAX_MIN)");
    175 
    176   mpfr_clear (x);
    177 }
    178 
    179 int
    180 main (int argc, char *argv[])
    181 {
    182   tests_start_mpfr ();
    183 
    184   check_set_uj (2, 128, 50);
    185   check_set_uj_2exp ();
    186   check_set_sj ();
    187   check_set_sj_2exp ();
    188 
    189   tests_end_mpfr ();
    190   return 0;
    191 }
    192 
    193 #endif
    194