chiark / gitweb /
Bring CGI docs pretty much up to date
[disorder] / lib / test.c
index 0682979e3323e01304c3209f45d267a058286422..f5dd036d443eff8581549c539b8b6ab4e38e592a 100644 (file)
 /** @file lib/test.c @brief Library tests */
 
 #include "test.h"
+#include "version.h"
+#include <getopt.h>
 
 long long tests, errors;
 int fail_first;
+int verbose;
 
 void count_error(void) {
   ++errors;
@@ -34,7 +37,7 @@ const char *format(const char *s) {
   struct dynstr d;
   int c;
   char buf[10];
-  
+
   dynstr_init(&d);
   while((c = (unsigned char)*s++)) {
     if(c >= ' ' && c <= '~')
@@ -52,7 +55,7 @@ const char *format_utf32(const uint32_t *s) {
   struct dynstr d;
   uint32_t c;
   char buf[64];
-  
+
   dynstr_init(&d);
   while((c = *s++)) {
     sprintf(buf, " %04lX", (long)c);
@@ -90,82 +93,46 @@ const char *do_printf(const char *fmt, ...) {
   return s;
 }
 
-int main(void) {
+static const struct option options[] = {
+  { "verbose", no_argument, 0, 'v' },
+  { "fail-first", no_argument, 0, 'F' },
+  { "help", no_argument, 0, 'h' },
+  { "version", no_argument, 0, 'V' },
+};
+
+/* display usage message and terminate */
+static void help(void) {
+  xprintf("Usage:\n"
+         "  %s [OPTIONS]\n"
+         "Options:\n"
+         "  --help, -h               Display usage message\n"
+         "  --version, -V            Display version number\n"
+          "  --verbose, -v            Verbose output\n"
+          "  --fail-first, -F         Stop on first failure\n",
+          progname);
+  xfclose(stdout);
+  exit(0);
+}
+
+void test_init(int argc, char **argv) {
+  int n;
+
+  set_progname(argv);
   mem_init();
-  fail_first = !!getenv("FAIL_FIRST");
-  insist('\n' == 0x0A);
-  insist('\r' == 0x0D);
-  insist(' ' == 0x20);
-  insist('0' == 0x30);
-  insist('9' == 0x39);
-  insist('A' == 0x41);
-  insist('Z' == 0x5A);
-  insist('a' == 0x61);
-  insist('z' == 0x7A);
-  /* addr.c */
-  test_addr();
-  /* asprintf.c */
-  /* authhash.c */
-  /* basen.c */
-  test_basen();
-  /* charset.c */
-  /* client.c */
-  /* configuration.c */
-  /* event.c */
-  /* filepart.c */
-  test_filepart();
-  /* fprintf.c */
-  /* heap.c */
-  test_heap();
-  /* hex.c */
-  test_hex();
-  /* inputline.c */
-  /* kvp.c */
-  test_kvp();
-  /* log.c */
-  /* mem.c */
-  /* mime.c */
-  test_mime();
-  test_cookies();
-  /* mixer.c */
-  /* plugin.c */
-  /* printf.c */
-  test_printf();
-  /* queue.c */
-  /* sink.c */
-  test_sink();
-  /* snprintf.c */
-  /* split.c */
-  test_split();
-  /* syscalls.c */
-  /* table.c */
-  /* unicode.c */
-  test_unicode();
-  /* utf8.c */
-  test_utf8();
-  /* vector.c */
-  /* words.c */
-  test_casefold();
-  test_words();
-  /* wstat.c */
-  test_wstat();
-  /* signame.c */
-  test_signame();
-  /* cache.c */
-  test_cache();
-  /* selection.c */
-  test_selection();
-  test_hash();
-  test_url();
-  test_regsub();
-  test_bits();
-  test_vector();
-  test_syscalls();
-  test_trackname();
-  fprintf(stderr,  "%lld errors out of %lld tests\n", errors, tests);
-  return !!errors;
+  while((n = getopt_long(argc, argv, "vFhV", options, 0)) >= 0) {
+    switch(n) {
+    case 'v': verbose = 1; break;
+    case 'F': fail_first = 1; break;
+    case 'h': help();
+    case 'V': version(progname);
+    default: exit(1);
+    }
+  }
+  if(getenv("FAIL_FIRST"))
+    fail_first = 1;
 }
-  
+
+
 /*
 Local Variables:
 c-basic-offset:2