summaryrefslogtreecommitdiffstats
path: root/test/test-lib.h
blob: de9c10fde4ecfbc388988c9504bbb19ba4d592a1 (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
#ifndef TEST_LIB
#define TEST_LIB

#include <config.h>
#include <stdlib.h>

struct lt_config_shared *config_init(void);
void config_clear(struct lt_config_shared *sh);
int fout_read(struct lt_config_shared *sh, char *buf, int size);

enum {
	RE_TEST_TYPE_STR = 1,
	RE_TEST_TYPE_INT = 2,
	RE_TEST_TYPE_PID = 3,
};

enum {
	RE_TEST_OFF_UNDEF = -1,
};

enum {
	RE_TEST_OK = -1,
};

struct re_test_data {
	int type;
	int so;
	int eo;
	char *pat;
};

int re_test(char *line, struct re_test_data *data, int cnt);

#define TEST_START() sh->disabled = 0
#define TEST_STOP()  sh->disabled = 1

#define PASSED() \
do { \
	printf("."); \
	fflush(NULL); \
} while(0)

#define FAILED(fmt, args...) \
do { \
	char lpbuf[1024]; \
	sprintf(lpbuf, "%s:%d failed - %s", \
		__FUNCTION__, \
		__LINE__, \
		fmt); \
	printf(lpbuf, ## args); \
	fflush(NULL); \
	exit(-1); \
} while(0)

#define TEST(testfn) \
do { \
	if (testfn(sh)) { \
		printf("FAILED %s\n", #testfn); \
		return -1; \
	} \
} while(0)

#define BUFSIZE 1000

#define LOCAL_TEST(data, cnt, test) \
do { \
	char buf[BUFSIZE]; \
	int ret; \
	TEST_START(); \
	test; \
	TEST_STOP(); \
	ret = fout_read(sh, buf, BUFSIZE); \
	if (!ret) \
		return -1; \
	ret = re_test(buf, data, cnt); \
	if (RE_TEST_OK != ret) \
		FAILED("test %i, pattern '%s'\n", ret, data[ret].pat); \
} while(0)

#endif