xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tcopysign.c (revision d25ffa98a4bfca1fe272f3c182496ec9934faac7)
1 /* Test file for mpfr_copysign, mpfr_setsign and mpfr_signbit.
2 
3 Copyright 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 copysign_variant (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y,
30                   mpfr_rnd_t rnd_mode, int k)
31 {
32   mpfr_clear_flags ();
33   switch (k)
34     {
35     case 0:
36       mpfr_copysign (z, x, y, MPFR_RNDN);
37       return;
38     case 1:
39       (mpfr_copysign) (z, x, y, MPFR_RNDN);
40       return;
41     case 2:
42       mpfr_setsign (z, x, mpfr_signbit (y), MPFR_RNDN);
43       return;
44     case 3:
45       mpfr_setsign (z, x, (mpfr_signbit) (y), MPFR_RNDN);
46       return;
47     case 4:
48       (mpfr_setsign) (z, x, mpfr_signbit (y), MPFR_RNDN);
49       return;
50     case 5:
51       (mpfr_setsign) (z, x, (mpfr_signbit) (y), MPFR_RNDN);
52       return;
53     }
54 }
55 
56 int
57 main (void)
58 {
59   mpfr_t x, y, z;
60   int i, j, k;
61 
62   tests_start_mpfr ();
63 
64   mpfr_init (x);
65   mpfr_init (y);
66   mpfr_init (z);
67 
68   for (i = 0; i <= 1; i++)
69     for (j = 0; j <= 1; j++)
70       for (k = 0; k <= 5; k++)
71         {
72           mpfr_set_nan (x);
73           i ? MPFR_SET_NEG (x) : MPFR_SET_POS (x);
74           mpfr_set_nan (y);
75           j ? MPFR_SET_NEG (y) : MPFR_SET_POS (y);
76           copysign_variant (z, x, y, MPFR_RNDN, k);
77           if (MPFR_SIGN (z) != MPFR_SIGN (y) || !mpfr_nanflag_p ())
78             {
79               printf ("Error in mpfr_copysign (%cNaN, %cNaN)\n",
80                       i ? '-' : '+', j ? '-' : '+');
81               exit (1);
82             }
83 
84           mpfr_set_si (x, i ? -1250 : 1250, MPFR_RNDN);
85           mpfr_set_nan (y);
86           j ? MPFR_SET_NEG (y) : MPFR_SET_POS (y);
87           copysign_variant (z, x, y, MPFR_RNDN, k);
88           if (i != j)
89             mpfr_neg (x, x, MPFR_RNDN);
90           if (! mpfr_equal_p (z, x) || mpfr_nanflag_p ())
91             {
92               printf ("Error in mpfr_copysign (%c1250, %cNaN)\n",
93                       i ? '-' : '+', j ? '-' : '+');
94               exit (1);
95             }
96 
97           mpfr_set_si (x, i ? -1250 : 1250, MPFR_RNDN);
98           mpfr_set_si (y, j ? -1717 : 1717, MPFR_RNDN);
99           copysign_variant (z, x, y, MPFR_RNDN, k);
100           if (i != j)
101             mpfr_neg (x, x, MPFR_RNDN);
102           if (! mpfr_equal_p (z, x) || mpfr_nanflag_p ())
103             {
104               printf ("Error in mpfr_copysign (%c1250, %c1717)\n",
105                       i ? '-' : '+', j ? '-' : '+');
106               exit (1);
107             }
108         }
109 
110   mpfr_clear (x);
111   mpfr_clear (y);
112   mpfr_clear (z);
113 
114   tests_end_mpfr ();
115   return 0;
116 }
117