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

     1  /* Test mpf_cmp_si and mpf_cmp_z.
     2  
     3  Copyright 2000, 2001, 2004, 2015 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 "gmp.h"
    23  #include "gmp-impl.h"
    24  #include "tests.h"
    25  
    26  #define SGN(x)       ((x) < 0 ? -1 : (x) == 0 ? 0 : 1)
    27  
    28  void
    29  check_data (void)
    30  {
    31    static const struct {
    32      int         a_base;
    33      const char  *a;
    34      const char  *b;
    35      int         want;
    36    } data[] = {
    37      { 10, "0",  "1", -1 },
    38      { 10, "0",  "0",  0 },
    39      { 10, "0", "-1",  1 },
    40  
    41      { 10, "1",  "1", 0 },
    42      { 10, "1",  "0", 1 },
    43      { 10, "1", "-1", 1 },
    44  
    45      { 10, "-1",  "1", -1 },
    46      { 10, "-1",  "0", -1 },
    47      { 10, "-1", "-1", 0 },
    48  
    49      { 10, "1.5", "2", -1 },
    50      { 10, "1.5", "1",  1 },
    51      { 10, "0.5", "1", -1 },
    52  
    53      { 10, "-1.5", "-2",  1 },
    54      { 10, "-1.5", "-1", -1 },
    55      { 10, "-0.5", "-1",  1 },
    56  
    57      { 16,         "0", "-0x80000000",  1 },
    58      { 16,  "80000000", "-0x80000000",  1 },
    59      { 16,  "80000001", "-0x80000000",  1 },
    60      { 16, "-80000000", "-0x80000000",  0 },
    61      { 16, "-80000001", "-0x80000000", -1 },
    62      { 16, "-FF0080000001", "-0x80000000", -1 },
    63  
    64      { 16,                 "0", "-0x8000000000000000",  1 },
    65      { 16,  "8000000000000000", "-0x8000000000000000",  1 },
    66      { 16,  "8000000000000001", "-0x8000000000000000",  1 },
    67      { 16, "-8000000000000000", "-0x8000000000000000",  0 },
    68      { 16, "-8000000000000000.1", "-0x8000000000000000", -1 },
    69      { 16, "-FF008000000000000001", "-0x8000000000000000", -1 },
    70  
    71      { 16,                 "0", "-0x876543210FEDCBA9876543210000000",  1 },
    72      { 16,  "876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000",  1 },
    73      { 16,  "876543210FEDCBA9876543210000001", "-0x876543210FEDCBA9876543210000000",  1 },
    74      { 16, "-876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000",  0 },
    75      { 16, "-876543210FEDCBA9876543210000000.1", "-0x876543210FEDCBA9876543210000000", -1 },
    76      { 16, "-FF00876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000", -1 },
    77    };
    78  
    79    mpf_t  a;
    80    mpz_t  bz;
    81    long   b;
    82    int    got;
    83    int    i;
    84  
    85    mpf_init2 (a, 128);
    86    mpz_init (bz);
    87    for (i = 0; i < numberof (data); i++)
    88      {
    89        mpf_set_str_or_abort (a, data[i].a, data[i].a_base);
    90        mpz_set_str_or_abort (bz, data[i].b, 0);
    91  
    92        if (mpz_fits_slong_p (bz))
    93          {
    94            b = mpz_get_si (bz);
    95            got = mpf_cmp_si (a, b);
    96            if (SGN (got) != data[i].want)
    97              {
    98                printf ("mpf_cmp_si wrong on data[%d]\n", i);
    99                printf ("  a="); mpf_out_str (stdout, 10, 0, a);
   100                printf (" (%s)\n", data[i].a);
   101                printf ("  b=%ld (%s)\n", b, data[i].b);
   102                printf ("  got=%d\n", got);
   103                printf ("  want=%d\n", data[i].want);
   104                abort();
   105              }
   106          }
   107  
   108        got = mpf_cmp_z (a, bz);
   109        if (SGN (got) != data[i].want)
   110        	{
   111  	  b = mpz_get_si (bz);
   112        	  printf ("mpf_cmp_z wrong on data[%d]\n", i);
   113        	  printf ("  a="); mpf_out_str (stdout, 10, 0, a);
   114        	  printf (" (%s)\n", data[i].a);
   115        	  printf ("  b=%ld (%s)\n", b, data[i].b);
   116        	  printf ("  got=%d\n", got);
   117        	  printf ("  want=%d\n", data[i].want);
   118        	  abort();
   119        	}
   120      }
   121  
   122    mpf_clear (a);
   123    mpz_clear (bz);
   124  }
   125  
   126  int
   127  main (void)
   128  {
   129    tests_start ();
   130  
   131    check_data ();
   132  
   133    tests_end ();
   134    exit (0);
   135  }