Replace strsignal if it does not work fine.
[gnulib.git] / tests / test-strsignal.c
1 /* Test of strsignal() function.
2    Copyright (C) 2008 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 Colin Watson <cjwatson@debian.org>, 2008.  */
19
20 #include <config.h>
21
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
37
38 #if HAVE_DECL_SYS_SIGLIST
39 # define ASSERT_DESCRIPTION(got, expect)
40 #else
41 /* In this case, we can guarantee some signal descriptions.  */
42 # define ASSERT_DESCRIPTION(got, expect) ASSERT (!strcmp (got, expect))
43 #endif
44
45 int
46 main (int argc, char **argv)
47 {
48   char *str;
49
50   /* We try a couple of signals, since not all signals are supported
51      everywhere.  Notwithstanding the #ifdef for neatness, SIGINT should in
52      fact be available on all platforms.  */
53
54 #ifdef SIGHUP
55   str = strsignal (SIGHUP);
56   ASSERT (str);
57   ASSERT (*str);
58   ASSERT_DESCRIPTION (str, "Hangup");
59 #endif
60
61 #ifdef SIGINT
62   str = strsignal (SIGINT);
63   ASSERT (str);
64   ASSERT (*str);
65   ASSERT_DESCRIPTION (str, "Interrupt");
66 #endif
67
68   /* Test that for out-of-range signal numbers the result is usable.  */
69
70   str = strsignal (-1);
71   ASSERT (str);
72   ASSERT (str != (char *) -1);
73   ASSERT (strlen (str));
74
75   str = strsignal (9249234);
76   ASSERT (str);
77   ASSERT (str != (char *) -1);
78   ASSERT (strlen (str));
79
80   return 0;
81 }