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