timespec_get.c revision fc2512cfb727d49529d8ed99164db871f4829b73
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi/*
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * This file and its contents are supplied under the terms of the
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * You may only use this file in accordance with the terms of version
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * 1.0 of the CDDL.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi *
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * A full copy of the text of the CDDL should have accompanied this
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * source. A copy of the CDDL is also available via the Internet at
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * http://www.illumos.org/license/CDDL.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi/*
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Copyright 2016 Joyent, Inc.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi/*
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Basic tests for timespec_get(3C).
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi#include <time.h>
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi#include <limits.h>
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi#include <sys/debug.h>
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchistatic int
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchitimespec_cmp(const struct timespec *ls, const struct timespec *rs)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi{
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (ls->tv_sec > rs->tv_sec)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (-1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (ls->tv_sec < rs->tv_sec)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (ls->tv_nsec > rs->tv_nsec)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (-1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (ls->tv_nsec > rs->tv_nsec)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (-1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (ls->tv_nsec < rs->tv_nsec)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (0);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi}
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchiint
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchimain(void)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi{
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi struct timespec ts, pre, post;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY0(timespec_get(&ts, TIME_UTC + 1));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY0(timespec_get(&ts, TIME_UTC - 1));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY0(timespec_get(&ts, UINT16_MAX));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY0(clock_gettime(CLOCK_REALTIME, &pre));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY3S(timespec_get(&ts, TIME_UTC), ==, TIME_UTC);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY0(clock_gettime(CLOCK_REALTIME, &post));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY3S(timespec_cmp(&pre, &post), ==, 1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY3S(timespec_cmp(&pre, &ts), ==, 1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERIFY3S(timespec_cmp(&ts, &post), ==, 1);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return (0);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi}