summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Evans <jasone@canonware.com>2013-12-09 04:52:21 (GMT)
committerJason Evans <jasone@canonware.com>2013-12-09 04:52:21 (GMT)
commit2a83ed0284e92c7ba4bd4efe9df149ac724b2f26 (patch)
tree915b5dbaa91c4a8dd3fdffedb98ce8abb48535b6
parent9f35a71a81adcfd6c0ea6461ecd2b84ac384e34f (diff)
downloadjemalloc-2a83ed0284e92c7ba4bd4efe9df149ac724b2f26.zip
jemalloc-2a83ed0284e92c7ba4bd4efe9df149ac724b2f26.tar.gz
jemalloc-2a83ed0284e92c7ba4bd4efe9df149ac724b2f26.tar.bz2
Refactor tests.
Refactor tests to use explicit testing assertions, rather than diff'ing test output. This makes the test code a bit shorter, more explicitly encodes testing intent, and makes test failure diagnosis more straightforward.
-rw-r--r--Makefile.in4
-rw-r--r--include/jemalloc/internal/chunk_dss.h2
-rw-r--r--include/jemalloc/internal/util.h2
-rw-r--r--src/chunk_mmap.c4
-rw-r--r--src/huge.c2
-rw-r--r--src/util.c6
-rw-r--r--test/include/test/test.h177
-rw-r--r--test/integration/ALLOCM_ARENA.c43
-rw-r--r--test/integration/ALLOCM_ARENA.exp2
-rw-r--r--test/integration/aligned_alloc.c84
-rw-r--r--test/integration/aligned_alloc.exp25
-rw-r--r--test/integration/allocated.c118
-rw-r--r--test/integration/allocated.exp2
-rw-r--r--test/integration/allocm.c201
-rw-r--r--test/integration/allocm.exp25
-rw-r--r--test/integration/mremap.c54
-rw-r--r--test/integration/mremap.exp2
-rw-r--r--test/integration/posix_memalign.c94
-rw-r--r--test/integration/posix_memalign.exp25
-rw-r--r--test/integration/rallocm.c203
-rw-r--r--test/integration/rallocm.exp2
-rw-r--r--test/integration/thread_arena.c59
-rw-r--r--test/integration/thread_arena.exp2
-rw-r--r--test/integration/thread_tcache_enabled.c97
-rw-r--r--test/integration/thread_tcache_enabled.exp2
-rw-r--r--test/src/test.c80
-rw-r--r--test/test.sh.in57
-rw-r--r--test/unit/bitmap.c80
-rw-r--r--test/unit/bitmap.exp2
-rw-r--r--test/unit/tsd.c49
-rw-r--r--test/unit/tsd.exp9
31 files changed, 843 insertions, 671 deletions
diff --git a/Makefile.in b/Makefile.in
index 242331c..ca16040 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -179,12 +179,12 @@ $(C_TESTLIB_INTEGRATION_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_TEST
$(C_UTIL_INTEGRATION_OBJS): $(objroot)src/%.integration.$(O): $(srcroot)src/%.c
$(C_TESTLIB_STRESS_OBJS): $(objroot)test/src/%.stress.$(O): $(srcroot)test/src/%.c
$(C_TESTLIB_STRESS_OBJS): CPPFLAGS += -DJEMALLOC_STRESS_TEST
-$(C_TESTLIB_OBJS): CPPFLAGS += -I$(objroot)test/include
+$(C_TESTLIB_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include
$(TESTS_UNIT_OBJS): CPPFLAGS += -DJEMALLOC_UNIT_TEST
$(TESTS_INTEGRATION_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_TEST
$(TESTS_STRESS_OBJS): CPPFLAGS += -DJEMALLOC_STRESS_TEST
$(TESTS_OBJS): $(objroot)test/%.$(O): $(srcroot)test/%.c
-$(TESTS_OBJS): CPPFLAGS += -I$(objroot)test/include
+$(TESTS_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include
ifneq ($(IMPORTLIB),$(SO))
$(C_OBJS): CPPFLAGS += -DDLLEXPORT
endif
diff --git a/include/jemalloc/internal/chunk_dss.h b/include/jemalloc/internal/chunk_dss.h
index 6585f07..4535ce0 100644
--- a/include/jemalloc/internal/chunk_dss.h
+++ b/include/jemalloc/internal/chunk_dss.h
@@ -7,7 +7,7 @@ typedef enum {
dss_prec_secondary = 2,
dss_prec_limit = 3
-} dss_prec_t ;
+} dss_prec_t;
#define DSS_PREC_DEFAULT dss_prec_secondary
#define DSS_DEFAULT "secondary"
diff --git a/include/jemalloc/internal/util.h b/include/jemalloc/internal/util.h
index bf18d3c..a8904d0 100644
--- a/include/jemalloc/internal/util.h
+++ b/include/jemalloc/internal/util.h
@@ -84,7 +84,7 @@
/******************************************************************************/
#ifdef JEMALLOC_H_EXTERNS
-int buferror(char *buf, size_t buflen);
+int buferror(int err, char *buf, size_t buflen);
uintmax_t malloc_strtoumax(const char *nptr, char **endptr, int base);
void malloc_write(const char *s);
diff --git a/src/chunk_mmap.c b/src/chunk_mmap.c
index 8a42e75..2056d79 100644
--- a/src/chunk_mmap.c
+++ b/src/chunk_mmap.c
@@ -43,7 +43,7 @@ pages_map(void *addr, size_t size)
if (munmap(ret, size) == -1) {
char buf[BUFERROR_BUF];
- buferror(buf, sizeof(buf));
+ buferror(get_errno(), buf, sizeof(buf));
malloc_printf("<jemalloc: Error in munmap(): %s\n",
buf);
if (opt_abort)
@@ -69,7 +69,7 @@ pages_unmap(void *addr, size_t size)
{
char buf[BUFERROR_BUF];
- buferror(buf, sizeof(buf));
+ buferror(get_errno(), buf, sizeof(buf));
malloc_printf("<jemalloc>: Error in "
#ifdef _WIN32
"VirtualFree"
diff --git a/src/huge.c b/src/huge.c
index aa08d43..443b400 100644
--- a/src/huge.c
+++ b/src/huge.c
@@ -169,7 +169,7 @@ huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra,
*/
char buf[BUFERROR_BUF];
- buferror(buf, sizeof(buf));
+ buferror(get_errno(), buf, sizeof(buf));
malloc_printf("<jemalloc>: Error in mremap(): %s\n",
buf);
if (opt_abort)
diff --git a/src/util.c b/src/util.c
index 679fa76..d2ca4f2 100644
--- a/src/util.c
+++ b/src/util.c
@@ -77,7 +77,7 @@ malloc_write(const char *s)
* provide a wrapper.
*/
int
-buferror(char *buf, size_t buflen)
+buferror(int err, char *buf, size_t buflen)
{
#ifdef _WIN32
@@ -85,14 +85,14 @@ buferror(char *buf, size_t buflen)
(LPSTR)buf, buflen, NULL);
return (0);
#elif defined(_GNU_SOURCE)
- char *b = strerror_r(errno, buf, buflen);
+ char *b = strerror_r(err, buf, buflen);
if (b != buf) {
strncpy(buf, b, buflen);
buf[buflen-1] = '\0';
}
return (0);
#else
- return (strerror_r(errno, buf, buflen));
+ return (strerror_r(err, buf, buflen));
#endif
}
diff --git a/test/include/test/test.h b/test/include/test/test.h
index ddbc55f..3376d33 100644
--- a/test/include/test/test.h
+++ b/test/include/test/test.h
@@ -1,2 +1,177 @@
-void test_fail(const char *format, ...) JEMALLOC_ATTR(format(printf, 1, 2));
+#define assert_cmp(t, a, b, cmp, neg_cmp, pri, fmt...) do { \
+ t a_ = (a); \
+ t b_ = (b); \
+ if (!(a_ cmp b_)) { \
+ p_test_fail( \
+ "%s:%s:%d: Failed assertion: " \
+ "(%s) "#cmp" (%s) --> " \
+ "%"pri" "#neg_cmp" %"pri": ", \
+ __func__, __FILE__, __LINE__, \
+ #a, #b, a_, b_, fmt); \
+ } \
+} while (0)
+
+#define assert_ptr_eq(a, b, fmt...) assert_cmp(void *, a, b, ==, \
+ !=, "p", fmt)
+#define assert_ptr_ne(a, b, fmt...) assert_cmp(void *, a, b, !=, \
+ ==, "p", fmt)
+#define assert_ptr_null(a, fmt...) assert_cmp(void *, a, NULL, ==, \
+ !=, "p", fmt)
+#define assert_ptr_not_null(a, fmt...) assert_cmp(void *, a, NULL, !=, \
+ ==, "p", fmt)
+
+#define assert_c_eq(a, b, fmt...) assert_cmp(char, a, b, ==, !=, "c", fmt)
+#define assert_c_ne(a, b, fmt...) assert_cmp(char, a, b, !=, ==, "c", fmt)
+#define assert_c_lt(a, b, fmt...) assert_cmp(char, a, b, <, >=, "c", fmt)
+#define assert_c_le(a, b, fmt...) assert_cmp(char, a, b, <=, >, "c", fmt)
+#define assert_c_ge(a, b, fmt...) assert_cmp(char, a, b, >=, <, "c", fmt)
+#define assert_c_gt(a, b, fmt...) assert_cmp(char, a, b, >, <=, "c", fmt)
+
+#define assert_x_eq(a, b, fmt...) assert_cmp(int, a, b, ==, !=, "#x", fmt)
+#define assert_x_ne(a, b, fmt...) assert_cmp(int, a, b, !=, ==, "#x", fmt)
+#define assert_x_lt(a, b, fmt...) assert_cmp(int, a, b, <, >=, "#x", fmt)
+#define assert_x_le(a, b, fmt...) assert_cmp(int, a, b, <=, >, "#x", fmt)
+#define assert_x_ge(a, b, fmt...) assert_cmp(int, a, b, >=, <, "#x", fmt)
+#define assert_x_gt(a, b, fmt...) assert_cmp(int, a, b, >, <=, "#x", fmt)
+
+#define assert_d_eq(a, b, fmt...) assert_cmp(int, a, b, ==, !=, "d", fmt)
+#define assert_d_ne(a, b, fmt...) assert_cmp(int, a, b, !=, ==, "d", fmt)
+#define assert_d_lt(a, b, fmt...) assert_cmp(int, a, b, <, >=, "d", fmt)
+#define assert_d_le(a, b, fmt...) assert_cmp(int, a, b, <=, >, "d", fmt)
+#define assert_d_ge(a, b, fmt...) assert_cmp(int, a, b, >=, <, "d", fmt)
+#define assert_d_gt(a, b, fmt...) assert_cmp(int, a, b, >, <=, "d", fmt)
+
+#define assert_u_eq(a, b, fmt...) assert_cmp(int, a, b, ==, !=, "u", fmt)
+#define assert_u_ne(a, b, fmt...) assert_cmp(int, a, b, !=, ==, "u", fmt)
+#define assert_u_lt(a, b, fmt...) assert_cmp(int, a, b, <, >=, "u", fmt)
+#define assert_u_le(a, b, fmt...) assert_cmp(int, a, b, <=, >, "u", fmt)
+#define assert_u_ge(a, b, fmt...) assert_cmp(int, a, b, >=, <, "u", fmt)
+#define assert_u_gt(a, b, fmt...) assert_cmp(int, a, b, >, <=, "u", fmt)
+
+#define assert_zd_eq(a, b, fmt...) assert_cmp(ssize_t, a, b, ==, \
+ !=, "zd", fmt)
+#define assert_zd_ne(a, b, fmt...) assert_cmp(ssize_t, a, b, !=, \
+ ==, "zd", fmt)
+#define assert_zd_lt(a, b, fmt...) assert_cmp(ssize_t, a, b, <, \
+ >=, "zd", fmt)
+#define assert_zd_le(a, b, fmt...) assert_cmp(ssize_t, a, b, <=, \
+ >, "zd", fmt)
+#define assert_zd_ge(a, b, fmt...) assert_cmp(ssize_t, a, b, >=, \
+ <, "zd", fmt)
+#define assert_zd_gt(a, b, fmt...) assert_cmp(ssize_t, a, b, >, \
+ <=, "zd", fmt)
+
+#define assert_zu_eq(a, b, fmt...) assert_cmp(size_t, a, b, ==, \
+ !=, "zu", fmt)
+#define assert_zu_ne(a, b, fmt...) assert_cmp(size_t, a, b, !=, \
+ ==, "zu", fmt)
+#define assert_zu_lt(a, b, fmt...) assert_cmp(size_t, a, b, <, \
+ >=, "zu", fmt)
+#define assert_zu_le(a, b, fmt...) assert_cmp(size_t, a, b, <=, \
+ >, "zu", fmt)
+#define assert_zu_ge(a, b, fmt...) assert_cmp(size_t, a, b, >=, \
+ <, "zu", fmt)
+#define assert_zu_gt(a, b, fmt...) assert_cmp(size_t, a, b, >, \
+ <=, "zu", fmt)
+
+#define assert_d64_eq(a, b, fmt...) assert_cmp(int64_t, a, b, ==, \
+ !=, PRId64, fmt)
+#define assert_d64_ne(a, b, fmt...) assert_cmp(int64_t, a, b, !=, \
+ ==, PRId64, fmt)
+#define assert_d64_lt(a, b, fmt...) assert_cmp(int64_t, a, b, <, \
+ >=, PRId64, fmt)
+#define assert_d64_le(a, b, fmt...) assert_cmp(int64_t, a, b, <=, \
+ >, PRId64, fmt)
+#define assert_d64_ge(a, b, fmt...) assert_cmp(int64_t, a, b, >=, \
+ <, PRId64, fmt)
+#define assert_d64_gt(a, b, fmt...) assert_cmp(int64_t, a, b, >, \
+ <=, PRId64, fmt)
+
+#define assert_u64_eq(a, b, fmt...) assert_cmp(uint64_t, a, b, ==, \
+ !=, PRIu64, fmt)
+#define assert_u64_ne(a, b, fmt...) assert_cmp(uint64_t, a, b, !=, \
+ ==, PRIu64, fmt)
+#define assert_u64_lt(a, b, fmt...) assert_cmp(uint64_t, a, b, <, \
+ >=, PRIu64, fmt)
+#define assert_u64_le(a, b, fmt...) assert_cmp(uint64_t, a, b, <=, \
+ >, PRIu64, fmt)
+#define assert_u64_ge(a, b, fmt...) assert_cmp(uint64_t, a, b, >=, \
+ <, PRIu64, fmt)
+#define assert_u64_gt(a, b, fmt...) assert_cmp(uint64_t, a, b, >, \
+ <=, PRIu64, fmt)
+
+#define assert_true(a, fmt...) do { \
+ bool a_ = (a); \
+ if (!(a_ == true)) { \
+ p_test_fail( \
+ "%s:%s:%d: Failed assertion: " \
+ "(%s) == true --> %s != true: %s\n", \
+ __func__, __FILE__, __LINE__, \
+ #a, a_ ? "true" : "false", fmt); \
+ } \
+} while (0)
+#define assert_false(a, fmt...) do { \
+ bool a_ = (a); \
+ if (!(a_ == false)) { \
+ p_test_fail( \
+ "%s:%s:%d: Failed assertion: " \
+ "(%s) == false --> %s != false: %s\n", \
+ __func__, __FILE__, __LINE__, \
+ #a, a_ ? "true" : "false", fmt); \
+ } \
+} while (0)
+
+#define assert_str_eq(a, b, fmt...) do { \
+ if (strcmp((a), (b))) { \
+ p_test_fail( \
+ "%s:%s:%d: Failed assertion: " \
+ "(%s) same as (%s) --> " \
+ "\"%s\" differs from \"%s\": %s\n", \
+ __func__, __FILE__, __LINE__, #a, #b, a, b, fmt); \
+ } \
+} while (0)
+#define assert_str_ne(a, b, fmt...) do { \
+ if (!strcmp((a), (b))) { \
+ p_test_fail( \
+ "%s:%s:%d: Failed assertion: " \
+ "(%s) differs from (%s) --> " \
+ "\"%s\" same as \"%s\": %s\n", \
+ __func__, __FILE__, __LINE__, #a, #b, a, b, fmt); \
+ } \
+} while (0)
+
+/*
+ * If this enum changes, corresponding changes in test/test.sh.in are also
+ * necessary.
+ */
+typedef enum {
+ test_status_pass = 0,
+ test_status_skip = 1,
+ test_status_fail = 2,
+
+ test_status_count = 3
+} test_status_t;
+
+typedef void (test_t)(void);
+
+#define TEST_BEGIN(f) \
+static void \
+f(void) \
+{ \
+ p_test_init(#f);
+
+#define TEST_END \
+ p_test_fini(); \
+}
+
+#define test(tests...) \
+ p_test(tests, NULL)
+
void test_skip(const char *format, ...) JEMALLOC_ATTR(format(printf, 1, 2));
+void test_fail(const char *format, ...) JEMALLOC_ATTR(format(printf, 1, 2));
+
+/* For private use by macros. */
+test_status_t p_test(test_t* t, ...);
+void p_test_init(const char *name);
+void p_test_fini(void);
+void p_test_fail(const char *format, ...);
diff --git a/test/integration/ALLOCM_ARENA.c b/test/integration/ALLOCM_ARENA.c
index e83056c..ec91c59 100644
--- a/test/integration/ALLOCM_ARENA.c
+++ b/test/integration/ALLOCM_ARENA.c
@@ -7,16 +7,12 @@ je_thread_start(void *arg)
{
unsigned thread_ind = (unsigned)(uintptr_t)arg;
unsigned arena_ind;
- int r;
void *p;
size_t rsz, sz;
sz = sizeof(arena_ind);
- if (mallctl("arenas.extend", &arena_ind, &sz, NULL, 0)
- != 0) {
- malloc_printf("Error in arenas.extend\n");
- abort();
- }
+ assert_d_eq(mallctl("arenas.extend", &arena_ind, &sz, NULL, 0), 0,
+ "Error in arenas.extend");
if (thread_ind % 4 != 3) {
size_t mib[3];
@@ -24,36 +20,25 @@ je_thread_start(void *arg)
const char *dss_precs[] = {"disabled", "primary", "secondary"};
const char *dss = dss_precs[thread_ind %
(sizeof(dss_precs)/sizeof(char*))];
- if (mallctlnametomib("arena.0.dss", mib, &miblen) != 0) {
- malloc_printf("Error in mallctlnametomib()\n");
- abort();
- }
+ assert_d_eq(mallctlnametomib("arena.0.dss", mib, &miblen), 0,
+ "Error in mallctlnametomib()");
mib[1] = arena_ind;
- if (mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss,
- sizeof(const char *))) {
- malloc_printf("Error in mallctlbymib()\n");
- abort();
- }
+ assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss,
+ sizeof(const char *)), 0, "Error in mallctlbymib()");
}
- r = allocm(&p, &rsz, 1, ALLOCM_ARENA(arena_ind));
- if (r != ALLOCM_SUCCESS) {
- malloc_printf("Unexpected allocm() error\n");
- abort();
- }
+ assert_d_eq(allocm(&p, &rsz, 1, ALLOCM_ARENA(arena_ind)),
+ ALLOCM_SUCCESS, "Unexpected allocm() error");
dallocm(p, 0);
return (NULL);
}
-int
-main(void)
+TEST_BEGIN(test_ALLOCM_ARENA)
{
je_thread_t threads[NTHREADS];
unsigned i;
- malloc_printf("Test begin\n");
-
for (i = 0; i < NTHREADS; i++) {
je_thread_create(&threads[i], je_thread_start,
(void *)(uintptr_t)i);
@@ -61,7 +46,13 @@ main(void)
for (i = 0; i < NTHREADS; i++)
je_thread_join(threads[i], NULL);
+}
+TEST_END
+
+int
+main(void)
+{
- malloc_printf("Test end\n");
- return (0);
+ return (test(
+ test_ALLOCM_ARENA));
}
diff --git a/test/integration/ALLOCM_ARENA.exp b/test/integration/ALLOCM_ARENA.exp
deleted file mode 100644
index 369a88d..0000000
--- a/test/integration/ALLOCM_ARENA.exp
+++ /dev/null
@@ -1,2 +0,0 @@
-Test begin
-Test end
diff --git a/test/integration/aligned_alloc.c b/test/integration/aligned_alloc.c
index 2c44751..73f73f8 100644
--- a/test/integration/aligned_alloc.c
+++ b/test/integration/aligned_alloc.c
@@ -5,34 +5,32 @@
#define MAXALIGN ((size_t)0x2000000LU)
#define NITER 4
-int
-main(void)
+TEST_BEGIN(test_alignment_errors)
{
- size_t alignment, size, total;
- unsigned i;
- void *p, *ps[NITER];
-
- malloc_printf("Test begin\n");
+ size_t alignment;
+ void *p;
- /* Test error conditions. */
alignment = 0;
set_errno(0);
p = aligned_alloc(alignment, 1);
- if (p != NULL || get_errno() != EINVAL) {
- malloc_printf(
- "Expected error for invalid alignment %zu\n", alignment);
- }
+ assert_false(p != NULL || get_errno() != EINVAL,
+ "Expected error for invalid alignment %zu", alignment);
for (alignment = sizeof(size_t); alignment < MAXALIGN;
alignment <<= 1) {
set_errno(0);
p = aligned_alloc(alignment + 1, 1);
- if (p != NULL || get_errno() != EINVAL) {
- malloc_printf(
- "Expected error for invalid alignment %zu\n",
- alignment + 1);
- }
+ assert_false(p != NULL || get_errno() != EINVAL,
+ "Expected error for invalid alignment %zu",
+ alignment + 1);
}
+}
+TEST_END
+
+TEST_BEGIN(test_oom_errors)
+{
+ size_t alignment, size;
+ void *p;
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x8000000000000000);
@@ -43,11 +41,9 @@ main(void)
#endif
set_errno(0);
p = aligned_alloc(alignment, size);
- if (p != NULL || get_errno() != ENOMEM) {
- malloc_printf(
- "Expected error for aligned_alloc(%zu, %zu)\n",
- alignment, size);
- }
+ assert_false(p != NULL || get_errno() != ENOMEM,
+ "Expected error for aligned_alloc(%zu, %zu)",
+ alignment, size);
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x4000000000000000);
@@ -58,11 +54,9 @@ main(void)
#endif
set_errno(0);
p = aligned_alloc(alignment, size);
- if (p != NULL || get_errno() != ENOMEM) {
- malloc_printf(
- "Expected error for aligned_alloc(%zu, %zu)\n",
- alignment, size);
- }
+ assert_false(p != NULL || get_errno() != ENOMEM,
+ "Expected error for aligned_alloc(%zu, %zu)",
+ alignment, size);
alignment = 0x10LU;
#if LG_SIZEOF_PTR == 3
@@ -72,11 +66,17 @@ main(void)
#endif
set_errno(0);
p = aligned_alloc(alignment, size);
- if (p != NULL || get_errno() != ENOMEM) {
- malloc_printf(
- "Expected error for aligned_alloc(&p, %zu, %zu)\n",
- alignment, size);
- }
+ assert_false(p != NULL || get_errno() != ENOMEM,
+ "Expected error for aligned_alloc(&p, %zu, %zu)",
+ alignment, size);
+}
+TEST_END
+
+TEST_BEGIN(test_alignment_and_size)
+{
+ size_t alignment, size, total;
+ unsigned i;
+ void *ps[NITER];
for (i = 0; i < NITER; i++)
ps[i] = NULL;
@@ -85,7 +85,6 @@ main(void)
alignment <= MAXALIGN;
alignment <<= 1) {
total = 0;
- malloc_printf("Alignment: %zu\n", alignment);
for (size = 1;
size < 3 * alignment && size < (1U << 31);
size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
@@ -94,10 +93,11 @@ main(void)
if (ps[i] == NULL) {
char buf[BUFERROR_BUF];
- buferror(buf, sizeof(buf));
+ buferror(get_errno(), buf, sizeof(buf));
test_fail(
- "Error for size %zu (%#zx): %s\n",
- size, size, buf);
+ "Error for alignment=%zu, "
+ "size=%zu (%#zx): %s",
+ alignment, size, size, buf);
}
total += malloc_usable_size(ps[i]);
if (total >= (MAXALIGN << 1))
@@ -111,7 +111,15 @@ main(void)
}
}
}
+}
+TEST_END
+
+int
+main(void)
+{
- malloc_printf("Test end\n");
- return (0);
+ return (test(
+ test_alignment_errors,
+ test_oom_errors,
+ test_alignment_and_size));
}
diff --git a/test/integration/aligned_alloc.exp b/test/integration/aligned_alloc.exp
deleted file mode 100644
index b5061c7..0000000
--- a/test/integration/aligned_alloc.exp
+++ /dev/null
@@ -1,25 +0,0 @@
-Test begin
-Alignment: 8
-Alignment: 16
-Alignment: 32
-Alignment: 64
-Alignment: 128
-Alignment: 256
-Alignment: 512
-Alignment: 1024
-Alignment: 2048
-Alignment: 4096
-Alignment: 8192
-Alignment: 16384
-Alignment: 32768
-Alignment: 65536
-Alignment: 131072
-Alignment: 262144
-Alignment: 524288
-Alignment: 1048576
-Alignment: 2097152
-Alignment: 4194304
-Alignment: 8388608
-Alignment: 16777216
-Alignment: 33554432
-Test end
diff --git a/test/integration/allocated.c b/test/integration/allocated.c
index 73ea738..156451d 100644
--- a/test/integration/allocated.c
+++ b/test/integration/allocated.c
@@ -1,5 +1,13 @@
#include "test/jemalloc_test.h"
+static const bool config_stats =
+#ifdef JEMALLOC_STATS
+ true
+#else
+ false
+#endif
+ ;
+
void *
je_thread_start(void *arg)
{
@@ -11,65 +19,57 @@ je_thread_start(void *arg)
sz = sizeof(a0);
if ((err = mallctl("thread.allocated", &a0, &sz, NULL, 0))) {
- if (err == ENOENT) {
-#ifdef JEMALLOC_STATS
- assert(false);
-#endif
- goto label_return;
- }
- test_fail("%s(): Error in mallctl(): %s\n", __func__,
+ if (err == ENOENT)
+ goto label_ENOENT;
+ test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
}
sz = sizeof(ap0);
if ((err = mallctl("thread.allocatedp", &ap0, &sz, NULL, 0))) {
- if (err == ENOENT) {
-#ifdef JEMALLOC_STATS
- assert(false);
-#endif
- goto label_return;
- }
- test_fail("%s(): Error in mallctl(): %s\n", __func__,
+ if (err == ENOENT)
+ goto label_ENOENT;
+ test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
}
- assert(*ap0 == a0);
+ assert_u64_eq(*ap0, a0,
+ "\"thread.allocatedp\" should provide a pointer to internal "
+ "storage");
sz = sizeof(d0);
if ((err = mallctl("thread.deallocated", &d0, &sz, NULL, 0))) {
- if (err == ENOENT) {
-#ifdef JEMALLOC_STATS
- assert(false);
-#endif
- goto label_return;
- }
- test_fail("%s(): Error in mallctl(): %s\n", __func__,
+ if (err == ENOENT)
+ goto label_ENOENT;
+ test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
}
sz = sizeof(dp0);
if ((err = mallctl("thread.deallocatedp", &dp0, &sz, NULL, 0))) {
- if (err == ENOENT) {
-#ifdef JEMALLOC_STATS
- assert(false);
-#endif
- goto label_return;
- }
- test_fail("%s(): Error in mallctl(): %s\n", __func__,
+ if (err == ENOENT)
+ goto label_ENOENT;
+ test_fail("%s(): Error in mallctl(): %s", __func__,
strerror(err));
}
- assert(*dp0 == d0);
+ assert_u64_eq(*dp0, d0,
+ "\"thread.deallocatedp\" should provide a pointer to internal "
+ "storage");
p = malloc(1);
- if (p == NULL)
- test_fail("%s(): Error in malloc()\n", __func__);
+ assert_ptr_not_null(p, "Unexpected malloc() error");
sz = sizeof(a1);
mallctl("thread.allocated", &a1, &sz, NULL, 0);
sz = sizeof(ap1);
mallctl("thread.allocatedp", &ap1, &sz, NULL, 0);
- assert(*ap1 == a1);
- assert(ap0 == ap1);
+ assert_u64_eq(*ap1, a1,
+ "Dereferenced \"thread.allocatedp\" value should equal "
+ "\"thread.allocated\" value");
+ assert_ptr_eq(ap0, ap1,
+ "Pointer returned by \"thread.allocatedp\" should not change");
usize = malloc_usable_size(p);
- assert(a0 + usize <= a1);
+ assert_u64_le(a0 + usize, a1,
+ "Allocated memory counter should increase by at least the amount "
+ "explicitly allocated");
free(p);
@@ -77,35 +77,49 @@ je_thread_start(void *arg)
mallctl("thread.deallocated", &d1, &sz, NULL, 0);
sz = sizeof(dp1);
mallctl("thread.deallocatedp", &dp1, &sz, NULL, 0);
- assert(*dp1 == d1);
- assert(dp0 == dp1);
+ assert_u64_eq(*dp1, d1,
+ "Dereferenced \"thread.deallocatedp\" value should equal "
+ "\"thread.deallocated\" value");
+ assert_ptr_eq(dp0, dp1,
+ "Pointer returned by \"thread.deallocatedp\" should not change");
- assert(d0 + usize <= d1);
+ assert_u64_le(d0 + usize, d1,
+ "Deallocated memory counter should increase by at least the amount "
+ "explicitly deallocated");
-label_return:
+ return (NULL);
+label_ENOENT:
+ assert_false(config_stats,
+ "ENOENT should only be returned if stats are disabled");
+ test_skip("\"thread.allocated\" mallctl not available");
return (NULL);
}
-int
-main(void)
+TEST_BEGIN(test_main_thread)
{
- int ret = 0;
- je_thread_t thread;
-
- malloc_printf("Test begin\n");
je_thread_start(NULL);
+}
+TEST_END
- je_thread_create(&thread, je_thread_start, NULL);
- je_thread_join(thread, NULL);
-
- je_thread_start(NULL);
+TEST_BEGIN(test_subthread)
+{
+ je_thread_t thread;
je_thread_create(&thread, je_thread_start, NULL);
je_thread_join(thread, NULL);
+}
+TEST_END
- je_thread_start(NULL);
+int
+main(void)
+{
- malloc_printf("Test end\n");
- return (ret);
+ /* Run tests multiple times to check for bad interactions. */
+ return (test(
+ test_main_thread,
+ test_subthread,
+ test_main_thread,
+ test_subthread,
+ test_main_thread));
}
diff --git a/test/integration/allocated.exp b/test/integration/allocated.exp
deleted file mode 100644
index 369a88d..0000000
--- a/test/integration/allocated.exp
+++ /dev/null
@@ -1,2 +0,0 @@
-Test begin
-Test end
diff --git a/test/integration/allocm.c b/test/integration/allocm.c
index 3b89282..59dfcf1 100644
--- a/test/integration/allocm.c
+++ b/test/integration/allocm.c
@@ -5,61 +5,44 @@
#define MAXALIGN ((size_t)0x2000000LU)
#define NITER 4
-int
-main(void)
+TEST_BEGIN(test_basic)
{
- int r;
+ size_t nsz, rsz, sz;
void *p;
- size_t nsz, rsz, sz, alignment, total;
- unsigned i;
- void *ps[NITER];
-
- malloc_printf("Test begin\n");
sz = 42;
nsz = 0;
- r = nallocm(&nsz, sz, 0);
- if (r != ALLOCM_SUCCESS) {
- malloc_printf("Unexpected nallocm() error\n");
- abort();
- }
+ assert_d_eq(nallocm(&nsz, sz, 0), ALLOCM_SUCCESS,
+ "Unexpected nallocm() error");
rsz = 0;
- r = allocm(&p, &rsz, sz, 0);
- if (r != ALLOCM_SUCCESS) {
- malloc_printf("Unexpected allocm() error\n");
- abort();
- }
- if (rsz < sz)
- malloc_printf("Real size smaller than expected\n");
- if (nsz != rsz)
- malloc_printf("nallocm()/allocm() rsize mismatch\n");
- if (dallocm(p, 0) != ALLOCM_SUCCESS)
- malloc_printf("Unexpected dallocm() error\n");
+ assert_d_eq(allocm(&p, &rsz, sz, 0), ALLOCM_SUCCESS,
+ "Unexpected allocm() error");
+ assert_zu_ge(rsz, sz, "Real size smaller than expected");
+ assert_zu_eq(nsz, rsz, "nallocm()/allocm() rsize mismatch");
+ assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
+ "Unexpected dallocm() error");
- r = allocm(&p, NULL, sz, 0);
- if (r != ALLOCM_SUCCESS) {
- malloc_printf("Unexpected allocm() error\n");
- abort();
- }
- if (dallocm(p, 0) != ALLOCM_SUCCESS)
- malloc_printf("Unexpected dallocm() error\n");
+ assert_d_eq(allocm(&p, NULL, sz, 0), ALLOCM_SUCCESS,
+ "Unexpected allocm() error");
+ assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
+ "Unexpected dallocm() error");
nsz = 0;
- r = nallocm(&nsz, sz, ALLOCM_ZERO);
- if (r != ALLOCM_SUCCESS) {
- malloc_printf("Unexpected nallocm() error\n");
- abort();
- }
+ assert_d_eq(nallocm(&nsz, sz, ALLOCM_ZERO), ALLOCM_SUCCESS,
+ "Unexpected nallocm() error");
rsz = 0;
- r = allocm(&p, &rsz, sz, ALLOCM_ZERO);
- if (r != ALLOCM_SUCCESS) {
- malloc_printf("Unexpected allocm() error\n");
- abort();
- }
- if (nsz != rsz)
- malloc_printf("nallocm()/allocm() rsize mismatch\n");
- if (dallocm(p, 0) != ALLOCM_SUCCESS)
- malloc_printf("Unexpected dallocm() error\n");
+ assert_d_eq(allocm(&p, &rsz, sz, ALLOCM_ZERO), ALLOCM_SUCCESS,
+ "Unexpected allocm() error");
+ assert_zu_eq(nsz, rsz, "nallocm()/allocm() rsize mismatch");
+ assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
+ "Unexpected dallocm() error");
+}
+TEST_END
+
+TEST_BEGIN(test_alignment_errors)
+{
+ void *p;
+ size_t nsz, rsz, sz, alignment;
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x8000000000000000);
@@ -69,21 +52,14 @@ main(void)
sz = 0x80000000LU;
#endif
nsz = 0;
- r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
- if (r == ALLOCM_SUCCESS) {
- malloc_printf(
- "Expected error for nallocm(&nsz, %zu, %#x)\n",
- sz, ALLOCM_ALIGN(alignment));
- }
+ assert_d_ne(nallocm(&nsz, sz, ALLOCM_ALIGN(alignment)), ALLOCM_SUCCESS,
+ "Expected error for nallocm(&nsz, %zu, %#x)",
+ sz, ALLOCM_ALIGN(alignment));
rsz = 0;
- r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
- if (r == ALLOCM_SUCCESS) {
- malloc_printf(
- "Expected error for allocm(&p, %zu, %#x)\n",
- sz, ALLOCM_ALIGN(alignment));
- }
- if (nsz != rsz)
- malloc_printf("nallocm()/allocm() rsize mismatch\n");
+ assert_d_ne(allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment)),
+ ALLOCM_SUCCESS, "Expected error for allocm(&p, %zu, %#x)",
+ sz, ALLOCM_ALIGN(alignment));
+ assert_zu_eq(nsz, rsz, "nallocm()/allocm() rsize mismatch");
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x4000000000000000);
@@ -93,16 +69,12 @@ main(void)
sz = 0x84000001LU;
#endif
nsz = 0;
- r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected nallocm() error\n");
+ assert_d_eq(nallocm(&nsz, sz, ALLOCM_ALIGN(alignment)), ALLOCM_SUCCESS,
+ "Unexpected nallocm() error");
rsz = 0;
- r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
- if (r == ALLOCM_SUCCESS) {
- malloc_printf(
- "Expected error for allocm(&p, %zu, %#x)\n",
- sz, ALLOCM_ALIGN(alignment));
- }
+ assert_d_ne(allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment)),
+ ALLOCM_SUCCESS, "Expected error for allocm(&p, %zu, %#x)",
+ sz, ALLOCM_ALIGN(alignment));
alignment = 0x10LU;
#if LG_SIZEOF_PTR == 3
@@ -111,21 +83,23 @@ main(void)
sz = 0xfffffff0LU;
#endif
nsz = 0;
- r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
- if (r == ALLOCM_SUCCESS) {
- malloc_printf(
- "Expected error for nallocm(&nsz, %zu, %#x)\n",
- sz, ALLOCM_ALIGN(alignment));
- }
+ assert_d_ne(nallocm(&nsz, sz, ALLOCM_ALIGN(alignment)), ALLOCM_SUCCESS,
+ "Expected error for nallocm(&nsz, %zu, %#x)",
+ sz, ALLOCM_ALIGN(alignment));
rsz = 0;
- r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
- if (r == ALLOCM_SUCCESS) {
- malloc_printf(
- "Expected error for allocm(&p, %zu, %#x)\n",
- sz, ALLOCM_ALIGN(alignment));
- }
- if (nsz != rsz)
- malloc_printf("nallocm()/allocm() rsize mismatch\n");
+ assert_d_ne(allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment)),
+ ALLOCM_SUCCESS, "Expected error for allocm(&p, %zu, %#x)",
+ sz, ALLOCM_ALIGN(alignment));
+ assert_zu_eq(nsz, rsz, "nallocm()/allocm() rsize mismatch");
+}
+TEST_END
+
+TEST_BEGIN(test_alignment_and_size)
+{
+ int r;
+ size_t nsz, rsz, sz, alignment, total;
+ unsigned i;
+ void *ps[NITER];
for (i = 0; i < NITER; i++)
ps[i] = NULL;
@@ -134,44 +108,35 @@ main(void)
alignment <= MAXALIGN;
alignment <<= 1) {
total = 0;
- malloc_printf("Alignment: %zu\n", alignment);
for (sz = 1;
sz < 3 * alignment && sz < (1U << 31);
sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
for (i = 0; i < NITER; i++) {
nsz = 0;
- r = nallocm(&nsz, sz,
- ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
- if (r != ALLOCM_SUCCESS) {
- test_fail(
- "nallocm() error for size %zu"
- " (%#zx): %d\n",
- sz, sz, r);
- }
+ r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment) |
+ ALLOCM_ZERO);
+ assert_d_eq(r, ALLOCM_SUCCESS,
+ "nallocm() error for alignment=%zu, "
+ "size=%zu (%#zx): %d",
+ alignment, sz, sz, r);
rsz = 0;
r = allocm(&ps[i], &rsz, sz,
ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
- if (r != ALLOCM_SUCCESS) {
- test_fail(
- "allocm() error for size %zu"
- " (%#zx): %d\n",
- sz, sz, r);
- }
- if (rsz < sz) {
- malloc_printf(
- "Real size smaller than"
- " expected\n");
- }
- if (nsz != rsz) {
- malloc_printf(
- "nallocm()/allocm() rsize"
- " mismatch\n");
- }
- if ((uintptr_t)p & (alignment-1)) {
- malloc_printf(
- "%p inadequately aligned for"
- " alignment: %zu\n", p, alignment);
- }
+ assert_d_eq(r, ALLOCM_SUCCESS,
+ "allocm() error for alignment=%zu, "
+ "size=%zu (%#zx): %d",
+ alignment, sz, sz, r);
+ assert_zu_ge(rsz, sz,
+ "Real size smaller than expected for "
+ "alignment=%zu, size=%zu", alignment, sz);
+ assert_zu_eq(nsz, rsz,
+ "nallocm()/allocm() rsize mismatch for "
+ "alignment=%zu, size=%zu", alignment, sz);
+ assert_ptr_null(
+ (void *)((uintptr_t)ps[i] & (alignment-1)),
+ "%p inadequately aligned for"
+ " alignment=%zu, size=%zu", ps[i],
+ alignment, sz);
sallocm(ps[i], &rsz, 0);
total += rsz;
if (total >= (MAXALIGN << 1))
@@ -185,7 +150,15 @@ main(void)
}
}
}
+}
+TEST_END
+
+int
+main(void)
+{
- malloc_printf("Test end\n");
- return (0);
+ return (test(
+ test_basic,
+ test_alignment_errors,
+ test_alignment_and_size));
}
diff --git a/test/integration/allocm.exp b/test/integration/allocm.exp
deleted file mode 100644
index b5061c7..0000000
--- a/test/integration/allocm.exp
+++ /dev/null
@@ -1,25 +0,0 @@
-Test begin
-Alignment: 8
-Alignment: 16
-Alignment: 32
-Alignment: 64
-Alignment: 128
-Alignment: 256
-Alignment: 512
-Alignment: 1024
-Alignment: 2048
-Alignment: 4096
-Alignment: 8192
-Alignment: 16384
-Alignment: 32768
-Alignment: 65536
-Alignment: 131072
-Alignment: 262144
-Alignment: 524288
-Alignment: 1048576
-Alignment: 2097152
-Alignment: 4194304
-Alignment: 8388608
-Alignment: 16777216
-Alignment: 33554432
-Test end
diff --git a/test/integration/mremap.c b/test/integration/mremap.c
index cdef9de..a7fb7ef 100644
--- a/test/integration/mremap.c
+++ b/test/integration/mremap.c
@@ -1,59 +1,45 @@
#include "test/jemalloc_test.h"
-int
-main(void)
+TEST_BEGIN(test_mremap)
{
- int ret, err;
+ int err;
size_t sz, lg_chunk, chunksize, i;
char *p, *q;
- malloc_printf("Test begin\n");
-
sz = sizeof(lg_chunk);
- if ((err = mallctl("opt.lg_chunk", &lg_chunk, &sz, NULL, 0))) {
- assert(err != ENOENT);
- malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
- strerror(err));
- ret = 1;
- goto label_return;
- }
+ err = mallctl("opt.lg_chunk", &lg_chunk, &sz, NULL, 0);
+ assert_d_eq(err, 0, "Error in mallctl(): %s", strerror(err));
chunksize = ((size_t)1U) << lg_chunk;
p = (char *)malloc(chunksize);
- if (p == NULL) {
- malloc_printf("malloc(%zu) --> %p\n", chunksize, p);
- ret = 1;
- goto label_return;
- }
+ assert_ptr_not_null(p, "malloc(%zu) --> %p", chunksize, p);
memset(p, 'a', chunksize);
q = (char *)realloc(p, chunksize * 2);
- if (q == NULL) {
- malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize * 2,
- q);
- ret = 1;
- goto label_return;
- }
+ assert_ptr_not_null(q, "realloc(%p, %zu) --> %p", p, chunksize * 2,
+ q);
for (i = 0; i < chunksize; i++) {
- assert(q[i] == 'a');
+ assert_c_eq(q[i], 'a',
+ "realloc() should preserve existing bytes across copies");
}
p = q;
q = (char *)realloc(p, chunksize);
- if (q == NULL) {
- malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize, q);
- ret = 1;
- goto label_return;
- }
+ assert_ptr_not_null(q, "realloc(%p, %zu) --> %p", p, chunksize, q);
for (i = 0; i < chunksize; i++) {
- assert(q[i] == 'a');
+ assert_c_eq(q[i], 'a',
+ "realloc() should preserve existing bytes across copies");
}
free(q);
+}
+TEST_END
+
+int
+main(void)
+{
- ret = 0;
-label_return:
- malloc_printf("Test end\n");
- return (ret);
+ return (test(
+ test_mremap));
}
diff --git a/test/integration/mremap.exp b/test/integration/mremap.exp
deleted file mode 100644
index 369a88d..0000000
--- a/test/integration/mremap.exp
+++ /dev/null
@@ -1,2 +0,0 @@
-Test begin
-Test end
diff --git a/test/integration/posix_memalign.c b/test/integration/posix_memalign.c
index dc5cd0e..48ca0b3 100644
--- a/test/integration/posix_memalign.c
+++ b/test/integration/posix_memalign.c
@@ -5,35 +5,30 @@
#define MAXALIGN ((size_t)0x2000000LU)
#define NITER 4
-int
-main(void)
+TEST_BEGIN(test_alignment_errors)
{
- size_t alignment, size, total;
- unsigned i;
- int err;
- void *p, *ps[NITER];
-
- malloc_printf("Test begin\n");
+ size_t alignment;
+ void *p;
- /* Test error conditions. */
for (alignment = 0; alignment < sizeof(void *); alignment++) {
- err = posix_memalign(&p, alignment, 1);
- if (err != EINVAL) {
- malloc_printf(
- "Expected error for invalid alignment %zu\n",
- alignment);
- }
+ assert_d_eq(posix_memalign(&p, alignment, 1), EINVAL,
+ "Expected error for invalid alignment %zu",
+ alignment);
}
for (alignment = sizeof(size_t); alignment < MAXALIGN;
alignment <<= 1) {
- err = posix_memalign(&p, alignment + 1, 1);
- if (err == 0) {
- malloc_printf(
- "Expected error for invalid alignment %zu\n",
- alignment + 1);
- }
+ assert_d_ne(posix_memalign(&p, alignment + 1, 1), 0,
+ "Expected error for invalid alignment %zu",
+ alignment + 1);
}
+}
+TEST_END
+
+TEST_BEGIN(test_oom_errors)
+{
+ size_t alignment, size;
+ void *p;
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x8000000000000000);
@@ -42,12 +37,9 @@ main(void)
alignment = 0x80000000LU;
size = 0x80000000LU;
#endif
- err = posix_memalign(&p, alignment, size);
- if (err == 0) {
- malloc_printf(
- "Expected error for posix_memalign(&p, %zu, %zu)\n",
- alignment, size);
- }
+ assert_d_ne(posix_memalign(&p, alignment, size), 0,
+ "Expected error for posix_memalign(&p, %zu, %zu)",
+ alignment, size);
#if LG_SIZEOF_PTR == 3
alignment = UINT64_C(0x4000000000000000);
@@ -56,12 +48,9 @@ main(void)
alignment = 0x40000000LU;
size = 0x84000001LU;
#endif
- err = posix_memalign(&p, alignment, size);
- if (err == 0) {
- malloc_printf(
- "Expected error for posix_memalign(&p, %zu, %zu)\n",
- alignment, size);
- }
+ assert_d_ne(posix_memalign(&p, alignment, size), 0,
+ "Expected error for posix_memalign(&p, %zu, %zu)",
+ alignment, size);
alignment = 0x10LU;
#if LG_SIZEOF_PTR == 3
@@ -69,12 +58,18 @@ main(void)
#else
size = 0xfffffff0LU;
#endif
- err = posix_memalign(&p, alignment, size);
- if (err == 0) {
- malloc_printf(
- "Expected error for posix_memalign(&p, %zu, %zu)\n",
- alignment, size);
- }
+ assert_d_ne(posix_memalign(&p, alignment, size), 0,
+ "Expected error for posix_memalign(&p, %zu, %zu)",
+ alignment, size);
+}
+TEST_END
+
+TEST_BEGIN(test_alignment_and_size)
+{
+ size_t alignment, size, total;
+ unsigned i;
+ int err;
+ void *ps[NITER];
for (i = 0; i < NITER; i++)
ps[i] = NULL;
@@ -83,7 +78,6 @@ main(void)
alignment <= MAXALIGN;
alignment <<= 1) {
total = 0;
- malloc_printf("Alignment: %zu\n", alignment);
for (size = 1;
size < 3 * alignment && size < (1U << 31);
size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
@@ -91,9 +85,13 @@ main(void)
err = posix_memalign(&ps[i],
alignment, size);
if (err) {
+ char buf[BUFERROR_BUF];
+
+ buferror(get_errno(), buf, sizeof(buf));
test_fail(
- "Error for size %zu (%#zx): %s\n",
- size, size, strerror(err));
+ "Error for alignment=%zu, "
+ "size=%zu (%#zx): %s",
+ alignment, size, size, buf);
}
total += malloc_usable_size(ps[i]);
if (total >= (MAXALIGN << 1))
@@ -107,7 +105,15 @@ main(void)
}
}
}
+}
+TEST_END
+
+int
+main(void)
+{
- malloc_printf("Test end\n");
- return (0);
+ return (test(
+ test_alignment_errors,
+ test_oom_errors,
+ test_alignment_and_size));
}
diff --git a/test/integration/posix_memalign.exp b/test/integration/posix_memalign.exp
deleted file mode 100644
index b5061c7..0000000
--- a/test/integration/posix_memalign.exp
+++ /dev/null
@@ -1,25 +0,0 @@
-Test begin
-Alignment: 8
-Alignment: 16
-Alignment: 32
-Alignment: 64
-Alignment: 128
-Alignment: 256
-Alignment: 512
-Alignment: 1024
-Alignment: 2048
-Alignment: 4096
-Alignment: 8192
-Alignment: 16384
-Alignment: 32768
-Alignment: 65536
-Alignment: 131072
-Alignment: 262144
-Alignment: 524288
-Alignment: 1048576
-Alignment: 2097152
-Alignment: 4194304
-Alignment: 8388608
-Alignment: 16777216
-Alignment: 33554432
-Test end
diff --git a/test/integration/rallocm.c b/test/integration/rallocm.c
index 2c10dba..c13cd69 100644
--- a/test/integration/rallocm.c
+++ b/test/integration/rallocm.c
@@ -2,127 +2,112 @@
#include "test/jemalloc_test.h"
-int
-main(void)
+TEST_BEGIN(test_same_size)
{
- size_t pagesize;
void *p, *q;
size_t sz, tsz;
- int r;
-
- malloc_printf("Test begin\n");
-
- /* Get page size. */
- {
-#ifdef _WIN32
- SYSTEM_INFO si;
- GetSystemInfo(&si);
- pagesize = (size_t)si.dwPageSize;
-#else
- long result = sysconf(_SC_PAGESIZE);
- assert(result != -1);
- pagesize = (size_t)result;
-#endif
- }
- r = allocm(&p, &sz, 42, 0);
- if (r != ALLOCM_SUCCESS) {
- malloc_printf("Unexpected allocm() error\n");
- abort();
- }
+ assert_d_eq(allocm(&p, &sz, 42, 0), ALLOCM_SUCCESS,
+ "Unexpected allocm() error");
q = p;
- r = rallocm(&q, &tsz, sz, 0, ALLOCM_NO_MOVE);
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected rallocm() error\n");
- if (q != p)
- malloc_printf("Unexpected object move\n");
- if (tsz != sz) {
- malloc_printf("Unexpected size change: %zu --> %zu\n",
- sz, tsz);
- }
+ assert_d_eq(rallocm(&q, &tsz, sz, 0, ALLOCM_NO_MOVE), ALLOCM_SUCCESS,
+ "Unexpected rallocm() error");
+ assert_ptr_eq(q, p, "Unexpected object move");
+ assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
- q = p;
- r = rallocm(&q, &tsz, sz, 5, ALLOCM_NO_MOVE);
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected rallocm() error\n");
- if (q != p)
- malloc_printf("Unexpected object move\n");
- if (tsz != sz) {
- malloc_printf("Unexpected size change: %zu --> %zu\n",
- sz, tsz);
- }
+ assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
+ "Unexpected dallocm() error");
+}
+TEST_END
+
+TEST_BEGIN(test_extra_no_move)
+{
+ void *p, *q;
+ size_t sz, tsz;
+
+ assert_d_eq(allocm(&p, &sz, 42, 0), ALLOCM_SUCCESS,
+ "Unexpected allocm() error");
q = p;
- r = rallocm(&q, &tsz, sz + 5, 0, ALLOCM_NO_MOVE);
- if (r != ALLOCM_ERR_NOT_MOVED)
- malloc_printf("Unexpected rallocm() result\n");
- if (q != p)
- malloc_printf("Unexpected object move\n");
- if (tsz != sz) {
- malloc_printf("Unexpected size change: %zu --> %zu\n",
- sz, tsz);
- }
+ assert_d_eq(rallocm(&q, &tsz, sz, sz-42, ALLOCM_NO_MOVE),
+ ALLOCM_SUCCESS, "Unexpected rallocm() error");
+ assert_ptr_eq(q, p, "Unexpected object move");
+ assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
+
+ assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
+ "Unexpected dallocm() error");
+}
+TEST_END
+
+TEST_BEGIN(test_no_move_fail)
+{
+ void *p, *q;
+ size_t sz, tsz;
+
+ assert_d_eq(allocm(&p, &sz, 42, 0), ALLOCM_SUCCESS,
+ "Unexpected allocm() error");
q = p;
- r = rallocm(&q, &tsz, sz + 5, 0, 0);
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected rallocm() error\n");
- if (q == p)
- malloc_printf("Expected object move\n");
- if (tsz == sz) {
- malloc_printf("Expected size change: %zu --> %zu\n",
- sz, tsz);
- }
- p = q;
- sz = tsz;
-
- r = rallocm(&q, &tsz, pagesize*2, 0, 0);
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected rallocm() error\n");
- if (q == p)
- malloc_printf("Expected object move\n");
- if (tsz == sz) {
- malloc_printf("Expected size change: %zu --> %zu\n",
- sz, tsz);
- }
- p = q;
- sz = tsz;
-
- r = rallocm(&q, &tsz, pagesize*4, 0, 0);
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected rallocm() error\n");
- if (tsz == sz) {
- malloc_printf("Expected size change: %zu --> %zu\n",
- sz, tsz);
- }
- p = q;
- sz = tsz;
-
- r = rallocm(&q, &tsz, pagesize*2, 0, ALLOCM_NO_MOVE);
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected rallocm() error\n");
- if (q != p)
- malloc_printf("Unexpected object move\n");
- if (tsz == sz) {
- malloc_printf("Expected size change: %zu --> %zu\n",
- sz, tsz);
- }
- sz = tsz;
-
- r = rallocm(&q, &tsz, pagesize*4, 0, ALLOCM_NO_MOVE);
- if (r != ALLOCM_SUCCESS)
- malloc_printf("Unexpected rallocm() error\n");
- if (q != p)
- malloc_printf("Unexpected object move\n");
- if (tsz == sz) {
- malloc_printf("Expected size change: %zu --> %zu\n",
- sz, tsz);
+ assert_d_eq(rallocm(&q, &tsz, sz + 5, 0, ALLOCM_NO_MOVE),
+ ALLOCM_ERR_NOT_MOVED, "Unexpected rallocm() result");
+ assert_ptr_eq(q, p, "Unexpected object move");
+ assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
+
+ assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
+ "Unexpected dallocm() error");
+}
+TEST_END
+
+TEST_BEGIN(test_grow_and_shrink)
+{
+ void *p, *q;
+ size_t tsz;
+#define NCYCLES 3
+ unsigned i, j;
+#define NSZS 2500
+ size_t szs[NSZS];
+#define MAXSZ ZU(12 * 1024 * 1024)
+
+ assert_d_eq(allocm(&p, &szs[0], 1, 0), ALLOCM_SUCCESS,
+ "Unexpected allocm() error");
+
+ for (i = 0; i < NCYCLES; i++) {
+ for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
+ q = p;
+ assert_d_eq(rallocm(&q, &szs[j], szs[j-1]+1, 0, 0),
+ ALLOCM_SUCCESS,
+ "Unexpected rallocm() error for size=%zu-->%zu",
+ szs[j-1], szs[j-1]+1);
+ assert_zu_ne(szs[j], szs[j-1]+1,
+ "Expected size to at least: %zu", szs[j-1]+1);
+ p = q;
+ }
+
+ for (j--; j > 0; j--) {
+ q = p;
+ assert_d_eq(rallocm(&q, &tsz, szs[j-1], 0, 0),
+ ALLOCM_SUCCESS,
+ "Unexpected rallocm() error for size=%zu-->%zu",
+ szs[j], szs[j-1]);
+ assert_zu_eq(tsz, szs[j-1],
+ "Expected size=%zu, got size=%zu", szs[j-1], tsz);
+ p = q;
+ }
}
- sz = tsz;
- dallocm(p, 0);
+ assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
+ "Unexpected dallocm() error");
+}
+TEST_END
+
+int
+main(void)
+{
- malloc_printf("Test end\n");
- return (0);
+ return (test(
+ test_same_size,
+ test_extra_no_move,
+ test_no_move_fail,
+ test_grow_and_shrink));
}
diff --git a/test/integration/rallocm.exp b/test/integration/rallocm.exp
deleted file mode 100644
index 369a88d..0000000
--- a/test/integration/rallocm.exp
+++ /dev/null
@@ -1,2 +0,0 @@
-Test begin
-Test end
diff --git a/test/integration/thread_arena.c b/test/integration/thread_arena.c
index eb5b098..cf0aad7 100644
--- a/test/integration/thread_arena.c
+++ b/test/integration/thread_arena.c
@@ -12,36 +12,33 @@ je_thread_start(void *arg)
int err;
p = malloc(1);
- if (p == NULL) {
- malloc_printf("%s(): Error in malloc()\n", __func__);
- return (void *)1;
- }
+ assert_ptr_not_null(p, "Error in malloc()");
free(p);
size = sizeof(arena_ind);
if ((err = mallctl("thread.arena", &arena_ind, &size, &main_arena_ind,
sizeof(main_arena_ind)))) {
- malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
- strerror(err));
- return (void *)1;
+ char buf[BUFERROR_BUF];
+
+ buferror(err, buf, sizeof(buf));
+ test_fail("Error in mallctl(): %s", buf);
}
size = sizeof(arena_ind);
- if ((err = mallctl("thread.arena", &arena_ind, &size, NULL,
- 0))) {
- malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
- strerror(err));
- return (void *)1;
+ if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) {
+ char buf[BUFERROR_BUF];
+
+ buferror(err, buf, sizeof(buf));
+ test_fail("Error in mallctl(): %s", buf);
}
- assert(arena_ind == main_arena_ind);
+ assert_u_eq(arena_ind, main_arena_ind,
+ "Arena index should be same as for main thread");
return (NULL);
}
-int
-main(void)
+TEST_BEGIN(test_thread_arena)
{
- int ret = 0;
void *p;
unsigned arena_ind;
size_t size;
@@ -49,21 +46,15 @@ main(void)
je_thread_t threads[NTHREADS];
unsigned i;
- malloc_printf("Test begin\n");
-
p = malloc(1);
- if (p == NULL) {
- malloc_printf("%s(): Error in malloc()\n", __func__);
- ret = 1;
- goto label_return;
- }
+ assert_ptr_not_null(p, "Error in malloc()");
size = sizeof(arena_ind);
if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) {
- malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
- strerror(err));
- ret = 1;
- goto label_return;
+ char buf[BUFERROR_BUF];
+
+ buferror(err, buf, sizeof(buf));
+ test_fail("Error in mallctl(): %s", buf);
}
for (i = 0; i < NTHREADS; i++) {
@@ -74,11 +65,15 @@ main(void)
for (i = 0; i < NTHREADS; i++) {
intptr_t join_ret;
je_thread_join(threads[i], (void *)&join_ret);
- if (join_ret != 0)
- ret = 1;
+ assert_zd_eq(join_ret, 0, "Unexpected thread join error");
}
+}
+TEST_END
+
+int
+main(void)
+{
-label_return:
- malloc_printf("Test end\n");
- return (ret);
+ return (test(
+ test_thread_arena));
}
diff --git a/test/integration/thread_arena.exp b/test/integration/thread_arena.exp
deleted file mode 100644
index 369a88d..0000000
--- a/test/integration/thread_arena.exp
+++ /dev/null
@@ -1,2 +0,0 @@
-Test begin
-Test end
diff --git a/test/integration/thread_tcache_enabled.c b/test/integration/thread_tcache_enabled.c
index f9da052..52e8a11 100644
--- a/test/integration/thread_tcache_enabled.c
+++ b/test/integration/thread_tcache_enabled.c
@@ -1,5 +1,13 @@
#include "test/jemalloc_test.h"
+static const bool config_tcache =
+#ifdef JEMALLOC_TCACHE
+ true
+#else
+ false
+#endif
+ ;
+
void *
je_thread_start(void *arg)
{
@@ -10,81 +18,96 @@ je_thread_start(void *arg)
sz = sizeof(bool);
if ((err = mallctl("thread.tcache.enabled", &e0, &sz, NULL, 0))) {
if (err == ENOENT) {
-#ifdef JEMALLOC_TCACHE
- assert(false);
-#endif
+ assert_false(config_tcache,
+ "ENOENT should only be returned if tcache is "
+ "disabled");
}
- goto label_return;
+ goto label_ENOENT;
}
if (e0) {
e1 = false;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz)
- == 0);
- assert(e0);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz),
+ 0, "Unexpected mallctl() error");
+ assert_true(e0, "tcache should be enabled");
}
e1 = true;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0 == false);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_false(e0, "tcache should be disabled");
e1 = true;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_true(e0, "tcache should be enabled");
e1 = false;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_true(e0, "tcache should be enabled");
e1 = false;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0 == false);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_false(e0, "tcache should be disabled");
free(malloc(1));
e1 = true;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0 == false);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_false(e0, "tcache should be disabled");
free(malloc(1));
e1 = true;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_true(e0, "tcache should be enabled");
free(malloc(1));
e1 = false;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_true(e0, "tcache should be enabled");
free(malloc(1));
e1 = false;
- assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0);
- assert(e0 == false);
+ assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
+ "Unexpected mallctl() error");
+ assert_false(e0, "tcache should be disabled");
free(malloc(1));
-label_return:
+ return (NULL);
+label_ENOENT:
+ test_skip("\"thread.tcache.enabled\" mallctl not available");
return (NULL);
}
-int
-main(void)
+TEST_BEGIN(test_main_thread)
{
- int ret = 0;
- je_thread_t thread;
-
- malloc_printf("Test begin\n");
je_thread_start(NULL);
+}
+TEST_END
- je_thread_create(&thread, je_thread_start, NULL);
- je_thread_join(thread, NULL);
-
- je_thread_start(NULL);
+TEST_BEGIN(test_subthread)
+{
+ je_thread_t thread;
je_thread_create(&thread, je_thread_start, NULL);
je_thread_join(thread, NULL);
+}
+TEST_END
- je_thread_start(NULL);
+int
+main(void)
+{
- malloc_printf("Test end\n");
- return (ret);
+ /* Run tests multiple times to check for bad interactions. */
+ return (test(
+ test_main_thread,
+ test_subthread,
+ test_main_thread,
+ test_subthread,
+ test_main_thread));
}
diff --git a/test/integration/thread_tcache_enabled.exp b/test/integration/thread_tcache_enabled.exp
deleted file mode 100644
index 369a88d..0000000
--- a/test/integration/thread_tcache_enabled.exp
+++ /dev/null
@@ -1,2 +0,0 @@
-Test begin
-Test end
diff --git a/test/src/test.c b/test/src/test.c
index 1bc34b4..eb1f5ef 100644
--- a/test/src/test.c
+++ b/test/src/test.c
@@ -1,28 +1,96 @@
#include "test/jemalloc_test.h"
-#define JEMALLOC_TEST_EXIT_FAIL 1
-#define JEMALLOC_TEST_EXIT_SKIP 2
+static unsigned test_count = 0;
+static test_status_t test_counts[test_status_count] = {0, 0, 0};
+static test_status_t test_status = test_status_pass;
+static const char * test_name = "";
JEMALLOC_ATTR(format(printf, 1, 2))
void
-test_fail(const char *format, ...)
+test_skip(const char *format, ...)
{
va_list ap;
va_start(ap, format);
malloc_vcprintf(NULL, NULL, format, ap);
va_end(ap);
- exit(JEMALLOC_TEST_EXIT_FAIL);
+ test_status = test_status_skip;
}
JEMALLOC_ATTR(format(printf, 1, 2))
void
-test_skip(const char *format, ...)
+test_fail(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ malloc_vcprintf(NULL, NULL, format, ap);
+ va_end(ap);
+ test_status = test_status_fail;
+}
+
+static const char *
+test_status_string(test_status_t test_status)
+{
+
+ switch (test_status) {
+ case test_status_pass: return "pass";
+ case test_status_skip: return "skip";
+ case test_status_fail: return "fail";
+ default: not_reached();
+ }
+}
+
+void
+p_test_init(const char *name)
+{
+
+ test_count++;
+ test_status = test_status_pass;
+ test_name = name;
+}
+
+void
+p_test_fini(void)
+{
+
+ test_counts[test_status]++;
+ malloc_printf("%s: %s\n", test_name, test_status_string(test_status));
+}
+
+test_status_t
+p_test(test_t* t, ...)
+{
+ test_status_t ret = test_status_pass;
+ va_list ap;
+
+ va_start(ap, t);
+ for (; t != NULL; t = va_arg(ap, test_t*)) {
+ t();
+ if (test_status > ret)
+ ret = test_status;
+ }
+ va_end(ap);
+
+ malloc_printf("tests: %u, pass: %u, skip: %u, fail: %u\n",
+ test_count,
+ test_counts[test_status_pass],
+ test_counts[test_status_skip],
+ test_counts[test_status_fail]);
+
+ return (ret);
+}
+
+void
+p_test_fail(const char *format, ...)
{
va_list ap;
va_start(ap, format);
malloc_vcprintf(NULL, NULL, format, ap);
+ format = va_arg(ap, const char *);
+ malloc_vcprintf(NULL, NULL, format, ap);
va_end(ap);
- exit(JEMALLOC_TEST_EXIT_SKIP);
+ malloc_printf("\n");
+ test_status = test_status_fail;
}
diff --git a/test/test.sh.in b/test/test.sh.in
index 726cd63..93c1978 100644
--- a/test/test.sh.in
+++ b/test/test.sh.in
@@ -11,27 +11,42 @@ case @abi@ in
;;
esac
-total=0
-failures=0
-echo "========================================="
+# Corresponds to test_status_t.
+pass_code=0
+skip_code=1
+fail_code=2
+
+echo "================================================================================"
+pass_count=0
+skip_count=0
+fail_count=0
for t in $@; do
- total=`expr $total + 1`
- /bin/echo -n "${t} ... "
+ echo "${t}:"
${t}@exe@ @abs_srcroot@ @abs_objroot@ > @objroot@${t}.out 2>&1
- result=$?
- if [ -e "@srcroot@${t}.exp" ] ; then
- diff -w -u @srcroot@${t}.exp @objroot@${t}.out >/dev/null 2>&1
- fail=$?
- if [ "${fail}" -eq "1" ] ; then
- failures=`expr ${failures} + 1`
- echo "*** FAIL ***"
- else
- echo "pass"
- fi
- else
- echo "*** FAIL *** (.exp file is missing)"
- failures=`expr ${failures} + 1`
- fi
+ result_code=$?
+ /bin/echo -n " "
+ tail -n 1 @objroot@${t}.out
+ case ${result_code} in
+ ${pass_code})
+ pass_count=$((pass_count+1))
+ ;;
+ ${skip_code})
+ skip_count=$((skip_count+1))
+ ;;
+ ${fail_code})
+ fail_count=$((fail_count+1))
+ echo " *** ${t} failure; see @objroot@${t}.out for full output ***" 1>&2
+ ;;
+ *)
+ echo "Test harness error" 1>&2
+ exit 1
+ esac
done
-echo "========================================="
-echo "Failures: ${failures}/${total}"
+echo "================================================================================"
+echo "Test suite summary: pass: ${pass_count}, skip: ${skip_count}, fail: ${fail_count}"
+
+if [ ${fail_count} -eq 0 ] ; then
+ exit 0
+else
+ exit 1
+fi
diff --git a/test/unit/bitmap.c b/test/unit/bitmap.c
index 37c3043..8086b88 100644
--- a/test/unit/bitmap.c
+++ b/test/unit/bitmap.c
@@ -6,21 +6,21 @@
# define MAXBITS (1U << LG_BITMAP_MAXBITS)
#endif
-static void
-test_bitmap_size(void)
+TEST_BEGIN(test_bitmap_size)
{
size_t i, prev_size;
prev_size = 0;
for (i = 1; i <= MAXBITS; i++) {
size_t size = bitmap_size(i);
- assert(size >= prev_size);
+ assert_true(size >= prev_size,
+ "Bitmap size is smaller than expected");
prev_size = size;
}
}
+TEST_END
-static void
-test_bitmap_init(void)
+TEST_BEGIN(test_bitmap_init)
{
size_t i;
@@ -33,15 +33,17 @@ test_bitmap_init(void)
bitmap_info_ngroups(&binfo));
bitmap_init(bitmap, &binfo);
- for (j = 0; j < i; j++)
- assert(bitmap_get(bitmap, &binfo, j) == false);
+ for (j = 0; j < i; j++) {
+ assert_false(bitmap_get(bitmap, &binfo, j),
+ "Bit should be unset");
+ }
free(bitmap);
}
}
}
+TEST_END
-static void
-test_bitmap_set(void)
+TEST_BEGIN(test_bitmap_set)
{
size_t i;
@@ -56,14 +58,15 @@ test_bitmap_set(void)
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
- assert(bitmap_full(bitmap, &binfo));
+ assert_true(bitmap_full(bitmap, &binfo),
+ "All bits should be set");
free(bitmap);
}
}
}
+TEST_END
-static void
-test_bitmap_unset(void)
+TEST_BEGIN(test_bitmap_unset)
{
size_t i;
@@ -78,19 +81,21 @@ test_bitmap_unset(void)
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
- assert(bitmap_full(bitmap, &binfo));
+ assert_true(bitmap_full(bitmap, &binfo),
+ "All bits should be set");
for (j = 0; j < i; j++)
bitmap_unset(bitmap, &binfo, j);
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
- assert(bitmap_full(bitmap, &binfo));
+ assert_true(bitmap_full(bitmap, &binfo),
+ "All bits should be set");
free(bitmap);
}
}
}
+TEST_END
-static void
-test_bitmap_sfu(void)
+TEST_BEGIN(test_bitmap_sfu)
{
size_t i;
@@ -104,9 +109,13 @@ test_bitmap_sfu(void)
bitmap_init(bitmap, &binfo);
/* Iteratively set bits starting at the beginning. */
- for (j = 0; j < i; j++)
- assert(bitmap_sfu(bitmap, &binfo) == j);
- assert(bitmap_full(bitmap, &binfo));
+ for (j = 0; j < i; j++) {
+ assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
+ "First unset bit should be just after "
+ "previous first unset bit");
+ }
+ assert_true(bitmap_full(bitmap, &binfo),
+ "All bits should be set");
/*
* Iteratively unset bits starting at the end, and
@@ -114,10 +123,13 @@ test_bitmap_sfu(void)
*/
for (j = i - 1; j >= 0; j--) {
bitmap_unset(bitmap, &binfo, j);
- assert(bitmap_sfu(bitmap, &binfo) == j);
+ assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
+ "First unset bit should the bit previously "
+ "unset");
bitmap_unset(bitmap, &binfo, j);
}
- assert(bitmap_get(bitmap, &binfo, 0) == false);
+ assert_false(bitmap_get(bitmap, &binfo, 0),
+ "Bit should be unset");
/*
* Iteratively set bits starting at the beginning, and
@@ -125,27 +137,29 @@ test_bitmap_sfu(void)
*/
for (j = 1; j < i; j++) {
bitmap_set(bitmap, &binfo, j - 1);
- assert(bitmap_sfu(bitmap, &binfo) == j);
+ assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
+ "First unset bit should be just after the "
+ "bit previously set");
bitmap_unset(bitmap, &binfo, j);
}
- assert(bitmap_sfu(bitmap, &binfo) == i - 1);
- assert(bitmap_full(bitmap, &binfo));
+ assert_zd_eq(bitmap_sfu(bitmap, &binfo), i - 1,
+ "First unset bit should be the last bit");
+ assert_true(bitmap_full(bitmap, &binfo),
+ "All bits should be set");
free(bitmap);
}
}
}
+TEST_END
int
main(void)
{
- malloc_printf("Test begin\n");
-
- test_bitmap_size();
- test_bitmap_init();
- test_bitmap_set();
- test_bitmap_unset();
- test_bitmap_sfu();
- malloc_printf("Test end\n");
- return (0);
+ return (test(
+ test_bitmap_size,
+ test_bitmap_init,
+ test_bitmap_set,
+ test_bitmap_unset,
+ test_bitmap_sfu));
}
diff --git a/test/unit/bitmap.exp b/test/unit/bitmap.exp
deleted file mode 100644
index 369a88d..0000000
--- a/test/unit/bitmap.exp
+++ /dev/null
@@ -1,2 +0,0 @@
-Test begin
-Test end
diff --git a/test/unit/tsd.c b/test/unit/tsd.c
index dacddff..1160a79 100644
--- a/test/unit/tsd.c
+++ b/test/unit/tsd.c
@@ -4,12 +4,16 @@
typedef unsigned int data_t;
+static bool data_cleanup_executed;
+
void
data_cleanup(void *arg)
{
data_t *data = (data_t *)arg;
- malloc_printf("Cleanup for data %x.\n", *data);
+ assert_x_eq(*data, THREAD_DATA,
+ "Argument passed into cleanup function should match tsd value");
+ data_cleanup_executed = true;
}
malloc_tsd_protos(, data, data_t)
@@ -21,34 +25,47 @@ malloc_tsd_funcs(, data, data_t, DATA_INIT, data_cleanup)
void *
je_thread_start(void *arg)
{
- data_t d = (data_t)(uintptr_t) arg;
- malloc_printf("Initial tsd_get returns %x. Expected %x.\n",
- *data_tsd_get(), DATA_INIT);
+ data_t d = (data_t)(uintptr_t)arg;
+ assert_x_eq(*data_tsd_get(), DATA_INIT,
+ "Initial tsd get should return initialization value");
data_tsd_set(&d);
- malloc_printf("After tsd_set: %x. Expected %x.\n",
- *data_tsd_get(), d);
+ assert_x_eq(*data_tsd_get(), d,
+ "After tsd set, tsd get should return value that was set");
d = 0;
- malloc_printf("After resetting local data: %x. Expected %x.\n",
- *data_tsd_get(), (data_t)(uintptr_t) arg);
+ assert_x_eq(*data_tsd_get(), (data_t)(uintptr_t)arg,
+ "Resetting local data should have no effect on tsd");
return NULL;
}
-int
-main(void)
+TEST_BEGIN(test_tsd_main_thread)
{
- je_thread_t thread;
-
- malloc_printf("Test begin\n");
- data_tsd_boot();
je_thread_start((void *) 0xa5f3e329);
+}
+TEST_END
+TEST_BEGIN(test_tsd_sub_thread)
+{
+ je_thread_t thread;
+
+ data_cleanup_executed = false;
je_thread_create(&thread, je_thread_start, (void *) THREAD_DATA);
je_thread_join(thread, NULL);
+ assert_true(data_cleanup_executed,
+ "Cleanup function should have executed");
+}
+TEST_END
+
+int
+main(void)
+{
+
+ data_tsd_boot();
- malloc_printf("Test end\n");
- return (0);
+ return (test(
+ test_tsd_main_thread,
+ test_tsd_sub_thread));
}
diff --git a/test/unit/tsd.exp b/test/unit/tsd.exp
deleted file mode 100644
index b4abedc..0000000
--- a/test/unit/tsd.exp
+++ /dev/null
@@ -1,9 +0,0 @@
-Test begin
-Initial tsd_get returns 12345678. Expected 12345678.
-After tsd_set: a5f3e329. Expected a5f3e329.
-After resetting local data: a5f3e329. Expected a5f3e329.
-Initial tsd_get returns 12345678. Expected 12345678.
-After tsd_set: 72b65c10. Expected 72b65c10.
-After resetting local data: 72b65c10. Expected 72b65c10.
-Cleanup for data 72b65c10.
-Test end