| 1 |
|
| 2 |
#pragma once |
| 3 |
|
| 4 |
#ifdef UNIT_TEST |
| 5 |
|
| 6 |
#include <stdbool.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ |
| 12 |
defined(__NetBSD__) || defined(__OpenBSD__) |
| 13 |
#define USE_SYSCTL_FOR_ARGS 1 |
| 14 |
|
| 15 |
#include <sys/types.h> |
| 16 |
#include <sys/sysctl.h> |
| 17 |
|
| 18 |
#include <unistd.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
struct test_file_metadata; |
| 22 |
|
| 23 |
struct test_failure { |
| 24 |
bool present; |
| 25 |
const char *message; |
| 26 |
const char *file; |
| 27 |
int line; |
| 28 |
bool owned; |
| 29 |
}; |
| 30 |
|
| 31 |
struct test_case_metadata { |
| 32 |
void (*fn)(struct test_case_metadata *, struct test_file_metadata *); |
| 33 |
struct test_failure failure; |
| 34 |
const char *name; |
| 35 |
struct test_case_metadata *next; |
| 36 |
}; |
| 37 |
|
| 38 |
struct test_file_metadata { |
| 39 |
bool registered; |
| 40 |
const char *name; |
| 41 |
struct test_file_metadata *next; |
| 42 |
struct test_case_metadata *tests; |
| 43 |
}; |
| 44 |
|
| 45 |
struct test_file_metadata __attribute__((weak)) * test_file_head; |
| 46 |
|
| 47 |
#define SET_FAILURE(_message, _owned) \ |
| 48 |
metadata->failure = (struct test_failure) { \ |
| 49 |
.message = _message, .file = __FILE__, .line = __LINE__, \ |
| 50 |
.present = true, .owned = _owned, \ |
| 51 |
} |
| 52 |
|
| 53 |
#define TEST_EQUAL(a, b) \ |
| 54 |
do { \ |
| 55 |
if ((a) != (b)) { \ |
| 56 |
SET_FAILURE(#a " != " #b, false); \ |
| 57 |
return; \ |
| 58 |
} \ |
| 59 |
} while (0) |
| 60 |
|
| 61 |
#define TEST_NOTEQUAL(a, b) \ |
| 62 |
do { \ |
| 63 |
if ((a) == (b)) { \ |
| 64 |
SET_FAILURE(#a " == " #b, false); \ |
| 65 |
return; \ |
| 66 |
} \ |
| 67 |
} while (0) |
| 68 |
|
| 69 |
#define TEST_TRUE(a) \ |
| 70 |
do { \ |
| 71 |
if (!(a)) { \ |
| 72 |
SET_FAILURE(#a " is not true", false); \ |
| 73 |
return; \ |
| 74 |
} \ |
| 75 |
} while (0) |
| 76 |
|
| 77 |
#define TEST_STREQUAL(a, b) \ |
| 78 |
do { \ |
| 79 |
if (strcmp(a, b) != 0) { \ |
| 80 |
const char *test_strequal__part2 = " != " #b; \ |
| 81 |
size_t test_strequal__len = \ |
| 82 |
strlen(a) + strlen(test_strequal__part2) + 3; \ |
| 83 |
char *test_strequal__buf = malloc(test_strequal__len); \ |
| 84 |
snprintf(test_strequal__buf, test_strequal__len, "\"%s\"%s", a, \ |
| 85 |
test_strequal__part2); \ |
| 86 |
SET_FAILURE(test_strequal__buf, true); \ |
| 87 |
return; \ |
| 88 |
} \ |
| 89 |
} while (0) |
| 90 |
|
| 91 |
#define TEST_STRNEQUAL(a, b, len) \ |
| 92 |
do { \ |
| 93 |
if (strncmp(a, b, len) != 0) { \ |
| 94 |
const char *test_strnequal__part2 = " != " #b; \ |
| 95 |
size_t test_strnequal__len2 = \ |
| 96 |
len + strlen(test_strnequal__part2) + 3; \ |
| 97 |
char *test_strnequal__buf = malloc(test_strnequal__len2); \ |
| 98 |
snprintf(test_strnequal__buf, test_strnequal__len2, \ |
| 99 |
"\"%.*s\"%s", (int)len, a, test_strnequal__part2); \ |
| 100 |
SET_FAILURE(test_strnequal__buf, true); \ |
| 101 |
return; \ |
| 102 |
} \ |
| 103 |
} while (0) |
| 104 |
|
| 105 |
#define TEST_STREQUAL3(str, expected, len) \ |
| 106 |
do { \ |
| 107 |
if (len != strlen(expected) || strncmp(str, expected, len) != 0) { \ |
| 108 |
const char *test_strequal3__part2 = " != " #expected; \ |
| 109 |
size_t test_strequal3__len2 = \ |
| 110 |
len + strlen(test_strequal3__part2) + 3; \ |
| 111 |
char *test_strequal3__buf = malloc(test_strequal3__len2); \ |
| 112 |
snprintf(test_strequal3__buf, test_strequal3__len2, \ |
| 113 |
"\"%.*s\"%s", (int)len, str, test_strequal3__part2); \ |
| 114 |
SET_FAILURE(test_strequal3__buf, true); \ |
| 115 |
return; \ |
| 116 |
} \ |
| 117 |
} while (0) |
| 118 |
|
| 119 |
#define TEST_CASE(_name) \ |
| 120 |
static void __test_h_##_name(struct test_case_metadata *, \ |
| 121 |
struct test_file_metadata *); \ |
| 122 |
static struct test_file_metadata __test_h_file; \ |
| 123 |
static struct test_case_metadata __test_h_meta_##_name = { \ |
| 124 |
.name = #_name, \ |
| 125 |
.fn = __test_h_##_name, \ |
| 126 |
}; \ |
| 127 |
static void __attribute__((constructor(101))) __test_h_##_name##_register(void) { \ |
| 128 |
__test_h_meta_##_name.next = __test_h_file.tests; \ |
| 129 |
__test_h_file.tests = &__test_h_meta_##_name; \ |
| 130 |
if (!__test_h_file.registered) { \ |
| 131 |
__test_h_file.name = __FILE__; \ |
| 132 |
__test_h_file.next = test_file_head; \ |
| 133 |
test_file_head = &__test_h_file; \ |
| 134 |
__test_h_file.registered = true; \ |
| 135 |
} \ |
| 136 |
} \ |
| 137 |
static void __test_h_##_name( \ |
| 138 |
struct test_case_metadata *metadata __attribute__((unused)), \ |
| 139 |
struct test_file_metadata *file_metadata __attribute__((unused))) |
| 140 |
|
| 141 |
extern void __attribute__((weak)) (*test_h_unittest_setup)(void); |
| 142 |
|
| 143 |
|
| 144 |
static inline void __attribute__((constructor(102))) run_tests(void) { |
| 145 |
bool should_run = false; |
| 146 |
#ifdef USE_SYSCTL_FOR_ARGS |
| 147 |
int mib[] = { |
| 148 |
CTL_KERN, |
| 149 |
#if defined(__NetBSD__) || defined(__OpenBSD__) |
| 150 |
KERN_PROC_ARGS, |
| 151 |
getpid(), |
| 152 |
KERN_PROC_ARGV, |
| 153 |
#else |
| 154 |
KERN_PROC, |
| 155 |
KERN_PROC_ARGS, |
| 156 |
getpid(), |
| 157 |
#endif |
| 158 |
}; |
| 159 |
char *arg = NULL; |
| 160 |
size_t arglen; |
| 161 |
sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &arglen, NULL, 0); |
| 162 |
arg = malloc(arglen); |
| 163 |
sysctl(mib, sizeof(mib) / sizeof(mib[0]), arg, &arglen, NULL, 0); |
| 164 |
#else |
| 165 |
FILE *cmdlinef = fopen("/proc/self/cmdline", "r"); |
| 166 |
char *arg = NULL; |
| 167 |
int arglen; |
| 168 |
fscanf(cmdlinef, "%ms%n", &arg, &arglen); |
| 169 |
fclose(cmdlinef); |
| 170 |
#endif |
| 171 |
for (char *pos = arg; pos < arg + arglen; pos += strlen(pos) + 1) { |
| 172 |
if (strcmp(pos, "--unittest") == 0) { |
| 173 |
should_run = true; |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
free(arg); |
| 178 |
|
| 179 |
if (!should_run) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
if (&test_h_unittest_setup) { |
| 184 |
test_h_unittest_setup(); |
| 185 |
} |
| 186 |
|
| 187 |
struct test_file_metadata *i = test_file_head; |
| 188 |
int failed = 0, success = 0; |
| 189 |
while (i) { |
| 190 |
fprintf(stderr, "Running tests from %s:\n", i->name); |
| 191 |
struct test_case_metadata *j = i->tests; |
| 192 |
while (j) { |
| 193 |
fprintf(stderr, "\t%s ... ", j->name); |
| 194 |
j->failure.present = false; |
| 195 |
j->fn(j, i); |
| 196 |
if (j->failure.present) { |
| 197 |
fprintf(stderr, "failed (%s at %s:%d)\n", j->failure.message, |
| 198 |
j->failure.file, j->failure.line); |
| 199 |
if (j->failure.owned) { |
| 200 |
free((char *)j->failure.message); |
| 201 |
j->failure.message = NULL; |
| 202 |
} |
| 203 |
failed++; |
| 204 |
} else { |
| 205 |
fprintf(stderr, "passed\n"); |
| 206 |
success++; |
| 207 |
} |
| 208 |
j = j->next; |
| 209 |
} |
| 210 |
fprintf(stderr, "\n"); |
| 211 |
i = i->next; |
| 212 |
} |
| 213 |
int total = failed + success; |
| 214 |
fprintf(stderr, "Test results: passed %d/%d, failed %d/%d\n", success, total, |
| 215 |
failed, total); |
| 216 |
exit(failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 217 |
} |
| 218 |
|
| 219 |
#else |
| 220 |
|
| 221 |
#include <stdbool.h> |
| 222 |
|
| 223 |
#define TEST_CASE(name) static void __attribute__((unused)) __test_h_##name(void) |
| 224 |
|
| 225 |
#define TEST_EQUAL(a, b) (void)((a) == (b)) |
| 226 |
#define TEST_NOTEQUAL(a, b) (void)((a) != (b)) |
| 227 |
#define TEST_TRUE(a) (void)(a) |
| 228 |
#define TEST_STREQUAL(a, b) \ |
| 229 |
(void)(a); \ |
| 230 |
(void)(b) |
| 231 |
#define TEST_STRNEQUAL(a, b, len) \ |
| 232 |
(void)(a); \ |
| 233 |
(void)(b); \ |
| 234 |
(void)(len) |
| 235 |
#define TEST_STREQUAL3(str, expected, len) \ |
| 236 |
(void)(str); \ |
| 237 |
(void)(expected); \ |
| 238 |
(void)(len) |
| 239 |
#endif |