tests: add signature checks
[gnulib.git] / tests / test-roundl.c
1 /* Test of rounding to nearest, breaking ties away from zero.
2    Copyright (C) 2007-2009 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Ben Pfaff <blp@gnu.org>, 2007.
19    Based heavily on Bruno Haible's test-truncl.c. */
20
21 #include <config.h>
22
23 #include <math.h>
24
25 #include "signature.h"
26 SIGNATURE_CHECK (roundl, long double, (long double));
27
28 #include <float.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "fpucw.h"
33 #include "isnanl-nolibm.h"
34 #include "nan.h"
35
36 #define ASSERT(expr) \
37   do                                                                         \
38     {                                                                        \
39       if (!(expr))                                                           \
40         {                                                                    \
41           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42           fflush (stderr);                                                   \
43           abort ();                                                          \
44         }                                                                    \
45     }                                                                        \
46   while (0)
47
48 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
49    So we use minus_zero instead.
50    IRIX cc can't put -0.0L into .data, but can compute at runtime.
51    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
52    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
53 #if defined __hpux || defined __sgi
54 static long double
55 compute_minus_zero (void)
56 {
57   return -LDBL_MIN * LDBL_MIN;
58 }
59 # define minus_zero compute_minus_zero ()
60 #else
61 long double minus_zero = -0.0L;
62 #endif
63
64 int
65 main ()
66 {
67   DECL_LONG_DOUBLE_ROUNDING
68
69   BEGIN_LONG_DOUBLE_ROUNDING ();
70
71   /* Zero.  */
72   ASSERT (roundl (0.0L) == 0.0L);
73   ASSERT (roundl (minus_zero) == 0.0L);
74   /* Positive numbers.  */
75   ASSERT (roundl (0.3L) == 0.0L);
76   ASSERT (roundl (0.5L) == 1.0L);
77   ASSERT (roundl (0.7L) == 1.0L);
78   ASSERT (roundl (1.0L) == 1.0L);
79   ASSERT (roundl (1.5L) == 2.0L);
80   ASSERT (roundl (2.5L) == 3.0L);
81   ASSERT (roundl (1.999L) == 2.0L);
82   ASSERT (roundl (2.0L) == 2.0L);
83   ASSERT (roundl (65535.999L) == 65536.0L);
84   ASSERT (roundl (65536.0L) == 65536.0L);
85   ASSERT (roundl (65536.001L) == 65536.0L);
86   ASSERT (roundl (2.341e31L) == 2.341e31L);
87   /* Negative numbers.  */
88   ASSERT (roundl (-0.3L) == 0.0L);
89   ASSERT (roundl (-0.5L) == -1.0L);
90   ASSERT (roundl (-0.7L) == -1.0L);
91   ASSERT (roundl (-1.0L) == -1.0L);
92   ASSERT (roundl (-1.5L) == -2.0L);
93   ASSERT (roundl (-2.5L) == -3.0L);
94   ASSERT (roundl (-1.999L) == -2.0L);
95   ASSERT (roundl (-2.0L) == -2.0L);
96   ASSERT (roundl (-65535.999L) == -65536.0L);
97   ASSERT (roundl (-65536.0L) == -65536.0L);
98   ASSERT (roundl (-65536.001L) == -65536.0L);
99   ASSERT (roundl (-2.341e31L) == -2.341e31L);
100   /* Infinite numbers.  */
101   ASSERT (roundl (1.0 / 0.0L) == 1.0 / 0.0L);
102   ASSERT (roundl (-1.0 / 0.0L) == -1.0 / 0.0L);
103   /* NaNs.  */
104   ASSERT (isnanl (roundl (NaNl ())));
105
106   return 0;
107 }