tests: avoid compiler warnings
[gnulib.git] / tests / test-sleep.c
1 /* Test of sleep() function.
2    Copyright (C) 2007-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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 #if HAVE_DECL_ALARM
40 static void
41 handle_alarm (int sig)
42 {
43   if (sig != SIGALRM)
44     _exit (1);
45 }
46 #endif
47
48 int
49 main (void)
50 {
51   ASSERT (sleep (1) <= 1);
52
53   ASSERT (sleep (0) == 0);
54
55 #if HAVE_DECL_ALARM
56   {
57     const unsigned int pentecost = 50 * 24 * 60 * 60; /* 50 days.  */
58     unsigned int remaining;
59     signal (SIGALRM, handle_alarm);
60     alarm (1);
61     remaining = sleep (pentecost);
62     ASSERT (pentecost - 10 < remaining && remaining <= pentecost);
63   }
64 #endif
65
66   return 0;
67 }