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