1 /* Test mpz_com, mpz_and, mpz_ior, and mpz_xor.
2
3 Copyright 1993, 1994, 1996, 1997, 2001, 2013 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 #include "tests.h"
25
26 void dump_abort (void);
27 void debug_mp (mpz_t, int);
28
29 int
main(int argc,char ** argv)30 main (int argc, char **argv)
31 {
32 mpz_t x, y, r1, r2;
33 mpz_t t1, t2, t3;
34 mp_size_t xsize, ysize;
35 int i;
36 int reps = 100000;
37 gmp_randstate_ptr rands;
38 mpz_t bs;
39 unsigned long bsi, size_range;
40
41 tests_start ();
42 rands = RANDS;
43
44 mpz_init (bs);
45
46 if (argc == 2)
47 reps = atoi (argv[1]);
48
49 mpz_init (x);
50 mpz_init (y);
51 mpz_init (r1);
52 mpz_init (r2);
53 mpz_init (t1);
54 mpz_init (t2);
55 mpz_init (t3);
56
57 mpz_set_si (x, -1);
58 mpz_set_ui (y, 0);
59 for (i = 0; i < 300; i++)
60 {
61 mpz_mul_2exp (x, x, 1);
62
63 mpz_and (r1, x, x);
64 MPZ_CHECK_FORMAT (r1);
65 if (mpz_cmp (r1, x) != 0)
66 dump_abort ();
67
68 mpz_ior (r2, x, x);
69 MPZ_CHECK_FORMAT (r2);
70 if (mpz_cmp (r2, x) != 0)
71 dump_abort ();
72
73 mpz_xor (t1, x, x);
74 MPZ_CHECK_FORMAT (t1);
75 if (mpz_cmp_si (t1, 0) != 0)
76 dump_abort ();
77
78 mpz_ior (t1, x, y);
79 MPZ_CHECK_FORMAT (t1);
80 if (mpz_cmp (t1, x) != 0)
81 dump_abort ();
82
83 mpz_xor (t2, x, y);
84 MPZ_CHECK_FORMAT (t2);
85 if (mpz_cmp (t2, x) != 0)
86 dump_abort ();
87
88 mpz_com (t2, x);
89 MPZ_CHECK_FORMAT (t2);
90 mpz_xor (t3, t2, x);
91 MPZ_CHECK_FORMAT (t3);
92 if (mpz_cmp_si (t3, -1) != 0)
93 dump_abort ();
94 }
95
96 for (i = 0; i < reps; i++)
97 {
98 mpz_urandomb (bs, rands, 32);
99 size_range = mpz_get_ui (bs) % 8 + 2;
100
101 mpz_urandomb (bs, rands, size_range);
102 xsize = mpz_get_ui (bs);
103 mpz_rrandomb (x, rands, xsize);
104 mpz_urandomb (bs, rands, 1);
105 bsi = mpz_get_ui (bs);
106 if ((bsi & 1) != 0)
107 mpz_neg (x, x);
108
109 mpz_urandomb (bs, rands, size_range);
110 ysize = mpz_get_ui (bs);
111 mpz_rrandomb (y, rands, ysize);
112 mpz_urandomb (bs, rands, 1);
113 bsi = mpz_get_ui (bs);
114 if ((bsi & 1) != 0)
115 mpz_neg (y, y);
116
117 mpz_com (r1, x);
118 MPZ_CHECK_FORMAT (r1);
119 mpz_com (r1, r1);
120 MPZ_CHECK_FORMAT (r1);
121 if (mpz_cmp (r1, x) != 0)
122 dump_abort ();
123
124 mpz_com (r1, y);
125 MPZ_CHECK_FORMAT (r1);
126 mpz_com (r2, r1);
127 MPZ_CHECK_FORMAT (r2);
128 if (mpz_cmp (r2, y) != 0)
129 dump_abort ();
130
131 mpz_com (t1, x);
132 MPZ_CHECK_FORMAT (t1);
133 mpz_com (t2, y);
134 MPZ_CHECK_FORMAT (t2);
135 mpz_and (t3, t1, t2);
136 MPZ_CHECK_FORMAT (t3);
137 mpz_com (r1, t3);
138 MPZ_CHECK_FORMAT (r1);
139 mpz_ior (r2, x, y);
140 MPZ_CHECK_FORMAT (r2);
141 if (mpz_cmp (r1, r2) != 0)
142 dump_abort ();
143
144 mpz_com (t1, x);
145 MPZ_CHECK_FORMAT (t1);
146 mpz_com (t2, y);
147 MPZ_CHECK_FORMAT (t2);
148 mpz_ior (t3, t1, t2);
149 MPZ_CHECK_FORMAT (t3);
150 mpz_com (r1, t3);
151 MPZ_CHECK_FORMAT (r1);
152 mpz_and (r2, x, y);
153 MPZ_CHECK_FORMAT (r2);
154 if (mpz_cmp (r1, r2) != 0)
155 dump_abort ();
156
157 mpz_ior (t1, x, y);
158 MPZ_CHECK_FORMAT (t1);
159 mpz_and (t2, x, y);
160 MPZ_CHECK_FORMAT (t2);
161 mpz_com (t3, t2);
162 MPZ_CHECK_FORMAT (t3);
163 mpz_and (r1, t1, t3);
164 MPZ_CHECK_FORMAT (r1);
165 mpz_xor (r2, x, y);
166 MPZ_CHECK_FORMAT (r2);
167 if (mpz_cmp (r1, r2) != 0)
168 dump_abort ();
169 }
170
171 mpz_clear (bs);
172 mpz_clear (x);
173 mpz_clear (y);
174 mpz_clear (r1);
175 mpz_clear (r2);
176 mpz_clear (t1);
177 mpz_clear (t2);
178 mpz_clear (t3);
179
180 tests_end ();
181 exit (0);
182 }
183
184 void
dump_abort()185 dump_abort ()
186 {
187 abort();
188 }
189
190 void
debug_mp(mpz_t x,int base)191 debug_mp (mpz_t x, int base)
192 {
193 mpz_out_str (stderr, base, x); fputc ('\n', stderr);
194 }
195