github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpf/t-cmp_d.c (about)

     1  /* Test mpf_cmp_d.
     2  
     3  Copyright 2001, 2003, 2003, 2005 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library test suite.
     6  
     7  The GNU MP Library test suite is free software; you can redistribute it
     8  and/or modify it under the terms of the GNU General Public License as
     9  published by the Free Software Foundation; either version 3 of the License,
    10  or (at your option) any later version.
    11  
    12  The GNU MP Library test suite is distributed in the hope that it will be
    13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    15  Public License for more details.
    16  
    17  You should have received a copy of the GNU General Public License along with
    18  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    19  
    20  #include <stdio.h>
    21  #include <stdlib.h>
    22  #include <string.h>
    23  
    24  #include "gmp.h"
    25  #include "gmp-impl.h"
    26  #include "tests.h"
    27  
    28  
    29  #define SGN(n)  ((n) > 0 ? 1 : (n) < 0 ? -1 : 0)
    30  
    31  void
    32  check_one (const char *name, mpf_srcptr x, double y, int cmp)
    33  {
    34    int   got;
    35  
    36    got = mpf_cmp_d (x, y);
    37    if (SGN(got) != cmp)
    38      {
    39        int i;
    40        printf    ("mpf_cmp_d wrong (from %s)\n", name);
    41        printf    ("  got  %d\n", got);
    42        printf    ("  want %d\n", cmp);
    43        mpf_trace ("  x", x);
    44        printf    ("  y %g\n", y);
    45        mp_trace_base=-16;
    46        mpf_trace ("  x", x);
    47        printf    ("  y %g\n", y);
    48        printf    ("  y");
    49        for (i = 0; i < sizeof(y); i++)
    50          printf (" %02X", (unsigned) ((unsigned char *) &y)[i]);
    51        printf ("\n");
    52        abort ();
    53      }
    54  }
    55  
    56  void
    57  check_infinity (void)
    58  {
    59    mpf_t   x;
    60    double  y = tests_infinity_d ();
    61    if (y == 0.0)
    62      return;
    63  
    64    mpf_init (x);
    65  
    66    /* 0 cmp inf */
    67    mpf_set_ui (x, 0L);
    68    check_one ("check_infinity", x,  y, -1);
    69    check_one ("check_infinity", x, -y,  1);
    70  
    71    /* 123 cmp inf */
    72    mpf_set_ui (x, 123L);
    73    check_one ("check_infinity", x,  y, -1);
    74    check_one ("check_infinity", x, -y,  1);
    75  
    76    /* -123 cmp inf */
    77    mpf_set_si (x, -123L);
    78    check_one ("check_infinity", x,  y, -1);
    79    check_one ("check_infinity", x, -y,  1);
    80  
    81    /* 2^5000 cmp inf */
    82    mpf_set_ui (x, 1L);
    83    mpf_mul_2exp (x, x, 5000L);
    84    check_one ("check_infinity", x,  y, -1);
    85    check_one ("check_infinity", x, -y,  1);
    86  
    87    /* -2^5000 cmp inf */
    88    mpf_neg (x, x);
    89    check_one ("check_infinity", x,  y, -1);
    90    check_one ("check_infinity", x, -y,  1);
    91  
    92    mpf_clear (x);
    93  }
    94  
    95  int
    96  main (int argc, char *argv[])
    97  {
    98    tests_start ();
    99  
   100    check_infinity ();
   101  
   102    tests_end ();
   103    exit (0);
   104  }