1 /* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_tdiv_qr_ui, mpz_tdiv_q_ui,
2 mpz_tdiv_r_ui, mpz_mul_ui.
4 Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
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.
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.
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. */
39 mpz_t quotient, remainder;
40 mpz_t quotient2, remainder2;
42 mp_size_t dividend_size;
48 reps = atoi (argv[1]);
54 mpz_init (remainder2);
57 for (i = 0; i < reps; i++)
59 dividend_size = urandom () % SIZE - SIZE/2;
60 mpz_random2 (dividend, dividend_size);
66 mpz_tdiv_qr_ui (quotient, remainder, dividend, divisor);
67 mpz_tdiv_q_ui (quotient2, dividend, divisor);
68 mpz_tdiv_r_ui (remainder2, dividend, divisor);
70 /* First determine that the quotients and remainders computed
71 with different functions are equal. */
72 if (mpz_cmp (quotient, quotient2) != 0)
73 dump_abort (dividend, divisor);
74 if (mpz_cmp (remainder, remainder2) != 0)
75 dump_abort (dividend, divisor);
77 /* Check if the sign of the quotient is correct. */
78 if (mpz_cmp_ui (quotient, 0) != 0)
79 if ((mpz_cmp_ui (quotient, 0) < 0)
80 != (mpz_cmp_ui (dividend, 0) < 0))
81 dump_abort (dividend, divisor);
83 /* Check if the remainder has the same sign as the dividend
84 (quotient rounded towards 0). */
85 if (mpz_cmp_ui (remainder, 0) != 0)
86 if ((mpz_cmp_ui (remainder, 0) < 0) != (mpz_cmp_ui (dividend, 0) < 0))
87 dump_abort (dividend, divisor);
89 mpz_mul_ui (temp, quotient, divisor);
90 mpz_add (temp, temp, remainder);
91 if (mpz_cmp (temp, dividend) != 0)
92 dump_abort (dividend, divisor);
94 mpz_abs (remainder, remainder);
95 if (mpz_cmp_ui (remainder, divisor) >= 0)
96 dump_abort (dividend, divisor);
102 dump_abort (dividend, divisor)
106 fprintf (stderr, "ERROR\n");
107 fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
108 fprintf (stderr, "divisor = %lX\n", divisor);
116 mpz_out_str (stderr, base, x); fputc ('\n', stderr);