Ticket #6707: test-sleep.c

File test-sleep.c, 1.7 KB (added by scottmc, 10 years ago)

this should build with a simple: gcc test-sleep -o test-sleep

Line 
1/* -*- buffer-read-only: t -*- vi: set ro: */
2/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3/* Test of sleep() function.
4 Copyright (C) 2007-2010 Free Software Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
20
21#include <unistd.h>
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <signal.h>
26
27static void
28handle_alarm (int sig)
29{
30 if (sig != SIGALRM)
31 _exit (1);
32}
33
34#define ASSERT(expr) \
35 do \
36 { \
37 if (!(expr)) \
38 { \
39 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40 abort (); \
41 } \
42 } \
43 while (0)
44
45int
46main (void)
47{
48 ASSERT(sleep (1) <= 1);
49 ASSERT(sleep (0) == 0);
50 {
51 const unsigned int pentecost = 50 * 24 * 60 * 60; /* 50 days. */
52 unsigned int remaining;
53 signal (SIGALRM, handle_alarm);
54 alarm (1);
55 remaining = sleep (pentecost);
56 printf("pentecost=%d, remaining = %d \n", pentecost, remaining);
57 ASSERT(pentecost - 10 < remaining && remaining <= pentecost);
58 }
59 return 0;
60}