chiark / gitweb /
Merge the Disobedience rewrite.
[disorder] / libtests / test.h
index cb26b62068369aa3f8fef7a7ad768f82afc90f49..48c8956ca1aa175f2561ba9612ee3ffec1b9b476 100644 (file)
 #include <sys/stat.h>
 #include <unistd.h>
 #include <signal.h>
-#include <sys/wait.h>
 #include <stddef.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <sys/un.h>
 #include <pcre.h>
+#include <setjmp.h>
 
 #include "mem.h"
 #include "log.h"
@@ -68,6 +68,7 @@
 extern long long tests, errors;
 extern int fail_first;
 extern int verbose;
+extern int skipped;
 
 /** @brief Checks that @p expr is nonzero */
 #define insist(expr) do {                              \
@@ -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);                                              \
@@ -135,7 +154,11 @@ void test_init(int argc, char **argv);
     if(errors || verbose)                                               \
       fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n",  \
               errors, tests);                                           \
-    return !!errors;                                                    \
+    if(errors)                                                          \
+      return 1;                                                         \
+    if(skipped)                                                         \
+      return 77;                                                        \
+    return 0;                                                           \
   }                                                                     \
                                                                         \
   struct swallow_semicolon