Blame tests/mpz/t-scan.c

Packit 5c3484
/* Tests of mpz_scan0 and mpz_scan1.
Packit 5c3484
Packit 5c3484
Copyright 2000-2003 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library test suite.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is free software; you can redistribute it
Packit 5c3484
and/or modify it under the terms of the GNU General Public License as
Packit 5c3484
published by the Free Software Foundation; either version 3 of the License,
Packit 5c3484
or (at your option) any later version.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is distributed in the hope that it will be
Packit 5c3484
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5c3484
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 5c3484
Public License for more details.
Packit 5c3484
Packit 5c3484
You should have received a copy of the GNU General Public License along with
Packit 5c3484
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "tests.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
unsigned long
Packit 5c3484
refmpz_scan (mpz_srcptr z, unsigned long i, int sought)
Packit 5c3484
{
Packit 5c3484
  unsigned long  z_bits = (unsigned long) ABSIZ(z) * GMP_NUMB_BITS;
Packit 5c3484
Packit 5c3484
  do
Packit 5c3484
    {
Packit 5c3484
      if (mpz_tstbit (z, i) == sought)
Packit 5c3484
        return i;
Packit 5c3484
      i++;
Packit 5c3484
    }
Packit 5c3484
  while (i <= z_bits);
Packit 5c3484
Packit 5c3484
  return ULONG_MAX;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
unsigned long
Packit 5c3484
refmpz_scan0 (mpz_srcptr z, unsigned long starting_bit)
Packit 5c3484
{
Packit 5c3484
  return refmpz_scan (z, starting_bit, 0);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
unsigned long
Packit 5c3484
refmpz_scan1 (mpz_srcptr z, unsigned long starting_bit)
Packit 5c3484
{
Packit 5c3484
  return refmpz_scan (z, starting_bit, 1);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
check_ref (void)
Packit 5c3484
{
Packit 5c3484
  static const int offset[] = {
Packit 5c3484
    -2, -1, 0, 1, 2, 3
Packit 5c3484
  };
Packit 5c3484
Packit 5c3484
  mpz_t          z;
Packit 5c3484
  int            test, neg, sought, oindex, o;
Packit 5c3484
  mp_size_t      size, isize;
Packit 5c3484
  unsigned long  start, got, want;
Packit 5c3484
Packit 5c3484
  mpz_init (z);
Packit 5c3484
  for (test = 0; test < 5; test++)
Packit 5c3484
    {
Packit 5c3484
      for (size = 0; size < 5; size++)
Packit 5c3484
        {
Packit 5c3484
          mpz_random2 (z, size);
Packit 5c3484
Packit 5c3484
          for (neg = 0; neg <= 1; neg++)
Packit 5c3484
            {
Packit 5c3484
              if (neg)
Packit 5c3484
                mpz_neg (z, z);
Packit 5c3484
Packit 5c3484
              for (isize = 0; isize <= size; isize++)
Packit 5c3484
                {
Packit 5c3484
                  for (oindex = 0; oindex < numberof (offset); oindex++)
Packit 5c3484
                    {
Packit 5c3484
                      o = offset[oindex];
Packit 5c3484
                      if ((int) isize*GMP_NUMB_BITS < -o)
Packit 5c3484
                        continue;  /* start would be negative */
Packit 5c3484
Packit 5c3484
                      start = isize*GMP_NUMB_BITS + o;
Packit 5c3484
Packit 5c3484
                      for (sought = 0; sought <= 1; sought++)
Packit 5c3484
                        {
Packit 5c3484
                          if (sought == 0)
Packit 5c3484
                            {
Packit 5c3484
                              got = mpz_scan0 (z, start);
Packit 5c3484
                              want = refmpz_scan0 (z, start);
Packit 5c3484
                            }
Packit 5c3484
                          else
Packit 5c3484
                            {
Packit 5c3484
                              got = mpz_scan1 (z, start);
Packit 5c3484
                              want = refmpz_scan1 (z, start);
Packit 5c3484
                            }
Packit 5c3484
Packit 5c3484
                          if (got != want)
Packit 5c3484
                            {
Packit 5c3484
                              printf ("wrong at test=%d, size=%ld, neg=%d, start=%lu, sought=%d\n",
Packit 5c3484
                                      test, size, neg, start, sought);
Packit 5c3484
                              printf ("   z 0x");
Packit 5c3484
                              mpz_out_str (stdout, -16, z);
Packit 5c3484
                              printf ("\n");
Packit 5c3484
                              printf ("   got=%lu, want=%lu\n", got, want);
Packit 5c3484
                              exit (1);
Packit 5c3484
                            }
Packit 5c3484
                        }
Packit 5c3484
                    }
Packit 5c3484
                }
Packit 5c3484
            }
Packit 5c3484
        }
Packit 5c3484
    }
Packit 5c3484
  mpz_clear (z);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (int argc, char *argv[])
Packit 5c3484
{
Packit 5c3484
  tests_start ();
Packit 5c3484
Packit 5c3484
  check_ref ();
Packit 5c3484
Packit 5c3484
  tests_end ();
Packit 5c3484
  exit (0);
Packit 5c3484
}