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

     1  /* Exercise mpz_get_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  
    27  void
    28  check_data (void)
    29  {
    30    static const struct {
    31      const char  *n;
    32      long        want;
    33    } data[] = {
    34      { "0",      0L },
    35      { "1",      1L },
    36      { "-1",     -1L },
    37      { "2",      2L },
    38      { "-2",     -2L },
    39      { "12345",  12345L },
    40      { "-12345", -12345L },
    41    };
    42  
    43    int    i;
    44    mpz_t  n;
    45    long   got;
    46  
    47    mpz_init (n);
    48    for (i = 0; i < numberof (data); i++)
    49      {
    50        mpz_set_str_or_abort (n, data[i].n, 0);
    51  
    52        got = mpz_get_si (n);
    53        if (got != data[i].want)
    54  	{
    55  	  printf ("mpz_get_si wrong at data[%d]\n", i);
    56  	  printf ("   n     \"%s\" (", data[i].n);
    57  	  mpz_out_str (stdout, 10, n); printf (", hex ");
    58  	  mpz_out_str (stdout, 16, n); printf (")\n");
    59  	  printf ("   got   %ld (0x%lX)\n", got, got);
    60  	  printf ("   want  %ld (0x%lX)\n", data[i].want, data[i].want);
    61  	  abort();
    62  	}
    63      }
    64    mpz_clear (n);
    65  }
    66  
    67  
    68  void
    69  check_max (void)
    70  {
    71    mpz_t  n;
    72    long   want;
    73    long   got;
    74  
    75    mpz_init (n);
    76  
    77  #define CHECK_MAX(name)                                 \
    78    if (got != want)                                      \
    79      {                                                   \
    80        printf ("mpz_get_si wrong on %s\n", name);        \
    81        printf ("   n    ");                              \
    82        mpz_out_str (stdout, 10, n); printf (", hex ");   \
    83        mpz_out_str (stdout, 16, n); printf ("\n");       \
    84        printf ("   got  %ld, hex %lX\n", got, got);      \
    85        printf ("   want %ld, hex %lX\n", want, want);    \
    86        abort();                                          \
    87      }
    88  
    89    want = LONG_MAX;
    90    mpz_set_si (n, want);
    91    got = mpz_get_si (n);
    92    CHECK_MAX ("LONG_MAX");
    93  
    94    want = LONG_MIN;
    95    mpz_set_si (n, want);
    96    got = mpz_get_si (n);
    97    CHECK_MAX ("LONG_MIN");
    98  
    99    /* The following checks that -0x100000000 gives -0x80000000.  This doesn't
   100       actually fit in a long and the result from mpz_get_si() is undefined,
   101       but -0x80000000 is what comes out currently, and it should be that
   102       value irrespective of the mp_limb_t size (long or long long).  */
   103  
   104    want = LONG_MIN;
   105    mpz_mul_2exp (n, n, 1);
   106    CHECK_MAX ("-0x100...00");
   107  
   108    mpz_clear (n);
   109  }
   110  
   111  
   112  int
   113  main (void)
   114  {
   115    tests_start ();
   116  
   117    check_data ();
   118    check_max ();
   119  
   120    tests_end ();
   121    exit (0);
   122  }