Blame rt/tst-clock.c

Packit Bot 0c2104
/* Test program for POSIX clock_* functions.
Packit Bot 0c2104
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit Bot 0c2104
   This file is part of the GNU C Library.
Packit Bot 0c2104
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
Packit Bot 0c2104
Packit Bot 0c2104
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot 0c2104
   modify it under the terms of the GNU Lesser General Public
Packit Bot 0c2104
   License as published by the Free Software Foundation; either
Packit Bot 0c2104
   version 2.1 of the License, or (at your option) any later version.
Packit Bot 0c2104
Packit Bot 0c2104
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot 0c2104
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 0c2104
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot 0c2104
   Lesser General Public License for more details.
Packit Bot 0c2104
Packit Bot 0c2104
   You should have received a copy of the GNU Lesser General Public
Packit Bot 0c2104
   License along with the GNU C Library; if not, see
Packit Bot 0c2104
   <http://www.gnu.org/licenses/>.  */
Packit Bot 0c2104
Packit Bot 0c2104
#include <stdio.h>
Packit Bot 0c2104
#include <string.h>
Packit Bot 0c2104
#include <time.h>
Packit Bot 0c2104
#include <stdint.h>
Packit Bot 0c2104
Packit Bot 0c2104
Packit Bot 0c2104
/* We want to see output immediately.  */
Packit Bot 0c2104
#define STDOUT_UNBUFFERED
Packit Bot 0c2104
Packit Bot 0c2104
/* We expect to run at least 10 seconds.  */
Packit Bot 0c2104
#define TIMEOUT 15
Packit Bot 0c2104
Packit Bot 0c2104
static int
Packit Bot 0c2104
clock_test (clockid_t cl)
Packit Bot 0c2104
{
Packit Bot 0c2104
  struct timespec old_ts;
Packit Bot 0c2104
  struct timespec ts;
Packit Bot 0c2104
  struct timespec waitit;
Packit Bot 0c2104
  int result = 0;
Packit Bot 0c2104
  int i;
Packit Bot 0c2104
Packit Bot 0c2104
  memset (&ts, '\0', sizeof ts);
Packit Bot 0c2104
Packit Bot 0c2104
  waitit.tv_sec = 0;
Packit Bot 0c2104
  waitit.tv_nsec = 500000000;
Packit Bot 0c2104
Packit Bot 0c2104
  /* Get and print resolution of the clock.  */
Packit Bot 0c2104
  if (clock_getres (cl, &ts) == 0)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
Packit Bot 0c2104
	{
Packit Bot 0c2104
	  printf ("clock %d: nanosecond value of resolution wrong\n", cl);
Packit Bot 0c2104
	  result = 1;
Packit Bot 0c2104
	}
Packit Bot 0c2104
      else
Packit Bot 0c2104
	printf ("clock %d: resolution = %jd.%09jd secs\n",
Packit Bot 0c2104
		cl, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
Packit Bot 0c2104
    }
Packit Bot 0c2104
  else
Packit Bot 0c2104
    {
Packit Bot 0c2104
      printf ("clock %d: cannot get resolution\n", cl);
Packit Bot 0c2104
      result = 1;
Packit Bot 0c2104
    }
Packit Bot 0c2104
Packit Bot 0c2104
  memset (&ts, '\0', sizeof ts);
Packit Bot 0c2104
  memset (&old_ts, '\0', sizeof old_ts);
Packit Bot 0c2104
Packit Bot 0c2104
  /* Next get the current time value a few times.  */
Packit Bot 0c2104
  for (i = 0; i < 10; ++i)
Packit Bot 0c2104
    {
Packit Bot 0c2104
      if (clock_gettime (cl, &ts) == 0)
Packit Bot 0c2104
	{
Packit Bot 0c2104
	  if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
Packit Bot 0c2104
	    {
Packit Bot 0c2104
	      printf ("clock %d: nanosecond value of time wrong (try %d)\n",
Packit Bot 0c2104
		      cl, i);
Packit Bot 0c2104
	      result = 1;
Packit Bot 0c2104
	    }
Packit Bot 0c2104
	  else
Packit Bot 0c2104
	    {
Packit Bot 0c2104
	      printf ("clock %d: time = %jd.%09jd secs\n",
Packit Bot 0c2104
		      cl, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
Packit Bot 0c2104
Packit Bot 0c2104
	      if (memcmp (&ts, &old_ts, sizeof ts) == 0)
Packit Bot 0c2104
		{
Packit Bot 0c2104
		  printf ("clock %d: time hasn't changed (try %d)\n", cl, i);
Packit Bot 0c2104
		  result = 1;
Packit Bot 0c2104
Packit Bot 0c2104
		  old_ts = ts;
Packit Bot 0c2104
		}
Packit Bot 0c2104
	    }
Packit Bot 0c2104
	}
Packit Bot 0c2104
      else
Packit Bot 0c2104
	{
Packit Bot 0c2104
	  printf ("clock %d: cannot get time (try %d)\n", cl, i);
Packit Bot 0c2104
	  result = 1;
Packit Bot 0c2104
	}
Packit Bot 0c2104
Packit Bot 0c2104
      /* Wait a bit before the next iteration.  */
Packit Bot 0c2104
      nanosleep (&waitit, NULL);
Packit Bot 0c2104
    }
Packit Bot 0c2104
Packit Bot 0c2104
  return result;
Packit Bot 0c2104
}
Packit Bot 0c2104
Packit Bot 0c2104
static int
Packit Bot 0c2104
do_test (void)
Packit Bot 0c2104
{
Packit Bot 0c2104
  clockid_t cl;
Packit Bot 0c2104
  int result;
Packit Bot 0c2104
Packit Bot 0c2104
  result = clock_test (CLOCK_REALTIME);
Packit Bot 0c2104
Packit Bot 0c2104
  if (clock_getcpuclockid (0, &cl) == 0)
Packit Bot 0c2104
    /* XXX It's not yet a bug when this fails.  */
Packit Bot 0c2104
    clock_test (cl);
Packit Bot 0c2104
  else
Packit Bot 0c2104
	  printf("CPU clock unavailble, skipping test\n");
Packit Bot 0c2104
Packit Bot 0c2104
  return result;
Packit Bot 0c2104
}
Packit Bot 0c2104
#define TEST_FUNCTION do_test ()
Packit Bot 0c2104
Packit Bot 0c2104
Packit Bot 0c2104
#include "../test-skeleton.c"