Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / tmin_prec.c
1 /* Test file for mpfr_min_prec.
2
3 Copyright 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "mpfr-test.h"
27
28 int
29 main (int argc, char *argv[])
30 {
31   mpfr_t x;
32   mpfr_prec_t ret;
33   unsigned long i;
34
35   tests_start_mpfr ();
36
37   mpfr_init2 (x, 53);
38
39   /* Check special values */
40   mpfr_set_nan (x);
41   ret = mpfr_min_prec (x);
42   MPFR_ASSERTN (ret == 0);
43
44   mpfr_set_inf (x, 1);
45   ret = mpfr_min_prec (x);
46   MPFR_ASSERTN (ret == 0);
47
48   mpfr_set_inf (x, -1);
49   ret = mpfr_min_prec (x);
50   MPFR_ASSERTN (ret == 0);
51
52   mpfr_set_ui (x, 0, MPFR_RNDN);
53   ret = mpfr_min_prec (x);
54   MPFR_ASSERTN (ret == 0);
55
56   /* Some constants */
57
58   mpfr_set_ui (x, 1, MPFR_RNDN);
59   ret = mpfr_min_prec (x);
60   MPFR_ASSERTN (ret == 1);
61
62   mpfr_set_ui (x, 17, MPFR_RNDN);
63   ret = mpfr_min_prec (x);
64   MPFR_ASSERTN (ret == 5);
65
66   mpfr_set_ui (x, 42, MPFR_RNDN);
67   ret = mpfr_min_prec (x);
68   MPFR_ASSERTN (ret == 5);
69
70   mpfr_set_prec (x, 256);
71   for (i = 0; i <= 255; i++)
72     {
73       mpfr_set_ui_2exp (x, 1, i, MPFR_RNDN);
74       mpfr_add_ui (x, x, 1, MPFR_RNDN);
75       ret = mpfr_min_prec (x);
76       if (ret != i + 1)
77         {
78           printf ("Error for x = 2^%lu + 1\n", i);
79           printf ("Expected %lu, got %lu\n", i + 1, (unsigned long) ret);
80           exit (1);
81         }
82     }
83
84   for (i = MPFR_PREC_MIN; i <= 255; i++)
85     {
86       mpfr_set_prec (x, i);
87       mpfr_set_ui_2exp (x, 1, i, MPFR_RNDN);
88       mpfr_sub_ui (x, x, 1, MPFR_RNDN);
89       ret = mpfr_min_prec (x);
90       if (ret != i)
91         {
92           printf ("Error for x = 2^%lu - 1\n", i);
93           printf ("Expected %lu, got %lu\n", i, (unsigned long) ret);
94           exit (1);
95         }
96     }
97
98   mpfr_clear (x);
99
100   tests_end_mpfr ();
101   return 0;
102 }