1/* Test mpf_set_q.
2
3Copyright 2004 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include "gmp-impl.h"
23#include "tests.h"
24
25
26void
27check_one (mpf_ptr got, mpq_srcptr q)
28{
29  mpf_t  n, d;
30
31  mpf_set_q (got, q);
32
33  PTR(n) = PTR(&q->_mp_num);
34  SIZ(n) = SIZ(&q->_mp_num);
35  EXP(n) = ABSIZ(&q->_mp_num);
36
37  PTR(d) = PTR(&q->_mp_den);
38  SIZ(d) = SIZ(&q->_mp_den);
39  EXP(d) = ABSIZ(&q->_mp_den);
40
41  if (! refmpf_validate_division ("mpf_set_q", got, n, d))
42    {
43      mp_trace_base = -16;
44      mpq_trace ("   q", q);
45      abort ();
46    }
47}
48
49void
50check_rand (void)
51{
52  unsigned long  min_prec = __GMPF_BITS_TO_PREC (1);
53  gmp_randstate_ptr  rands = RANDS;
54  unsigned long  prec;
55  mpf_t  got;
56  mpq_t  q;
57  int    i;
58
59  mpf_init (got);
60  mpq_init (q);
61
62  for (i = 0; i < 400; i++)
63    {
64      /* result precision */
65      prec = min_prec + gmp_urandomm_ui (rands, 20L);
66      refmpf_set_prec_limbs (got, prec);
67
68      /* num */
69      prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
70      mpz_rrandomb (mpq_numref(q), rands, prec);
71
72      /* possibly negative num */
73      if (gmp_urandomb_ui (rands, 1L))
74        mpz_neg (mpq_numref(q), mpq_numref(q));
75
76      /* den, non-zero */
77      do {
78        prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
79        mpz_rrandomb (mpq_denref(q), rands, prec);
80      } while (mpz_sgn (mpq_denref(q)) <= 0);
81
82      check_one (got, q);
83    }
84
85  mpf_clear (got);
86  mpq_clear (q);
87}
88
89void
90check_various (void)
91{
92  mpf_t got;
93  mpq_t q;
94
95  mpf_init (got);
96  mpq_init (q);
97
98  /* 1/1 == 1 */
99  mpf_set_prec (got, 20L);
100  mpq_set_ui (q, 1L, 1L);
101  mpf_set_q (got, q);
102  MPF_CHECK_FORMAT (got);
103  ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0);
104
105  /* 1/(2^n+1), a case where truncating the divisor would be wrong */
106  mpf_set_prec (got, 500L);
107  mpq_set_ui (q, 1L, 1L);
108  mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L);
109  mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L);
110  check_one (got, q);
111
112  mpf_clear (got);
113  mpq_clear (q);
114}
115
116int
117main (void)
118{
119  tests_start ();
120
121  check_various ();
122  check_rand ();
123
124  tests_end ();
125  exit (0);
126}
127