bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] tests: factor st_ctime-comparison out of two headers


From: Jim Meyering
Subject: [PATCH] tests: factor st_ctime-comparison out of two headers
Date: Thu, 17 Nov 2011 15:35:14 +0100

This replaces each of twelve 3-line ASSERT statements with a
one-line equivalent.  There are still another five in test-l?chown.h,
but I've left them as is.


>From 14e4d7a5e32a3e88fc41a132918441277928a1e3 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 17 Nov 2011 15:28:55 +0100
Subject: [PATCH] tests: factor out st_ctime-comparison code (a dozen uses)

* tests/test-utimens-common.h (ctime_compare): Define.
* tests/test-futimens.h (test_futimens): Replace open-coded equivalent.
* tests/test-lutimens.h (test_lutimens): Likewise.
* tests/test-utimens.h (test_utimens): Likewise.
---
 ChangeLog                   |    6 ++++++
 tests/test-futimens.h       |   16 ++++------------
 tests/test-lutimens.h       |   16 ++++------------
 tests/test-utimens-common.h |   17 +++++++++++++++++
 tests/test-utimens.h        |   16 ++++------------
 5 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3fc2c94..7f2bb5f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2011-11-17  Jim Meyering  <address@hidden>

+       tests: factor st_ctime-comparison out of two headers
+       * tests/test-utimens-common.h (ctime_compare): Define.
+       * tests/test-futimens.h (test_futimens): Replace open-coded equivalent.
+       * tests/test-lutimens.h (test_lutimens): Likewise.
+       * tests/test-utimens.h (test_utimens): Likewise.
+
        test-getcwd: don't leave behind a confdir3/ directory upon interrupt
        Invoke the test program via an init.sh-using wrapper.
        * tests/test-getcwd.sh: New file.
