]> pd.if.org Git - ctap/commitdiff
reverse test parameters, add prove
authorNathan Wagner <nw@hydaspes.if.org>
Sun, 17 Jun 2018 17:36:17 +0000 (17:36 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sun, 17 Jun 2018 17:36:17 +0000 (17:36 +0000)
Makefile
ctap.c
ctap.h
main.c
prove.c

index 3408d8c1491374f94a2b039669f228ea0ea07fc7..e416fabec2304c616554ef753bf18c704b6ccfa7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,4 +8,4 @@ clean:
        rm -rf *.o main
 
 test:  main prove
-       ./prove ./main
+       @./prove ./main
diff --git a/ctap.c b/ctap.c
index 9bdf9f395b541d275a2ae93453389ac972e73d93..02fadf17232d5861cee04286225ea54cf23ca1c1 100644 (file)
--- a/ctap.c
+++ b/ctap.c
@@ -171,69 +171,97 @@ void diag(const char *fmt, ...) {
        printf("\n");
 }
 
-void is_hex(unsigned long wanted, unsigned long seen, const char *fmt, ...) {
+void is_hex(unsigned long have, unsigned long want, const char *fmt, ...) {
        va_list args;
+       int pass;
+
+       pass = have == want;
        if (fmt) {
                va_start(args, fmt);
-               okv(wanted == seen, fmt, args);
+               okv(pass, fmt, args);
                va_end(args);
        } else {
-               ok(wanted == seen, NULL);
+               ok(pass, NULL);
        }
-       if (wanted != seen) {
-               diag("wanted: %ld", wanted);
-               diag("got   : %ld", seen);
+       if (!pass) {
+               diag("wanted: %ld", want);
+               diag("got   : %ld", have);
        }
 }
 
+void is_int(long have, long want, const char *fmt, ...) {
+       va_list args;
+       int pass;
+
+       pass = have == want;
+       if (fmt) {
+               va_start(args, fmt);
+               okv(pass, fmt, args);
+               va_end(args);
+       } else {
+               ok(pass, NULL);
+       }
+       if (!pass) {
+               diag("wanted: %ld", want);
+               diag("got   : %ld", have);
+       }
+}
 
-void is_int(long wanted, long seen, const char *fmt, ...) {
+void is_double(double have, double want, double eps, const char *fmt, ...) {
+       int pass;
        va_list args;
+
+       pass = fabs(want - have) <= eps;
        if (fmt) {
                va_start(args, fmt);
-               okv(wanted == seen, fmt, args);
+               okv(pass, fmt, args);
                va_end(args);
        } else {
-               ok(wanted == seen, NULL);
+               ok(pass, NULL);
        }
-       if (wanted != seen) {
-               diag("wanted: %ld", wanted);
-               diag("got   : %ld", seen);
+       if (!pass) {
+               diag("wanted: %f", want);
+               diag("got   : %f", have);
        }
 }
 
-void is_double(double wanted, double seen, double eps, const char *fmt, ...) {
+void is_compare(void *have, void *want,
+               int (*cmp)(void *,void *),
+               const char *fmt, ...
+               ) {
        int pass;
        va_list args;
 
-       pass = fabs(wanted - seen) <= eps;
+       pass = !cmp(have, want);
        if (fmt) {
                va_start(args, fmt);
                okv(pass, fmt, args);
                va_end(args);
        } else {
-               ok(wanted == seen, NULL);
+               ok(pass, NULL);
        }
+       /* no way to print them with out more functions
        if (!pass) {
-               diag("wanted: %f", wanted);
-               diag("got   : %f", seen);
+               diag("wanted: %s", want);
+               diag("got   : %s", have);
        }
+       */
 }
 
-void is_string(const char *wanted, const char *seen, const char *fmt, ...) {
+void is_string(const char *have, const char *want, const char *fmt, ...) {
        int pass;
        va_list args;
 
-       pass = !strcmp(wanted,seen);
+       pass = !strcmp(have,want);
        if (fmt) {
                va_start(args, fmt);
                okv(pass, fmt, args);
                va_end(args);
        } else {
-               ok(wanted == seen, NULL);
+               ok(pass, NULL);
        }
        if (!pass) {
-               diag("wanted: %s", wanted);
-               diag("got   : %s", seen);
+               diag("wanted: %s", want);
+               diag("got   : %s", have);
        }
 }
diff --git a/ctap.h b/ctap.h
index c641d2f6f7e3e164ecbe506a53df51193b97770e..17135225b87933dd84526029a80daad2faa594db 100644 (file)
--- a/ctap.h
+++ b/ctap.h
@@ -21,5 +21,8 @@ void is_hex(unsigned long wanted, unsigned long seen, const char *fmt, ...);
 void is_int(long wanted, long seen, const char *fmt, ...);
 void is_double(double wanted, double seen, double eps, const char *fmt, ...);
 void is_string(const char *wanted, const char *seen, const char *fmt, ...);
+void is_compare(void *have, void *want, int (*cmp)(void *,void *),
+                const char *fmt, ...
+                ); 
 
 #endif
diff --git a/main.c b/main.c
index 55fbecd465948abc5b2397d1dd05d838a7f1c10b..75bbf0dbdeeb64a1717e40f4b4aaabe62a646c35 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,3 +1,5 @@
+#include <string.h>
+
 #include "ctap.h"
 
 int main(void) {
@@ -22,5 +24,7 @@ int main(void) {
        is_double(1.0,1.1,0.5,"is_double epsilon pass");
        is_string("foo", "foo", "is_string pass");
 
+       is_compare("foo", "foo", (int (*)(void *, void *))strcmp, "string via strcmp");
+
        return 0;
 }
diff --git a/prove.c b/prove.c
index a0931c4c71e88484bcfc8826d7f214549536c8b2..afb5a73e2c5a16742562a00e1697f413c2582d07 100644 (file)
--- a/prove.c
+++ b/prove.c
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <regex.h>
@@ -72,6 +73,9 @@ int runone(struct testrun *run) {
                close(1); /* close stdout */
                dup(pipefd[1]); /* set stdout to the write end of the pipe */
                execlp(run->name, run->name, NULL);
+               /* only gets here if exec failed */
+               printf("Bail Out! exec %s failed: %s\n", run->name, strerror(errno));
+               exit(EXIT_FAILURE);
        }
 
        close(pipefd[1]);
@@ -120,6 +124,15 @@ int runone(struct testrun *run) {
                        int skip = 0;
                        int pass = 0;
 
+                       run->ran++;
+
+                       if (run->interactive) {
+                               backup(written);
+                               written = printf("%d/%d", run->ran, run->plan);
+                               fflush(stdout);
+                       }
+
+
                        /* if the "not" match fails, the test passed */
                        pass = match[1].rm_so == -1;
 
@@ -133,26 +146,26 @@ int runone(struct testrun *run) {
                        }
 
                        /* check for diagnostics */
-                       if (test != run->ran+1) {
+                       if (test != run->ran) {
                                fprintf(stderr, "expected test %d, got %d\n", run->ran+1, test);
                        }
 
-                       run->ran++;
-
-                       if (pass || todo) {
+                       /* pass, todo, or skip are all pass */
+                       if (pass || todo || skip) {
                                run->pass++;
+                       } else {
+                               run->fail++;
                        }
 
-                       if (todo) {
-                               if (pass) {
-                                       run->todo_pass++;
-                               }
+                       if (todo && pass) {
+                               run->todo_pass++;
                        }
+
                        if (skip) {
                                run->skip++;
                        }
 
-                       if (!pass) {
+                       if (!pass && !todo) {
                                struct result *r;
                                r = malloc(sizeof *r);
 
@@ -168,11 +181,6 @@ int runone(struct testrun *run) {
                                /* push test number onto fail list */
                        }
 
-                       if (run->interactive) {
-                               backup(written);
-                               written = printf("%d/%d", run->ran, run->plan);
-                               fflush(stdout);
-                       }
                }
                else if (rmatch(&diagnostic, line, match)) {
 
@@ -280,7 +288,6 @@ int main(int ac, char *av[]) {
        for (i=optind; i<ac; i++) {
                run = clear;
                run.name = av[i];
-               run.plan = -1;
                run.interactive = interactive;
 
                if (progress) {
@@ -298,20 +305,31 @@ int main(int ac, char *av[]) {
                total.ran += run.ran;
                total.pass += run.pass;
                total.fail += run.fail;
+               total.skip += run.skip;
 
                if (progress) {
-                       if (run.fail > 0 ) printf("not ");
-                       printf("ok\n");
+                       if (run.bailout) {
+                               printf("Bail out!\n");
+                       } else {
+                               if (run.fail > 0 ) printf("not ");
+                               printf("ok\n");
+                       }
                }
 
                runs[i-optind] = run;
        }
 
        if (summary) {
-               printf("ran: %d/%d, pass: %d, fail: %d, %.2f%% ok\n",
+               printf("ran: %d/%d, pass: %d, fail: %d, skip: %d",
                                total.ran, total.plan, total.pass, total.fail,
-                               100.0*(double)total.pass/(double)total.plan
+                               total.skip
                      );
+               if (total.plan > 0) {
+                       printf(", %.2f%% ok",
+                               100.0*(double)total.pass/(double)total.plan
+                             );
+               }
+               printf("\n");
 
        }