From 45b248e6bbfa5b7daa1ef19b777edc7b3fe967ce Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Sat, 4 Aug 2012 00:51:48 +0300 Subject: [PATCH] [PATCH 3/7] test: add new test tool parse-time for date/time parser --- c4/bb220975715d92ac4ea9687475c2696329a42b | 274 ++++++++++++++++++++++ 1 file changed, 274 insertions(+) create mode 100644 c4/bb220975715d92ac4ea9687475c2696329a42b diff --git a/c4/bb220975715d92ac4ea9687475c2696329a42b b/c4/bb220975715d92ac4ea9687475c2696329a42b new file mode 100644 index 000000000..ebdc1f2d2 --- /dev/null +++ b/c4/bb220975715d92ac4ea9687475c2696329a42b @@ -0,0 +1,274 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 6DF91431FC0 + for ; Fri, 3 Aug 2012 14:52:09 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id EeTDFi46zCvY for ; + Fri, 3 Aug 2012 14:52:07 -0700 (PDT) +Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com + [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 66127431FAF + for ; Fri, 3 Aug 2012 14:52:06 -0700 (PDT) +Received: by mail-lpp01m010-f53.google.com with SMTP id u2so681800lag.26 + for ; Fri, 03 Aug 2012 14:52:06 -0700 (PDT) +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=google.com; s=20120113; + h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references + :in-reply-to:references:mime-version:content-type + :content-transfer-encoding:x-gm-message-state; + bh=Xz4EV6zcUuunFx+642pEKaTQoStHEO8PJ07uzI+eTQU=; + b=GM+/dAyhmjMurc/AIW8njEoWj6DUi9gNmevKGxp5FnRkqBJ8ItlqoaEv4HLVb09aMo + f78rFOhfeItt3QCm6gM0AkbWXVYMyajlJLaVaNRTnAvJovhuAy1RrGdrtJiuE58RQ85U + nZ743ONUvPCXTgqNXcRXaMxVDWJEf0eQxbDEr1+zmaQDfgu5esV0ajG5HFSe6CAtQ9nb + ZyXxVSDH6Sip+YrT+Vh/oZuT006D4kwmCcaHsYqxX7KnWF+FzElyz0LfbTUVBNCHgvmV + 2k8/SGDJIKhQAHHAFXBZdFsrzO2UCrGb4qK7meeJ4ahhwaaZQWqj+bgNkOppoDFZIiQ3 + VCWw== +Received: by 10.112.46.166 with SMTP id w6mr1202283lbm.100.1344030725959; + Fri, 03 Aug 2012 14:52:05 -0700 (PDT) +Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi. + [80.223.81.27]) + by mx.google.com with ESMTPS id jj5sm9743671lab.1.2012.08.03.14.52.04 + (version=SSLv3 cipher=OTHER); Fri, 03 Aug 2012 14:52:05 -0700 (PDT) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH 3/7] test: add new test tool parse-time for date/time parser +Date: Sat, 4 Aug 2012 00:51:48 +0300 +Message-Id: + <6ef33295de46833913336b9e5be8b8f4845f6394.1344028781.git.jani@nikula.org> +X-Mailer: git-send-email 1.7.9.5 +In-Reply-To: +References: +In-Reply-To: +References: +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit +X-Gm-Message-State: + ALoCoQmO2UkdKDWCdOzrfxKWAige6IlcBzWyRmv+75Q28csnzk2RIDMmFg2V9ytwRW7TusiT7ajn +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Fri, 03 Aug 2012 21:52:09 -0000 + +Add a tool to support testing the date/time parser module directly and +independent of the rest of notmuch. +--- + test/Makefile.local | 9 +++- + test/basic | 2 +- + test/parse-time.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++ + 3 files changed, 153 insertions(+), 3 deletions(-) + create mode 100644 test/parse-time.c + +diff --git a/test/Makefile.local b/test/Makefile.local +index 4a6a4b1..729f38e 100644 +--- a/test/Makefile.local ++++ b/test/Makefile.local +@@ -19,9 +19,13 @@ $(dir)/smtp-dummy: $(smtp_dummy_modules) + $(dir)/symbol-test: $(dir)/symbol-test.o + $(call quiet,CXX) $^ -o $@ -Llib -lnotmuch -lxapian + ++$(dir)/parse-time: $(dir)/parse-time.o lib/parse-time-string.o ++ $(call quiet,CC) $^ -o $@ ++ + .PHONY: test check + +-test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test ++test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \ ++ $(dir)/parse-time + + test: all test-binaries + @${dir}/notmuch-test $(OPTIONS) +@@ -31,4 +35,5 @@ check: test + SRCS := $(SRCS) $(smtp_dummy_srcs) + CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \ + $(dir)/symbol-test $(dir)/symbol-test.o \ +- $(dir)/arg-test $(dir)/arg-test.o ++ $(dir)/arg-test $(dir)/arg-test.o \ ++ $(dir)/parse-time $(dir)/parse-time.o +diff --git a/test/basic b/test/basic +index d6aed24..c658b95 100755 +--- a/test/basic ++++ b/test/basic +@@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test' + eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test) + tests_in_suite=$(for i in $TESTS; do echo $i; done | sort) + available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -executable -printf '%f\n' | \ +- sed -r -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \ ++ sed -r -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|parse-time)$/d" | \ + sort) + test_expect_equal "$tests_in_suite" "$available" + +diff --git a/test/parse-time.c b/test/parse-time.c +new file mode 100644 +index 0000000..b4de76b +--- /dev/null ++++ b/test/parse-time.c +@@ -0,0 +1,145 @@ ++/* ++ * parse time string - user friendly date and time parser ++ * Copyright © 2012 Jani Nikula ++ * ++ * 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 2 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 . ++ * ++ * Author: Jani Nikula ++ */ ++ ++#include ++#include ++#include ++#include ++ ++#include "parse-time-string.h" ++ ++/* ++ * concat argv[start]...argv[end - 1], separating them by a single ++ * space, to a malloced string ++ */ ++static char * ++concat_args (int start, int end, char *argv[]) ++{ ++ int i; ++ size_t len = 1; ++ char *p; ++ ++ for (i = start; i < end; i++) ++ len += strlen (argv[i]) + 1; ++ ++ p = malloc (len); ++ if (!p) ++ return NULL; ++ ++ *p = 0; ++ ++ for (i = start; i < end; i++) { ++ if (i != start) ++ strcat (p, " "); ++ strcat (p, argv[i]); ++ } ++ ++ return p; ++} ++ ++#define DEFAULT_FORMAT "%a %b %d %T %z %Y" ++ ++static void ++usage (const char *name) ++{ ++ printf ("Usage: %s [options ...] \n\n", name); ++ printf ( ++ "Parse and display it in given format.\n\n" ++ " -f, --format=FMT output format, FMT according to strftime(3)\n" ++ " (default: \"%s\")\n" ++ " -n, --now=N use N seconds since epoch as now (default: now)\n" ++ " -u, --up round result up (default: no rounding)\n" ++ " -d, --down round result down (default: no rounding)\n" ++ " -h, --help print this help\n", ++ DEFAULT_FORMAT); ++} ++ ++int ++main (int argc, char *argv[]) ++{ ++ int r; ++ struct tm tm; ++ time_t result; ++ time_t now; ++ time_t *nowp = NULL; ++ char *argstr; ++ int round = PARSE_TIME_NO_ROUND; ++ char buf[1024]; ++ const char *format = DEFAULT_FORMAT; ++ struct option options[] = { ++ { "help", no_argument, NULL, 'h' }, ++ { "up", no_argument, NULL, 'u' }, ++ { "down", no_argument, NULL, 'd' }, ++ { "format", required_argument, NULL, 'f' }, ++ { "now", required_argument, NULL, 'n' }, ++ { NULL, 0, NULL, 0 }, ++ }; ++ ++ for (;;) { ++ int c; ++ ++ c = getopt_long (argc, argv, "hudf:n:", options, NULL); ++ if (c == -1) ++ break; ++ ++ switch (c) { ++ case 'f': ++ /* output format */ ++ format = optarg; ++ break; ++ case 'u': ++ round = PARSE_TIME_ROUND_UP; ++ break; ++ case 'd': ++ round = PARSE_TIME_ROUND_DOWN; ++ break; ++ case 'n': ++ /* specify now in seconds since epoch */ ++ now = (time_t) strtol (optarg, NULL, 10); ++ if (now >= (time_t) 0) ++ nowp = &now; ++ break; ++ case 'h': ++ case '?': ++ default: ++ usage (argv[0]); ++ return 1; ++ } ++ } ++ ++ argstr = concat_args (optind, argc, argv); ++ if (!argstr) ++ return 1; ++ ++ r = parse_time_string (argstr, &result, nowp, round); ++ ++ free (argstr); ++ ++ if (r) ++ return 1; ++ ++ if (!localtime_r (&result, &tm)) ++ return 1; ++ ++ strftime (buf, sizeof (buf), format, &tm); ++ printf ("%s\n", buf); ++ ++ return 0; ++} +-- +1.7.9.5 + -- 2.26.2