1 /* tinterv_q.c -- Test mpfi_interv_q.
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
check(mpfi_ptr i,mpq_srcptr a,mpq_srcptr b,mpfr_srcptr expected_left,mpfr_srcptr expected_right,int expected_inex)30 check (mpfi_ptr i, mpq_srcptr a, mpq_srcptr b,
31        mpfr_srcptr expected_left, mpfr_srcptr expected_right,
32        int expected_inex)
33 {
34   int inex;
35 
36   inex = mpfi_interv_q (i, a, b);
37   if (inex != expected_inex) {
38     printf ("Error: mpfi_interv_q (i, a, b) returns %d instead of %d\n",
39             inex, expected_inex);
40     printf ("precision(i) = %lu\na =", mpfi_get_prec (i));
41     mpq_out_str (stdout, 10, a);
42     printf ("\nb = ");
43     mpq_out_str (stdout, 10, b);
44     printf ("\n");
45     exit (1);
46   }
47   if (!same_mpfr_value (&(i->left), expected_left)
48       || !same_mpfr_value (&(i->right), expected_right)) {
49     printf ("Error: mpfi_interv_q (i, a, b) failed.\n");
50     printf ("\na = ");
51     mpq_out_str (stdout, 10, a);
52     printf ("\nb = ");
53     mpq_out_str (stdout, 10, b);
54     printf ("\ngot    i = ");
55     mpfi_out_str (stdout, 10, 0, i);
56     printf ("\nexpected = [");
57     mpfr_out_str (stdout, 10, 0, expected_left, MPFI_RNDD);
58     printf (", ");
59     mpfr_out_str (stdout, 10, 0, expected_right, MPFI_RNDU);
60     printf ("]\n");
61     exit (1);
62   }
63 }
64 
65 int
main(int argc,char ** argv)66 main (int argc, char **argv)
67 {
68   mpfr_t x, y;
69 
70   mpq_t a, b;
71   mpfi_t i;
72 
73   mpq_init (a);
74   mpq_init (b);
75   mpfi_init2 (i, 53);
76   mpfr_init2 (x, 53);
77   mpfr_init2 (y, 53);
78 
79   mpq_set_si (a, -1, 3);
80   mpq_set_ui (b, +1, 1024);
81   mpfr_set_q (x, a, MPFI_RNDD);
82   mpfr_set_q (y, b, MPFI_RNDU);
83   check (i, a, b, x, y, MPFI_FLAGS_LEFT_ENDPOINT_INEXACT);
84   check (i, b, a, x, y, MPFI_FLAGS_LEFT_ENDPOINT_INEXACT);
85   check (i, b, b, y, y, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT);
86   mpfr_set_q (y, a, MPFI_RNDU);
87   check (i, a, a, x, y, MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT);
88 
89   mpq_clear (a);
90   mpq_clear (b);
91   mpfi_clear (i);
92   mpfr_clear (x);
93   mpfr_clear (y);
94 
95   return 0;
96 }
97