(root)/
coreutils-9.4/
gnulib-tests/
test-gettime-res.c
       1  /*
       2   * Copyright 2022-2023 Free Software Foundation, Inc.
       3   * Written by Paul Eggert.
       4   *
       5   * This program is free software: you can redistribute it and/or modify
       6   * it under the terms of the GNU General Public License as published by
       7   * the Free Software Foundation, either version 3 of the License, or
       8   * (at your option) any later version.
       9   *
      10   * This program is distributed in the hope that it will be useful,
      11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13   * GNU General Public License for more details.
      14   *
      15   * You should have received a copy of the GNU General Public License
      16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <config.h>
      19  
      20  #include <timespec.h>
      21  
      22  #include <stdio.h>
      23  
      24  int
      25  main (void)
      26  {
      27    long int res = gettime_res ();
      28    printf ("gettime_res returned %ld ns\n", res);
      29  
      30    if (res <= 0)
      31      {
      32        fprintf (stderr, "gettime_res value %ld not positive\n", res);
      33        return 1;
      34      }
      35  
      36    if (res < TIMESPEC_HZ)
      37      {
      38        if (TIMESPEC_HZ % res != 0)
      39          {
      40            fprintf (stderr,
      41                     ("gettime_res value %ld ns is smaller than %d"
      42                      " but does not divide it\n"),
      43                     res, TIMESPEC_HZ);
      44            return 1;
      45          }
      46      }
      47    else
      48      {
      49        if (res % TIMESPEC_HZ != 0)
      50          {
      51            fprintf (stderr,
      52                     ("gettime_res value %ld ns is larger than %d"
      53                      " but is not a multiple of it\n"),
      54                     res, TIMESPEC_HZ);
      55            return 1;
      56          }
      57      }
      58  
      59    int saw_res = 0;
      60  
      61    for (int i = 0; i < 100000; i++)
      62      {
      63        struct timespec t = current_timespec ();
      64        if (res < TIMESPEC_HZ
      65            ? t.tv_nsec % res != 0
      66            : t.tv_nsec != 0 || t.tv_sec % (res / TIMESPEC_HZ) != 0)
      67          {
      68            fprintf (stderr,
      69                     ("current_timespec returned %lld.%09ld which is not"
      70                      " a multiple of the resolution, %ld ns\n"),
      71                     (long long) t.tv_sec, t.tv_nsec, res);
      72            return 1;
      73          }
      74        if (res < TIMESPEC_HZ
      75            ? (t.tv_nsec / res % 2 != 0
      76               || t.tv_nsec / res % 5 != 0)
      77            : (t.tv_sec / (res / TIMESPEC_HZ) % 2 != 0
      78               || t.tv_sec / (res / TIMESPEC_HZ) % 5 != 0))
      79          saw_res = 1;
      80      }
      81  
      82    if (saw_res == 0)
      83      fprintf (stderr,
      84               ("warning: all timestamps had coarser"
      85                " resolution than %ld ns\n"),
      86               res);
      87  
      88    return 0;
      89  }