chiark / gitweb /
fatal() testing for dateparse()
authorRichard Kettlewell <rjk@greenend.org.uk>
Tue, 27 May 2008 21:37:05 +0000 (22:37 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Tue, 27 May 2008 21:37:05 +0000 (22:37 +0100)
.bzrignore
lib/Makefile.am
libtests/Makefile.am
libtests/t-dateparse.c
libtests/test.c
libtests/test.h

index 8824dd5..9a601a2 100644 (file)
@@ -193,3 +193,4 @@ libtests/t-event
 libtests/t-dateparse
 libtests/Makefile
 cgi/Makefile
+libtests/index.html
index 95f6d01..ba53792 100644 (file)
@@ -125,6 +125,7 @@ rebuild-unicode:
        $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) -c $< > $@.new
        mv $@.new $@
 
-CLEANFILES=definitions.h definitions.h.new version-string versionstring.h
+CLEANFILES=definitions.h definitions.h.new version-string versionstring.h \
+          *.gcda *.gcov *.gcno
 
 EXTRA_DIST=trackdb.c trackdb-stub.c
index f5fbb00..5c7cc54 100644 (file)
@@ -70,5 +70,7 @@ make-coverage-reports:
 
 EXTRA_DIST=t-macros-1.tmpl t-macros-2
 
+CLEANFILES=*.gcda *.gcov *.gcno
+
 DISTCLEANFILES=GraphemeBreakTest.txt NormalizationTest.txt     \
               WordBreakTest.txt
index 1e07c74..a0d8e31 100644 (file)
@@ -48,6 +48,13 @@ static void test_dateparse(void) {
   check_date(now, "%H:%M:%S", localtime);
   /* This one needs a bodge: */
   check_date(now - now % 60, "%H:%M", localtime);
+  /* Reject invalid formats */
+  check_fatal(dateparse("12"));
+  check_fatal(dateparse("12:34:56:23"));
+  /* Reject invalid values */
+  check_fatal(dateparse("25:34"));
+  check_fatal(dateparse("23:61"));
+  check_fatal(dateparse("23:23:62"));
 }
 
 TEST(dateparse);
index f5dd036..d37d70f 100644 (file)
@@ -93,6 +93,13 @@ const char *do_printf(const char *fmt, ...) {
   return s;
 }
 
+jmp_buf fatal_env;
+
+void test_exitfn(int rc) {
+  assert(rc != 0);
+  longjmp(fatal_env, rc);
+}
+
 static const struct option options[] = {
   { "verbose", no_argument, 0, 'v' },
   { "fail-first", no_argument, 0, 'F' },
index cb26b62..8af85d5 100644 (file)
@@ -37,6 +37,7 @@
 #include <netinet/in.h>
 #include <sys/un.h>
 #include <pcre.h>
+#include <setjmp.h>
 
 #include "mem.h"
 #include "log.h"
@@ -121,6 +122,21 @@ extern int verbose;
   ++tests;                                                      \
 } while(0)
 
+#define check_fatal(WHAT) do {                                          \
+  void (*const save_exitfn)(int) attribute((noreturn)) = exitfn;        \
+                                                                        \
+  exitfn = test_exitfn;                                                 \
+  if(setjmp(fatal_env) == 0) {                                          \
+    fprintf(stderr, "Expect an error:\n ");                             \
+    (void)(WHAT);                                                       \
+    fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n",              \
+                     __FILE__, __LINE__, #WHAT);                        \
+    count_error();                                                      \
+  }                                                                     \
+  ++tests;                                                              \
+  exitfn = save_exitfn;                                                 \
+} while(0)
+
 void count_error(void);
 const char *format(const char *s);
 const char *format_utf32(const uint32_t *s);
@@ -128,6 +144,9 @@ uint32_t *ucs4parse(const char *s);
 const char *do_printf(const char *fmt, ...);
 void test_init(int argc, char **argv);
 
+extern jmp_buf fatal_env;
+void test_exitfn(int) attribute((noreturn));
+
 #define TEST(name)                                                      \
   int main(int argc, char **argv) {                                     \
     test_init(argc, argv);                                              \