xref: /titanic_50/usr/src/test/libc-tests/tests/timespec_get.c (revision 2c164fafa089aa352e513b095e1ecd0abd29c61f)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2016 Joyent, Inc.
14  */
15 
16 /*
17  * Basic tests for timespec_get(3C).
18  */
19 
20 #include <time.h>
21 #include <limits.h>
22 #include <sys/debug.h>
23 
24 static int
25 timespec_cmp(const struct timespec *ls, const struct timespec *rs)
26 {
27 	if (ls->tv_sec > rs->tv_sec)
28 		return (-1);
29 	if (ls->tv_sec < rs->tv_sec)
30 		return (1);
31 	if (ls->tv_nsec > rs->tv_nsec)
32 		return (-1);
33 	if (ls->tv_nsec > rs->tv_nsec)
34 		return (-1);
35 	if (ls->tv_nsec < rs->tv_nsec)
36 		return (1);
37 
38 	return (0);
39 }
40 
41 int
42 main(void)
43 {
44 	struct timespec ts, pre, post;
45 
46 	VERIFY0(timespec_get(&ts, TIME_UTC + 1));
47 	VERIFY0(timespec_get(&ts, TIME_UTC - 1));
48 	VERIFY0(timespec_get(&ts, UINT16_MAX));
49 
50 	VERIFY0(clock_gettime(CLOCK_REALTIME, &pre));
51 	VERIFY3S(timespec_get(&ts, TIME_UTC), ==, TIME_UTC);
52 	VERIFY0(clock_gettime(CLOCK_REALTIME, &post));
53 	VERIFY3S(timespec_cmp(&pre, &post), ==, 1);
54 
55 	VERIFY3S(timespec_cmp(&pre, &ts), ==, 1);
56 	VERIFY3S(timespec_cmp(&ts, &post), ==, 1);
57 
58 	return (0);
59 }
60