/*
 *      based on gettimeofday02.c  from LTP
 * DESCRIPTION
 *      Check if gettimeofday is monotonous
 *
 * ALGORITHM
 *      Call gettimeofday() to get a t1 (fist value)
 *      call it again to get t2, see if t2 < t1, set t2 = t1, repeat 
 */

#include <stdio.h>
#include <sys/time.h>

int main(int ac, char **av)
{
	struct timeval tv1, tv2;
	int c = 0, h = 0;

	gettimeofday(&tv1, NULL);
	puts("run     oldsec    oldusec       nsec       nusec      dsec      dusec");
	while (c < 1000) {
		gettimeofday(&tv2, NULL);
		if ((tv2.tv_usec < tv1.tv_usec) && (tv2.tv_sec <= tv1.tv_sec))
			h++;
		printf("%3d %10i %10i %10i %10i %10i %10i\n", c,
		       (int)tv1.tv_sec, (int)tv1.tv_usec,
		       (int)tv2.tv_sec, (int)tv2.tv_usec,
		       (int)(tv1.tv_sec - tv2.tv_sec),
		       (int)(tv1.tv_usec - tv2.tv_usec));
		c++;
		tv1 = tv2;
	}
	printf("  Runs: %d  Hits: %d\n", c, h);
	return 0;
}

