1 #include "test/jemalloc_test.h" 2 3 static unsigned test_count = 0; 4 static test_status_t test_counts[test_status_count] = {0, 0, 0}; 5 static test_status_t test_status = test_status_pass; 6 static const char * test_name = ""; 7 8 JEMALLOC_FORMAT_PRINTF(1, 2) 9 void 10 test_skip(const char *format, ...) 11 { 12 va_list ap; 13 14 va_start(ap, format); 15 malloc_vcprintf(NULL, NULL, format, ap); 16 va_end(ap); 17 malloc_printf("\n"); 18 test_status = test_status_skip; 19 } 20 21 JEMALLOC_FORMAT_PRINTF(1, 2) 22 void 23 test_fail(const char *format, ...) 24 { 25 va_list ap; 26 27 va_start(ap, format); 28 malloc_vcprintf(NULL, NULL, format, ap); 29 va_end(ap); 30 malloc_printf("\n"); 31 test_status = test_status_fail; 32 } 33 34 static const char * 35 test_status_string(test_status_t test_status) 36 { 37 38 switch (test_status) { 39 case test_status_pass: return "pass"; 40 case test_status_skip: return "skip"; 41 case test_status_fail: return "fail"; 42 default: not_reached(); 43 } 44 } 45 46 void 47 p_test_init(const char *name) 48 { 49 50 test_count++; 51 test_status = test_status_pass; 52 test_name = name; 53 } 54 55 void 56 p_test_fini(void) 57 { 58 59 test_counts[test_status]++; 60 malloc_printf("%s: %s\n", test_name, test_status_string(test_status)); 61 } 62 63 static test_status_t 64 p_test_impl(bool do_malloc_init, test_t *t, va_list ap) 65 { 66 test_status_t ret; 67 68 if (do_malloc_init) { 69 /* 70 * Make sure initialization occurs prior to running tests. 71 * Tests are special because they may use internal facilities 72 * prior to triggering initialization as a side effect of 73 * calling into the public API. 74 */ 75 if (nallocx(1, 0) == 0) { 76 malloc_printf("Initialization error"); 77 return (test_status_fail); 78 } 79 } 80 81 ret = test_status_pass; 82 for (; t != NULL; t = va_arg(ap, test_t *)) { 83 t(); 84 if (test_status > ret) 85 ret = test_status; 86 } 87 88 malloc_printf("--- %s: %u/%u, %s: %u/%u, %s: %u/%u ---\n", 89 test_status_string(test_status_pass), 90 test_counts[test_status_pass], test_count, 91 test_status_string(test_status_skip), 92 test_counts[test_status_skip], test_count, 93 test_status_string(test_status_fail), 94 test_counts[test_status_fail], test_count); 95 96 return (ret); 97 } 98 99 test_status_t 100 p_test(test_t *t, ...) 101 { 102 test_status_t ret; 103 va_list ap; 104 105 ret = test_status_pass; 106 va_start(ap, t); 107 ret = p_test_impl(true, t, ap); 108 va_end(ap); 109 110 return (ret); 111 } 112 113 test_status_t 114 p_test_no_malloc_init(test_t *t, ...) 115 { 116 test_status_t ret; 117 va_list ap; 118 119 ret = test_status_pass; 120 va_start(ap, t); 121 ret = p_test_impl(false, t, ap); 122 va_end(ap); 123 124 return (ret); 125 } 126 127 void 128 p_test_fail(const char *prefix, const char *message) 129 { 130 131 malloc_cprintf(NULL, NULL, "%s%s\n", prefix, message); 132 test_status = test_status_fail; 133 } 134