]>
git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_test.c
9 #include "dispatch_test.h"
11 #define _test_print(_file, _line, _desc, \
12 _expr, _fmt1, _val1, _fmt2, _val2) do { \
13 const char* _exprstr = _expr ? "PASS" : "FAIL"; \
14 char _linestr[BUFSIZ]; \
16 snprintf(_linestr, sizeof(_linestr), \
17 " (%s:%ld)", _file, _line); \
22 printf("\tValue: " _fmt1 "\n" \
29 printf("\tActual: " _fmt1 "\n" \
30 "\tExpected: " _fmt2 "\n" \
39 printf("\t%s:%ld\n", _file, _line); \
45 test_start(const char* desc
) {
46 printf("\n==================================================\n");
47 printf("[TEST] %s\n", desc
);
48 printf("[PID] %d\n", getpid());
49 printf("==================================================\n\n");
50 usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
53 #define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
55 _test_ptr_null(const char* file
, long line
, const char* desc
, const void* ptr
) {
56 _test_print(file
, line
, desc
,
57 (ptr
== NULL
), "%p", ptr
, "%p", (void*)0);
60 #define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
62 _test_ptr_notnull(const char* file
, long line
, const char* desc
, const void* ptr
) {
63 _test_print(file
, line
, desc
,
64 (ptr
!= NULL
), "%p", ptr
, "%p", ptr
?: (void*)~0);
67 #define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
69 _test_ptr(const char* file
, long line
, const char* desc
, const void* actual
, const void* expected
) {
70 _test_print(file
, line
, desc
,
71 (actual
== expected
), "%p", actual
, "%p", expected
);
74 #define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
76 _test_long(const char* file
, long line
, const char* desc
, long actual
, long expected
) {
77 _test_print(file
, line
, desc
,
78 (actual
== expected
), "%ld", actual
, "%ld", expected
);
81 #define test_long_less_than(a, b, c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
83 _test_long_less_than(const char* file
, long line
, const char* desc
, long actual
, long expected_max
) {
84 _test_print(file
, line
, desc
, (actual
< expected_max
), "%ld", actual
, "<%ld", expected_max
);
87 #define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
89 _test_double_less_than(const char* file
, long line
, const char* desc
, double val
, double max_expected
) {
90 _test_print(file
, line
, desc
, (val
< max_expected
), "%f", val
, "<%f", max_expected
);
93 #define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
95 _test_double_less_than_or_equal(const char* file
, long line
, const char* desc
, double val
, double max_expected
) {
96 _test_print(file
, line
, desc
, (val
<= max_expected
), "%f", val
, "<%f", max_expected
);
99 #define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
101 _test_errno(const char* file
, long line
, const char* desc
, long actual
, long expected
) {
104 asprintf(&actual_str
, "%ld\t%s", actual
, actual
? strerror(actual
) : "");
105 asprintf(&expected_str
, "%ld\t%s", expected
, expected
? strerror(expected
) : "");
106 _test_print(file
, line
, desc
,
107 (actual
== expected
), "%s", actual_str
, "%s", expected_str
);
114 extern char **environ
;
118 test_stop_after_delay((void *)(intptr_t)0);
122 test_stop_after_delay(void *delay
) {
128 sleep((int)(intptr_t)delay
);
131 if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS
);
133 /* leaks doesn't work against debug variant malloc */
134 if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS
);
136 snprintf(pidstr
, sizeof(pidstr
), "%d", getpid());
137 char* args
[] = { "./leaks-wrapper", pidstr
, NULL
};
138 res
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
139 if (res
== 0 && pid
> 0) {
141 waitpid(pid
, &status
, 0);
142 test_long("Leaks", status
, 0);