openat: don’t close (-1)
[gnulib.git] / tests / test-gettime-res.c
blobf1dbf2395003dbb7f48c9d31c1faff6476492e81
1 /*
2 * Copyright 2022-2024 Free Software Foundation, Inc.
3 * Written by Paul Eggert.
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.
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.
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/>. */
18 #include <config.h>
20 #include <timespec.h>
22 #include <stdio.h>
24 int
25 main (void)
27 long int res = gettime_res ();
28 printf ("gettime_res returned %ld ns\n", res);
30 if (res <= 0)
32 fprintf (stderr, "gettime_res value %ld not positive\n", res);
33 return 1;
36 if (res < TIMESPEC_HZ)
38 if (TIMESPEC_HZ % res != 0)
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;
47 else
49 if (res % TIMESPEC_HZ != 0)
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;
59 int saw_res = 0;
61 for (int i = 0; i < 100000; i++)
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)
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;
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;
82 if (saw_res == 0)
83 fprintf (stderr,
84 ("warning: all timestamps had coarser"
85 " resolution than %ld ns\n"),
86 res);
88 return 0;