strerror_r-posix: port to cygwin
[gnulib.git] / tests / test-strerror_r.c
1 /* Test of strerror_r() function.
2    Copyright (C) 2007-2011 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 #include <config.h>
19
20 #include <string.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (strerror_r, int, (int, char *, size_t));
24
25 #include <errno.h>
26
27 #include "macros.h"
28
29 int
30 main (void)
31 {
32   char buf[100];
33   int ret;
34
35   /* Test results with valid errnum and enough room.  */
36
37   buf[0] = '\0';
38   ASSERT (strerror_r (EACCES, buf, sizeof (buf)) == 0);
39   ASSERT (buf[0] != '\0');
40
41   buf[0] = '\0';
42   ASSERT (strerror_r (ETIMEDOUT, buf, sizeof (buf)) == 0);
43   ASSERT (buf[0] != '\0');
44
45   buf[0] = '\0';
46   ASSERT (strerror_r (EOVERFLOW, buf, sizeof (buf)) == 0);
47   ASSERT (buf[0] != '\0');
48
49   /* Test results with out-of-range errnum and enough room.  */
50
51   buf[0] = '^';
52   ret = strerror_r (0, buf, sizeof (buf));
53   ASSERT (ret == 0 || ret == EINVAL);
54   if (ret == 0)
55     ASSERT (buf[0] != '^');
56
57   buf[0] = '^';
58   ret = strerror_r (-3, buf, sizeof (buf));
59   ASSERT (ret == 0 || ret == EINVAL);
60   if (ret == 0)
61     ASSERT (buf[0] != '^');
62
63   /* Test results with a too small buffer.  */
64
65   ASSERT (strerror_r (EACCES, buf, sizeof (buf)) == 0);
66   {
67     size_t len = strlen (buf);
68     size_t i;
69
70     for (i = 0; i <= len; i++)
71       {
72         strcpy (buf, "BADFACE");
73         ret = strerror_r (EACCES, buf, i);
74         if (ret == 0)
75           {
76             /* Truncated result.  POSIX allows this, and it actually
77                happens on AIX 6.1 and Cygwin.  */
78             ASSERT ((strcmp (buf, "BADFACE") == 0) == (i == 0));
79           }
80         else
81           {
82             /* Failure.  */
83             ASSERT (ret == ERANGE);
84             /* buf is clobbered nevertheless, on FreeBSD and MacOS X.  */
85           }
86       }
87
88     strcpy (buf, "BADFACE");
89     ret = strerror_r (EACCES, buf, len + 1);
90     ASSERT (ret == 0);
91   }
92
93   return 0;
94 }