dyld-733.8.tar.gz
[apple/dyld.git] / testing / include / test_support.h
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <unistd.h>
4 #include <mach-o/dyld.h>
5 #include <sys/param.h>
6
7 #if __LP64__
8 extern struct mach_header_64 __dso_handle;
9 #else
10 extern struct mach_header __dso_handle;
11 #endif
12
13 static bool sIsATTY = false;
14 static const char * sTestName = NULL;
15 static uint64_t sTestCount = 0;
16
17 __attribute__((constructor))
18 static
19 void BEGIN(int argc, const char* argv[], const char* envp[]) {
20 // Set up values we need to print in PASS() and FAIL()
21 sIsATTY = isatty(fileno(stdout));
22 sTestName = argv[0];
23 // Early returnif this not the main executbale, we only need to print the [BEGIN] line once
24 if (__dso_handle.filetype != MH_EXECUTE) {
25 return;
26 }
27 printf("[BEGIN]");
28 for (uint32_t i = 0; envp[i] != NULL; ++i) {
29 if (strncmp("DYLD_", envp[i], 5) == 0) {
30 printf(" %s", envp[i]);
31 }
32 }
33 char buffer[MAXPATHLEN];
34 uint32_t bufsize = MAXPATHLEN;
35 if (_NSGetExecutablePath(buffer, &bufsize) == 0) {
36 printf(" %s", buffer);
37 } else {
38 printf(" %s", argv[0]);
39 }
40 for (uint32_t i = 1; i < argc; ++i) {
41 printf (" %s", argv[i]);
42 }
43 printf("\n");
44 }
45
46 __attribute__((format(printf, 1, 2)))
47 static
48 void PASS(const char *format, ...) {
49 if (sIsATTY) {
50 printf("[\033[0;32mPASS\033[0m] %s (%llu): ", sTestName, sTestCount++);
51 } else {
52 printf("[PASS] %s (%llu): ", sTestName, sTestCount++);
53 }
54 va_list args;
55 va_start (args, format);
56 vprintf (format, args);
57 va_end (args);
58 printf("\n");
59 }
60
61 __attribute__((format(printf, 1, 2)))
62 static
63 void FAIL(const char *format, ...) {
64 if (sIsATTY) {
65 printf("[\033[0;31mFAIL\033[0m] %s (%llu): ", sTestName, sTestCount++);
66 } else {
67 printf("[FAIL] %s (%llu): ", sTestName, sTestCount++);
68 }
69 va_list args;
70 va_start (args, format);
71 vprintf (format, args);
72 va_end (args);
73 printf("\n");
74 }