chiark / gitweb /
fatal() testing for dateparse()
[disorder] / libtests / test.h
index cb26b62068369aa3f8fef7a7ad768f82afc90f49..8af85d515c5fead40b1d624d28a58adde1ec431a 100644 (file)
@@ -37,6 +37,7 @@
 #include <netinet/in.h>
 #include <sys/un.h>
 #include <pcre.h>
 #include <netinet/in.h>
 #include <sys/un.h>
 #include <pcre.h>
+#include <setjmp.h>
 
 #include "mem.h"
 #include "log.h"
 
 #include "mem.h"
 #include "log.h"
@@ -121,6 +122,21 @@ extern int verbose;
   ++tests;                                                      \
 } while(0)
 
   ++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);
 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);
 
 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);                                              \
 #define TEST(name)                                                      \
   int main(int argc, char **argv) {                                     \
     test_init(argc, argv);                                              \