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