1 | /* Test mpz_sizeinbase. |
---|
2 | |
---|
3 | Copyright 2001, 2002 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU MP Library. |
---|
6 | |
---|
7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU Lesser General Public License as published by |
---|
9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
15 | License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include "gmp.h" |
---|
25 | #include "gmp-impl.h" |
---|
26 | #include "tests.h" |
---|
27 | |
---|
28 | |
---|
29 | /* Create a fake mpz consisting of just a single 1 bit, with totbits being |
---|
30 | the total number of bits, inclusive of that 1 bit. */ |
---|
31 | void |
---|
32 | mpz_fake_bits (mpz_ptr z, unsigned long totbits) |
---|
33 | { |
---|
34 | static mp_limb_t n; |
---|
35 | unsigned long zero_bits, zero_limbs; |
---|
36 | |
---|
37 | zero_bits = totbits - 1; |
---|
38 | zero_limbs = zero_bits / GMP_NUMB_BITS; |
---|
39 | zero_bits %= GMP_NUMB_BITS; |
---|
40 | |
---|
41 | SIZ(z) = zero_limbs + 1; |
---|
42 | PTR(z) = (&n) - (SIZ(z) - 1); |
---|
43 | n = CNST_LIMB(1) << zero_bits; |
---|
44 | |
---|
45 | ASSERT_ALWAYS (mpz_sizeinbase (z, 2) == totbits); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | /* This was seen to fail on a GNU/Linux powerpc32 with gcc 2.95.2, |
---|
50 | apparently due to a doubtful value of mp_bases[10].chars_per_bit_exactly |
---|
51 | (0X1.34413509F79FDP-2 whereas 0X1.34413509F79FFP-2 is believed correct). |
---|
52 | Presumably this is a glibc problem when gcc converts the decimal string |
---|
53 | in mp_bases.c, or maybe it's only a function of the rounding mode during |
---|
54 | compilation. */ |
---|
55 | void |
---|
56 | check_sample (void) |
---|
57 | { |
---|
58 | unsigned long totbits = 198096465; |
---|
59 | int base = 10; |
---|
60 | size_t want = 59632979; |
---|
61 | size_t got; |
---|
62 | mpz_t z; |
---|
63 | |
---|
64 | mpz_fake_bits (z, totbits); |
---|
65 | got = mpz_sizeinbase (z, base); |
---|
66 | if (got != want) |
---|
67 | { |
---|
68 | printf ("mpz_sizeinbase\n"); |
---|
69 | printf (" base %d\n", base); |
---|
70 | printf (" totbits %lu\n", totbits); |
---|
71 | printf (" got %u\n", got); |
---|
72 | printf (" want %u\n", want); |
---|
73 | abort (); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | int |
---|
79 | main (void) |
---|
80 | { |
---|
81 | tests_start (); |
---|
82 | |
---|
83 | /* Disabled due to the bogosity of trying to fake an _mp_d pointer to |
---|
84 | below an object. Has been seen to fail on a hppa system. */ |
---|
85 | |
---|
86 | /* check_sample (); */ |
---|
87 | |
---|
88 | tests_end (); |
---|
89 | exit (0); |
---|
90 | } |
---|