1 #define ASSERT_BUFSIZE 256 2 3 #define assert_cmp(t, a, b, cmp, neg_cmp, pri, ...) do { \ 4 t a_ = (a); \ 5 t b_ = (b); \ 6 if (!(a_ cmp b_)) { \ 7 char prefix[ASSERT_BUFSIZE]; \ 8 char message[ASSERT_BUFSIZE]; \ 9 malloc_snprintf(prefix, sizeof(prefix), \ 10 "%s:%s:%d: Failed assertion: " \ 11 "(%s) "#cmp" (%s) --> " \ 12 "%"pri" "#neg_cmp" %"pri": ", \ 13 __func__, __FILE__, __LINE__, \ 14 #a, #b, a_, b_); \ 15 malloc_snprintf(message, sizeof(message), __VA_ARGS__); \ 16 p_test_fail(prefix, message); \ 17 } \ 18 } while (0) 19 20 #define assert_ptr_eq(a, b, ...) assert_cmp(void *, a, b, ==, \ 21 !=, "p", __VA_ARGS__) 22 #define assert_ptr_ne(a, b, ...) assert_cmp(void *, a, b, !=, \ 23 ==, "p", __VA_ARGS__) 24 #define assert_ptr_null(a, ...) assert_cmp(void *, a, NULL, ==, \ 25 !=, "p", __VA_ARGS__) 26 #define assert_ptr_not_null(a, ...) assert_cmp(void *, a, NULL, !=, \ 27 ==, "p", __VA_ARGS__) 28 29 #define assert_c_eq(a, b, ...) assert_cmp(char, a, b, ==, !=, "c", __VA_ARGS__) 30 #define assert_c_ne(a, b, ...) assert_cmp(char, a, b, !=, ==, "c", __VA_ARGS__) 31 #define assert_c_lt(a, b, ...) assert_cmp(char, a, b, <, >=, "c", __VA_ARGS__) 32 #define assert_c_le(a, b, ...) assert_cmp(char, a, b, <=, >, "c", __VA_ARGS__) 33 #define assert_c_ge(a, b, ...) assert_cmp(char, a, b, >=, <, "c", __VA_ARGS__) 34 #define assert_c_gt(a, b, ...) assert_cmp(char, a, b, >, <=, "c", __VA_ARGS__) 35 36 #define assert_x_eq(a, b, ...) assert_cmp(int, a, b, ==, !=, "#x", __VA_ARGS__) 37 #define assert_x_ne(a, b, ...) assert_cmp(int, a, b, !=, ==, "#x", __VA_ARGS__) 38 #define assert_x_lt(a, b, ...) assert_cmp(int, a, b, <, >=, "#x", __VA_ARGS__) 39 #define assert_x_le(a, b, ...) assert_cmp(int, a, b, <=, >, "#x", __VA_ARGS__) 40 #define assert_x_ge(a, b, ...) assert_cmp(int, a, b, >=, <, "#x", __VA_ARGS__) 41 #define assert_x_gt(a, b, ...) assert_cmp(int, a, b, >, <=, "#x", __VA_ARGS__) 42 43 #define assert_d_eq(a, b, ...) assert_cmp(int, a, b, ==, !=, "d", __VA_ARGS__) 44 #define assert_d_ne(a, b, ...) assert_cmp(int, a, b, !=, ==, "d", __VA_ARGS__) 45 #define assert_d_lt(a, b, ...) assert_cmp(int, a, b, <, >=, "d", __VA_ARGS__) 46 #define assert_d_le(a, b, ...) assert_cmp(int, a, b, <=, >, "d", __VA_ARGS__) 47 #define assert_d_ge(a, b, ...) assert_cmp(int, a, b, >=, <, "d", __VA_ARGS__) 48 #define assert_d_gt(a, b, ...) assert_cmp(int, a, b, >, <=, "d", __VA_ARGS__) 49 50 #define assert_u_eq(a, b, ...) assert_cmp(int, a, b, ==, !=, "u", __VA_ARGS__) 51 #define assert_u_ne(a, b, ...) assert_cmp(int, a, b, !=, ==, "u", __VA_ARGS__) 52 #define assert_u_lt(a, b, ...) assert_cmp(int, a, b, <, >=, "u", __VA_ARGS__) 53 #define assert_u_le(a, b, ...) assert_cmp(int, a, b, <=, >, "u", __VA_ARGS__) 54 #define assert_u_ge(a, b, ...) assert_cmp(int, a, b, >=, <, "u", __VA_ARGS__) 55 #define assert_u_gt(a, b, ...) assert_cmp(int, a, b, >, <=, "u", __VA_ARGS__) 56 57 #define assert_ld_eq(a, b, ...) assert_cmp(long, a, b, ==, \ 58 !=, "ld", __VA_ARGS__) 59 #define assert_ld_ne(a, b, ...) assert_cmp(long, a, b, !=, \ 60 ==, "ld", __VA_ARGS__) 61 #define assert_ld_lt(a, b, ...) assert_cmp(long, a, b, <, \ 62 >=, "ld", __VA_ARGS__) 63 #define assert_ld_le(a, b, ...) assert_cmp(long, a, b, <=, \ 64 >, "ld", __VA_ARGS__) 65 #define assert_ld_ge(a, b, ...) assert_cmp(long, a, b, >=, \ 66 <, "ld", __VA_ARGS__) 67 #define assert_ld_gt(a, b, ...) assert_cmp(long, a, b, >, \ 68 <=, "ld", __VA_ARGS__) 69 70 #define assert_lu_eq(a, b, ...) assert_cmp(unsigned long, \ 71 a, b, ==, !=, "lu", __VA_ARGS__) 72 #define assert_lu_ne(a, b, ...) assert_cmp(unsigned long, \ 73 a, b, !=, ==, "lu", __VA_ARGS__) 74 #define assert_lu_lt(a, b, ...) assert_cmp(unsigned long, \ 75 a, b, <, >=, "lu", __VA_ARGS__) 76 #define assert_lu_le(a, b, ...) assert_cmp(unsigned long, \ 77 a, b, <=, >, "lu", __VA_ARGS__) 78 #define assert_lu_ge(a, b, ...) assert_cmp(unsigned long, \ 79 a, b, >=, <, "lu", __VA_ARGS__) 80 #define assert_lu_gt(a, b, ...) assert_cmp(unsigned long, \ 81 a, b, >, <=, "lu", __VA_ARGS__) 82 83 #define assert_qd_eq(a, b, ...) assert_cmp(long long, a, b, ==, \ 84 !=, "qd", __VA_ARGS__) 85 #define assert_qd_ne(a, b, ...) assert_cmp(long long, a, b, !=, \ 86 ==, "qd", __VA_ARGS__) 87 #define assert_qd_lt(a, b, ...) assert_cmp(long long, a, b, <, \ 88 >=, "qd", __VA_ARGS__) 89 #define assert_qd_le(a, b, ...) assert_cmp(long long, a, b, <=, \ 90 >, "qd", __VA_ARGS__) 91 #define assert_qd_ge(a, b, ...) assert_cmp(long long, a, b, >=, \ 92 <, "qd", __VA_ARGS__) 93 #define assert_qd_gt(a, b, ...) assert_cmp(long long, a, b, >, \ 94 <=, "qd", __VA_ARGS__) 95 96 #define assert_qu_eq(a, b, ...) assert_cmp(unsigned long long, \ 97 a, b, ==, !=, "qu", __VA_ARGS__) 98 #define assert_qu_ne(a, b, ...) assert_cmp(unsigned long long, \ 99 a, b, !=, ==, "qu", __VA_ARGS__) 100 #define assert_qu_lt(a, b, ...) assert_cmp(unsigned long long, \ 101 a, b, <, >=, "qu", __VA_ARGS__) 102 #define assert_qu_le(a, b, ...) assert_cmp(unsigned long long, \ 103 a, b, <=, >, "qu", __VA_ARGS__) 104 #define assert_qu_ge(a, b, ...) assert_cmp(unsigned long long, \ 105 a, b, >=, <, "qu", __VA_ARGS__) 106 #define assert_qu_gt(a, b, ...) assert_cmp(unsigned long long, \ 107 a, b, >, <=, "qu", __VA_ARGS__) 108 109 #define assert_jd_eq(a, b, ...) assert_cmp(intmax_t, a, b, ==, \ 110 !=, "jd", __VA_ARGS__) 111 #define assert_jd_ne(a, b, ...) assert_cmp(intmax_t, a, b, !=, \ 112 ==, "jd", __VA_ARGS__) 113 #define assert_jd_lt(a, b, ...) assert_cmp(intmax_t, a, b, <, \ 114 >=, "jd", __VA_ARGS__) 115 #define assert_jd_le(a, b, ...) assert_cmp(intmax_t, a, b, <=, \ 116 >, "jd", __VA_ARGS__) 117 #define assert_jd_ge(a, b, ...) assert_cmp(intmax_t, a, b, >=, \ 118 <, "jd", __VA_ARGS__) 119 #define assert_jd_gt(a, b, ...) assert_cmp(intmax_t, a, b, >, \ 120 <=, "jd", __VA_ARGS__) 121 122 #define assert_ju_eq(a, b, ...) assert_cmp(uintmax_t, a, b, ==, \ 123 !=, "ju", __VA_ARGS__) 124 #define assert_ju_ne(a, b, ...) assert_cmp(uintmax_t, a, b, !=, \ 125 ==, "ju", __VA_ARGS__) 126 #define assert_ju_lt(a, b, ...) assert_cmp(uintmax_t, a, b, <, \ 127 >=, "ju", __VA_ARGS__) 128 #define assert_ju_le(a, b, ...) assert_cmp(uintmax_t, a, b, <=, \ 129 >, "ju", __VA_ARGS__) 130 #define assert_ju_ge(a, b, ...) assert_cmp(uintmax_t, a, b, >=, \ 131 <, "ju", __VA_ARGS__) 132 #define assert_ju_gt(a, b, ...) assert_cmp(uintmax_t, a, b, >, \ 133 <=, "ju", __VA_ARGS__) 134 135 #define assert_zd_eq(a, b, ...) assert_cmp(ssize_t, a, b, ==, \ 136 !=, "zd", __VA_ARGS__) 137 #define assert_zd_ne(a, b, ...) assert_cmp(ssize_t, a, b, !=, \ 138 ==, "zd", __VA_ARGS__) 139 #define assert_zd_lt(a, b, ...) assert_cmp(ssize_t, a, b, <, \ 140 >=, "zd", __VA_ARGS__) 141 #define assert_zd_le(a, b, ...) assert_cmp(ssize_t, a, b, <=, \ 142 >, "zd", __VA_ARGS__) 143 #define assert_zd_ge(a, b, ...) assert_cmp(ssize_t, a, b, >=, \ 144 <, "zd", __VA_ARGS__) 145 #define assert_zd_gt(a, b, ...) assert_cmp(ssize_t, a, b, >, \ 146 <=, "zd", __VA_ARGS__) 147 148 #define assert_zu_eq(a, b, ...) assert_cmp(size_t, a, b, ==, \ 149 !=, "zu", __VA_ARGS__) 150 #define assert_zu_ne(a, b, ...) assert_cmp(size_t, a, b, !=, \ 151 ==, "zu", __VA_ARGS__) 152 #define assert_zu_lt(a, b, ...) assert_cmp(size_t, a, b, <, \ 153 >=, "zu", __VA_ARGS__) 154 #define assert_zu_le(a, b, ...) assert_cmp(size_t, a, b, <=, \ 155 >, "zu", __VA_ARGS__) 156 #define assert_zu_ge(a, b, ...) assert_cmp(size_t, a, b, >=, \ 157 <, "zu", __VA_ARGS__) 158 #define assert_zu_gt(a, b, ...) assert_cmp(size_t, a, b, >, \ 159 <=, "zu", __VA_ARGS__) 160 161 #define assert_d32_eq(a, b, ...) assert_cmp(int32_t, a, b, ==, \ 162 !=, FMTd32, __VA_ARGS__) 163 #define assert_d32_ne(a, b, ...) assert_cmp(int32_t, a, b, !=, \ 164 ==, FMTd32, __VA_ARGS__) 165 #define assert_d32_lt(a, b, ...) assert_cmp(int32_t, a, b, <, \ 166 >=, FMTd32, __VA_ARGS__) 167 #define assert_d32_le(a, b, ...) assert_cmp(int32_t, a, b, <=, \ 168 >, FMTd32, __VA_ARGS__) 169 #define assert_d32_ge(a, b, ...) assert_cmp(int32_t, a, b, >=, \ 170 <, FMTd32, __VA_ARGS__) 171 #define assert_d32_gt(a, b, ...) assert_cmp(int32_t, a, b, >, \ 172 <=, FMTd32, __VA_ARGS__) 173 174 #define assert_u32_eq(a, b, ...) assert_cmp(uint32_t, a, b, ==, \ 175 !=, FMTu32, __VA_ARGS__) 176 #define assert_u32_ne(a, b, ...) assert_cmp(uint32_t, a, b, !=, \ 177 ==, FMTu32, __VA_ARGS__) 178 #define assert_u32_lt(a, b, ...) assert_cmp(uint32_t, a, b, <, \ 179 >=, FMTu32, __VA_ARGS__) 180 #define assert_u32_le(a, b, ...) assert_cmp(uint32_t, a, b, <=, \ 181 >, FMTu32, __VA_ARGS__) 182 #define assert_u32_ge(a, b, ...) assert_cmp(uint32_t, a, b, >=, \ 183 <, FMTu32, __VA_ARGS__) 184 #define assert_u32_gt(a, b, ...) assert_cmp(uint32_t, a, b, >, \ 185 <=, FMTu32, __VA_ARGS__) 186 187 #define assert_d64_eq(a, b, ...) assert_cmp(int64_t, a, b, ==, \ 188 !=, FMTd64, __VA_ARGS__) 189 #define assert_d64_ne(a, b, ...) assert_cmp(int64_t, a, b, !=, \ 190 ==, FMTd64, __VA_ARGS__) 191 #define assert_d64_lt(a, b, ...) assert_cmp(int64_t, a, b, <, \ 192 >=, FMTd64, __VA_ARGS__) 193 #define assert_d64_le(a, b, ...) assert_cmp(int64_t, a, b, <=, \ 194 >, FMTd64, __VA_ARGS__) 195 #define assert_d64_ge(a, b, ...) assert_cmp(int64_t, a, b, >=, \ 196 <, FMTd64, __VA_ARGS__) 197 #define assert_d64_gt(a, b, ...) assert_cmp(int64_t, a, b, >, \ 198 <=, FMTd64, __VA_ARGS__) 199 200 #define assert_u64_eq(a, b, ...) assert_cmp(uint64_t, a, b, ==, \ 201 !=, FMTu64, __VA_ARGS__) 202 #define assert_u64_ne(a, b, ...) assert_cmp(uint64_t, a, b, !=, \ 203 ==, FMTu64, __VA_ARGS__) 204 #define assert_u64_lt(a, b, ...) assert_cmp(uint64_t, a, b, <, \ 205 >=, FMTu64, __VA_ARGS__) 206 #define assert_u64_le(a, b, ...) assert_cmp(uint64_t, a, b, <=, \ 207 >, FMTu64, __VA_ARGS__) 208 #define assert_u64_ge(a, b, ...) assert_cmp(uint64_t, a, b, >=, \ 209 <, FMTu64, __VA_ARGS__) 210 #define assert_u64_gt(a, b, ...) assert_cmp(uint64_t, a, b, >, \ 211 <=, FMTu64, __VA_ARGS__) 212 213 #define assert_b_eq(a, b, ...) do { \ 214 bool a_ = (a); \ 215 bool b_ = (b); \ 216 if (!(a_ == b_)) { \ 217 char prefix[ASSERT_BUFSIZE]; \ 218 char message[ASSERT_BUFSIZE]; \ 219 malloc_snprintf(prefix, sizeof(prefix), \ 220 "%s:%s:%d: Failed assertion: " \ 221 "(%s) == (%s) --> %s != %s: ", \ 222 __func__, __FILE__, __LINE__, \ 223 #a, #b, a_ ? "true" : "false", \ 224 b_ ? "true" : "false"); \ 225 malloc_snprintf(message, sizeof(message), __VA_ARGS__); \ 226 p_test_fail(prefix, message); \ 227 } \ 228 } while (0) 229 #define assert_b_ne(a, b, ...) do { \ 230 bool a_ = (a); \ 231 bool b_ = (b); \ 232 if (!(a_ != b_)) { \ 233 char prefix[ASSERT_BUFSIZE]; \ 234 char message[ASSERT_BUFSIZE]; \ 235 malloc_snprintf(prefix, sizeof(prefix), \ 236 "%s:%s:%d: Failed assertion: " \ 237 "(%s) != (%s) --> %s == %s: ", \ 238 __func__, __FILE__, __LINE__, \ 239 #a, #b, a_ ? "true" : "false", \ 240 b_ ? "true" : "false"); \ 241 malloc_snprintf(message, sizeof(message), __VA_ARGS__); \ 242 p_test_fail(prefix, message); \ 243 } \ 244 } while (0) 245 #define assert_true(a, ...) assert_b_eq(a, true, __VA_ARGS__) 246 #define assert_false(a, ...) assert_b_eq(a, false, __VA_ARGS__) 247 248 #define assert_str_eq(a, b, ...) do { \ 249 if (strcmp((a), (b))) { \ 250 char prefix[ASSERT_BUFSIZE]; \ 251 char message[ASSERT_BUFSIZE]; \ 252 malloc_snprintf(prefix, sizeof(prefix), \ 253 "%s:%s:%d: Failed assertion: " \ 254 "(%s) same as (%s) --> " \ 255 "\"%s\" differs from \"%s\": ", \ 256 __func__, __FILE__, __LINE__, #a, #b, a, b); \ 257 malloc_snprintf(message, sizeof(message), __VA_ARGS__); \ 258 p_test_fail(prefix, message); \ 259 } \ 260 } while (0) 261 #define assert_str_ne(a, b, ...) do { \ 262 if (!strcmp((a), (b))) { \ 263 char prefix[ASSERT_BUFSIZE]; \ 264 char message[ASSERT_BUFSIZE]; \ 265 malloc_snprintf(prefix, sizeof(prefix), \ 266 "%s:%s:%d: Failed assertion: " \ 267 "(%s) differs from (%s) --> " \ 268 "\"%s\" same as \"%s\": ", \ 269 __func__, __FILE__, __LINE__, #a, #b, a, b); \ 270 malloc_snprintf(message, sizeof(message), __VA_ARGS__); \ 271 p_test_fail(prefix, message); \ 272 } \ 273 } while (0) 274 275 #define assert_not_reached(...) do { \ 276 char prefix[ASSERT_BUFSIZE]; \ 277 char message[ASSERT_BUFSIZE]; \ 278 malloc_snprintf(prefix, sizeof(prefix), \ 279 "%s:%s:%d: Unreachable code reached: ", \ 280 __func__, __FILE__, __LINE__); \ 281 malloc_snprintf(message, sizeof(message), __VA_ARGS__); \ 282 p_test_fail(prefix, message); \ 283 } while (0) 284 285 /* 286 * If this enum changes, corresponding changes in test/test.sh.in are also 287 * necessary. 288 */ 289 typedef enum { 290 test_status_pass = 0, 291 test_status_skip = 1, 292 test_status_fail = 2, 293 294 test_status_count = 3 295 } test_status_t; 296 297 typedef void (test_t)(void); 298 299 #define TEST_BEGIN(f) \ 300 static void \ 301 f(void) \ 302 { \ 303 p_test_init(#f); 304 305 #define TEST_END \ 306 goto label_test_end; \ 307 label_test_end: \ 308 p_test_fini(); \ 309 } 310 311 #define test(...) \ 312 p_test(__VA_ARGS__, NULL) 313 314 #define test_no_malloc_init(...) \ 315 p_test_no_malloc_init(__VA_ARGS__, NULL) 316 317 #define test_skip_if(e) do { \ 318 if (e) { \ 319 test_skip("%s:%s:%d: Test skipped: (%s)", \ 320 __func__, __FILE__, __LINE__, #e); \ 321 goto label_test_end; \ 322 } \ 323 } while (0) 324 325 void test_skip(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2); 326 void test_fail(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2); 327 328 /* For private use by macros. */ 329 test_status_t p_test(test_t *t, ...); 330 test_status_t p_test_no_malloc_init(test_t *t, ...); 331 void p_test_init(const char *name); 332 void p_test_fini(void); 333 void p_test_fail(const char *prefix, const char *message); 334