Tests for module 'isnanl-nolibm'.
[gnulib.git] / tests / test-isnanl.c
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 <config.h>
21
22 #if HAVE_LONG_DOUBLE
23
24 #include "isnanl.h"
25
26 #include <limits.h>
27 #include <stdlib.h>
28
29 #define ASSERT(expr) if (!(expr)) abort ();
30
31 int
32 main ()
33 {
34   /* Finite values.  */
35   ASSERT (!isnanl (3.141L));
36   ASSERT (!isnanl (3.141e30L));
37   ASSERT (!isnanl (3.141e-30L));
38   ASSERT (!isnanl (-2.718L));
39   ASSERT (!isnanl (-2.718e30L));
40   ASSERT (!isnanl (-2.718e-30L));
41   /* Infinite values.  */
42   ASSERT (!isnanl (1.0L / 0.0L));
43   ASSERT (!isnanl (-1.0L / 0.0L));
44   /* Quiet NaN.  */
45   ASSERT (isnanl (0.0L / 0.0L));
46 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
47   /* Signalling NaN.  */
48   {
49     #define NWORDS \
50       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
51     typedef union { long double value; unsigned int word[NWORDS]; }
52             memory_long_double;
53     memory_long_double m;
54     m.value = 0.0L / 0.0L;
55 # if LDBL_EXPBIT0_BIT > 0
56     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
57 # else
58     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
59       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
60 # endif
61     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
62       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
63     ASSERT (isnanl (m.value));
64   }
65 #endif
66   return 0;
67 }
68
69 #else
70
71 int
72 main ()
73 {
74   return 0;
75 }
76
77 #endif