1 #include "test.h"
2
3 #include <stdio.h>
4 #include <stdarg.h>
5
6 enum { MAX_LEVELS = 10 };
7 static int tests_done[MAX_LEVELS];
8 static int tests_failed[MAX_LEVELS];
9 static int plan_test[MAX_LEVELS];
10 static int level = -1;
11
12 void
__space(FILE * stream)13 __space(FILE *stream)
14 {
15 for (int i = 0 ; i < level; i++) {
16 fprintf(stream, " ");
17 }
18 }
19
20 void
plan(int count)21 plan(int count)
22 {
23 ++level;
24 plan_test[level] = count;
25 tests_done[level] = 0;
26 tests_failed[level] = 0;
27
28 __space(stdout);
29 printf("%d..%d\n", 1, plan_test[level]);
30 }
31
32 int
check_plan(void)33 check_plan(void)
34 {
35 int r = 0;
36 if (tests_done[level] != plan_test[level]) {
37 __space(stderr);
38 fprintf(stderr,
39 "# Looks like you planned %d tests but ran %d.\n",
40 plan_test[level], tests_done[level]);
41 r = -1;
42 }
43
44 if (tests_failed[level]) {
45 __space(stderr);
46 fprintf(stderr,
47 "# Looks like you failed %d test of %d run.\n",
48 tests_failed[level], tests_done[level]);
49 r = tests_failed[level];
50 }
51 --level;
52 if (level >= 0) {
53 is(r, 0, "subtests");
54 }
55 return r;
56 }
57
58 int
__ok(int condition,const char * fmt,...)59 __ok(int condition, const char *fmt, ...)
60 {
61 va_list ap;
62
63 __space(stdout);
64 printf("%s %d - ", condition ? "ok" : "not ok", ++tests_done[level]);
65 if (!condition)
66 tests_failed[level]++;
67 va_start(ap, fmt);
68 vprintf(fmt, ap);
69 printf("\n");
70 return condition;
71 }
72
73