797c8beb7e235a7f91b7684e5fca5eb111f2d2cc
[gnulib.git] / tests / test-nanosleep.c
1 /* Test of nanosleep() function.
2    Copyright (C) 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 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 #include <time.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (nanosleep, int, (struct timespec const *, struct timespec *));
25
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 #include "intprops.h"
33
34 #define ASSERT(expr) \
35   do                                                                         \
36     {                                                                        \
37       if (!(expr))                                                           \
38         {                                                                    \
39           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40           fflush (stderr);                                                   \
41           abort ();                                                          \
42         }                                                                    \
43     }                                                                        \
44   while (0)
45
46 #if HAVE_DECL_ALARM
47 static void
48 handle_alarm (int sig)
49 {
50   if (sig != SIGALRM)
51     _exit (1);
52 }
53 #endif
54
55 int
56 main (void)
57 {
58   struct timespec ts;
59
60   ts.tv_sec = 1000;
61   ts.tv_nsec = -1;
62   errno = 0;
63   ASSERT (nanosleep (&ts, NULL) == -1);
64   ASSERT (errno == EINVAL);
65   ts.tv_nsec = 1000000000;
66   errno = 0;
67   ASSERT (nanosleep (&ts, NULL) == -1);
68   ASSERT (errno == EINVAL);
69
70   ts.tv_sec = 0;
71   ts.tv_nsec = 1;
72   ASSERT (nanosleep (&ts, &ts) == 0);
73   /* Remaining time is only defined on EINTR failure; but on success,
74      it is typically either 0 or unchanged from input.  At any rate,
75      it shouldn't be randomly changed to unrelated values.  */
76   ASSERT (ts.tv_sec == 0);
77   ASSERT (ts.tv_nsec == 0 || ts.tv_nsec == 1);
78   ts.tv_nsec = 0;
79   ASSERT (nanosleep (&ts, NULL) == 0);
80
81 #if HAVE_DECL_ALARM
82   {
83     const time_t pentecost = 50 * 24 * 60 * 60; /* 50 days.  */
84     signal (SIGALRM, handle_alarm);
85     alarm (1);
86     ts.tv_sec = pentecost;
87     ts.tv_nsec = 999999999;
88     errno = 0;
89     ASSERT (nanosleep (&ts, &ts) == -1);
90     ASSERT (errno == EINTR);
91     ASSERT (pentecost - 10 < ts.tv_sec && ts.tv_sec <= pentecost);
92     ASSERT (0 <= ts.tv_nsec && ts.tv_nsec <= 999999999);
93   }
94 #endif
95
96   return 0;
97 }