• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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
p_test_init(const char * name)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
p_test_fini(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 test_status_t
p_test(test_t * t,...)64 p_test(test_t *t, ...)
65 {
66 	test_status_t ret;
67 	va_list ap;
68 
69 	/*
70 	 * Make sure initialization occurs prior to running tests.  Tests are
71 	 * special because they may use internal facilities prior to triggering
72 	 * initialization as a side effect of calling into the public API.  This
73 	 * is a final safety that works even if jemalloc_constructor() doesn't
74 	 * run, as for MSVC builds.
75 	 */
76 	if (nallocx(1, 0) == 0) {
77 		malloc_printf("Initialization error");
78 		return (test_status_fail);
79 	}
80 
81 	ret = test_status_pass;
82 	va_start(ap, t);
83 	for (; t != NULL; t = va_arg(ap, test_t *)) {
84 		t();
85 		if (test_status > ret)
86 			ret = test_status;
87 	}
88 	va_end(ap);
89 
90 	malloc_printf("--- %s: %u/%u, %s: %u/%u, %s: %u/%u ---\n",
91 	    test_status_string(test_status_pass),
92 	    test_counts[test_status_pass], test_count,
93 	    test_status_string(test_status_skip),
94 	    test_counts[test_status_skip], test_count,
95 	    test_status_string(test_status_fail),
96 	    test_counts[test_status_fail], test_count);
97 
98 	return (ret);
99 }
100 
101 void
p_test_fail(const char * prefix,const char * message)102 p_test_fail(const char *prefix, const char *message)
103 {
104 
105 	malloc_cprintf(NULL, NULL, "%s%s\n", prefix, message);
106 	test_status = test_status_fail;
107 }
108