~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/tests/mpz/t-hamdist.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2006-05-17 02:46:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060517024626-lljr08ftv9g9vefl
Tags: upstream-0.9h-20060510
ImportĀ upstreamĀ versionĀ 0.9h-20060510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test mpz_hamdist.
 
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
void
 
30
check_twobits (void)
 
31
{
 
32
  unsigned long  i, j, got, want;
 
33
  mpz_t  x, y;
 
34
 
 
35
  mpz_init (x);
 
36
  mpz_init (y);
 
37
  for (i = 0; i < 5 * GMP_NUMB_BITS; i++)
 
38
    {
 
39
      for (j = 0; j < 5 * GMP_NUMB_BITS; j++)
 
40
        {
 
41
          mpz_set_ui (x, 0L);
 
42
          mpz_setbit (x, i);
 
43
          mpz_set_ui (y, 0L);
 
44
          mpz_setbit (y, j);
 
45
 
 
46
          want = 2 * (i != j);
 
47
          got = mpz_hamdist (x, y);
 
48
          if (got != want)
 
49
            {
 
50
              printf    ("mpz_hamdist wrong on 2 bits pos/pos\n");
 
51
            wrong:
 
52
              printf    ("  i    %lu\n", i);
 
53
              printf    ("  j    %lu\n", j);
 
54
              printf    ("  got  %lu\n", got);
 
55
              printf    ("  want %lu\n", want);
 
56
              mpz_trace ("  x   ", x);
 
57
              mpz_trace ("  y   ", y);
 
58
              abort();
 
59
            }
 
60
 
 
61
          mpz_neg (x, x);
 
62
          mpz_neg (y, y);
 
63
          want = ABS ((long) (i-j));
 
64
          got = mpz_hamdist (x, y);
 
65
          if (got != want)
 
66
            {
 
67
              printf    ("mpz_hamdist wrong on 2 bits neg/neg\n");
 
68
              goto wrong;
 
69
            }
 
70
        }
 
71
 
 
72
    }
 
73
  mpz_clear (x);
 
74
  mpz_clear (y);
 
75
}
 
76
 
 
77
 
 
78
void
 
79
check_rand (void)
 
80
{
 
81
  gmp_randstate_ptr  rands = RANDS;
 
82
  unsigned long  got, want;
 
83
  int    i;
 
84
  mpz_t  x, y;
 
85
 
 
86
  mpz_init (x);
 
87
  mpz_init (y);
 
88
 
 
89
  for (i = 0; i < 2000; i++)
 
90
    {
 
91
      mpz_erandomb (x, rands, 6 * GMP_NUMB_BITS);
 
92
      mpz_negrandom (x, rands);
 
93
      mpz_mul_2exp (x, x, urandom() % (4 * GMP_NUMB_BITS));
 
94
 
 
95
      mpz_erandomb (y, rands, 6 * GMP_NUMB_BITS);
 
96
      mpz_negrandom (y, rands);
 
97
      mpz_mul_2exp (y, y, urandom() % (4 * GMP_NUMB_BITS));
 
98
 
 
99
      want = refmpz_hamdist (x, y);
 
100
      got = mpz_hamdist (x, y);
 
101
      if (got != want)
 
102
        {
 
103
          printf    ("mpz_hamdist wrong on random\n");
 
104
          printf    ("  got  %lu\n", got);
 
105
          printf    ("  want %lu\n", want);
 
106
          mpz_trace ("  x   ", x);
 
107
          mpz_trace ("  y   ", y);
 
108
          abort();
 
109
        }
 
110
    }
 
111
  mpz_clear (x);
 
112
  mpz_clear (y);
 
113
}
 
114
 
 
115
int
 
116
main (void)
 
117
{
 
118
  tests_start ();
 
119
  mp_trace_base = -16;
 
120
 
 
121
  check_twobits ();
 
122
  check_rand ();
 
123
 
 
124
  tests_end ();
 
125
  exit (0);
 
126
}