Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / tsgn.c
1 /* tsgn -- Test for the sign of a floating point number.
2
3 Copyright 2003, 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 static void
29 check_special (void)
30 {
31   mpfr_t x;
32   int ret = 0;
33
34   mpfr_init (x);
35   MPFR_SET_ZERO (x);
36   if ((mpfr_sgn) (x) != 0 || mpfr_sgn (x) != 0)
37     {
38       printf("Sgn error for 0.\n");
39       ret = 1;
40     }
41   MPFR_SET_INF (x);
42   MPFR_SET_POS (x);
43   if ((mpfr_sgn) (x) != 1 || mpfr_sgn (x) != 1)
44     {
45       printf("Sgn error for +Inf.\n");
46       ret = 1;
47     }
48   MPFR_SET_INF (x);
49   MPFR_SET_NEG (x);
50   if ((mpfr_sgn) (x) != -1 || mpfr_sgn (x) != -1)
51     {
52       printf("Sgn error for -Inf.\n");
53       ret = 1;
54     }
55   MPFR_SET_NAN (x);
56   mpfr_clear_flags ();
57   if ((mpfr_sgn) (x) != 0 || !mpfr_erangeflag_p ())
58     {
59       printf("Sgn error for NaN.\n");
60       ret = 1;
61     }
62   mpfr_clear_flags ();
63   if (mpfr_sgn (x) != 0 || !mpfr_erangeflag_p ())
64     {
65       printf("Sgn error for NaN.\n");
66       ret = 1;
67     }
68   mpfr_clear (x);
69   if (ret)
70     exit (ret);
71 }
72
73 static void
74 check_sgn(void)
75 {
76   mpfr_t x;
77   int i, s1, s2;
78
79   mpfr_init(x);
80   for(i = 0 ; i < 100 ; i++)
81     {
82       mpfr_urandomb (x, RANDS);
83       if (i&1)
84         {
85           MPFR_SET_POS(x);
86           s2 = 1;
87         }
88       else
89         {
90           MPFR_SET_NEG(x);
91           s2 = -1;
92         }
93       s1 = mpfr_sgn(x);
94       if (s1 < -1 || s1 > 1)
95         {
96           printf("Error for sgn: out of range.\n");
97           goto lexit;
98         }
99       else if (MPFR_IS_NAN(x) || MPFR_IS_ZERO(x))
100         {
101           if (s1 != 0)
102             {
103               printf("Error for sgn: Nan or Zero should return 0.\n");
104               goto lexit;
105             }
106         }
107       else if (s1 != s2)
108         {
109           printf("Error for sgn. Return %d instead of %d.\n", s1, s2);
110           goto lexit;
111         }
112     }
113   mpfr_clear(x);
114   return;
115
116  lexit:
117   mpfr_clear(x);
118   exit(1);
119 }
120
121 int
122 main (int argc, char *argv[])
123 {
124   tests_start_mpfr ();
125
126   check_special ();
127   check_sgn ();
128
129   tests_end_mpfr ();
130   return 0;
131 }