(root)/
mpfr-4.2.1/
tests/
tcmp_ui.c
       1  /* Test file for mpfr_cmp_ui and mpfr_cmp_si.
       2  
       3  Copyright 1999, 2001-2023 Free Software Foundation, Inc.
       4  Contributed by the AriC and Caramba 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  https://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  #ifdef TCMP_UI_CHECK_NAN
      24  
      25  mpfr_clear_erangeflag ();
      26  c = mpfr_cmp_si (x, TCMP_UI_CHECK_NAN);
      27  if (c != 0 || !mpfr_erangeflag_p ())
      28    {
      29      printf ("NaN error on %d (1)\n", TCMP_UI_CHECK_NAN);
      30      exit (1);
      31    }
      32  mpfr_clear_erangeflag ();
      33  c = (mpfr_cmp_si) (x, TCMP_UI_CHECK_NAN);
      34  if (c != 0 || !mpfr_erangeflag_p ())
      35    {
      36      printf ("NaN error on %d (2)\n", TCMP_UI_CHECK_NAN);
      37      exit (1);
      38    }
      39  if (TCMP_UI_CHECK_NAN >= 0)
      40    {
      41      mpfr_clear_erangeflag ();
      42      c = mpfr_cmp_ui (x, TCMP_UI_CHECK_NAN);
      43      if (c != 0 || !mpfr_erangeflag_p ())
      44        {
      45          printf ("NaN error on %d (3)\n", TCMP_UI_CHECK_NAN);
      46          exit (1);
      47        }
      48      mpfr_clear_erangeflag ();
      49      c = (mpfr_cmp_ui) (x, TCMP_UI_CHECK_NAN);
      50      if (c != 0 || !mpfr_erangeflag_p ())
      51        {
      52          printf ("NaN error on %d (4)\n", TCMP_UI_CHECK_NAN);
      53          exit (1);
      54        }
      55    }
      56  
      57  #else
      58  
      59  #include "mpfr-test.h"
      60  
      61  static void
      62  check_nan (void)
      63  {
      64    mpfr_t x;
      65    int c, i;
      66  
      67    mpfr_init (x);
      68    mpfr_set_nan (x);
      69    /* We need constants to completely test the macros. */
      70  #undef TCMP_UI_CHECK_NAN
      71  #define TCMP_UI_CHECK_NAN -17
      72  #include "tcmp_ui.c"
      73  #undef TCMP_UI_CHECK_NAN
      74  #define TCMP_UI_CHECK_NAN 0
      75  #include "tcmp_ui.c"
      76  #undef TCMP_UI_CHECK_NAN
      77  #define TCMP_UI_CHECK_NAN 17
      78  #include "tcmp_ui.c"
      79    for (i = -17; i <= 17; i += 17)
      80      {
      81  #undef TCMP_UI_CHECK_NAN
      82  #define TCMP_UI_CHECK_NAN i
      83  #include "tcmp_ui.c"
      84      }
      85    mpfr_clear (x);
      86  }
      87  
      88  /* Since mpfr_cmp_ui and mpfr_cmp_si are also implemented by a macro
      89     with __builtin_constant_p for GCC, check that side effects are
      90     handled correctly. */
      91  static void
      92  check_macros (void)
      93  {
      94    mpfr_t x;
      95    int c;
      96  
      97    mpfr_init2 (x, 32);
      98  
      99    c = 0;
     100    mpfr_set_ui (x, 17, MPFR_RNDN);
     101    if (mpfr_cmp_ui (x, 17) != 0)
     102      {
     103        printf ("Error 1 on mpfr_cmp_ui(x,17) in check_macros\n");
     104        exit (1);
     105      }
     106    if (mpfr_cmp_ui (x, (c++, 17)) != 0)
     107      {
     108        printf ("Error 2 on mpfr_cmp_ui(x,17) in check_macros\n");
     109        exit (1);
     110      }
     111    if (c != 1)
     112      {
     113        /* Failure in r13626 on x86_64 with the clang-9 1:9-1 Debian package,
     114           with any optimization level: c = 2 instead of 1
     115           Bug report: https://bugs.llvm.org/show_bug.cgi?id=43557
     116           New URL: https://github.com/llvm/llvm-project/issues/42902
     117           Apparently fixed in the clang-9 1:9.0.1-12 Debian package. */
     118        /* [2020-06-17]
     119           If one adds tcc support for macros using __builtin_constant_p
     120           in mpfr.h by testing __TINYC__, one also gets a failure.
     121           Bug report: https://savannah.nongnu.org/bugs/?58606
     122           "__builtin_constant_p is buggy on argument with side effect and
     123           constant value" */
     124        printf ("Error 3 on mpfr_cmp_ui(x,17) in check_macros\n"
     125                "(c = %d instead of 1)\n", c);
     126        exit (1);
     127      }
     128    if (mpfr_cmp_si (x, 17) != 0)
     129      {
     130        printf ("Error 1 on mpfr_cmp_si(x,17) in check_macros\n");
     131        exit (1);
     132      }
     133    if (mpfr_cmp_si (x, (c++, 17)) != 0)
     134      {
     135        printf ("Error 2 on mpfr_cmp_si(x,17) in check_macros\n");
     136        exit (1);
     137      }
     138    if (c != 2)
     139      {
     140        printf ("Error 3 on mpfr_cmp_si(x,17) in check_macros\n"
     141                "(c = %d instead of 2)\n", c);
     142        exit (1);
     143      }
     144  
     145    c = 0;
     146    mpfr_set_ui (x, 0, MPFR_RNDN);
     147    if (mpfr_cmp_ui (x, 0) != 0)
     148      {
     149        printf ("Error 1 on mpfr_cmp_ui(x,0) in check_macros\n");
     150        exit (1);
     151      }
     152    if (mpfr_cmp_ui (x, (c++, 0)) != 0)
     153      {
     154        printf ("Error 2 on mpfr_cmp_ui(x,0) in check_macros\n");
     155        exit (1);
     156      }
     157    if (c != 1)
     158      {
     159        printf ("Error 3 on mpfr_cmp_ui(x,0) in check_macros\n"
     160                "(c = %d instead of 1)\n", c);
     161        exit (1);
     162      }
     163    if (mpfr_cmp_si (x, 0) != 0)
     164      {
     165        printf ("Error 1 on mpfr_cmp_si(x,0) in check_macros\n");
     166        exit (1);
     167      }
     168    if (mpfr_cmp_si (x, (c++, 0)) != 0)
     169      {
     170        printf ("Error 2 on mpfr_cmp_si(x,0) in check_macros\n");
     171        exit (1);
     172      }
     173    if (c != 2)
     174      {
     175        printf ("Error 3 on mpfr_cmp_si(x,0) in check_macros\n"
     176                "(c = %d instead of 2)\n", c);
     177        exit (1);
     178      }
     179  
     180    mpfr_clear (x);
     181  }
     182  
     183  /* Bug in r7114 */
     184  static void
     185  test_macros (void)
     186  {
     187    mpfr_t x[3];
     188    mpfr_ptr p;
     189  
     190    mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
     191    mpfr_set_ui (x[0], 0, MPFR_RNDN);
     192    p = x[0];
     193    if (mpfr_cmp_ui (p++, 0) != 0)
     194      {
     195        printf ("Error in mpfr_cmp_ui macro: result should be 0.\n");
     196        exit (1);
     197      }
     198    if (p != x[1])
     199      {
     200        printf ("Error in mpfr_cmp_ui macro: p - x[0] = %d (expecting 1)\n",
     201                (int) (p - x[0]));
     202        exit (1);
     203      }
     204    p = x[0];
     205    if (mpfr_cmp_si (p++, 0) != 0)
     206      {
     207        printf ("Error in mpfr_cmp_si macro: result should be 0.\n");
     208        exit (1);
     209      }
     210    if (p != x[1])
     211      {
     212        printf ("Error in mpfr_cmp_si macro: p - x[0] = %d (expecting 1)\n",
     213                (int) (p - x[0]));
     214        exit (1);
     215      }
     216    mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
     217  }
     218  
     219  int
     220  main (void)
     221  {
     222    mpfr_t x;
     223    unsigned long i;
     224    long s;
     225  
     226    tests_start_mpfr ();
     227  
     228    mpfr_init(x);
     229  
     230    /* tests for cmp_ui */
     231    mpfr_set_ui (x, 3, MPFR_RNDZ);
     232    if ((mpfr_cmp_ui) (x, i = 3) != 0)
     233      {
     234        printf ("Error in mpfr_cmp_ui(3.0, 3)\n");
     235        exit (1);
     236      }
     237    if (mpfr_cmp_ui (x, i = 2) <= 0)
     238      {
     239        printf ("Error in mpfr_cmp_ui(3.0,2)\n");
     240        exit (1);
     241      }
     242    if (mpfr_cmp_ui (x, i = 4) >= 0)
     243      {
     244        printf ("Error in mpfr_cmp_ui(3.0,4)\n");
     245        exit (1);
     246      }
     247    mpfr_set_ui (x, 0, MPFR_RNDZ);
     248    mpfr_neg (x, x, MPFR_RNDZ);
     249    if (mpfr_cmp_ui (x, i = 0))
     250      {
     251        printf ("Error in mpfr_cmp_ui(0.0,0)\n");
     252        exit (1);
     253      }
     254    mpfr_set_ui (x, 1, MPFR_RNDZ);
     255    if (mpfr_cmp_ui (x, i = 0) == 0)
     256      {
     257        printf ("Error in mpfr_cmp_ui(1.0,0)\n");
     258        exit (1);
     259      }
     260  
     261    mpfr_set_inf (x, 1);
     262    if (mpfr_cmp_ui (x, 1) <= 0)
     263      {
     264        printf ("Error in mpfr_cmp_ui (Inf, 0)\n");
     265        exit (1);
     266      }
     267    mpfr_set_inf (x, -1);
     268    if (mpfr_cmp_ui (x, 1) >= 0)
     269      {
     270        printf ("Error in mpfr_cmp_ui (-Inf, 0)\n");
     271        exit (1);
     272      }
     273  
     274    mpfr_set_si (x, -1, MPFR_RNDN);
     275    MPFR_ASSERTN(mpfr_cmp_ui (x, 1) < 0);
     276    MPFR_ASSERTN(mpfr_cmp_ui (x, 0) < 0);
     277  
     278    mpfr_set_ui (x, 1, MPFR_RNDN);
     279    MPFR_ASSERTN(mpfr_cmp_ui (x, 0) > 0);
     280  
     281    /* tests for cmp_si */
     282    (mpfr_set_si) (x, -3, MPFR_RNDZ);
     283    if ((mpfr_cmp_si) (x, s = -3) != 0)
     284      {
     285        printf ("Error in mpfr_cmp_si(-3.0,-3)\n");
     286        exit (1);
     287      }
     288    if (mpfr_cmp_si (x, s = -4) <= 0)
     289      {
     290        printf ("Error in mpfr_cmp_si(-3.0,-4)\n");
     291        exit (1);
     292      }
     293    if (mpfr_cmp_si (x, s = 1) >= 0)
     294      {
     295        printf ("Error in mpfr_cmp_si(-3.0,1)\n");
     296        exit (1);
     297      }
     298  
     299    mpfr_set_inf (x, 1);
     300    if (mpfr_cmp_si (x, -1) <= 0)
     301      {
     302        printf ("Error in mpfr_cmp_si (Inf, 0)\n");
     303        exit (1);
     304      }
     305    mpfr_set_inf (x, -1);
     306    if (mpfr_cmp_si (x, -1) >= 0)
     307      {
     308        printf ("Error in mpfr_cmp_si (-Inf, 0)\n");
     309        exit (1);
     310      }
     311  
     312    /* case b=0 */
     313    mpfr_set_ui (x, 0, MPFR_RNDZ);
     314    MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
     315    MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
     316    MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);
     317  
     318    /* case i=0 */
     319    mpfr_set_ui (x, 1, MPFR_RNDZ);
     320    MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
     321    mpfr_set_ui (x, 0, MPFR_RNDZ);
     322    MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
     323    mpfr_neg (x, x, MPFR_RNDZ);
     324    MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
     325    mpfr_set_si (x, -1, MPFR_RNDZ);
     326    MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);
     327  
     328    /* case large x */
     329    mpfr_set_str_binary (x, "1E100");
     330    MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
     331    MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);
     332    MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);
     333    mpfr_set_str_binary (x, "-1E100");
     334    MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);
     335    MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
     336    MPFR_ASSERTN(mpfr_cmp_si (x, -1) < 0);
     337  
     338    /* corner case */
     339    mpfr_set_ui (x, 1, MPFR_RNDZ);
     340    mpfr_mul_2ui (x, x, GMP_NUMB_BITS - 1, MPFR_RNDZ);
     341    /* now EXP(x)=GMP_NUMB_BITS */
     342    MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);
     343  
     344    mpfr_clear (x);
     345  
     346    check_nan ();
     347    check_macros ();
     348    test_macros ();
     349  
     350    tests_end_mpfr ();
     351    return 0;
     352  }
     353  
     354  #endif