Upload Tizen:Base source
[external/gmp.git] / tests / mpz / dive.c
1 /* Test mpz_mul, mpz_divexact.
2
3 Copyright 1996, 2001, 2002 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "tests.h"
26
27 int
28 main (int argc, char **argv)
29 {
30   mpz_t op1, op2;
31   mpz_t prod, quot;
32   mp_size_t size;
33   int i;
34   int reps = 5000;
35   gmp_randstate_ptr rands;
36   mpz_t bs;
37   unsigned long bsi, size_range;
38
39   tests_start ();
40   TESTS_REPS (reps, argv, argc);
41
42   rands = RANDS;
43
44   mp_trace_base = -16;
45
46   mpz_init (bs);
47
48   mpz_init (op1);
49   mpz_init (op2);
50   mpz_init (prod);
51   mpz_init (quot);
52
53   for (i = 0; i < reps; i++)
54     {
55       mpz_urandomb (bs, rands, 32);
56       size_range = mpz_get_ui (bs) % 17 + 2; /* 0..2047 bit operands */
57
58       mpz_urandomb (bs, rands, size_range);
59       size = mpz_get_ui (bs);
60       mpz_rrandomb (op1, rands, size);
61
62       do
63         {
64           mpz_urandomb (bs, rands, size_range);
65           size = mpz_get_ui (bs);
66           mpz_rrandomb (op2, rands, size);
67         }
68       while (mpz_sgn (op2) == 0);
69
70       mpz_urandomb (bs, rands, 2);
71       bsi = mpz_get_ui (bs);
72       if ((bsi & 1) != 0)
73         mpz_neg (op1, op1);
74       if ((bsi & 2) != 0)
75         mpz_neg (op2, op2);
76
77       mpz_mul (prod, op1, op2);
78
79       mpz_divexact (quot, prod, op2);
80       MPZ_CHECK_FORMAT (quot);
81
82       if (mpz_cmp (quot, op1) != 0)
83         {
84           printf ("Wrong results:\n");
85           mpz_trace ("  got     ", quot);
86           mpz_trace ("  want    ", op1);
87           mpz_trace ("  dividend", prod);
88           mpz_trace ("  divisor ", op2);
89           abort ();
90         }
91     }
92
93   mpz_clear (bs);
94   mpz_clear (op1);
95   mpz_clear (op2);
96   mpz_clear (prod);
97   mpz_clear (quot);
98
99   tests_end ();
100   exit (0);
101 }