xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpn/t-scan.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Test mpn_scan0 and mpn_scan1.
2 
3 Copyright 2002 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include "gmp-impl.h"
24 
25 #include "tests.h"
26 
27 
28 #define SIZE  ((mp_size_t) 3)
29 mp_limb_t  x[SIZE+1];
30 
31 void
check(void)32 check (void)
33 {
34   unsigned long  i, got, want;
35 
36   x[SIZE] = 1;
37   for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
38     {
39       got = refmpn_scan1 (x, i);
40       want = mpn_scan1 (x, i);
41       if (got != want)
42         {
43           printf ("mpn_scan1\n");
44           printf ("  i     %lu\n", i);
45           printf ("  got   %lu\n", got);
46           printf ("  want  %lu\n", want);
47           mpn_trace ("  x    ", x, SIZE);
48           abort ();
49         }
50     }
51 
52   x[SIZE] = 0;
53   for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
54     {
55       got = refmpn_scan0 (x, i);
56       want = mpn_scan0 (x, i);
57       if (got != want)
58         {
59           printf ("mpn_scan0\n");
60           printf ("  i     %lu\n", i);
61           printf ("  got   %lu\n", got);
62           printf ("  want  %lu\n", want);
63           mpn_trace ("  x    ", x, SIZE);
64           abort ();
65         }
66     }
67 }
68 
69 void
check_twobits(void)70 check_twobits (void)
71 {
72 #define TWOBITS(a, b) \
73   ((CNST_LIMB(1) << (a)) | (CNST_LIMB(1) << (b)))
74 
75   refmpn_zero (x, SIZE);
76   x[0] = TWOBITS (1, 0);
77   check ();
78 
79   refmpn_zero (x, SIZE);
80   x[0] = TWOBITS (GMP_NUMB_BITS-1, 1);
81   check ();
82 
83   refmpn_zero (x, SIZE);
84   x[0] = CNST_LIMB(1);
85   x[1] = CNST_LIMB(1);
86   check ();
87 
88   refmpn_zero (x, SIZE);
89   x[0] = CNST_LIMB(1) << (GMP_NUMB_BITS-1);
90   x[1] = CNST_LIMB(1);
91   check ();
92 
93   refmpn_zero (x, SIZE);
94   x[1] = TWOBITS (1, 0);
95   check ();
96 
97   refmpn_zero (x, SIZE);
98   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. */
104 void
check_twobits_exhaustive(void)105 check_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 
121 void
check_rand(void)122 check_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 
133 int
main(void)134 main (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