~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/tests/mpz/t-popcount.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test mpz_popcount.
 
2
 
 
3
Copyright 2001 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
 
 
30
void
 
31
check_onebit (void)
 
32
{
 
33
  mpz_t          n;
 
34
  unsigned long  i, got;
 
35
 
 
36
  mpz_init (n);
 
37
  for (i = 0; i < 5 * BITS_PER_MP_LIMB; i++)
 
38
    {
 
39
      mpz_setbit (n, i);
 
40
      got = mpz_popcount (n);
 
41
      if (got != 1)
 
42
        {
 
43
          printf ("mpz_popcount wrong on single bit at %lu\n", i); 
 
44
          printf ("   got %lu, want 1\n", got);
 
45
          abort();                                    
 
46
        }
 
47
      mpz_clrbit (n, i);
 
48
    }
 
49
  mpz_clear (n);
 
50
}
 
51
 
 
52
 
 
53
void
 
54
check_data (void)
 
55
{
 
56
  static const struct {
 
57
    const char     *n;
 
58
    unsigned long  want;
 
59
  } data[] = {
 
60
    { "-1", ~ (unsigned long) 0 },
 
61
    { "-12345678", ~ (unsigned long) 0 },
 
62
    { "0", 0 },
 
63
    { "1", 1 },
 
64
    { "3", 2 },
 
65
    { "5", 2 },
 
66
    { "0xFFFF", 16 },
 
67
    { "0xFFFFFFFF", 32 },
 
68
    { "0xFFFFFFFFFFFFFFFF", 64 },
 
69
    { "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128 },
 
70
  };
 
71
 
 
72
  unsigned long   got;
 
73
  int    i;
 
74
  mpz_t  n;
 
75
 
 
76
  mpz_init (n);
 
77
  for (i = 0; i < numberof (data); i++)
 
78
    {
 
79
      mpz_set_str_or_abort (n, data[i].n, 0);
 
80
      got = mpz_popcount (n);
 
81
      if (got != data[i].want)
 
82
        {
 
83
          printf ("mpz_popcount wrong at data[%d]\n", i); 
 
84
          printf ("   n     \"%s\"\n", data[i].n);
 
85
          printf ("         ");   mpz_out_str (stdout, 10, n); printf ("\n");
 
86
          printf ("         0x"); mpz_out_str (stdout, 16, n); printf ("\n");
 
87
          printf ("   got   %lu\n", got);
 
88
          printf ("   want  %lu\n", data[i].want);
 
89
          abort();                                    
 
90
        }
 
91
    }
 
92
  mpz_clear (n);
 
93
}
 
94
 
 
95
 
 
96
int
 
97
main (void)
 
98
{
 
99
  tests_start ();
 
100
 
 
101
  check_onebit ();
 
102
  check_data ();
 
103
 
 
104
  tests_end ();
 
105
  exit (0);
 
106
}