Annotation of OpenXM/src/kan96xx/gmp-2.0.2/mpz/tests/t-tdiv.c, Revision 1.1.1.1
1.1 maekawa 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 (C) 1991, 1993, 1994, 1996 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 Library General Public License as published by
10: the Free Software Foundation; either version 2 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 Library General Public
16: License for more details.
17:
18: You should have received a copy of the GNU Library 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 "gmp.h"
25: #include "gmp-impl.h"
26: #include "urandom.h"
27:
28: void debug_mp ();
29:
30: #ifndef SIZE
31: #define SIZE 16
32: #endif
33:
34: main (argc, argv)
35: int argc;
36: char **argv;
37: {
38: mpz_t dividend, divisor;
39: mpz_t quotient, remainder;
40: mpz_t quotient2, remainder2;
41: mpz_t temp;
42: mp_size_t dividend_size, divisor_size;
43: int i;
44: int reps = 100000;
45:
46: if (argc == 2)
47: reps = atoi (argv[1]);
48:
49: mpz_init (dividend);
50: mpz_init (divisor);
51: mpz_init (quotient);
52: mpz_init (remainder);
53: mpz_init (quotient2);
54: mpz_init (remainder2);
55: mpz_init (temp);
56:
57: for (i = 0; i < reps; i++)
58: {
59: dividend_size = urandom () % SIZE - SIZE/2;
60: mpz_random2 (dividend, dividend_size);
61:
62: divisor_size = urandom () % SIZE - SIZE/2;
63: mpz_random2 (divisor, divisor_size);
64: if (mpz_cmp_ui (divisor, 0) == 0)
65: continue;
66:
67: mpz_tdiv_qr (quotient, remainder, dividend, divisor);
68: mpz_tdiv_q (quotient2, dividend, divisor);
69: mpz_tdiv_r (remainder2, dividend, divisor);
70:
71: /* First determine that the quotients and remainders computed
72: with different functions are equal. */
73: if (mpz_cmp (quotient, quotient2) != 0)
74: dump_abort (dividend, divisor);
75: if (mpz_cmp (remainder, remainder2) != 0)
76: dump_abort (dividend, divisor);
77:
78: /* Check if the sign of the quotient is correct. */
79: if (mpz_cmp_ui (quotient, 0) != 0)
80: if ((mpz_cmp_ui (quotient, 0) < 0)
81: != ((mpz_cmp_ui (dividend, 0) ^ mpz_cmp_ui (divisor, 0)) < 0))
82: dump_abort (dividend, divisor);
83:
84: /* Check if the remainder has the same sign as the dividend
85: (quotient rounded towards 0). */
86: if (mpz_cmp_ui (remainder, 0) != 0)
87: if ((mpz_cmp_ui (remainder, 0) < 0) != (mpz_cmp_ui (dividend, 0) < 0))
88: dump_abort (dividend, divisor);
89:
90: mpz_mul (temp, quotient, divisor);
91: mpz_add (temp, temp, remainder);
92: if (mpz_cmp (temp, dividend) != 0)
93: dump_abort (dividend, divisor);
94:
95: mpz_abs (temp, divisor);
96: mpz_abs (remainder, remainder);
97: if (mpz_cmp (remainder, temp) >= 0)
98: dump_abort (dividend, divisor);
99: }
100:
101: exit (0);
102: }
103:
104: dump_abort (dividend, divisor)
105: mpz_t dividend, divisor;
106: {
107: fprintf (stderr, "ERROR\n");
108: fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
109: fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
110: abort();
111: }
112:
113: void
114: debug_mp (x, base)
115: mpz_t x;
116: {
117: mpz_out_str (stderr, base, x); fputc ('\n', stderr);
118: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>