-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.h
111 lines (91 loc) · 2.45 KB
/
test.h
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
103
104
105
106
107
108
109
110
111
/*
* gbsplay is a Gameboy sound player
*
* 2011-2020 (C) by Tobias Diedrich <ranma+gbsplay@tdiedrich.de>
*
* Licensed under GNU GPL v1 or, at your option, any later version.
*/
#ifndef _TEST_H_
#define _TEST_H_
#include <stdio.h>
#include <stdlib.h>
#define ASSERT_EQUAL(fmt, a, b) do { \
if ((a) != (b)) { \
fprintf(stderr, "FAIL\nTest failed: "fmt"!="fmt" at %s:%d\n", (a), (b), __FILE__, __LINE__); \
exit(1); \
} \
} while(0)
#define ASSERT_ARRAY_EQUAL(fmt, a, b) do { \
int pass = 1; \
int i; \
char *cmp; \
for (i=0; i<sizeof(a)/sizeof(*(a)); i++) { \
if ((a)[i] == (b)[i]) \
continue; \
pass = 0; \
break; \
} \
if (pass) \
return; \
fprintf(stderr, "FAIL\nTest failed at %s:%d\n", __FILE__, __LINE__); \
for (i=0; i<sizeof(a)/sizeof(*(a)); i++) { \
if ((a)[i] == (b)[i]) \
cmp = "=="; \
else \
cmp = "!="; \
fprintf(stderr, "%4d: "fmt" %s "fmt"\n", i, (a)[i], cmp, (b)[i]); \
} \
exit(1); \
} while(0)
#ifdef ENABLE_TEST
#if defined(__GNUC__) && !defined(__APPLE__)
#include "config.h"
#include "common.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define test __attribute__((section(".test")))
#define test_entries __attribute__((section(".test_entries")))
typedef void (*test_fn)(void);
struct test_entry {
test_fn func;
const char* const name;
};
#define TEST(func) test_entries struct test_entry test_ ## func = { func, #func }
#define TEST_EOF test_entries struct test_entry test__end = { 0 };
test_entries struct test_entry test__head = { 0 };
test_entries struct test_entry test__align = { 0 };
extern struct test_entry test__end;
int main(int argc, char** argv)
{
void *head = &test__head;
void *align = &test__align;
if ((align - head) != sizeof(test__head)) {
fprintf(stderr, "Expected alignment constraints don't hold!\n");
exit(1);
}
struct test_entry *tests = &test__head;
int num_tests = (&test__end - &test__head);
int i;
printf(" %d tests:\n", num_tests-2);
for (i=2; i<num_tests; i++) {
printf(" %s: ", tests[i].name);
tests[i].func();
printf("ok\n");
}
return 0;
}
#else
int main(int argc, char** argv)
{
printf(" builtin tests not supported on this platform.\n");
return 0;
}
#endif /* defined(__GNUC__) && !defined(__APPLE__) */
#endif /* ENABLE_TEST */
#ifndef TEST_EOF
#define test static __attribute__((unused))
#define TEST(func) static __attribute__((unused)) int test_ ## func
#define TEST_EOF static __attribute__((unused)) int test_eof
#endif
#endif /* _TEST_H_ */