source: trunk/third/gmp/tests/mpz/t-fdiv.c @ 18191

Revision 18191, 4.1 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_fdiv_qr, mpz_fdiv_q,
2   mpz_fdiv_r, mpz_mul.
3
4Copyright 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 2.1 of the License, or (at your
11option) any later version.
12
13The GNU MP Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
20the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21MA 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
30void dump_abort _PROTO ((mpz_t, mpz_t));
31void debug_mp _PROTO ((mpz_t, int));
32
33int
34main (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_fdiv_qr (quotient, remainder, dividend, divisor);
90      mpz_fdiv_q (quotient2, dividend, divisor);
91      mpz_fdiv_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 divisor
107         (quotient rounded towards minus infinity).  */
108      if (mpz_cmp_ui (remainder, 0) != 0)
109        if ((mpz_cmp_ui (remainder, 0) < 0) != (mpz_cmp_ui (divisor, 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
136void
137dump_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
145void
146debug_mp (mpz_t x, int base)
147{
148  mpz_out_str (stderr, base, x); fputc ('\n', stderr);
149}
Note: See TracBrowser for help on using the repository browser.