~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/tests/mpf/t-set_q.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2007-04-09 11:51:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070409115151-ql8cr0kalzx1jmla
Tags: 0.9i-20070324-2
Upload to unstable. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test mpf_set_q.
 
2
 
 
3
Copyright 2004 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 2.1 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; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
MA 02110-1301, USA. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include "gmp.h"
 
25
#include "gmp-impl.h"
 
26
#include "tests.h"
 
27
 
 
28
 
 
29
void
 
30
check_one (mpf_ptr got, mpq_srcptr q)
 
31
{
 
32
  mpf_t  n, d;
 
33
 
 
34
  mpf_set_q (got, q);
 
35
 
 
36
  PTR(n) = PTR(&q->_mp_num);
 
37
  SIZ(n) = SIZ(&q->_mp_num);
 
38
  EXP(n) = ABSIZ(&q->_mp_num);
 
39
 
 
40
  PTR(d) = PTR(&q->_mp_den);
 
41
  SIZ(d) = SIZ(&q->_mp_den);
 
42
  EXP(d) = ABSIZ(&q->_mp_den);
 
43
 
 
44
  if (! refmpf_validate_division ("mpf_set_q", got, n, d))
 
45
    {
 
46
      mp_trace_base = -16;
 
47
      mpq_trace ("   q", q);
 
48
      abort ();
 
49
    }
 
50
}
 
51
 
 
52
void
 
53
check_rand (void)
 
54
{
 
55
  unsigned long  min_prec = __GMPF_BITS_TO_PREC (1);
 
56
  gmp_randstate_ptr  rands = RANDS;
 
57
  unsigned long  prec;
 
58
  mpf_t  got;
 
59
  mpq_t  q;
 
60
  int    i;
 
61
 
 
62
  mpf_init (got);
 
63
  mpq_init (q);
 
64
 
 
65
  for (i = 0; i < 400; i++)
 
66
    {
 
67
      /* result precision */
 
68
      prec = min_prec + gmp_urandomm_ui (rands, 20L);
 
69
      refmpf_set_prec_limbs (got, prec);
 
70
 
 
71
      /* num */
 
72
      prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
 
73
      mpz_rrandomb (mpq_numref(q), rands, prec);
 
74
 
 
75
      /* possibly negative num */
 
76
      if (gmp_urandomb_ui (rands, 1L))
 
77
        mpz_neg (mpq_numref(q), mpq_numref(q));
 
78
 
 
79
      /* den, non-zero */
 
80
      do {
 
81
        prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
 
82
        mpz_rrandomb (mpq_denref(q), rands, prec);
 
83
      } while (mpz_sgn (mpq_denref(q)) <= 0);
 
84
 
 
85
      check_one (got, q);
 
86
    }
 
87
 
 
88
  mpf_clear (got);
 
89
  mpq_clear (q);
 
90
}
 
91
 
 
92
void
 
93
check_various (void)
 
94
{
 
95
  mpf_t got;
 
96
  mpq_t q;
 
97
 
 
98
  mpf_init (got);
 
99
  mpq_init (q);
 
100
 
 
101
  /* 1/1 == 1 */
 
102
  mpf_set_prec (got, 20L);
 
103
  mpq_set_ui (q, 1L, 1L);
 
104
  mpf_set_q (got, q);
 
105
  MPF_CHECK_FORMAT (got);
 
106
  ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0);
 
107
 
 
108
  /* 1/(2^n+1), a case where truncating the divisor would be wrong */
 
109
  mpf_set_prec (got, 500L);
 
110
  mpq_set_ui (q, 1L, 1L);
 
111
  mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L);
 
112
  mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L);
 
113
  check_one (got, q);
 
114
 
 
115
  mpf_clear (got);
 
116
  mpq_clear (q);
 
117
}
 
118
 
 
119
int
 
120
main (void)
 
121
{
 
122
  tests_start ();
 
123
 
 
124
  check_various ();
 
125
  check_rand ();
 
126
 
 
127
  tests_end ();
 
128
  exit (0);
 
129
}