1 | /* Test mpz_com, mpz_and, mpz_ior, and mpz_xor. |
---|
2 | |
---|
3 | Copyright 1993, 1994, 1996, 1997, 2001 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU MP Library. |
---|
6 | |
---|
7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU Lesser General Public License as published by |
---|
9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
15 | License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | |
---|
25 | #include "gmp.h" |
---|
26 | #include "gmp-impl.h" |
---|
27 | #include "tests.h" |
---|
28 | |
---|
29 | void dump_abort _PROTO (()); |
---|
30 | void debug_mp _PROTO ((mpz_t, int)); |
---|
31 | |
---|
32 | int |
---|
33 | main (int argc, char **argv) |
---|
34 | { |
---|
35 | mpz_t x, y, r1, r2; |
---|
36 | mpz_t t1, t2, t3; |
---|
37 | mp_size_t xsize, ysize; |
---|
38 | int i; |
---|
39 | int reps = 40000; |
---|
40 | gmp_randstate_ptr rands; |
---|
41 | mpz_t bs; |
---|
42 | unsigned long bsi, size_range; |
---|
43 | |
---|
44 | tests_start (); |
---|
45 | rands = RANDS; |
---|
46 | |
---|
47 | mpz_init (bs); |
---|
48 | |
---|
49 | if (argc == 2) |
---|
50 | reps = atoi (argv[1]); |
---|
51 | |
---|
52 | mpz_init (x); |
---|
53 | mpz_init (y); |
---|
54 | mpz_init (r1); |
---|
55 | mpz_init (r2); |
---|
56 | mpz_init (t1); |
---|
57 | mpz_init (t2); |
---|
58 | mpz_init (t3); |
---|
59 | |
---|
60 | for (i = 0; i < reps; i++) |
---|
61 | { |
---|
62 | mpz_urandomb (bs, rands, 32); |
---|
63 | size_range = mpz_get_ui (bs) % 8 + 2; |
---|
64 | |
---|
65 | mpz_urandomb (bs, rands, size_range); |
---|
66 | xsize = mpz_get_ui (bs); |
---|
67 | mpz_rrandomb (x, rands, xsize); |
---|
68 | mpz_urandomb (bs, rands, 1); |
---|
69 | bsi = mpz_get_ui (bs); |
---|
70 | if ((bsi & 1) != 0) |
---|
71 | mpz_neg (x, x); |
---|
72 | |
---|
73 | mpz_urandomb (bs, rands, size_range); |
---|
74 | ysize = mpz_get_ui (bs); |
---|
75 | mpz_rrandomb (y, rands, ysize); |
---|
76 | mpz_urandomb (bs, rands, 1); |
---|
77 | bsi = mpz_get_ui (bs); |
---|
78 | if ((bsi & 1) != 0) |
---|
79 | mpz_neg (y, y); |
---|
80 | |
---|
81 | mpz_com (r1, x); |
---|
82 | MPZ_CHECK_FORMAT (r1); |
---|
83 | mpz_com (r1, r1); |
---|
84 | MPZ_CHECK_FORMAT (r1); |
---|
85 | if (mpz_cmp (r1, x) != 0) |
---|
86 | dump_abort (); |
---|
87 | |
---|
88 | mpz_com (r1, y); |
---|
89 | MPZ_CHECK_FORMAT (r1); |
---|
90 | mpz_com (r2, r1); |
---|
91 | MPZ_CHECK_FORMAT (r2); |
---|
92 | if (mpz_cmp (r2, y) != 0) |
---|
93 | dump_abort (); |
---|
94 | |
---|
95 | mpz_com (t1, x); |
---|
96 | MPZ_CHECK_FORMAT (t1); |
---|
97 | mpz_com (t2, y); |
---|
98 | MPZ_CHECK_FORMAT (t2); |
---|
99 | mpz_and (t3, t1, t2); |
---|
100 | MPZ_CHECK_FORMAT (t3); |
---|
101 | mpz_com (r1, t3); |
---|
102 | MPZ_CHECK_FORMAT (r1); |
---|
103 | mpz_ior (r2, x, y); |
---|
104 | MPZ_CHECK_FORMAT (r2); |
---|
105 | if (mpz_cmp (r1, r2) != 0) |
---|
106 | dump_abort (); |
---|
107 | |
---|
108 | mpz_com (t1, x); |
---|
109 | MPZ_CHECK_FORMAT (t1); |
---|
110 | mpz_com (t2, y); |
---|
111 | MPZ_CHECK_FORMAT (t2); |
---|
112 | mpz_ior (t3, t1, t2); |
---|
113 | MPZ_CHECK_FORMAT (t3); |
---|
114 | mpz_com (r1, t3); |
---|
115 | MPZ_CHECK_FORMAT (r1); |
---|
116 | mpz_and (r2, x, y); |
---|
117 | MPZ_CHECK_FORMAT (r2); |
---|
118 | if (mpz_cmp (r1, r2) != 0) |
---|
119 | dump_abort (); |
---|
120 | |
---|
121 | mpz_ior (t1, x, y); |
---|
122 | MPZ_CHECK_FORMAT (t1); |
---|
123 | mpz_and (t2, x, y); |
---|
124 | MPZ_CHECK_FORMAT (t2); |
---|
125 | mpz_com (t3, t2); |
---|
126 | MPZ_CHECK_FORMAT (t3); |
---|
127 | mpz_and (r1, t1, t3); |
---|
128 | MPZ_CHECK_FORMAT (r1); |
---|
129 | mpz_xor (r2, x, y); |
---|
130 | MPZ_CHECK_FORMAT (r2); |
---|
131 | if (mpz_cmp (r1, r2) != 0) |
---|
132 | dump_abort (); |
---|
133 | } |
---|
134 | |
---|
135 | mpz_clear (bs); |
---|
136 | mpz_clear (x); |
---|
137 | mpz_clear (y); |
---|
138 | mpz_clear (r1); |
---|
139 | mpz_clear (r2); |
---|
140 | mpz_clear (t1); |
---|
141 | mpz_clear (t2); |
---|
142 | mpz_clear (t3); |
---|
143 | |
---|
144 | tests_end (); |
---|
145 | exit (0); |
---|
146 | } |
---|
147 | |
---|
148 | void |
---|
149 | dump_abort () |
---|
150 | { |
---|
151 | abort(); |
---|
152 | } |
---|
153 | |
---|
154 | void |
---|
155 | debug_mp (mpz_t x, int base) |
---|
156 | { |
---|
157 | mpz_out_str (stderr, base, x); fputc ('\n', stderr); |
---|
158 | } |
---|