Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libgmp / mpz / tests / t-fdiv_ui.c
1 /* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_fdiv_qr_ui, mpz_fdiv_q_ui,
2    mpz_fdiv_r_ui, mpz_mul, mpz_mul_ui.
3
4 Copyright (C) 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;
39   mpz_t quotient, remainder;
40   mpz_t quotient2, remainder2;
41   mpz_t temp;
42   mp_size_t dividend_size;
43   mp_limb_t divisor;
44   int i;
45   int reps = 100000;
46
47   if (argc == 2)
48      reps = atoi (argv[1]);
49
50   mpz_init (dividend);
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 = urandom ();
63       if (divisor == 0)
64         continue;
65
66       mpz_fdiv_qr_ui (quotient, remainder, dividend, divisor);
67       mpz_fdiv_q_ui (quotient2, dividend, divisor);
68       mpz_fdiv_r_ui (remainder2, dividend, divisor);
69
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);
76
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);
82
83       /* Check if the remainder has the same sign as the divisor
84          (quotient rounded towards minus infinity).  */
85       if (mpz_cmp_ui (remainder, 0) != 0)
86         if (mpz_cmp_ui (remainder, 0) < 0)
87           dump_abort (dividend, divisor);
88
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);
93
94       mpz_abs (remainder, remainder);
95       if (mpz_cmp_ui (remainder, divisor) >= 0)
96         dump_abort (dividend, divisor);
97     }
98
99   exit (0);
100 }
101
102 dump_abort (dividend, divisor)
103      mpz_t dividend;
104      mp_limb_t divisor;
105 {
106   fprintf (stderr, "ERROR\n");
107   fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
108   fprintf (stderr, "divisor  = %lX\n", divisor);
109   abort();
110 }
111
112 void
113 debug_mp (x, base)
114      mpz_t x;
115 {
116   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
117 }