1 /* tmag.c -- Test mpfi_mag.
2 
3 Copyright 2010,
4                      Spaces project, Inria Lorraine
5                      and Salsa project, INRIA Rocquencourt,
6                      and Arenaire project, Inria Rhone-Alpes, France
7                      and Lab. ANO, USTL (Univ. of Lille),  France
8 
9 
10 This file is part of the MPFI Library.
11 
12 The MPFI Library is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or (at your
15 option) any later version.
16 
17 The MPFI Library is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20 License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public License
23 along with the MPFI Library; see the file COPYING.LIB.  If not, write to
24 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
25 MA 02110-1301, USA. */
26 
27 #include "mpfi-tests.h"
28 
29 void
test_random(mpfr_prec_t prec_min,mpfr_prec_t prec_max)30 test_random (mpfr_prec_t prec_min, mpfr_prec_t prec_max)
31 {
32   mpfr_t x;
33   mpfi_t i;
34   mpfr_prec_t prec;
35   int dl, dr, d0;
36   int ret;
37 
38   mpfr_init2 (x, prec_max);
39   mpfi_init2 (i, prec_max);
40 
41   for (prec = prec_min; prec < prec_max; prec++) {
42     mpfi_set_prec (i, prec);
43     mpfr_set_prec (x, prec);
44     random_interval (i);
45     ret = mpfi_mag (x, i);
46     dl = mpfr_cmp_abs (x, &(i->left));
47     dr = mpfr_cmp_abs (x, &(i->right));
48     if (dl < 0 || dr < 0) {
49       printf ("Error: mpfi_mag(x, I) returns a value x less than some "
50               "elements in I.\nI = ");
51       mpfi_out_str (stdout, 10, 0, i);
52       printf ("\nx = ");
53       mpfr_out_str (stdout, 10, 0, x, MPFI_RNDD);
54       printf ("\n");
55       exit (1);
56     }
57     d0 = mpfr_cmp_ui (x, 0);
58     if (d0 < 0) {
59       printf ("Error: mpfi_mag(x, I) returns a negative value.\nI = ");
60       mpfi_out_str (stdout, 10, 0, i);
61       printf ("\nx = ");
62       mpfr_out_str (stdout, 10, 0, x, MPFI_RNDD);
63       printf ("\n");
64       exit (1);
65     }
66     if (dl != 0 && dr != 0 && d0 != 0) {
67       printf ("Error: mpfi_mag(x, I) returns a value x that is not an "
68               "endpoint of abs(I).\nI = ");
69       mpfi_out_str (stdout, 10, 0, i);
70       printf ("\nx = ");
71       mpfr_out_str (stdout, 10, 0, x, MPFI_RNDD);
72       printf ("\n");
73       exit (1);
74     }
75     if (ret != 0) {
76       printf ("Error: mpfi_mag(x, I) returns a nonexact value x while the "
77               "precisions of x and I are equal.\nprecision(I) = %lu, I = ",
78               mpfi_get_prec (i));
79       mpfi_out_str (stdout, 10, 0, i);
80       printf ("\nprecision(x) = %lu, x = ", mpfr_get_prec (x));
81       mpfr_out_str (stdout, 10, 0, x, MPFI_RNDD);
82       printf ("\n");
83       exit (1);
84     }
85   }
86 
87   mpfr_clear (x);
88   mpfi_clear (i);
89 }
90 
91 int
main(int argc,char ** argv)92 main (int argc, char **argv)
93 {
94   struct mpfi_function_t i_mag;
95 
96   mpfi_fun_init_RI (&i_mag, mpfi_mag, NULL);
97 
98   test_start ();
99 
100   check_data (&i_mag, "mag.dat");
101   test_random (2, 1023);
102 
103   test_end ();
104   mpfi_fun_clear (&i_mag);
105 
106   return 0;
107 }
108