1/* Test mpn_scan0 and mpn_scan1.
2
3Copyright 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 3 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.  If not, see http://www.gnu.org/licenses/.  */
19
20#include <stdio.h>
21#include <stdlib.h>
22
23#include "gmp.h"
24#include "gmp-impl.h"
25
26#include "tests.h"
27
28
29#define SIZE  ((mp_size_t) 3)
30mp_limb_t  x[SIZE+1];
31
32void
33check (void)
34{
35  unsigned long  i, got, want;
36
37  x[SIZE] = 1;
38  for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
39    {
40      got = refmpn_scan1 (x, i);
41      want = mpn_scan1 (x, i);
42      if (got != want)
43        {
44          printf ("mpn_scan1\n");
45          printf ("  i     %lu\n", i);
46          printf ("  got   %lu\n", got);
47          printf ("  want  %lu\n", want);
48          mpn_trace ("  x    ", x, SIZE);
49          abort ();
50        }
51    }
52
53  x[SIZE] = 0;
54  for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
55    {
56      got = refmpn_scan0 (x, i);
57      want = mpn_scan0 (x, i);
58      if (got != want)
59        {
60          printf ("mpn_scan0\n");
61          printf ("  i     %lu\n", i);
62          printf ("  got   %lu\n", got);
63          printf ("  want  %lu\n", want);
64          mpn_trace ("  x    ", x, SIZE);
65          abort ();
66        }
67    }
68}
69
70void
71check_twobits (void)
72{
73#define TWOBITS(a, b) \
74  ((CNST_LIMB(1) << (a)) | (CNST_LIMB(1) << (b)))
75
76  refmpn_zero (x, SIZE);
77  x[0] = TWOBITS (1, 0);
78  check ();
79
80  refmpn_zero (x, SIZE);
81  x[0] = TWOBITS (GMP_NUMB_BITS-1, 1);
82  check ();
83
84  refmpn_zero (x, SIZE);
85  x[0] = CNST_LIMB(1);
86  x[1] = CNST_LIMB(1);
87  check ();
88
89  refmpn_zero (x, SIZE);
90  x[0] = CNST_LIMB(1) << (GMP_NUMB_BITS-1);
91  x[1] = CNST_LIMB(1);
92  check ();
93
94  refmpn_zero (x, SIZE);
95  x[1] = TWOBITS (1, 0);
96  check ();
97
98  refmpn_zero (x, SIZE);
99  x[1] = CNST_LIMB(1);
100  x[2] = CNST_LIMB(1);
101  check ();
102}
103
104/* This is unused, it takes too long, especially on 64-bit systems. */
105void
106check_twobits_exhaustive (void)
107{
108  unsigned long  i, j;
109
110  for (i = 0; i < GMP_NUMB_BITS * SIZE; i++)
111    {
112      for (j = 0; j < GMP_NUMB_BITS * SIZE; j++)
113        {
114          refmpn_zero (x, SIZE);
115          refmpn_setbit (x, i);
116          refmpn_setbit (x, j);
117          check ();
118        }
119    }
120}
121
122void
123check_rand (void)
124{
125  int  i;
126
127  for (i = 0; i < 100; i++)
128    {
129      refmpn_random2 (x, SIZE);
130      check ();
131    }
132}
133
134int
135main (void)
136{
137  mp_trace_base = -16;
138  tests_start ();
139
140  check_twobits ();
141  check_rand ();
142
143  tests_end ();
144  exit (0);
145}
146