diff options
author | tege <tege@gmplib.org> | 2005-05-01 15:01:19 +0200 |
---|---|---|
committer | tege <tege@gmplib.org> | 2005-05-01 15:01:19 +0200 |
commit | 344cb8312c8e13cb28457ad085b757605eb96404 (patch) | |
tree | d7e0db52a32905afa1d553c62aa6cb7d84703f66 /tests | |
parent | f144f7a2d5d0cfd024452b5d51c336f5cc0b5101 (diff) | |
download | gmp-344cb8312c8e13cb28457ad085b757605eb96404.tar.gz |
(check_random): New function.
(main): Call it.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/mpz/t-popcount.c | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/tests/mpz/t-popcount.c b/tests/mpz/t-popcount.c index 6a043b055..ff0243b34 100644 --- a/tests/mpz/t-popcount.c +++ b/tests/mpz/t-popcount.c @@ -1,6 +1,6 @@ /* Test mpz_popcount. -Copyright 2001 Free Software Foundation, Inc. +Copyright 2001, 2005 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -92,6 +92,70 @@ check_data (void) mpz_clear (n); } +unsigned long +refmpz_popcount (mpz_t arg) +{ + mp_size_t n, i; + unsigned long cnt; + mp_limb_t x; + + n = SIZ(arg); + if (n < 0) + return ~(unsigned long) 0; + + cnt = 0; + for (i = 0; i < n; i++) + { + x = PTR(arg)[i]; + while (x != 0) + { + cnt += (x & 1); + x >>= 1; + } + } + return cnt; +} + +void +check_random (void) +{ + gmp_randstate_ptr rands; + mpz_t bs; + mpz_t arg; + unsigned long arg_size, size_range; + unsigned long got, ref; + int i; + + rands = RANDS; + + mpz_init (bs); + mpz_init (arg); + + for (i = 0; i < 10000; i++) + { + mpz_urandomb (bs, rands, 32); + size_range = mpz_get_ui (bs) % 11 + 2; /* 0..4096 bit operands */ + + mpz_urandomb (bs, rands, size_range); + arg_size = mpz_get_ui (bs); + mpz_rrandomb (arg, rands, arg_size); + + got = mpz_popcount (arg); + ref = refmpz_popcount (arg); + if (got != ref) + { + printf ("mpz_popcount wrong\n", i); + printf (" "); mpz_out_str (stdout, 10, arg); printf ("\n"); + printf (" 0x"); mpz_out_str (stdout, 16, arg); printf ("\n"); + printf (" got %lu\n", got); + printf (" want %lu\n", ref); + abort(); + abort (); + } + } + mpz_clear (arg); + mpz_clear (bs); +} int main (void) @@ -100,6 +164,7 @@ main (void) check_onebit (); check_data (); + check_random (); tests_end (); exit (0); |