1267843Sdelphij/* Test mpn_scan0 and mpn_scan1.
2267843Sdelphij
3267843SdelphijCopyright 2002 Free Software Foundation, Inc.
4267843Sdelphij
5267843SdelphijThis file is part of the GNU MP Library test suite.
6267843Sdelphij
7267843SdelphijThe GNU MP Library test suite is free software; you can redistribute it
8267843Sdelphijand/or modify it under the terms of the GNU General Public License as
9267843Sdelphijpublished by the Free Software Foundation; either version 3 of the License,
10267843Sdelphijor (at your option) any later version.
11267843Sdelphij
12267843SdelphijThe GNU MP Library test suite is distributed in the hope that it will be
13267843Sdelphijuseful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14267843SdelphijMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15267843SdelphijPublic License for more details.
16267843Sdelphij
17267843SdelphijYou should have received a copy of the GNU General Public License along with
18267843Sdelphijthe GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19267843Sdelphij
20267843Sdelphij#include <stdio.h>
21267843Sdelphij#include <stdlib.h>
22267843Sdelphij
23267843Sdelphij#include "gmp-impl.h"
24267843Sdelphij
25267843Sdelphij#include "tests.h"
26267843Sdelphij
27267843Sdelphij
28267843Sdelphij#define SIZE  ((mp_size_t) 3)
29267843Sdelphijmp_limb_t  x[SIZE+1];
30267843Sdelphij
31267843Sdelphijvoid
32267843Sdelphijcheck (void)
33267843Sdelphij{
34267843Sdelphij  unsigned long  i, got, want;
35267843Sdelphij
36267843Sdelphij  x[SIZE] = 1;
37267843Sdelphij  for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
38267843Sdelphij    {
39267843Sdelphij      got = refmpn_scan1 (x, i);
40267843Sdelphij      want = mpn_scan1 (x, i);
41267843Sdelphij      if (got != want)
42267843Sdelphij        {
43267843Sdelphij          printf ("mpn_scan1\n");
44267843Sdelphij          printf ("  i     %lu\n", i);
45267843Sdelphij          printf ("  got   %lu\n", got);
46267843Sdelphij          printf ("  want  %lu\n", want);
47267843Sdelphij          mpn_trace ("  x    ", x, SIZE);
48267843Sdelphij          abort ();
49267843Sdelphij        }
50267843Sdelphij    }
51267843Sdelphij
52267843Sdelphij  x[SIZE] = 0;
53267843Sdelphij  for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
54267843Sdelphij    {
55267843Sdelphij      got = refmpn_scan0 (x, i);
56267843Sdelphij      want = mpn_scan0 (x, i);
57267843Sdelphij      if (got != want)
58267843Sdelphij        {
59267843Sdelphij          printf ("mpn_scan0\n");
60267843Sdelphij          printf ("  i     %lu\n", i);
61267843Sdelphij          printf ("  got   %lu\n", got);
62267843Sdelphij          printf ("  want  %lu\n", want);
63267843Sdelphij          mpn_trace ("  x    ", x, SIZE);
64267843Sdelphij          abort ();
65267843Sdelphij        }
66267843Sdelphij    }
67267843Sdelphij}
68267843Sdelphij
69267843Sdelphijvoid
70267843Sdelphijcheck_twobits (void)
71267843Sdelphij{
72267843Sdelphij#define TWOBITS(a, b) \
73267843Sdelphij  ((CNST_LIMB(1) << (a)) | (CNST_LIMB(1) << (b)))
74267843Sdelphij
75267843Sdelphij  refmpn_zero (x, SIZE);
76267843Sdelphij  x[0] = TWOBITS (1, 0);
77267843Sdelphij  check ();
78267843Sdelphij
79267843Sdelphij  refmpn_zero (x, SIZE);
80267843Sdelphij  x[0] = TWOBITS (GMP_NUMB_BITS-1, 1);
81267843Sdelphij  check ();
82267843Sdelphij
83267843Sdelphij  refmpn_zero (x, SIZE);
84267843Sdelphij  x[0] = CNST_LIMB(1);
85267843Sdelphij  x[1] = CNST_LIMB(1);
86267843Sdelphij  check ();
87267843Sdelphij
88267843Sdelphij  refmpn_zero (x, SIZE);
89267843Sdelphij  x[0] = CNST_LIMB(1) << (GMP_NUMB_BITS-1);
90267843Sdelphij  x[1] = CNST_LIMB(1);
91267843Sdelphij  check ();
92267843Sdelphij
93267843Sdelphij  refmpn_zero (x, SIZE);
94267843Sdelphij  x[1] = TWOBITS (1, 0);
95267843Sdelphij  check ();
96267843Sdelphij
97267843Sdelphij  refmpn_zero (x, SIZE);
98267843Sdelphij  x[1] = CNST_LIMB(1);
99  x[2] = CNST_LIMB(1);
100  check ();
101}
102
103/* This is unused, it takes too long, especially on 64-bit systems. */
104void
105check_twobits_exhaustive (void)
106{
107  unsigned long  i, j;
108
109  for (i = 0; i < GMP_NUMB_BITS * SIZE; i++)
110    {
111      for (j = 0; j < GMP_NUMB_BITS * SIZE; j++)
112        {
113          refmpn_zero (x, SIZE);
114          refmpn_setbit (x, i);
115          refmpn_setbit (x, j);
116          check ();
117        }
118    }
119}
120
121void
122check_rand (void)
123{
124  int  i;
125
126  for (i = 0; i < 100; i++)
127    {
128      refmpn_random2 (x, SIZE);
129      check ();
130    }
131}
132
133int
134main (void)
135{
136  mp_trace_base = -16;
137  tests_start ();
138
139  check_twobits ();
140  check_rand ();
141
142  tests_end ();
143  exit (0);
144}
145