xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpn/t-scan.c (revision 80d9064ac03cbb6a4174695f0d5b237c8766d3d0)
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 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)
30 mp_limb_t  x[SIZE+1];
31 
32 void
33 check (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 
70 void
71 check_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. */
105 void
106 check_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 
122 void
123 check_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 
134 int
135 main (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