From 519be192501bae330db78af728c4d6e26b9053ee Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Tue, 30 Oct 2012 22:32:34 +0200 Subject: test: add new test tool parse-time for date/time parser Add a smoke testing tool to support testing the date/time parser module directly and independent of the rest of notmuch. Credits to Michal Sojka for the stdin parsing idea and consequent massive improvement in testability. --- test/parse-time.c | 314 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 314 insertions(+) create mode 100644 test/parse-time.c (limited to 'test/parse-time.c') diff --git a/test/parse-time.c b/test/parse-time.c new file mode 100644 index 00000000..901a4dde --- /dev/null +++ b/test/parse-time.c @@ -0,0 +1,314 @@ +/* + * 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 +#include + +#include "parse-time-string.h" + +#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0])) + +static const char *parse_time_error_strings[] = { + [PARSE_TIME_OK] = "OK", + [PARSE_TIME_ERR] = "ERR", + [PARSE_TIME_ERR_LIB] = "LIB", + [PARSE_TIME_ERR_ALREADYSET] = "ALREADYSET", + [PARSE_TIME_ERR_FORMAT] = "FORMAT", + [PARSE_TIME_ERR_DATEFORMAT] = "DATEFORMAT", + [PARSE_TIME_ERR_TIMEFORMAT] = "TIMEFORMAT", + [PARSE_TIME_ERR_INVALIDDATE] = "INVALIDDATE", + [PARSE_TIME_ERR_INVALIDTIME] = "INVALIDTIME", + [PARSE_TIME_ERR_KEYWORD] = "KEYWORD", +}; + +static const char * +parse_time_strerror (unsigned int errnum) +{ + if (errnum < ARRAY_SIZE (parse_time_error_strings)) + return parse_time_error_strings[errnum]; + else + return NULL; +} + +/* + * 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. If is\n" + "not given, parse each line in stdin according to:\n\n" + " [(==>|==_>|==^>|==^^>)] [#]\n\n" + "and produce output:\n\n" + " (==>|==_>|==^>|==^^>)