Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / ttanh.c
1 /* Test file for mpfr_tanh.
2
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 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 #define TEST_FUNCTION mpfr_tanh
29 #define TEST_RANDOM_EMIN -36
30 #define TEST_RANDOM_EMAX 36
31 #include "tgeneric.c"
32
33 static void
34 special (void)
35 {
36   mpfr_t x;
37
38   mpfr_init (x);
39
40   mpfr_set_nan (x);
41   mpfr_tanh (x, x, MPFR_RNDN);
42   MPFR_ASSERTN(mpfr_nan_p (x));
43
44   mpfr_set_inf (x, 1);
45   mpfr_tanh (x, x, MPFR_RNDN);
46   MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0);
47
48   mpfr_set_inf (x, -1);
49   mpfr_tanh (x, x, MPFR_RNDN);
50   MPFR_ASSERTN(mpfr_cmp_si (x, -1) == 0);
51
52   mpfr_set_prec (x, 10);
53   mpfr_set_str_binary (x, "-0.1001011001");
54   mpfr_tanh (x, x, MPFR_RNDN);
55   MPFR_ASSERTN(mpfr_cmp_si_2exp (x, -135, -8) == 0);
56
57   mpfr_clear (x);
58 }
59
60 static void
61 special_overflow (void)
62 {
63   mpfr_t x, y;
64   int i;
65   mpfr_exp_t emin, emax;
66
67   emin = mpfr_get_emin ();
68   emax = mpfr_get_emax ();
69
70   mpfr_clear_overflow ();
71   set_emin (-125);
72   set_emax (128);
73   mpfr_init2 (x, 24);
74   mpfr_init2 (y, 24);
75
76   mpfr_set_str_binary (x, "0.101100100000000000110100E7");
77   i = mpfr_tanh (y, x, MPFR_RNDN);
78   if (mpfr_cmp_ui (y, 1) || i != 1)
79     {
80       printf("Overflow error (1). i=%d\ny=", i);
81       mpfr_dump (y);
82       exit (1);
83     }
84   MPFR_ASSERTN (!mpfr_overflow_p ());
85
86   i = mpfr_tanh (y, x, MPFR_RNDZ);
87   if (mpfr_cmp_str (y, "0.111111111111111111111111E0", 2, MPFR_RNDN)
88       || i != -1)
89     {
90       printf("Overflow error (2).i=%d\ny=", i);
91       mpfr_dump (y);
92       exit (1);
93     }
94   MPFR_ASSERTN (!mpfr_overflow_p ());
95
96   set_emin (emin);
97   set_emax (emax);
98
99   mpfr_set_str_binary (x, "0.1E1000000000");
100   i = mpfr_tanh (y, x, MPFR_RNDN);
101   if (mpfr_cmp_ui (y, 1) || i != 1)
102     {
103       printf("Overflow error (3). i=%d\ny=", i);
104       mpfr_dump (y);
105       exit (1);
106     }
107   MPFR_ASSERTN (!mpfr_overflow_p ());
108   mpfr_set_str_binary (x, "-0.1E1000000000");
109   i = mpfr_tanh (y, x, MPFR_RNDU);
110   if (mpfr_cmp_str (y, "-0.111111111111111111111111E0", 2, MPFR_RNDN)
111       || i != 1)
112     {
113       printf("Overflow error (4). i=%d\ny=", i);
114       mpfr_dump (y);
115       exit (1);
116     }
117
118   mpfr_clear (y);
119   mpfr_clear (x);
120 }
121
122 int
123 main (int argc, char *argv[])
124 {
125   tests_start_mpfr ();
126
127   special_overflow ();
128   special ();
129
130   test_generic (2, 100, 100);
131
132   data_check ("data/tanh", mpfr_tanh, "mpfr_tanh");
133   bad_cases (mpfr_tanh, mpfr_atanh, "mpfr_tanh", 256, -128, 0,
134              4, 128, 800, 100);
135
136   tests_end_mpfr ();
137   return 0;
138 }