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

     1  /* Test mpz_cmp_si.
     2  
     3  Copyright 2000, 2001 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      const char  *a, *b;
    33      int         want;
    34    } data[] = {
    35      { "0",  "1", -1 },
    36      { "0",  "0",  0 },
    37      { "0", "-1",  1 },
    38  
    39      { "1",  "1", 0 },
    40      { "1",  "0", 1 },
    41      { "1", "-1", 1 },
    42  
    43      { "-1",  "1", -1 },
    44      { "-1",  "0", -1 },
    45      { "-1", "-1", 0 },
    46  
    47      {           "0", "-0x80000000",  1 },
    48      {  "0x80000000", "-0x80000000",  1 },
    49      {  "0x80000001", "-0x80000000",  1 },
    50      { "-0x80000000", "-0x80000000",  0 },
    51      { "-0x80000001", "-0x80000000", -1 },
    52  
    53      {                   "0", "-0x8000000000000000",  1 },
    54      {  "0x8000000000000000", "-0x8000000000000000",  1 },
    55      {  "0x8000000000000001", "-0x8000000000000000",  1 },
    56      { "-0x8000000000000000", "-0x8000000000000000",  0 },
    57      { "-0x8000000000000001", "-0x8000000000000000", -1 },
    58    };
    59  
    60    mpz_t  a, bz;
    61    long   b;
    62    int    got;
    63    int    i;
    64  
    65    mpz_init (a);
    66    mpz_init (bz);
    67    for (i = 0; i < numberof (data); i++)
    68      {
    69        mpz_set_str_or_abort (a, data[i].a, 0);
    70        mpz_set_str_or_abort (bz, data[i].b, 0);
    71  
    72        if (mpz_fits_slong_p (bz))
    73  	{
    74  	  b = mpz_get_si (bz);
    75  	  got = mpz_cmp_si (a, b);
    76  	  if (SGN (got) != data[i].want)
    77  	    {
    78  	      printf ("mpz_cmp_si wrong on data[%d]\n", i);
    79  	      printf ("  a="); mpz_out_str (stdout, 10, a); printf ("\n");
    80  	      printf ("  b=%ld\n", b);
    81  	      printf ("  got=%d\n", got);
    82  	      printf ("  want=%d\n", data[i].want);
    83  	      abort();
    84  	    }
    85  	}
    86      }
    87  
    88    mpz_clear (a);
    89    mpz_clear (bz);
    90  }
    91  
    92  
    93  int
    94  main (void)
    95  {
    96    tests_start ();
    97  
    98    check_data ();
    99  
   100    tests_end ();
   101    exit (0);
   102  }