cmark

My personal build of CMark ✏️

harness.c (2014B)

 1 #include <stdarg.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 
 6 #include "harness.h"
 7 
 8 test_batch_runner *test_batch_runner_new() {
 9   return (test_batch_runner *)calloc(1, sizeof(test_batch_runner));
10 }
11 
12 static void test_result(test_batch_runner *runner, int cond, const char *msg,
13                         va_list ap) {
14   ++runner->test_num;
15 
16   if (cond) {
17     ++runner->num_passed;
18   } else {
19     fprintf(stderr, "FAILED test %d: ", runner->test_num);
20     vfprintf(stderr, msg, ap);
21     fprintf(stderr, "\n");
22     ++runner->num_failed;
23   }
24 }
25 
26 void SKIP(test_batch_runner *runner, int num_tests) {
27   runner->test_num += num_tests;
28   runner->num_skipped += num_tests;
29 }
30 
31 void OK(test_batch_runner *runner, int cond, const char *msg, ...) {
32   va_list ap;
33   va_start(ap, msg);
34   test_result(runner, cond, msg, ap);
35   va_end(ap);
36 }
37 
38 void INT_EQ(test_batch_runner *runner, int got, int expected, const char *msg,
39             ...) {
40   int cond = got == expected;
41 
42   va_list ap;
43   va_start(ap, msg);
44   test_result(runner, cond, msg, ap);
45   va_end(ap);
46 
47   if (!cond) {
48     fprintf(stderr, "  Got:      %d\n", got);
49     fprintf(stderr, "  Expected: %d\n", expected);
50   }
51 }
52 
53 void STR_EQ(test_batch_runner *runner, const char *got, const char *expected,
54             const char *msg, ...) {
55   int cond = strcmp(got, expected) == 0;
56 
57   va_list ap;
58   va_start(ap, msg);
59   test_result(runner, cond, msg, ap);
60   va_end(ap);
61 
62   if (!cond) {
63     fprintf(stderr, "  Got:      \"%s\"\n", got);
64     fprintf(stderr, "  Expected: \"%s\"\n", expected);
65   }
66 }
67 
68 int test_ok(test_batch_runner *runner) { return runner->num_failed == 0; }
69 
70 void test_print_summary(test_batch_runner *runner) {
71   int num_passed = runner->num_passed;
72   int num_skipped = runner->num_skipped;
73   int num_failed = runner->num_failed;
74 
75   fprintf(stderr, "%d tests passed, %d failed, %d skipped\n", num_passed,
76           num_failed, num_skipped);
77 
78   if (test_ok(runner)) {
79     fprintf(stderr, "PASS\n");
80   } else {
81     fprintf(stderr, "FAIL\n");
82   }
83 }