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

     1  /* Test mpz_sizeinbase.
     2  
     3  Copyright 2001, 2002 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  #if 0
    28    /* Disabled due to the bogosity of trying to fake an _mp_d pointer to
    29       below an object.  Has been seen to fail on a hppa system and on ia64.  */
    30  
    31  
    32  /* Create a fake mpz consisting of just a single 1 bit, with totbits being
    33     the total number of bits, inclusive of that 1 bit.  */
    34  void
    35  mpz_fake_bits (mpz_ptr z, unsigned long totbits)
    36  {
    37    static mp_limb_t  n;
    38    unsigned long     zero_bits, zero_limbs;
    39  
    40    zero_bits = totbits - 1;
    41    zero_limbs = zero_bits / GMP_NUMB_BITS;
    42    zero_bits %= GMP_NUMB_BITS;
    43  
    44    SIZ(z) = zero_limbs + 1;
    45    PTR(z) = (&n) - (SIZ(z) - 1);
    46    n = CNST_LIMB(1) << zero_bits;
    47  
    48    ASSERT_ALWAYS (mpz_sizeinbase (z, 2) == totbits);
    49  }
    50  
    51  
    52  /* This was seen to fail on a GNU/Linux powerpc32 with gcc 2.95.2,
    53     apparently due to a doubtful value of mp_bases[10].chars_per_bit_exactly
    54     (0X1.34413509F79FDP-2 whereas 0X1.34413509F79FFP-2 is believed correct).
    55     Presumably this is a glibc problem when gcc converts the decimal string
    56     in mp_bases.c, or maybe it's only a function of the rounding mode during
    57     compilation.  */
    58  void
    59  check_sample (void)
    60  {
    61    unsigned long  totbits = 198096465;
    62    int        base = 10;
    63    size_t     want = 59632979;
    64    size_t     got;
    65    mpz_t      z;
    66  
    67    mpz_fake_bits (z, totbits);
    68    got = mpz_sizeinbase (z, base);
    69    if (got != want)
    70      {
    71        printf ("mpz_sizeinbase\n");
    72        printf ("  base    %d\n",  base);
    73        printf ("  totbits %lu\n", totbits);
    74        printf ("  got     %u\n",  got);
    75        printf ("  want    %u\n",  want);
    76        abort ();
    77      }
    78  }
    79  #endif
    80  
    81  int
    82  main (void)
    83  {
    84    tests_start ();
    85  
    86    /* check_sample (); */
    87  
    88    tests_end ();
    89    exit (0);
    90  }