Annotation of OpenXM_contrib/gmp/tests/mpz/t-tdiv.c, Revision 1.1.1.1
1.1 ohara 1: /* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_tdiv_qr, mpz_tdiv_q,
2: mpz_tdiv_r, mpz_mul.
3:
4: Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
5:
6: This file is part of the GNU MP Library.
7:
8: The GNU MP Library is free software; you can redistribute it and/or modify
9: it under the terms of the GNU Lesser General Public License as published by
10: the Free Software Foundation; either version 2.1 of the License, or (at your
11: option) any later version.
12:
13: The GNU MP Library is distributed in the hope that it will be useful, but
14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16: License for more details.
17:
18: You should have received a copy of the GNU Lesser General Public License
19: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21: MA 02111-1307, USA. */
22:
23: #include <stdio.h>
24: #include <stdlib.h>
25:
26: #include "gmp.h"
27: #include "gmp-impl.h"
28: #include "tests.h"
29:
30: void dump_abort _PROTO ((mpz_t, mpz_t));
31: void debug_mp _PROTO ((mpz_t, int));
32:
33: int
34: main (int argc, char **argv)
35: {
36: mpz_t dividend, divisor;
37: mpz_t quotient, remainder;
38: mpz_t quotient2, remainder2;
39: mpz_t temp;
40: mp_size_t dividend_size, divisor_size;
41: int i;
42: int reps = 200;
43: gmp_randstate_ptr rands;
44: mpz_t bs;
45: unsigned long bsi, size_range;
46:
47: tests_start ();
48: rands = RANDS;
49:
50: mpz_init (bs);
51:
52: if (argc == 2)
53: reps = atoi (argv[1]);
54:
55: mpz_init (dividend);
56: mpz_init (divisor);
57: mpz_init (quotient);
58: mpz_init (remainder);
59: mpz_init (quotient2);
60: mpz_init (remainder2);
61: mpz_init (temp);
62:
63: for (i = 0; i < reps; i++)
64: {
65: mpz_urandomb (bs, rands, 32);
66: size_range = mpz_get_ui (bs) % 16 + 2; /* 0..131071 bit operands */
67:
68: do
69: {
70: mpz_urandomb (bs, rands, size_range);
71: divisor_size = mpz_get_ui (bs);
72: mpz_rrandomb (divisor, rands, divisor_size);
73: }
74: while (mpz_sgn (divisor) == 0);
75:
76: mpz_urandomb (bs, rands, size_range);
77: dividend_size = mpz_get_ui (bs) + divisor_size;
78: mpz_rrandomb (dividend, rands, dividend_size);
79:
80: mpz_urandomb (bs, rands, 2);
81: bsi = mpz_get_ui (bs);
82: if ((bsi & 1) != 0)
83: mpz_neg (dividend, dividend);
84: if ((bsi & 2) != 0)
85: mpz_neg (divisor, divisor);
86:
87: /* printf ("%ld %ld\n", SIZ (dividend), SIZ (divisor)); */
88:
89: mpz_tdiv_qr (quotient, remainder, dividend, divisor);
90: mpz_tdiv_q (quotient2, dividend, divisor);
91: mpz_tdiv_r (remainder2, dividend, divisor);
92:
93: /* First determine that the quotients and remainders computed
94: with different functions are equal. */
95: if (mpz_cmp (quotient, quotient2) != 0)
96: dump_abort (dividend, divisor);
97: if (mpz_cmp (remainder, remainder2) != 0)
98: dump_abort (dividend, divisor);
99:
100: /* Check if the sign of the quotient is correct. */
101: if (mpz_cmp_ui (quotient, 0) != 0)
102: if ((mpz_cmp_ui (quotient, 0) < 0)
103: != ((mpz_cmp_ui (dividend, 0) ^ mpz_cmp_ui (divisor, 0)) < 0))
104: dump_abort (dividend, divisor);
105:
106: /* Check if the remainder has the same sign as the dividend
107: (quotient rounded towards 0). */
108: if (mpz_cmp_ui (remainder, 0) != 0)
109: if ((mpz_cmp_ui (remainder, 0) < 0) != (mpz_cmp_ui (dividend, 0) < 0))
110: dump_abort (dividend, divisor);
111:
112: mpz_mul (temp, quotient, divisor);
113: mpz_add (temp, temp, remainder);
114: if (mpz_cmp (temp, dividend) != 0)
115: dump_abort (dividend, divisor);
116:
117: mpz_abs (temp, divisor);
118: mpz_abs (remainder, remainder);
119: if (mpz_cmp (remainder, temp) >= 0)
120: dump_abort (dividend, divisor);
121: }
122:
123: mpz_clear (bs);
124: mpz_clear (dividend);
125: mpz_clear (divisor);
126: mpz_clear (quotient);
127: mpz_clear (remainder);
128: mpz_clear (quotient2);
129: mpz_clear (remainder2);
130: mpz_clear (temp);
131:
132: tests_end ();
133: exit (0);
134: }
135:
136: void
137: dump_abort (mpz_t dividend, mpz_t divisor)
138: {
139: fprintf (stderr, "ERROR\n");
140: fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
141: fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
142: abort();
143: }
144:
145: void
146: debug_mp (mpz_t x, int base)
147: {
148: mpz_out_str (stderr, base, x); fputc ('\n', stderr);
149: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>