1 #include <errno.h>
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <pthread.h>
7
8 #define assert(_Expression) (void)( (!!(_Expression)) || (_my_assert(#_Expression, __FILE__, __LINE__), 0) )
9
_my_assert(char * message,char * file,unsigned int line)10 static __inline void _my_assert(char *message, char *file, unsigned int line)
11 {
12 fprintf(stderr, "Assertion failed: %s , file %s, line %u\n", message, file, line);
13 exit(1);
14 }
15
test_clock_nanosleep()16 void test_clock_nanosleep()
17 {
18 int rc;
19 struct timespec tp, request = { 1, 0 }, remain;
20
21 rc = clock_nanosleep(CLOCK_MONOTONIC, 0, &request, &remain);
22 assert(rc == -1 && errno == EINVAL);
23
24 rc = clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID, 0, &request, &remain);
25 assert(rc == -1 && errno == EINVAL);
26
27 rc = clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &request, &remain);
28 assert(rc == -1 && errno == EINVAL);
29
30 rc = clock_gettime(CLOCK_REALTIME, &tp);
31 assert(rc == 0);
32 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_REALTIME)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
33
34 rc = clock_nanosleep(CLOCK_REALTIME, 0, &request, &remain);
35 assert(rc == 0);
36
37 rc = clock_gettime(CLOCK_REALTIME, &tp);
38 assert(rc == 0);
39 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_REALTIME)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
40
41 request.tv_sec = tp.tv_sec + 1;
42 request.tv_nsec = 0;
43
44 rc = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &request, &remain);
45 assert(rc == 0);
46
47 rc = clock_gettime(CLOCK_REALTIME, &tp);
48 assert(rc == 0);
49 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_REALTIME)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
50 }
51
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54 test_clock_nanosleep();
55
56 return 0;
57 }
58