Tizen 2.1 base
[external/gmp.git] / tests / mpz / logic.c
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 3 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.  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 #include "tests.h"
26
27 void dump_abort __GMP_PROTO (());
28 void debug_mp __GMP_PROTO ((mpz_t, int));
29
30 int
31 main (int argc, char **argv)
32 {
33   mpz_t x, y, r1, r2;
34   mpz_t t1, t2, t3;
35   mp_size_t xsize, ysize;
36   int i;
37   int reps = 100000;
38   gmp_randstate_ptr rands;
39   mpz_t bs;
40   unsigned long bsi, size_range;
41
42   tests_start ();
43   rands = RANDS;
44
45   mpz_init (bs);
46
47   if (argc == 2)
48      reps = atoi (argv[1]);
49
50   mpz_init (x);
51   mpz_init (y);
52   mpz_init (r1);
53   mpz_init (r2);
54   mpz_init (t1);
55   mpz_init (t2);
56   mpz_init (t3);
57
58   for (i = 0; i < reps; i++)
59     {
60       mpz_urandomb (bs, rands, 32);
61       size_range = mpz_get_ui (bs) % 8 + 2;
62
63       mpz_urandomb (bs, rands, size_range);
64       xsize = mpz_get_ui (bs);
65       mpz_rrandomb (x, rands, xsize);
66       mpz_urandomb (bs, rands, 1);
67       bsi = mpz_get_ui (bs);
68       if ((bsi & 1) != 0)
69         mpz_neg (x, x);
70
71       mpz_urandomb (bs, rands, size_range);
72       ysize = mpz_get_ui (bs);
73       mpz_rrandomb (y, rands, ysize);
74       mpz_urandomb (bs, rands, 1);
75       bsi = mpz_get_ui (bs);
76       if ((bsi & 1) != 0)
77         mpz_neg (y, y);
78
79       mpz_com (r1, x);
80       MPZ_CHECK_FORMAT (r1);
81       mpz_com (r1, r1);
82       MPZ_CHECK_FORMAT (r1);
83       if (mpz_cmp (r1, x) != 0)
84         dump_abort ();
85
86       mpz_com (r1, y);
87       MPZ_CHECK_FORMAT (r1);
88       mpz_com (r2, r1);
89       MPZ_CHECK_FORMAT (r2);
90       if (mpz_cmp (r2, y) != 0)
91         dump_abort ();
92
93       mpz_com (t1, x);
94       MPZ_CHECK_FORMAT (t1);
95       mpz_com (t2, y);
96       MPZ_CHECK_FORMAT (t2);
97       mpz_and (t3, t1, t2);
98       MPZ_CHECK_FORMAT (t3);
99       mpz_com (r1, t3);
100       MPZ_CHECK_FORMAT (r1);
101       mpz_ior (r2, x, y);
102       MPZ_CHECK_FORMAT (r2);
103       if (mpz_cmp (r1, r2) != 0)
104         dump_abort ();
105
106       mpz_com (t1, x);
107       MPZ_CHECK_FORMAT (t1);
108       mpz_com (t2, y);
109       MPZ_CHECK_FORMAT (t2);
110       mpz_ior (t3, t1, t2);
111       MPZ_CHECK_FORMAT (t3);
112       mpz_com (r1, t3);
113       MPZ_CHECK_FORMAT (r1);
114       mpz_and (r2, x, y);
115       MPZ_CHECK_FORMAT (r2);
116       if (mpz_cmp (r1, r2) != 0)
117         dump_abort ();
118
119       mpz_ior (t1, x, y);
120       MPZ_CHECK_FORMAT (t1);
121       mpz_and (t2, x, y);
122       MPZ_CHECK_FORMAT (t2);
123       mpz_com (t3, t2);
124       MPZ_CHECK_FORMAT (t3);
125       mpz_and (r1, t1, t3);
126       MPZ_CHECK_FORMAT (r1);
127       mpz_xor (r2, x, y);
128       MPZ_CHECK_FORMAT (r2);
129       if (mpz_cmp (r1, r2) != 0)
130         dump_abort ();
131     }
132
133   mpz_clear (bs);
134   mpz_clear (x);
135   mpz_clear (y);
136   mpz_clear (r1);
137   mpz_clear (r2);
138   mpz_clear (t1);
139   mpz_clear (t2);
140   mpz_clear (t3);
141
142   tests_end ();
143   exit (0);
144 }
145
146 void
147 dump_abort ()
148 {
149   abort();
150 }
151
152 void
153 debug_mp (mpz_t x, int base)
154 {
155   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
156 }