File: test.h

package info (click to toggle)
gbsplay 0.0.93-3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 488 kB
  • sloc: ansic: 5,581; sh: 861; makefile: 358
file content (76 lines) | stat: -rw-r--r-- 1,724 bytes parent folder | download | duplicates (2)
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
/*
 * gbsplay is a Gameboy sound player
 *
 * 2011 (C) by Tobias Diedrich <ranma+gbsplay@tdiedrich.de>
 * Licensed under GNU GPL.
 */

#ifndef _TEST_H_
#define _TEST_H_

#include <stdio.h>

#define ASSERT_EQUAL(fmt, a, b) do { \
	typeof(a) aa; \
	typeof(a) bb; \
	aa = a; \
	bb = b; \
	if (aa != bb) { \
		fprintf(stderr, "FAIL\nTest failed: "fmt"!="fmt" at %s:%d\n", aa, bb, __FILE__, __LINE__); \
		exit(1); \
	} \
} while(0)

#ifndef ENABLE_TEST

#define test static __attribute__((unused))
#define TEST(func) static __attribute__((unused)) int test_ ## func
#define TEST_EOF static __attribute__((unused)) int test_eof

#else

#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* 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;
}

#endif /* ENABLE_TEST */

#endif