diff --git a/tests/test-futimens.h b/tests/test-futimens.h
index 9c9b1bb..648de67 100644
--- a/tests/test-futimens.h
+++ b/tests/test-futimens.h
@@ -54,9 +54,7 @@ test_futimens (int (*func) (int, struct timespec const *),
      source.  */
   ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
   if (check_ctime)
-    ASSERT (st1.st_ctime < st2.st_ctime
-            || (st1.st_ctime == st2.st_ctime
-                && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
+    ASSERT (ctime_compare (&st1, &st2) < 0);
   {
     /* On some NFS systems, the 'now' timestamp of creat or a NULL
        timespec is determined by the server, but the 'now' timestamp
@@ -126,9 +124,7 @@ test_futimens (int (*func) (int, struct timespec const *),
     ASSERT (0 <= get_stat_mtime_ns (&st2));
     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
     if (check_ctime)
-      ASSERT (st1.st_ctime < st2.st_ctime
-              || (st1.st_ctime == st2.st_ctime
-                  && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
+      ASSERT (ctime_compare (&st1, &st2) < 0);
   }

   /* Play with UTIME_OMIT, UTIME_NOW.  */
@@ -143,9 +139,7 @@ test_futimens (int (*func) (int, struct timespec const *),
     ASSERT (get_stat_atime_ns (&st3) <= BILLION / 2);
     ASSERT (utimecmp (BASE "file", &st1, &st3, 0) <= 0);
     if (check_ctime)
-      ASSERT (st2.st_ctime < st3.st_ctime
-              || (st2.st_ctime == st3.st_ctime
-                  && get_stat_ctime_ns (&st2) < get_stat_ctime_ns (&st3)));
+      ASSERT (ctime_compare (&st2, &st3) < 0);
     nap ();
     ts[0].tv_nsec = 0;
     ts[1].tv_nsec = UTIME_OMIT;
@@ -156,9 +150,7 @@ test_futimens (int (*func) (int, struct timespec const *),
     ASSERT (st3.st_mtime == st2.st_mtime);
     ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
     if (check_ctime)
-      ASSERT (st3.st_ctime < st2.st_ctime
-              || (st3.st_ctime == st2.st_ctime
-                  && get_stat_ctime_ns (&st3) < get_stat_ctime_ns (&st2)));
+      ASSERT (ctime_compare (&st3, &st2) < 0);
   }

   /* Cleanup.  */
diff --git a/tests/test-lutimens.h b/tests/test-lutimens.h
index e5b57eb..75d1642 100644
--- a/tests/test-lutimens.h
+++ b/tests/test-lutimens.h
@@ -61,9 +61,7 @@ test_lutimens (int (*func) (char const *, struct timespec 
const *), bool print)
   ASSERT (st2.st_atime == Y2K);
   ASSERT (st2.st_mtime == Y2K);
   if (check_ctime)
-    ASSERT (st1.st_ctime < st2.st_ctime
-            || (st1.st_ctime == st2.st_ctime
-                && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
+    ASSERT (ctime_compare (&st1, &st2) < 0);

   /* Play with symlink timestamps.  */
   if (symlink (BASE "file", BASE "link"))
@@ -143,9 +141,7 @@ test_lutimens (int (*func) (char const *, struct timespec 
const *), bool print)
     ASSERT (0 <= get_stat_mtime_ns (&st2));
     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
     if (check_ctime)
-      ASSERT (st1.st_ctime < st2.st_ctime
-              || (st1.st_ctime == st2.st_ctime
-                  && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
+      ASSERT (ctime_compare (&st1, &st2) < 0);
   }

   /* Play with UTIME_OMIT, UTIME_NOW.  */
@@ -163,9 +159,7 @@ test_lutimens (int (*func) (char const *, struct timespec 
const *), bool print)
       }
     ASSERT (utimecmp (BASE "link", &st1, &st3, 0) <= 0);
     if (check_ctime)
-      ASSERT (st2.st_ctime < st3.st_ctime
-              || (st2.st_ctime == st3.st_ctime
-                  && get_stat_ctime_ns (&st2) < get_stat_ctime_ns (&st3)));
+      ASSERT (ctime_compare (&st2, &st3) < 0);
     nap ();
     ts[0].tv_nsec = 0;
     ts[1].tv_nsec = UTIME_OMIT;
@@ -179,9 +173,7 @@ test_lutimens (int (*func) (char const *, struct timespec 
const *), bool print)
     ASSERT (st3.st_mtime == st2.st_mtime);
     ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
     if (check_ctime)
-      ASSERT (st3.st_ctime < st2.st_ctime
-              || (st3.st_ctime == st2.st_ctime
-                  && get_stat_ctime_ns (&st3) < get_stat_ctime_ns (&st2)));
+      ASSERT (ctime_compare (&st3, &st2) < 0);
   }

   /* Symlink to directory.  */
diff --git a/tests/test-utimens-common.h b/tests/test-utimens-common.h
index f3ffaf5..1730beb 100644
--- a/tests/test-utimens-common.h
+++ b/tests/test-utimens-common.h
@@ -57,4 +57,21 @@ enum {
 #  define check_ctime 1
 # endif

+/* Compare two st_ctime values.  Return -1, 0 or 1, respectively
+   when A's st_ctime is smaller than, equal to or greater than B's.  */
+static inline int
+ctime_compare (struct stat const *a, struct stat const *b)
+{
+  if (a->st_ctime < b->st_ctime)
+    return -1;
+  else if (b->st_ctime < a->st_ctime)
+    return 1;
+  else if (get_stat_ctime_ns (a) < get_stat_ctime_ns (b))
+    return -1;
+  else if (get_stat_ctime_ns (b) < get_stat_ctime_ns (a))
+    return 1;
+  else
+    return 0;
+}
+
 #endif /* GL_TEST_UTIMENS_COMMON */
diff --git a/tests/test-utimens.h b/tests/test-utimens.h
index c61bae5..80618e5 100644
--- a/tests/test-utimens.h
+++ b/tests/test-utimens.h
@@ -38,9 +38,7 @@ test_utimens (int (*func) (char const *, struct timespec 
const *), bool print)
   ASSERT (stat (BASE "file", &st2) == 0);
   ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
   if (check_ctime)
-    ASSERT (st1.st_ctime < st2.st_ctime
-            || (st1.st_ctime == st2.st_ctime
-                && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
+    ASSERT (ctime_compare (&st1, &st2) < 0);
   {
     /* On some NFS systems, the 'now' timestamp of creat or a NULL
        timespec is determined by the server, but the 'now' timestamp
@@ -102,9 +100,7 @@ test_utimens (int (*func) (char const *, struct timespec 
const *), bool print)
     ASSERT (0 <= get_stat_mtime_ns (&st2));
     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
     if (check_ctime)
-      ASSERT (st1.st_ctime < st2.st_ctime
-              || (st1.st_ctime == st2.st_ctime
-                  && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
+      ASSERT (ctime_compare (&st1, &st2) < 0);
   }

   /* Play with UTIME_OMIT, UTIME_NOW.  */
@@ -120,9 +116,7 @@ test_utimens (int (*func) (char const *, struct timespec 
const *), bool print)
     /* See comment above about this utimecmp call.  */
     ASSERT (0 <= utimecmp (BASE "file", &st3, &st1, UTIMECMP_TRUNCATE_SOURCE));
     if (check_ctime)
-      ASSERT (st2.st_ctime < st3.st_ctime
-              || (st2.st_ctime == st3.st_ctime
-                  && get_stat_ctime_ns (&st2) < get_stat_ctime_ns (&st3)));
+      ASSERT (ctime_compare (&st2, &st3) < 0);
     nap ();
     ts[0].tv_nsec = 0;
     ts[1].tv_nsec = UTIME_OMIT;
@@ -133,9 +127,7 @@ test_utimens (int (*func) (char const *, struct timespec 
const *), bool print)
     ASSERT (st3.st_mtime == st2.st_mtime);
     ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
     if (check_ctime)
-      ASSERT (st3.st_ctime < st2.st_ctime
-              || (st3.st_ctime == st2.st_ctime
-                  && get_stat_ctime_ns (&st3) < get_stat_ctime_ns (&st2)));
+      ASSERT (ctime_compare (&st3, &st2) < 0);
   }

   /* Make sure this dereferences symlinks.  */
--
1.7.8.rc2.3.g0911



reply via email to

[Prev in Thread] Current Thread [Next in Thread]