1 #include "config/config.h"
11 #include <sys/errno.h>
14 #include "dispatch_test.h"
16 #define _test_print(_file, _line, _desc, \
17 _expr, _fmt1, _val1, _fmt2, _val2) do { \
18 const char* _exprstr = _expr ? "PASS" : "FAIL"; \
19 char _linestr[BUFSIZ]; \
21 snprintf(_linestr, sizeof(_linestr), \
22 " (%s:%ld)", _file, _line); \
27 printf("\tValue: " _fmt1 "\n" \
34 printf("\tActual: " _fmt1 "\n" \
35 "\tExpected: " _fmt2 "\n" \
44 printf("\t%s:%ld\n", _file, _line); \
50 test_start(const char* desc) {
51 printf("\n==================================================\n");
52 printf("[TEST] %s\n", desc);
53 printf("[PID] %d\n", getpid());
54 printf("==================================================\n\n");
55 usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
58 #define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
60 _test_ptr_null(const char* file, long line, const char* desc, const void* ptr) {
61 _test_print(file, line, desc,
62 (ptr == NULL), "%p", ptr, "%p", (void*)0);
65 #define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
67 _test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr) {
68 _test_print(file, line, desc,
69 (ptr != NULL), "%p", ptr, "%p", ptr ?: (void*)~0);
72 #define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
74 _test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected) {
75 _test_print(file, line, desc,
76 (actual == expected), "%p", actual, "%p", expected);
79 #define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
81 _test_long(const char* file, long line, const char* desc, long actual, long expected) {
82 _test_print(file, line, desc,
83 (actual == expected), "%ld", actual, "%ld", expected);
86 #define test_long_less_than(a, b, c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
88 _test_long_less_than(const char* file, long line, const char* desc, long actual, long expected_max) {
89 _test_print(file, line, desc, (actual < expected_max), "%ld", actual, "<%ld", expected_max);
92 #define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
94 _test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected) {
95 _test_print(file, line, desc, (val < max_expected), "%f", val, "<%f", max_expected);
98 #define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
100 _test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected) {
101 _test_print(file, line, desc, (val <= max_expected), "%f", val, "<%f", max_expected);
104 #define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
106 _test_errno(const char* file, long line, const char* desc, long actual, long expected) {
109 asprintf(&actual_str, "%ld\t%s", actual, actual ? strerror(actual) : "");
110 asprintf(&expected_str, "%ld\t%s", expected, expected ? strerror(expected) : "");
111 _test_print(file, line, desc,
112 (actual == expected), "%s", actual_str, "%s", expected_str);
119 extern char **environ;
123 test_stop_after_delay((void *)(intptr_t)0);
127 test_stop_after_delay(void *delay) {
135 sleep((int)(intptr_t)delay);
139 if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS);
141 /* leaks doesn't work against debug variant malloc */
142 if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS);
144 snprintf(pidstr, sizeof(pidstr), "%d", getpid());
145 char* args[] = { "./leaks-wrapper", pidstr, NULL };
146 res = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
147 if (res == 0 && pid > 0) {
149 waitpid(pid, &status, 0);
150 test_long("Leaks", status, 0);