Back to index

glibc  2.9
tst-clock.c
Go to the documentation of this file.
00001 /* Test program for POSIX clock_* functions.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <time.h>
00024 
00025 
00026 /* We want to see output immediately.  */
00027 #define STDOUT_UNBUFFERED
00028 
00029 /* We expect to run at least 10 seconds.  */
00030 #define TIMEOUT 15
00031 
00032 static int
00033 clock_test (clockid_t cl)
00034 {
00035   struct timespec old_ts;
00036   struct timespec ts;
00037   struct timespec waitit;
00038   int result = 0;
00039   int i;
00040 
00041   memset (&ts, '\0', sizeof ts);
00042 
00043   waitit.tv_sec = 0;
00044   waitit.tv_nsec = 500000000;
00045 
00046   /* Get and print resolution of the clock.  */
00047   if (clock_getres (cl, &ts) == 0)
00048     {
00049       if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
00050        {
00051          printf ("clock %d: nanosecond value of resolution wrong\n", cl);
00052          result = 1;
00053        }
00054       else
00055        printf ("clock %d: resolution = %ld.%09ld secs\n",
00056               cl, ts.tv_sec, ts.tv_nsec);
00057     }
00058   else
00059     {
00060       printf ("clock %d: cannot get resolution\n", cl);
00061       result = 1;
00062     }
00063 
00064   memset (&ts, '\0', sizeof ts);
00065   memset (&old_ts, '\0', sizeof old_ts);
00066 
00067   /* Next get the current time value a few times.  */
00068   for (i = 0; i < 10; ++i)
00069     {
00070       if (clock_gettime (cl, &ts) == 0)
00071        {
00072          if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
00073            {
00074              printf ("clock %d: nanosecond value of time wrong (try %d)\n",
00075                     cl, i);
00076              result = 1;
00077            }
00078          else
00079            {
00080              printf ("clock %d: time = %ld.%09ld secs\n",
00081                     cl, ts.tv_sec, ts.tv_nsec);
00082 
00083              if (memcmp (&ts, &old_ts, sizeof ts) == 0)
00084               {
00085                 printf ("clock %d: time hasn't changed (try %d)\n", cl, i);
00086                 result = 1;
00087 
00088                 old_ts = ts;
00089               }
00090            }
00091        }
00092       else
00093        {
00094          printf ("clock %d: cannot get time (try %d)\n", cl, i);
00095          result = 1;
00096        }
00097 
00098       /* Wait a bit before the next iteration.  */
00099       nanosleep (&waitit, NULL);
00100     }
00101 
00102   return result;
00103 }
00104 
00105 static int
00106 do_test (void)
00107 {
00108   clockid_t cl;
00109   int result;
00110 
00111   result = clock_test (CLOCK_REALTIME);
00112 
00113   if (clock_getcpuclockid (0, &cl) == 0)
00114     /* XXX It's not yet a bug when this fails.  */
00115     clock_test (cl);
00116   else
00117          printf("CPU clock unavailble, skipping test\n");
00118 
00119   return result;
00120 }
00121 #define TEST_FUNCTION do_test ()
00122 
00123 
00124 #include "../test-skeleton.c"