xref: /openbsd-src/regress/lib/libpthread/include/test.h (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: test.h,v 1.1.1.1 2001/08/15 14:37:10 fgsch Exp $	*/
2 
3 #ifndef _h_test_
4 #define _h_test_
5 
6 #include <stdio.h>
7 #include <signal.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <stdarg.h>
11 
12 int	_thread_sys_write __P((int, const char*, size_t));
13 __dead void _thread_sys__exit __P((int)) __attribute__((__noreturn__));
14 
15 static __dead void __vpanic __P((const char *, const char *, const char *,
16 	int, const char *, va_list)) __attribute__((__noreturn__));
17 static __dead void __panic __P((const char *, const char *, const char *,
18 	int, const char *, ...)) __attribute__((__noreturn__));
19 
20 #if defined(__OpenBSD__) || defined(__FreeBSD__)
21 #include <pthread.h>
22 #include <pthread_np.h>
23 void	_thread_dump_info __P((void));
24 #define SET_NAME(x)	pthread_set_name_np(pthread_self(), x)
25 #define DUMP_INFO()	_thread_dump_info()
26 #else
27 #define SET_NAME(x)	/* nada */
28 #define DUMP_INFO()	/* nada */
29 #endif
30 
31 static void
32 __vpanic(type, errstr, filenm, lineno, fmt, ap)
33 	const char *type;
34 	const char *errstr;
35 	const char *filenm;
36 	int lineno;
37 	const char *fmt;
38 	va_list ap;
39 {
40 	char buf[1024];
41 
42 	/* "<type> at <filenm>:<lineno>: <fmt ap...>:<errstr>" */
43 	snprintf(buf, sizeof buf, "%s at %s:%d\n", type, filenm, lineno);
44 	_thread_sys_write(2, buf, strlen(buf));
45 	vsnprintf(buf, sizeof buf, fmt, ap);
46 	if (errstr != NULL) {
47 		strlcat(buf, ": ", sizeof buf);
48 		strlcat(buf, errstr, sizeof buf);
49 	}
50 	strlcat(buf, "\n", sizeof buf);
51 	_thread_sys_write(2, buf, strlen(buf));
52 
53 	DUMP_INFO();
54 	_thread_sys__exit(1);
55 
56 	_thread_sys_write(2, "[locking]\n", 10);
57 	while(1);
58 }
59 
60 static void
61 __panic(type, errstr, filenm, lineno, fmt)
62 	const char *type;
63 	const char *errstr;
64 	const char *filenm;
65 	int lineno;
66 	const char *fmt;
67 {
68 	va_list ap;
69 
70 	va_start(ap, fmt);
71 	__vpanic(type, errstr, filenm, lineno, fmt, ap);
72 	va_end(ap);
73 }
74 
75 #define DIE(e, m, args...) \
76 	__panic("died", strerror(e), __FILE__, __LINE__, m , ## args)
77 
78 #define PANIC(m, args...)  \
79 	__panic("panic", NULL, __FILE__, __LINE__, m, ## args)
80 
81 #define ASSERT(x) do { \
82 	if (!(x)) \
83 		__panic("assert failed", NULL, __FILE__, __LINE__, "%s", #x); \
84 } while(0)
85 
86 #define ASSERTe(x,rhs) do { \
87 	int _x; \
88 	_x = (x); \
89 	if (!(_x rhs)) { \
90 	    if (_x > 0) \
91 		__panic("assert failed", strerror(_x), __FILE__, __LINE__,  \
92 		    "%s %s", #x, #rhs); \
93 	    else \
94 		__panic("assert failed", NULL, __FILE__, __LINE__, \
95 		    "%s [=%d] %s", #x, _x, #rhs); \
96 	} \
97 } while(0)
98 
99 #define _T(x) __builtin_classify_type(x)
100 
101 #define _CHECK(x, rhs, efn) do { \
102 	typeof(x) _x; \
103 	_x = (x); \
104 	if (!(_x rhs)) \
105 		__panic("check failed", efn, __FILE__, __LINE__, \
106 		   ((_T(0) == _T(_x)   )? "failed check %s (=%d) %s " : \
107 		    (_T("") == _T(_x)  )? "failed check %s (=%s) %s " : \
108 		    (_T('x') == _T(_x) )? "failed check %s (=%c) %s " : \
109 		    (_T(0L) == _T(_x)  )? "failed check %s (=%ld) %s " : "?") \
110 		    , #x, _x, #rhs); \
111 } while(0)
112 
113 #define CHECKr(x) _CHECK(x, == 0, strerror(_x))
114 #define CHECKe(x) _CHECK(x, != -1, strerror(errno))
115 #define CHECKn(x) _CHECK(x, != 0, strerror(errno))
116 #define CHECKhn(x) _CHECK(x, != 0, hstrerror(h_errno))
117 
118 #define SUCCEED 	exit(0)
119 
120 #define OK		(0)
121 #define NOTOK		(-1)
122 
123 #endif _h_test_
124