summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Korb <bkorb@gnu.org>2008-11-17 13:02:25 +0100
committerBruno Haible <bruno@clisp.org>2008-11-17 13:02:25 +0100
commit21462b25befa9ac1e09e27b3d4df6952633a6ab4 (patch)
treea5f0b6bdabf4ba7a20e4b966690e703ecc6d540e
parenta0c05d1b383f42639b17fcc855fa7995b78e8dba (diff)
downloadgnulib-21462b25befa9ac1e09e27b3d4df6952633a6ab4.tar.gz
Tests for module 'parse-duration'.
-rw-r--r--ChangeLog4
-rw-r--r--modules/parse-duration-tests14
-rw-r--r--tests/test-parse-duration.c65
-rwxr-xr-xtests/test-parse-duration.sh71
4 files changed, 154 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 4dbc505472..b5a87b246a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
2008-11-17 Bruce Korb <bkorb@gnu.org>
+ * modules/parse-duration-tests: New file.
+ * tests/test-parse-duration.sh: New file.
+ * tests/test-parse-duration.c: New file.
+
New module 'parse-duration'.
* lib/parse-duration.h: New file.
* lib/parse-duration.c: New file.
diff --git a/modules/parse-duration-tests b/modules/parse-duration-tests
new file mode 100644
index 0000000000..d50a1655b1
--- /dev/null
+++ b/modules/parse-duration-tests
@@ -0,0 +1,14 @@
+Files:
+tests/test-parse-duration.sh
+tests/test-parse-duration.c
+
+Depends-on:
+strdup
+strerror
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-parse-duration.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@'
+check_PROGRAMS += test-parse-duration
diff --git a/tests/test-parse-duration.c b/tests/test-parse-duration.c
new file mode 100644
index 0000000000..12ba91e508
--- /dev/null
+++ b/tests/test-parse-duration.c
@@ -0,0 +1,65 @@
+/* Test of parsing durations.
+ Copyright (C) 2008 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
+ the Free Software Foundation; either version 3 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 <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "parse-duration.h"
+
+char *
+xstrdup(char const * p)
+{
+ return strdup (p);
+}
+
+int
+main (int argc, char *argv[])
+{
+ if (--argc <= 0)
+ {
+ fprintf (stderr, "USAGE: %s <time-spec> [...]", argv[0]);
+ return 1;
+ }
+
+ do
+ {
+ char const * arg = *++argv;
+ time_t res = parse_duration (arg);
+ if (errno != 0)
+ {
+ fprintf (stderr, "could not parse time: %s\n\terr %d - %s\n", arg,
+ errno, strerror (errno));
+ return 1;
+ }
+ printf ("%u\n", (unsigned int)res);
+ } while (--argc > 0);
+
+ return 0;
+}
+
+/*
+ * Local Variables:
+ * mode: C
+ * c-file-style: "gnu"
+ * indent-tabs-mode: nil
+ * End:
+ * end of parse-duration.c */
diff --git a/tests/test-parse-duration.sh b/tests/test-parse-duration.sh
new file mode 100755
index 0000000000..e6ebf90645
--- /dev/null
+++ b/tests/test-parse-duration.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+# Show all commands when run with environment variable VERBOSE=yes.
+test -z "$VERBOSE" || set -x
+prog=test-parse-duration
+
+exe=`pwd`/${prog}${EXEEXT}
+nl='
+'
+
+# func_tmpdir
+# creates a temporary directory.
+# Sets variable
+# - tmp pathname of freshly created temporary directory
+func_tmpdir ()
+{
+ # Use the environment variable TMPDIR, falling back to /tmp. This allows
+ # users to specify a different temporary directory, for example, if their
+ # /tmp is filled up or too small.
+ : ${TMPDIR=/tmp}
+ {
+ # Use the mktemp program if available. If not available, hide the error
+ # message.
+ tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
+ test -n "$tmp" && test -d "$tmp"
+ } ||
+ {
+ # Use a simple mkdir command. It is guaranteed to fail if the directory
+ # already exists. $RANDOM is bash specific and expands to empty in shells
+ # other than bash, ksh and zsh. Its use does not increase security;
+ # rather, it minimizes the probability of failure in a very cluttered /tmp
+ # directory.
+ tmp=$TMPDIR/gl$$-$RANDOM
+ (umask 077 && mkdir "$tmp")
+ } ||
+ {
+ echo "$0: cannot create a temporary directory in $TMPDIR" >&2
+ exit 1
+ }
+}
+
+die ()
+{
+ echo "${prog} fatal error: $*" >&2
+ exit 1
+}
+
+func_tmpdir
+trap "rm -rf ${tmp}" EXIT
+tmpf=${tmp}/tests.txt
+
+cat > ${tmpf} <<- _EOF_
+ 1 Y 2 M 3 W 4 d 5 h 6 m 7 s
+ P 00010225 T 05:06:07
+ P 1Y2M3W4D T 5H6M7S
+ 1 Y 2 M 25 D 5:6:7
+ 1 Y 2 M 25 d 5h 6:7
+ 1 Y 2 M 25 d 5h 6m 7
+ P 1-2-25 T 5:6:7
+ _EOF_
+
+ls -l $tmpf
+
+exec 3< ${tmpf}
+while read -u3 line
+do
+ v=`${exe} "${line}"` || die "Failed: ${exe} '${line}'"
+ test $v -eq 38898367 || die $v is not 38898367
+ echo OK: ${line}
+done
+exec 3>&-