(root)/
coreutils-9.4/
gnulib-tests/
test-libgmp.c
       1  /* Test of libgmp or its mini-gmp substitute.
       2     Copyright (C) 2020-2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  #include <config.h>
      18  
      19  /* Specification.  */
      20  #include <gmp.h>
      21  
      22  #include <limits.h>
      23  #include <string.h>
      24  
      25  #include "macros.h"
      26  
      27  #ifndef MINI_GMP_LIMB_TYPE
      28  /* Verify that the gmp.h header file was generated for the same
      29     machine word size as we are using.  */
      30  static_assert (GMP_NUMB_BITS == sizeof (mp_limb_t) * CHAR_BIT);
      31  #endif
      32  
      33  int
      34  main ()
      35  {
      36  #ifndef MINI_GMP_LIMB_TYPE
      37    /* Verify that the gmp.h header file and the libgmp library come from
      38       the same GMP version.  */
      39    {
      40      char gmp_header_version[32];
      41      sprintf (gmp_header_version, "%d.%d.%d", __GNU_MP_VERSION,
      42               __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL);
      43      if (strcmp (gmp_version, gmp_header_version) != 0)
      44        {
      45          char gmp_header_version2[32];
      46          if (__GNU_MP_VERSION_PATCHLEVEL > 0
      47              || (sprintf (gmp_header_version2, "%d.%d", __GNU_MP_VERSION,
      48                           __GNU_MP_VERSION_MINOR),
      49                  strcmp (gmp_version, gmp_header_version2) != 0))
      50            {
      51              fprintf (stderr,
      52                       "gmp header version (%s) does not match gmp library version (%s).\n",
      53                       gmp_header_version, gmp_version);
      54              exit (1);
      55            }
      56        }
      57    }
      58  #endif
      59  
      60    /* A simple sanity check that 2 + 2 = 4.  */
      61    static mp_limb_t const twobody[] = { 2 };
      62    static mpz_t const two = MPZ_ROINIT_N ((mp_limb_t *) twobody, 1);
      63    ASSERT (mpz_fits_slong_p (two));
      64    ASSERT (mpz_get_si (two) == 2);
      65  
      66    mpz_t four;
      67    mpz_init (four);
      68    mpz_add (four, two, two);
      69    ASSERT (mpz_fits_slong_p (four));
      70    ASSERT (mpz_get_si (four) == 4);
      71    mpz_clear (four);
      72  
      73    return 0;
      74  }