[PATCH v5 3/9] test: add new test tool parse-time for date/time parser
authorJani Nikula <jani@nikula.org>
Sun, 21 Oct 2012 21:22:26 +0000 (00:22 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:49:57 +0000 (09:49 -0800)
3d/703c69c7f58e9b58d19f84619672239d77bcc2 [new file with mode: 0644]

diff --git a/3d/703c69c7f58e9b58d19f84619672239d77bcc2 b/3d/703c69c7f58e9b58d19f84619672239d77bcc2
new file mode 100644 (file)
index 0000000..5968798
--- /dev/null
@@ -0,0 +1,413 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id CB996431FC0\r
+       for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:50 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id B9hjXVpsoJLg for <notmuch@notmuchmail.org>;\r
+       Sun, 21 Oct 2012 14:22:47 -0700 (PDT)\r
+Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
+       [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 9DDAA431FD0\r
+       for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:46 -0700 (PDT)\r
+Received: by mail-lb0-f181.google.com with SMTP id gg6so1515820lbb.26\r
+       for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:46 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
+       :in-reply-to:references:mime-version:content-type\r
+       :content-transfer-encoding:x-gm-message-state;\r
+       bh=fozFlLFWYRPIq5jUxgriYDkFz2iAlH550DL/Poy0uz8=;\r
+       b=PVVF0NdjhipnClVHeJ8KfkrcsFtnoT8BjIWz2vtySKta3LdIsLxPENKzcolFJfwiTS\r
+       SZFhkGFnRu2th/P+nYypDmMkLY+VBaojd1vCtQjUQnEzFHzNStPKulKb7N09mNiFB97I\r
+       rm6Dp4cqChOlFrny0Iuj9T7KFO52zyvLPGLLl+WloIAlFY9YOi/jO8jpwHK9Bu+d8Yvk\r
+       Lx3kIuWrJ6ampwAtIlH6rf1B1Mwsc3t9uvVLE9BRgEW8/aIToSztUqxfRxycld4STAJD\r
+       hODHAGpQgCqco4+U+iL8tNiZNCgpYY22exSk7mLAyb0duzr3JkRsxB2ht7eGsResC7Uw\r
+       /o6Q==\r
+Received: by 10.112.101.10 with SMTP id fc10mr2912074lbb.82.1350854566216;\r
+       Sun, 21 Oct 2012 14:22:46 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
+       [80.223.81.27])\r
+       by mx.google.com with ESMTPS id oj5sm2417681lab.8.2012.10.21.14.22.44\r
+       (version=SSLv3 cipher=OTHER); Sun, 21 Oct 2012 14:22:45 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject:\r
+ [PATCH v5 3/9] test: add new test tool parse-time for date/time parser\r
+Date: Mon, 22 Oct 2012 00:22:26 +0300\r
+Message-Id:\r
+ <75a8f129d5e0d824b3e04ddfc1816c45fa0ec70d.1350854171.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <cover.1350854171.git.jani@nikula.org>\r
+References: <cover.1350854171.git.jani@nikula.org>\r
+In-Reply-To: <cover.1350854171.git.jani@nikula.org>\r
+References: <cover.1350854171.git.jani@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-Gm-Message-State:\r
+ ALoCoQmOzixYrVfDjuQ7k4hg2i7RF4hyj/xUZhAGUulVcnYqHlCx3j16hV2RTX8+aR/6lWvhTXsV\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 21 Oct 2012 21:22:51 -0000\r
+\r
+Add a smoke testing tool to support testing the date/time parser\r
+module directly and independent of the rest of notmuch.\r
+\r
+Credits to Michal Sojka <sojkam1@fel.cvut.cz> for the stdin parsing\r
+idea and consequent massive improvement in testability.\r
+---\r
+ test/Makefile.local |    7 +-\r
+ test/basic          |    2 +-\r
+ test/parse-time.c   |  281 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 3 files changed, 288 insertions(+), 2 deletions(-)\r
+ create mode 100644 test/parse-time.c\r
+\r
+diff --git a/test/Makefile.local b/test/Makefile.local\r
+index 45df4c7..9ae130a 100644\r
+--- a/test/Makefile.local\r
++++ b/test/Makefile.local\r
+@@ -19,9 +19,13 @@ $(dir)/smtp-dummy: $(smtp_dummy_modules)\r
+ $(dir)/symbol-test: $(dir)/symbol-test.o\r
+       $(call quiet,CXX) $^ -o $@ -Llib -lnotmuch $(XAPIAN_LDFLAGS)\r
+ \r
++$(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o\r
++      $(call quiet,CC) $^ -o $@\r
++\r
+ .PHONY: test check\r
+ \r
+-test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test\r
++test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \\r
++      $(dir)/parse-time\r
+ \r
+ test: all test-binaries\r
+       @${dir}/notmuch-test $(OPTIONS)\r
+@@ -32,4 +36,5 @@ SRCS := $(SRCS) $(smtp_dummy_srcs)\r
+ CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \\r
+        $(dir)/symbol-test $(dir)/symbol-test.o \\r
+        $(dir)/arg-test $(dir)/arg-test.o \\r
++       $(dir)/parse-time $(dir)/parse-time.o \\r
+        $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*\r
+diff --git a/test/basic b/test/basic\r
+index 3b635c8..c47197c 100755\r
+--- a/test/basic\r
++++ b/test/basic\r
+@@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'\r
+ eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test)\r
+ tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)\r
+ available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111 | \\r
+-    sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \\r
++    sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|parse-time)$/d" | \\r
+     sort)\r
+ test_expect_equal "$tests_in_suite" "$available"\r
+ \r
+diff --git a/test/parse-time.c b/test/parse-time.c\r
+new file mode 100644\r
+index 0000000..5f73b85\r
+--- /dev/null\r
++++ b/test/parse-time.c\r
+@@ -0,0 +1,281 @@\r
++/*\r
++ * parse time string - user friendly date and time parser\r
++ * Copyright © 2012 Jani Nikula\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 2 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
++ *\r
++ * Author: Jani Nikula <jani@nikula.org>\r
++ */\r
++\r
++#include <assert.h>\r
++#include <ctype.h>\r
++#include <getopt.h>\r
++#include <stdio.h>\r
++#include <stdlib.h>\r
++#include <string.h>\r
++\r
++#include "parse-time-string.h"\r
++\r
++#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))\r
++\r
++/*\r
++ * concat argv[start]...argv[end - 1], separating them by a single\r
++ * space, to a malloced string\r
++ */\r
++static char *\r
++concat_args (int start, int end, char *argv[])\r
++{\r
++    int i;\r
++    size_t len = 1;\r
++    char *p;\r
++\r
++    for (i = start; i < end; i++)\r
++      len += strlen (argv[i]) + 1;\r
++\r
++    p = malloc (len);\r
++    if (!p)\r
++      return NULL;\r
++\r
++    *p = 0;\r
++\r
++    for (i = start; i < end; i++) {\r
++      if (i != start)\r
++          strcat (p, " ");\r
++      strcat (p, argv[i]);\r
++    }\r
++\r
++    return p;\r
++}\r
++\r
++#define DEFAULT_FORMAT "%a %b %d %T %z %Y"\r
++\r
++static void\r
++usage (const char *name)\r
++{\r
++    printf ("Usage: %s [options ...] [<date/time>]\n\n", name);\r
++    printf (\r
++      "Parse <date/time> and display it in given format. If <date/time> is\n"\r
++      "not given, parse each line in stdin according to:\n\n"\r
++      "  <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"\r
++      "and produce output:\n\n"\r
++      "  <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"\r
++      "preserving whitespace and comment in input. The operators ==>, ==_>,\n"\r
++      "==^>, and ==^^> define rounding as no rounding, round down, round up\n"\r
++      "inclusive, and round up, respectively.\n\n"\r
++\r
++      "  -f, --format=FMT output format, FMT according to strftime(3)\n"\r
++      "                   (default: \"%s\")\n"\r
++      "  -r, --ref=N      use N seconds since epoch as reference time\n"\r
++      "                   (default: now)\n"\r
++      "  -u, --^          round result up inclusive (default: no rounding)\n"\r
++      "  -U, --^^         round result up (default: no rounding)\n"\r
++      "  -d, --_          round result down (default: no rounding)\n"\r
++      "  -h, --help       print this help\n",\r
++      DEFAULT_FORMAT);\r
++}\r
++\r
++struct {\r
++    const char *operator;\r
++    int round;\r
++} operators[] = {\r
++    { "==>",  PARSE_TIME_NO_ROUND },\r
++    { "==_>", PARSE_TIME_ROUND_DOWN },\r
++    { "==^>", PARSE_TIME_ROUND_UP_INCLUSIVE },\r
++    { "==^^>",        PARSE_TIME_ROUND_UP },\r
++};\r
++\r
++static const char *\r
++find_operator_in_string (char *str, char **ptr, int *round)\r
++{\r
++    const char *oper = NULL;\r
++    unsigned int i;\r
++\r
++    for (i = 0; i < ARRAY_SIZE (operators); i++) {\r
++      char *p = strstr (str, operators[i].operator);\r
++      if (p) {\r
++          if (round)\r
++              *round = operators[i].round;\r
++          if (ptr)\r
++              *ptr = p;\r
++\r
++          oper = operators[i].operator;\r
++          break;\r
++      }\r
++    }\r
++\r
++    return oper;\r
++}\r
++\r
++static const char *\r
++get_operator (int round)\r
++{\r
++    const char *oper = NULL;\r
++    unsigned int i;\r
++\r
++    for (i = 0; i < ARRAY_SIZE(operators); i++) {\r
++      if (round == operators[i].round) {\r
++          oper = operators[i].operator;\r
++          break;\r
++      }\r
++    }\r
++\r
++    return oper;\r
++}\r
++\r
++static int\r
++parse_stdin (FILE *infile, time_t *ref, int round, const char *format)\r
++{\r
++    char *input = NULL;\r
++    char result[1024];\r
++    size_t inputsize;\r
++    ssize_t len;\r
++    struct tm tm;\r
++    time_t t;\r
++    int r;\r
++\r
++    while ((len = getline (&input, &inputsize, infile)) != -1) {\r
++      const char *oper;\r
++      char *trail, *tmp;\r
++\r
++      /* trail is trailing whitespace and (optional) comment */\r
++      trail = strchr (input, '#');\r
++      if (!trail)\r
++          trail = input + len;\r
++\r
++      while (trail > input && isspace ((unsigned char) *(trail-1)))\r
++          trail--;\r
++\r
++      if (trail == input) {\r
++          printf ("%s", input);\r
++          continue;\r
++      }\r
++\r
++      tmp = strdup (trail);\r
++      if (!tmp) {\r
++          fprintf (stderr, "strdup() failed\n");\r
++          continue;\r
++      }\r
++      *trail = '\0';\r
++      trail = tmp;\r
++\r
++      /* operator */\r
++      oper = find_operator_in_string (input, &tmp, &round);\r
++      if (oper) {\r
++          *tmp = '\0';\r
++      } else {\r
++          oper = get_operator (round);\r
++          assert (oper);\r
++      }\r
++\r
++      r = parse_time_string (input, &t, ref, round);\r
++      if (!r) {\r
++          if (!localtime_r (&t, &tm)) {\r
++              fprintf (stderr, "localtime_r() failed\n");\r
++              free (trail);\r
++              continue;\r
++          }\r
++\r
++          strftime (result, sizeof (result), format, &tm);\r
++      } else {\r
++          snprintf (result, sizeof (result), "ERROR: %d", r);\r
++      }\r
++\r
++      printf ("%s%s %s%s", input, oper, result, trail);\r
++      free (trail);\r
++    }\r
++\r
++    free (input);\r
++\r
++    return 0;\r
++}\r
++\r
++int\r
++main (int argc, char *argv[])\r
++{\r
++    int r;\r
++    struct tm tm;\r
++    time_t result;\r
++    time_t now;\r
++    time_t *nowp = NULL;\r
++    char *argstr;\r
++    int round = PARSE_TIME_NO_ROUND;\r
++    char buf[1024];\r
++    const char *format = DEFAULT_FORMAT;\r
++    struct option options[] = {\r
++      { "help",       no_argument,            NULL,   'h' },\r
++      { "^",          no_argument,            NULL,   'u' },\r
++      { "^^",         no_argument,            NULL,   'U' },\r
++      { "_",          no_argument,            NULL,   'd' },\r
++      { "format",     required_argument,      NULL,   'f' },\r
++      { "ref",        required_argument,      NULL,   'r' },\r
++      { NULL, 0, NULL, 0 },\r
++    };\r
++\r
++    for (;;) {\r
++      int c;\r
++\r
++      c = getopt_long (argc, argv, "huUdf:r:", options, NULL);\r
++      if (c == -1)\r
++          break;\r
++\r
++      switch (c) {\r
++      case 'f':\r
++          /* output format */\r
++          format = optarg;\r
++          break;\r
++      case 'u':\r
++          round = PARSE_TIME_ROUND_UP_INCLUSIVE;\r
++          break;\r
++      case 'U':\r
++          round = PARSE_TIME_ROUND_UP;\r
++          break;\r
++      case 'd':\r
++          round = PARSE_TIME_ROUND_DOWN;\r
++          break;\r
++      case 'r':\r
++          /* specify now in seconds since epoch */\r
++          now = (time_t) strtol (optarg, NULL, 10);\r
++          if (now >= (time_t) 0)\r
++              nowp = &now;\r
++          break;\r
++      case 'h':\r
++      case '?':\r
++      default:\r
++          usage (argv[0]);\r
++          return 1;\r
++      }\r
++    }\r
++\r
++    if (optind == argc)\r
++      return parse_stdin (stdin, nowp, round, format);\r
++\r
++    argstr = concat_args (optind, argc, argv);\r
++    if (!argstr)\r
++      return 1;\r
++\r
++    r = parse_time_string (argstr, &result, nowp, round);\r
++\r
++    free (argstr);\r
++\r
++    if (r)\r
++      return 1;\r
++\r
++    if (!localtime_r (&result, &tm))\r
++      return 1;\r
++\r
++    strftime (buf, sizeof (buf), format, &tm);\r
++    printf ("%s\n", buf);\r
++\r
++    return 0;\r
++}\r
+-- \r
+1.7.10.4\r
+\r