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