Home | History | Annotate | Line # | Download | only in tests
tset_sj.c revision 1.1
      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, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
      5 Contributed by the Arenaire and Cacao 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"       /* for a build within gmp */
     26 #endif
     27 
     28 #include <stdio.h>
     29 #include <stdlib.h>
     30 #include <limits.h>
     31 
     32 /* The ISO C99 standard specifies that in C++ implementations the
     33    INTMAX_MAX, ... macros should only be defined if explicitly requested.  */
     34 #if defined __cplusplus
     35 # define __STDC_LIMIT_MACROS
     36 # define __STDC_CONSTANT_MACROS
     37 #endif
     38 
     39 #if HAVE_INTTYPES_H
     40 # include <inttypes.h> /* for intmax_t */
     41 #else
     42 # if HAVE_STDINT_H
     43 #  include <stdint.h>
     44 # endif
     45 #endif
     46 
     47 #include "mpfr-test.h"
     48 
     49 #ifndef _MPFR_H_HAVE_INTMAX_T
     50 int
     51 main (void)
     52 {
     53   return 0;
     54 }
     55 #else
     56 
     57 #define ERROR(str) {printf("Error for "str"\n"); exit(1);}
     58 
     59 static int
     60 inexact_sign (int x)
     61 {
     62   return (x < 0) ? -1 : (x > 0);
     63 }
     64 
     65 static void
     66 check_set_uj (mpfr_prec_t pmin, mpfr_prec_t pmax, int N)
     67 {
     68   mpfr_t x, y;
     69   mpfr_prec_t p;
     70   int inex1, inex2, n;
     71   mp_limb_t limb;
     72 
     73   mpfr_inits2 (pmax, x, y, (mpfr_ptr) 0);
     74 
     75   for ( p = pmin ; p < pmax ; p++)
     76     {
     77       mpfr_set_prec (x, p);
     78       mpfr_set_prec (y, p);
     79       for (n = 0 ; n < N ; n++)
     80         {
     81           /* mp_limb_t may be unsigned long long */
     82           limb = (unsigned long) randlimb ();
     83           inex1 = mpfr_set_uj (x, limb, MPFR_RNDN);
     84           inex2 = mpfr_set_ui (y, limb, MPFR_RNDN);
     85           if (mpfr_cmp (x, y))
     86             {
     87               printf ("ERROR for mpfr_set_uj and j=%lu and p=%lu\n",
     88                       (unsigned long) limb, p);
     89               printf ("X="); mpfr_dump (x);
     90               printf ("Y="); mpfr_dump (y);
     91               exit (1);
     92             }
     93           if (inexact_sign (inex1) != inexact_sign (inex2))
     94             {
     95               printf ("ERROR for inexact(set_uj): j=%lu p=%lu\n"
     96                       "Inexact1= %d Inexact2= %d\n",
     97                       (unsigned long) limb, p, inex1, inex2);
     98               exit (1);
     99             }
    100         }
    101     }
    102   /* Special case */
    103   mpfr_set_prec (x, sizeof(uintmax_t)*CHAR_BIT);
    104   inex1 = mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN);
    105   if (inex1 != 0 || mpfr_sgn(x) <= 0)
    106     ERROR ("inexact / UINTMAX_MAX");
    107   inex1 = mpfr_add_ui (x, x, 1, MPFR_RNDN);
    108   if (inex1 != 0 || !mpfr_powerof2_raw (x)
    109       || MPFR_EXP (x) != (sizeof(uintmax_t)*CHAR_BIT+1) )
    110     ERROR ("power of 2");
    111   mpfr_set_uj (x, 0, MPFR_RNDN);
    112   if (!MPFR_IS_ZERO (x))
    113     ERROR ("Setting 0");
    114 
    115   mpfr_clears (x, y, (mpfr_ptr) 0);
    116 }
    117 
    118 static void
    119 check_set_uj_2exp (void)
    120 {
    121   mpfr_t x;
    122   int inex;
    123 
    124   mpfr_init2 (x, sizeof(uintmax_t)*CHAR_BIT);
    125 
    126   inex = mpfr_set_uj_2exp (x, 1, 0, MPFR_RNDN);
    127   if (inex || mpfr_cmp_ui(x, 1))
    128     ERROR("(1U,0)");
    129 
    130   inex = mpfr_set_uj_2exp (x, 1024, -10, MPFR_RNDN);
    131   if (inex || mpfr_cmp_ui(x, 1))
    132     ERROR("(1024U,-10)");
    133 
    134   inex = mpfr_set_uj_2exp (x, 1024, 10, MPFR_RNDN);
    135   if (inex || mpfr_cmp_ui(x, 1024L * 1024L))
    136     ERROR("(1024U,+10)");
    137 
    138   inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, 1000, MPFR_RNDN);
    139   inex |= mpfr_div_2ui (x, x, 1000, MPFR_RNDN);
    140   inex |= mpfr_add_ui (x, x, 1, MPFR_RNDN);
    141   if (inex || !mpfr_powerof2_raw (x)
    142       || MPFR_EXP (x) != (sizeof(uintmax_t)*CHAR_BIT+1) )
    143     ERROR("(UINTMAX_MAX)");
    144 
    145   inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, MPFR_EMAX_MAX-10, MPFR_RNDN);
    146   if (inex == 0 || !mpfr_inf_p (x))
    147     ERROR ("Overflow");
    148 
    149   inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, MPFR_EMIN_MIN-1000, MPFR_RNDN);
    150   if (inex == 0 || !MPFR_IS_ZERO (x))
    151     ERROR ("Underflow");
    152 
    153   mpfr_clear (x);
    154 }
    155 
    156 static void
    157 check_set_sj (void)
    158 {
    159   mpfr_t x;
    160   int inex;
    161 
    162   mpfr_init2 (x, sizeof(intmax_t)*CHAR_BIT-1);
    163 
    164   inex = mpfr_set_sj (x, -MPFR_INTMAX_MAX, MPFR_RNDN);
    165   inex |= mpfr_add_si (x, x, -1, MPFR_RNDN);
    166   if (inex || mpfr_sgn (x) >=0 || !mpfr_powerof2_raw (x)
    167       || MPFR_EXP (x) != (sizeof(intmax_t)*CHAR_BIT) )
    168     ERROR("set_sj (-INTMAX_MAX)");
    169 
    170   inex = mpfr_set_sj (x, 1742, MPFR_RNDN);
    171   if (inex || mpfr_cmp_ui (x, 1742))
    172     ERROR ("set_sj (1742)");
    173 
    174   mpfr_clear (x);
    175 }
    176 
    177 static void
    178 check_set_sj_2exp (void)
    179 {
    180   mpfr_t x;
    181   int inex;
    182 
    183   mpfr_init2 (x, sizeof(intmax_t)*CHAR_BIT-1);
    184 
    185   inex = mpfr_set_sj_2exp (x, MPFR_INTMAX_MIN, 1000, MPFR_RNDN);
    186   if (inex || mpfr_sgn (x) >=0 || !mpfr_powerof2_raw (x)
    187       || MPFR_EXP (x) != (sizeof(intmax_t)*CHAR_BIT+1000) )
    188     ERROR("set_sj_2exp (INTMAX_MIN)");
    189 
    190   mpfr_clear (x);
    191 }
    192 
    193 int
    194 main (int argc, char *argv[])
    195 {
    196   tests_start_mpfr ();
    197 
    198   check_set_uj (2, 128, 50);
    199   check_set_uj_2exp ();
    200   check_set_sj ();
    201   check_set_sj_2exp ();
    202 
    203   tests_end_mpfr ();
    204   return 0;
    205 }
    206 
    207 #endif
    208