source: trunk/third/gmp/tests/mpz/t-scan.c @ 18191

Revision 18191, 3.0 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Tests of mpz_scan0 and mpz_scan1.
2
3Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 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
29unsigned long
30refmpz_scan (mpz_srcptr z, unsigned long i, int sought)
31{
32  unsigned long  z_bits = (unsigned long) ABSIZ(z) * GMP_NUMB_BITS;
33
34  do
35    {
36      if (mpz_tstbit (z, i) == sought)
37        return i;
38      i++;
39    }
40  while (i <= z_bits);
41
42  return ULONG_MAX;
43}
44
45unsigned long
46refmpz_scan0 (mpz_srcptr z, unsigned long starting_bit)
47{
48  return refmpz_scan (z, starting_bit, 0);
49}
50
51unsigned long
52refmpz_scan1 (mpz_srcptr z, unsigned long starting_bit)
53{
54  return refmpz_scan (z, starting_bit, 1);
55}
56
57
58void
59check_ref (void)
60{
61  mpz_t          z;
62  int            test, size, neg, sought;
63  unsigned long  i, got, want;
64
65  mpz_init (z);
66  for (test = 0; test < 10; test++)
67    {
68      for (size = 0; size < 5; size++)
69        {
70          mpz_random2 (z, size);
71
72          for (neg = 0; neg <= 1; neg++)
73            {
74              if (neg)
75                mpz_neg (z, z);
76
77              for (i = 0; i < size * GMP_NUMB_BITS + 8; i++)
78                {
79                  for (sought = 0; sought <= 1; sought++)
80                    {
81                      if (sought == 0)
82                        {
83                          got = mpz_scan0 (z, i);
84                          want = refmpz_scan0 (z, i);
85                        }
86                      else
87                        {
88                          got = mpz_scan1 (z, i);
89                          want = refmpz_scan1 (z, i);
90                        }
91
92                      if (got != want)
93                        {
94                          printf ("wrong at test=%d, size=%d, neg=%d, i=%lu, sought=%d\n",
95                                  test, size, neg, i, sought);
96                          printf ("   z 0x");
97                          mpz_out_str (stdout, -16, z);
98                          printf ("\n");
99                          printf ("   got=%lu, want=%lu\n", got, want);
100                          exit (1);                 
101                        }
102                    }
103                }
104            }
105        }
106    }
107  mpz_clear (z);
108}
109
110
111int
112main (int argc, char *argv[])
113{
114  tests_start ();
115
116  check_ref ();
117
118  tests_end ();
119  exit (0);
120}
Note: See TracBrowser for help on using the repository browser.