File: dispatch_test.c

package info (click to toggle)
libdispatch 0~svn188-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,448 kB
  • ctags: 1,652
  • sloc: sh: 10,139; ansic: 8,999; makefile: 229; objc: 37; cpp: 14
file content (156 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download | duplicates (3)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "config/config.h"

#include <sys/types.h>
#include <sys/wait.h>

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/errno.h>
#include <string.h>

#include "dispatch_test.h"

#define _test_print(_file, _line, _desc, \
	_expr, _fmt1, _val1, _fmt2, _val2) do { \
	const char* _exprstr = _expr ? "PASS" : "FAIL"; \
	char _linestr[BUFSIZ]; \
	if (!_expr) { \
		snprintf(_linestr, sizeof(_linestr), \
			" (%s:%ld)", _file, _line); \
	} else { \
		_linestr[0] = 0; \
	} \
	if (_fmt2 == 0) { \
		printf("\tValue: " _fmt1 "\n"		\
			"[%s] %s%s\n",			\
			_val1,				\
			_exprstr,			\
			_desc,				\
			_linestr); 			\
	} else { \
		printf("\tActual: " _fmt1 "\n"	   \
			"\tExpected: " _fmt2 "\n"  \
			"[%s] %s%s\n",		   \
			_val1,			   \
			_val2,			   \
			_exprstr,		   \
			_desc,			   \
			_linestr);		   \
	} \
	if (!_expr) { \
		printf("\t%s:%ld\n", _file, _line); \
	} \
	fflush(stdout); \
} while (0);

void
test_start(const char* desc) {
	printf("\n==================================================\n");
	printf("[TEST] %s\n", desc);
	printf("[PID] %d\n", getpid());
	printf("==================================================\n\n");
	usleep(100000);	// give 'gdb --waitfor=' a chance to find this proc
}

#define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
void
_test_ptr_null(const char* file, long line, const char* desc, const void* ptr) {
	_test_print(file, line, desc,
		(ptr == NULL), "%p", ptr, "%p", (void*)0);
}

#define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
void
_test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr) {
	_test_print(file, line, desc,
		(ptr != NULL), "%p", ptr, "%p", ptr ?: (void*)~0);
}

#define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
void
_test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected) {
	_test_print(file, line, desc,
		(actual == expected), "%p", actual, "%p", expected);
}

#define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
void
_test_long(const char* file, long line, const char* desc, long actual, long expected) {
	_test_print(file, line, desc,
		(actual == expected), "%ld", actual, "%ld", expected);
}

#define test_long_less_than(a, b, c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
void
_test_long_less_than(const char* file, long line, const char* desc, long actual, long expected_max) {
	_test_print(file, line, desc, (actual < expected_max), "%ld", actual, "<%ld", expected_max);
}

#define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
void
_test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected) {
	_test_print(file, line, desc, (val < max_expected), "%f", val, "<%f", max_expected);
}

#define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
void
_test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected) {
	_test_print(file, line, desc, (val <= max_expected), "%f", val, "<%f", max_expected);
}

#define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
void
_test_errno(const char* file, long line, const char* desc, long actual, long expected) {
	char* actual_str;
	char* expected_str;
	asprintf(&actual_str, "%ld\t%s", actual, actual ? strerror(actual) : "");
	asprintf(&expected_str, "%ld\t%s", expected, expected ? strerror(expected) : "");
	_test_print(file, line, desc,
		(actual == expected), "%s", actual_str, "%s", expected_str);
	free(actual_str);
	free(expected_str);
}

#include <spawn.h>

extern char **environ;

void
test_stop(void) {
	test_stop_after_delay((void *)(intptr_t)0);
}

void
test_stop_after_delay(void *delay) {
#if HAVE_LEAKS
	int res;
	pid_t pid;
	char pidstr[10];
#endif

	if (delay != NULL) {
		sleep((int)(intptr_t)delay);
	}

#if HAVE_LEAKS
	if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS);

	/* leaks doesn't work against debug variant malloc */
	if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS);
	
	snprintf(pidstr, sizeof(pidstr), "%d", getpid());
	char* args[] = { "./leaks-wrapper", pidstr, NULL };
	res = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
	if (res == 0 && pid > 0) {
		int status;
		waitpid(pid, &status, 0);
		test_long("Leaks", status, 0);
	} else {
		perror(args[0]);
	}
#endif
	_exit(EXIT_SUCCESS);
}