tests: factor out st_ctime-comparison code (a dozen uses)
authorJim Meyering <meyering@redhat.com>
Thu, 17 Nov 2011 14:28:55 +0000 (15:28 +0100)
committerJim Meyering <meyering@redhat.com>
Thu, 17 Nov 2011 14:33:12 +0000 (15:33 +0100)
* 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
tests/test-futimens.h
tests/test-lutimens.h
tests/test-utimens-common.h
tests/test-utimens.h

index 3fc2c94..7f2bb5f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2011-11-17  Jim Meyering  <meyering@redhat.com>
 
+       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.
index 9c9b1bb..648de67 100644 (file)
@@ -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.  */
index e5b57eb..75d1642 100644 (file)
@@ -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.  */
index f3ffaf5..1730beb 100644 (file)
@@ -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 */
index c61bae5..80618e5 100644 (file)
@@ -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.  */