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