Tizen 2.1 base
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_test.c
1 #include "config/config.h"
2
3 #include <sys/types.h>
4 #include <sys/wait.h>
5
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <sys/errno.h>
12 #include <string.h>
13
14 #include "dispatch_test.h"
15
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]; \
20         if (!_expr) { \
21                 snprintf(_linestr, sizeof(_linestr), \
22                         " (%s:%ld)", _file, _line); \
23         } else { \
24                 _linestr[0] = 0; \
25         } \
26         if (_fmt2 == 0) { \
27                 printf("\tValue: " _fmt1 "\n"           \
28                         "[%s] %s%s\n",                  \
29                         _val1,                          \
30                         _exprstr,                       \
31                         _desc,                          \
32                         _linestr);                      \
33         } else { \
34                 printf("\tActual: " _fmt1 "\n"     \
35                         "\tExpected: " _fmt2 "\n"  \
36                         "[%s] %s%s\n",             \
37                         _val1,                     \
38                         _val2,                     \
39                         _exprstr,                  \
40                         _desc,                     \
41                         _linestr);                 \
42         } \
43         if (!_expr) { \
44                 printf("\t%s:%ld\n", _file, _line); \
45         } \
46         fflush(stdout); \
47 } while (0);
48
49 void
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
56 }
57
58 #define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
59 void
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);
63 }
64
65 #define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
66 void
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);
70 }
71
72 #define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
73 void
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);
77 }
78
79 #define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
80 void
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);
84 }
85
86 #define test_long_less_than(a, b, c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
87 void
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);
90 }
91
92 #define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
93 void
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);
96 }
97
98 #define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
99 void
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);
102 }
103
104 #define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
105 void
106 _test_errno(const char* file, long line, const char* desc, long actual, long expected) {
107         char* actual_str;
108         char* expected_str;
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);
113         free(actual_str);
114         free(expected_str);
115 }
116
117 #include <spawn.h>
118
119 extern char **environ;
120
121 void
122 test_stop(void) {
123         test_stop_after_delay((void *)(intptr_t)0);
124 }
125
126 void
127 test_stop_after_delay(void *delay) {
128 #if HAVE_LEAKS
129         int res;
130         pid_t pid;
131         char pidstr[10];
132 #endif
133
134         if (delay != NULL) {
135                 sleep((int)(intptr_t)delay);
136         }
137
138 #if HAVE_LEAKS
139         if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS);
140
141         /* leaks doesn't work against debug variant malloc */
142         if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS);
143         
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) {
148                 int status;
149                 waitpid(pid, &status, 0);
150                 test_long("Leaks", status, 0);
151         } else {
152                 perror(args[0]);
153         }
154 #endif
155         _exit(EXIT_SUCCESS);
156 }