From f67c0cfb02c920bd89f490c7790f991db45a0bc5 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 19 Apr 2022 15:13:09 -0700 Subject: [PATCH] gettime-res: add tests * modules/gettime-res-tests, tests/test-gettime-res.c: New files. --- ChangeLog | 5 +++ modules/gettime-res-tests | 12 ++++++ tests/test-gettime-res.c | 89 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 modules/gettime-res-tests create mode 100644 tests/test-gettime-res.c diff --git a/ChangeLog b/ChangeLog index 1e238d14e9..9bab736be4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2022-04-19 Paul Eggert + + gettime-res: add tests + * modules/gettime-res-tests, tests/test-gettime-res.c: New files. + 2022-04-16 Paul Eggert verify: port to Mac OS 10.7.5 diff --git a/modules/gettime-res-tests b/modules/gettime-res-tests new file mode 100644 index 0000000000..b169f1c187 --- /dev/null +++ b/modules/gettime-res-tests @@ -0,0 +1,12 @@ +Files: +tests/signature.h +tests/test-gettime-res.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-gettime-res +check_PROGRAMS += test-gettime-res +test_gettime_res_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) diff --git a/tests/test-gettime-res.c b/tests/test-gettime-res.c new file mode 100644 index 0000000000..0b13f93ec6 --- /dev/null +++ b/tests/test-gettime-res.c @@ -0,0 +1,89 @@ +/* + * Copyright 2022 Free Software Foundation, Inc. + * Written by Paul Eggert. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +#include + +#include + +#include + +int +main (void) +{ + long int res = gettime_res (); + printf ("gettime_res returned %ld ns\n", res); + + if (res <= 0) + { + fprintf (stderr, "gettime_res value %ld not positive\n", res); + return 1; + } + + if (res < TIMESPEC_HZ) + { + if (TIMESPEC_HZ % res != 0) + { + fprintf (stderr, + ("gettime_res value %ld ns is smaller than %d" + " but does not divide it\n"), + res, TIMESPEC_HZ); + return 1; + } + } + else + { + if (res % TIMESPEC_HZ != 0) + { + fprintf (stderr, + ("gettime_res value %ld ns is larger than %d" + " but is not a multiple of it\n"), + res, TIMESPEC_HZ); + return 1; + } + } + + int saw_res = 0; + + for (int i = 0; i < 100000; i++) + { + struct timespec t = current_timespec (); + if (res < TIMESPEC_HZ + ? t.tv_nsec % res != 0 + : t.tv_nsec != 0 || t.tv_sec % (res / TIMESPEC_HZ) != 0) + { + fprintf (stderr, + ("current_timespec returned %lld.%09ld which is not" + " a multiple of the resolution, %ld ns\n"), + (long long) t.tv_sec, t.tv_nsec, res); + return 1; + } + if (res < TIMESPEC_HZ + ? (t.tv_nsec / res % 2 != 0 + || t.tv_nsec / res % 5 != 0) + : (t.tv_sec / (res / TIMESPEC_HZ) % 2 != 0 + || t.tv_sec / (res / TIMESPEC_HZ) % 5 != 0)) + saw_res = 1; + } + + if (saw_res == 0) + fprintf (stderr, + ("warning: all timestamps had coarser" + " resolution than %ld ns\n"), + res); + + return 0; +} -- 2.35.1