link: LoadLibrary is not needed.
[gnulib.git] / tests / test-getdate.c
index 3433c09..ea70527 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of getdate() function.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008, 2009 Free Software Foundation, Inc.
 
    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
@@ -23,6 +23,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "progname.h"
+
 #include "getdate.h"
 
 #define ASSERT(expr)                                                   \
   printf ("string `%s' diff %d %d\n",                  \
          str, res.tv_sec - now.tv_sec, res.tv_nsec - now.tv_nsec);
 #else
-#define LOG(str, now, res) 0
+#define LOG(str, now, res) (void) 0
 #endif
 
+static const char* const day_table[] =
+{
+  "SUNDAY",
+  "MONDAY",
+  "TUESDAY",
+  "WEDNESDAY",
+  "THURSDAY",
+  "FRIDAY",
+  "SATURDAY",
+  NULL
+};
+
 int
 main (int argc, char **argv)
 {
@@ -53,6 +67,9 @@ main (int argc, char **argv)
   struct timespec result2;
   struct timespec now;
   const char *p;
+  int i;
+
+  set_program_name (argv[0]);
 
   now.tv_sec = 4711;
   now.tv_nsec = 1267;
@@ -207,5 +224,44 @@ main (int argc, char **argv)
   ASSERT (result.tv_sec == result2.tv_sec
          && result.tv_nsec == result2.tv_nsec);
 
+  /* Check that some "next Monday", "last Wednesday", etc. are correct.  */
+  putenv ("TZ=UTC0");
+  for (i = 0; day_table[i]; i++)
+    {
+      unsigned int thur2 = 7 * 24 * 3600; /* 2nd thursday */
+      char tmp[32];
+      sprintf (tmp, "NEXT %s", day_table[i]);
+      now.tv_sec = thur2 + 4711;
+      now.tv_nsec = 1267;
+      ASSERT (get_date (&result, tmp, &now));
+      LOG (tmp, now, result);
+      ASSERT (result.tv_nsec == 0);
+      ASSERT (result.tv_sec == thur2 + (i == 4 ? 7 : (i + 3) % 7) * 24 * 3600);
+
+      sprintf (tmp, "LAST %s", day_table[i]);
+      now.tv_sec = thur2 + 4711;
+      now.tv_nsec = 1267;
+      ASSERT (get_date (&result, tmp, &now));
+      LOG (tmp, now, result);
+      ASSERT (result.tv_nsec == 0);
+      ASSERT (result.tv_sec == thur2 + ((i + 3) % 7 - 7) * 24 * 3600);
+    }
+
+  p = "THURSDAY UTC+00";  /* The epoch was on Thursday.  */
+  now.tv_sec = 0;
+  now.tv_nsec = 0;
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  ASSERT (result.tv_sec == now.tv_sec
+         && result.tv_nsec == now.tv_nsec);
+
+  p = "FRIDAY UTC+00";
+  now.tv_sec = 0;
+  now.tv_nsec = 0;
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  ASSERT (result.tv_sec == 24 * 3600
+         && result.tv_nsec == now.tv_nsec);
+
   return 0;
 }