summaryrefslogtreecommitdiff
path: root/api_test/harness.c
blob: e8f320c54a391dc53eaccce6340d8d8c03b0e06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "harness.h"

test_batch_runner*
test_batch_runner_new()
{
	return (test_batch_runner *)calloc(1, sizeof(test_batch_runner));
}

static void
test_result(test_batch_runner *runner, int cond, const char *msg, va_list ap)
{
	++runner->test_num;

	if (cond) {
		++runner->num_passed;
	}
	else {
		fprintf(stderr, "FAILED test %d: ", runner->test_num);
		vfprintf(stderr, msg, ap);
		fprintf(stderr, "\n");
		++runner->num_failed;
	}
}

void
SKIP(test_batch_runner *runner, int num_tests)
{
	runner->test_num    += num_tests;
	runner->num_skipped += num_tests;
}

void
OK(test_batch_runner *runner, int cond, const char *msg, ...)
{
	va_list ap;
	va_start(ap, msg);
	test_result(runner, cond, msg, ap);
	va_end(ap);
}

void
INT_EQ(test_batch_runner *runner, int got, int expected, const char *msg, ...)
{
	int cond = got == expected;

	va_list ap;
	va_start(ap, msg);
	test_result(runner, cond, msg, ap);
	va_end(ap);

	if (!cond) {
		fprintf(stderr, "  Got:      %d\n", got);
		fprintf(stderr, "  Expected: %d\n", expected);
	}
}

void
STR_EQ(test_batch_runner *runner, const char *got, const char *expected,
       const char *msg, ...)
{
	int cond = strcmp(got, expected) == 0;

	va_list ap;
	va_start(ap, msg);
	test_result(runner, cond, msg, ap);
	va_end(ap);

	if (!cond) {
		fprintf(stderr, "  Got:      \"%s\"\n", got);
		fprintf(stderr, "  Expected: \"%s\"\n", expected);
	}
}

int
test_ok(test_batch_runner *runner)
{
	return runner->num_failed == 0;
}

void
test_print_summary(test_batch_runner *runner)
{
	int num_passed  = runner->num_passed;
	int num_skipped = runner->num_skipped;
	int num_failed  = runner->num_failed;

	fprintf(stderr, "%d tests passed, %d failed, %d skipped\n",
		num_passed, num_failed, num_skipped);

	if (test_ok(runner)) {
		fprintf(stderr, "PASS\n");
	}
	else {
		fprintf(stderr, "FAIL\n");
	}
}