]> Savannah Git Hosting - gnulib.git/commitdiff
gettime-res: add tests
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 19 Apr 2022 22:13:09 +0000 (15:13 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 19 Apr 2022 22:14:10 +0000 (15:14 -0700)
* modules/gettime-res-tests, tests/test-gettime-res.c: New files.

ChangeLog
modules/gettime-res-tests [new file with mode: 0644]
tests/test-gettime-res.c [new file with mode: 0644]

index 1e238d14e9e92e0e142b8ad809c40c2ce608be8c..9bab736be4a6671d210cfb88234fded1dee1fb6c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2022-04-19  Paul Eggert  <eggert@cs.ucla.edu>
+
+       gettime-res: add tests
+       * modules/gettime-res-tests, tests/test-gettime-res.c: New files.
+
 2022-04-16  Paul Eggert  <eggert@cs.ucla.edu>
 
        verify: port to Mac OS 10.7.5
diff --git a/modules/gettime-res-tests b/modules/gettime-res-tests
new file mode 100644 (file)
index 0000000..b169f1c
--- /dev/null
@@ -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 (file)
index 0000000..0b13f93
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <timespec.h>
+
+#include <stdio.h>
+
+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;
+}