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