Better ASSERT macro.
[gnulib.git] / tests / test-isnanl.h
1 /* Test of isnanl() substitute.
2    Copyright (C) 2007 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 2, 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 Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #define ASSERT(expr) \
25   do                                                                         \
26     {                                                                        \
27       if (!(expr))                                                           \
28         {                                                                    \
29           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
30           abort ();                                                          \
31         }                                                                    \
32     }                                                                        \
33   while (0)
34
35 int
36 main ()
37 {
38   /* Finite values.  */
39   ASSERT (!isnanl (3.141L));
40   ASSERT (!isnanl (3.141e30L));
41   ASSERT (!isnanl (3.141e-30L));
42   ASSERT (!isnanl (-2.718L));
43   ASSERT (!isnanl (-2.718e30L));
44   ASSERT (!isnanl (-2.718e-30L));
45   /* Infinite values.  */
46   ASSERT (!isnanl (1.0L / 0.0L));
47   ASSERT (!isnanl (-1.0L / 0.0L));
48   /* Quiet NaN.  */
49   ASSERT (isnanl (0.0L / 0.0L));
50 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
51   /* Signalling NaN.  */
52   {
53     #define NWORDS \
54       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
55     typedef union { long double value; unsigned int word[NWORDS]; }
56             memory_long_double;
57     memory_long_double m;
58     m.value = 0.0L / 0.0L;
59 # if LDBL_EXPBIT0_BIT > 0
60     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
61 # else
62     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
63       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
64 # endif
65     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
66       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
67     ASSERT (isnanl (m.value));
68   }
69 #endif
70   return 0;
71 }