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

     1  /* Test mpz_fits_*_p */
     2  
     3  /*
     4  Copyright 2001 Free Software Foundation, Inc.
     5  
     6  This file is part of the GNU MP Library test suite.
     7  
     8  The GNU MP Library test suite is free software; you can redistribute it
     9  and/or modify it under the terms of the GNU General Public License as
    10  published by the Free Software Foundation; either version 3 of the License,
    11  or (at your option) any later version.
    12  
    13  The GNU MP Library test suite is distributed in the hope that it will be
    14  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    16  Public License for more details.
    17  
    18  You should have received a copy of the GNU General Public License along with
    19  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    20  
    21  #include <stdio.h>
    22  #include <stdlib.h>
    23  #include "gmp.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  
    28  /* Nothing sophisticated here, just exercise mpz_fits_*_p on a small amount
    29     of data. */
    30  
    31  #define EXPECT_S(fun,name,answer)                                       \
    32    got = fun (z);                                                        \
    33    if (got != answer)                                                    \
    34      {                                                                   \
    35        printf ("%s (%s) got %d want %d\n", name, expr, got, answer);     \
    36        printf (" z size %d\n", SIZ(z));                                  \
    37        printf (" z dec "); mpz_out_str (stdout, 10, z); printf ("\n");   \
    38        printf (" z hex "); mpz_out_str (stdout, 16, z); printf ("\n");   \
    39        error = 1;                                                        \
    40      }
    41  
    42  #define EXPECT(fun,answer)  EXPECT_S(fun,#fun,answer)
    43  
    44  int
    45  main (void)
    46  {
    47    mpz_t       z;
    48    int         got;
    49    const char  *expr;
    50    int         error = 0;
    51  
    52    tests_start ();
    53    mpz_init (z);
    54  
    55    mpz_set_ui (z, 0L);
    56    expr = "0";
    57    EXPECT (mpz_fits_ulong_p, 1);
    58    EXPECT (mpz_fits_uint_p, 1);
    59    EXPECT (mpz_fits_ushort_p, 1);
    60    EXPECT (mpz_fits_slong_p, 1);
    61    EXPECT (mpz_fits_sint_p, 1);
    62    EXPECT (mpz_fits_sshort_p, 1);
    63  
    64    mpz_set_ui (z, 1L);
    65    expr = "1";
    66    EXPECT (mpz_fits_ulong_p, 1);
    67    EXPECT (mpz_fits_uint_p, 1);
    68    EXPECT (mpz_fits_ushort_p, 1);
    69    EXPECT (mpz_fits_slong_p, 1);
    70    EXPECT (mpz_fits_sint_p, 1);
    71    EXPECT (mpz_fits_sshort_p, 1);
    72  
    73    mpz_set_si (z, -1L);
    74    expr = "-1";
    75    EXPECT (mpz_fits_ulong_p, 0);
    76    EXPECT (mpz_fits_uint_p, 0);
    77    EXPECT (mpz_fits_ushort_p, 0);
    78    EXPECT (mpz_fits_slong_p, 1);
    79    EXPECT (mpz_fits_sint_p, 1);
    80    EXPECT (mpz_fits_sshort_p, 1);
    81  
    82    mpz_set_ui (z, 1L);
    83    mpz_mul_2exp (z, z, 5L*GMP_LIMB_BITS);
    84    expr = "2^(5*BPML)";
    85    EXPECT (mpz_fits_ulong_p, 0);
    86    EXPECT (mpz_fits_uint_p, 0);
    87    EXPECT (mpz_fits_ushort_p, 0);
    88    EXPECT (mpz_fits_slong_p, 0);
    89    EXPECT (mpz_fits_sint_p, 0);
    90    EXPECT (mpz_fits_sshort_p, 0);
    91  
    92  
    93    mpz_set_ui (z, (unsigned long) USHRT_MAX);
    94    expr = "USHRT_MAX";
    95    EXPECT (mpz_fits_ulong_p, 1);
    96    EXPECT (mpz_fits_uint_p, 1);
    97    EXPECT (mpz_fits_ushort_p, 1);
    98  
    99    mpz_set_ui (z, (unsigned long) USHRT_MAX);
   100    mpz_add_ui (z, z, 1L);
   101    expr = "USHRT_MAX + 1";
   102    EXPECT (mpz_fits_ushort_p, 0);
   103  
   104  
   105    mpz_set_ui (z, (unsigned long) UINT_MAX);
   106    expr = "UINT_MAX";
   107    EXPECT (mpz_fits_ulong_p, 1);
   108    EXPECT (mpz_fits_uint_p, 1);
   109  
   110    mpz_set_ui (z, (unsigned long) UINT_MAX);
   111    mpz_add_ui (z, z, 1L);
   112    expr = "UINT_MAX + 1";
   113    EXPECT (mpz_fits_uint_p, 0);
   114  
   115  
   116    mpz_set_ui (z, ULONG_MAX);
   117    expr = "ULONG_MAX";
   118    EXPECT (mpz_fits_ulong_p, 1);
   119  
   120    mpz_set_ui (z, ULONG_MAX);
   121    mpz_add_ui (z, z, 1L);
   122    expr = "ULONG_MAX + 1";
   123    EXPECT (mpz_fits_ulong_p, 0);
   124  
   125  
   126    mpz_set_si (z, (long) SHRT_MAX);
   127    expr = "SHRT_MAX";
   128    EXPECT (mpz_fits_slong_p, 1);
   129    EXPECT (mpz_fits_sint_p, 1);
   130    EXPECT (mpz_fits_sshort_p, 1);
   131  
   132    mpz_set_si (z, (long) SHRT_MAX);
   133    mpz_add_ui (z, z, 1L);
   134    expr = "SHRT_MAX + 1";
   135    EXPECT (mpz_fits_sshort_p, 0);
   136  
   137  
   138    mpz_set_si (z, (long) INT_MAX);
   139    expr = "INT_MAX";
   140    EXPECT (mpz_fits_slong_p, 1);
   141    EXPECT (mpz_fits_sint_p, 1);
   142  
   143    mpz_set_si (z, (long) INT_MAX);
   144    mpz_add_ui (z, z, 1L);
   145    expr = "INT_MAX + 1";
   146    EXPECT (mpz_fits_sint_p, 0);
   147  
   148  
   149    mpz_set_si (z, LONG_MAX);
   150    expr = "LONG_MAX";
   151    EXPECT (mpz_fits_slong_p, 1);
   152  
   153    mpz_set_si (z, LONG_MAX);
   154    mpz_add_ui (z, z, 1L);
   155    expr = "LONG_MAX + 1";
   156    EXPECT (mpz_fits_slong_p, 0);
   157  
   158  
   159    mpz_set_si (z, (long) SHRT_MIN);
   160    expr = "SHRT_MIN";
   161    EXPECT (mpz_fits_slong_p, 1);
   162    EXPECT (mpz_fits_sint_p, 1);
   163    EXPECT (mpz_fits_sshort_p, 1);
   164  
   165    mpz_set_si (z, (long) SHRT_MIN);
   166    mpz_sub_ui (z, z, 1L);
   167    expr = "SHRT_MIN + 1";
   168    EXPECT (mpz_fits_sshort_p, 0);
   169  
   170  
   171    mpz_set_si (z, (long) INT_MIN);
   172    expr = "INT_MIN";
   173    EXPECT (mpz_fits_slong_p, 1);
   174    EXPECT (mpz_fits_sint_p, 1);
   175  
   176    mpz_set_si (z, (long) INT_MIN);
   177    mpz_sub_ui (z, z, 1L);
   178    expr = "INT_MIN + 1";
   179    EXPECT (mpz_fits_sint_p, 0);
   180  
   181  
   182    mpz_set_si (z, LONG_MIN);
   183    expr = "LONG_MIN";
   184    EXPECT (mpz_fits_slong_p, 1);
   185  
   186    mpz_set_si (z, LONG_MIN);
   187    mpz_sub_ui (z, z, 1L);
   188    expr = "LONG_MIN + 1";
   189    EXPECT (mpz_fits_slong_p, 0);
   190  
   191  
   192    if (error)
   193      abort ();
   194  
   195    mpz_clear (z);
   196    tests_end ();
   197    exit (0);
   198  }