Refactor to support more varied testing.
Refactor the test harness to support three types of tests: - unit: White box unit tests. These tests have full access to all internal jemalloc library symbols. Though in actuality all symbols are prefixed by jet_, macro-based name mangling abstracts this away from test code. - integration: Black box integration tests. These tests link with the installable shared jemalloc library, and with the exception of some utility code and configure-generated macro definitions, they have no access to jemalloc internals. - stress: Black box stress tests. These tests link with the installable shared jemalloc library, as well as with an internal allocator with symbols prefixed by jet_ (same as for unit tests) that can be used to allocate data structures that are internal to the test code. Move existing tests into test/{unit,integration}/ as appropriate. Split out internal parts of jemalloc_defs.h.in and put them in jemalloc_internal_defs.h.in. This reduces internals exposure to applications that #include <jemalloc/jemalloc.h>. Refactor jemalloc.h header generation so that a single header file results, and the prototypes can be used to generate jet_ prototypes for tests. Split jemalloc.h.in into multiple parts (jemalloc_defs.h.in, jemalloc_macros.h.in, jemalloc_protos.h.in, jemalloc_mangle.h.in) and use a shell script to generate a unified jemalloc.h at configure time. Change the default private namespace prefix from "" to "je_". Add missing private namespace mangling. Remove hard-coded private_namespace.h. Instead generate it and private_unnamespace.h from private_symbols.txt. Use similar logic for public symbols, which aids in name mangling for jet_ symbols. Add test_warn() and test_fail(). Replace existing exit(1) calls with test_fail() calls.
This commit is contained in:
111
test/include/test/jemalloc_test.h.in
Normal file
111
test/include/test/jemalloc_test.h.in
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Define always-enabled assertion macros, so that test assertions execute even
|
||||
* if assertions are disabled in the library code. These definitions must
|
||||
* exist prior to including "jemalloc/internal/util.h".
|
||||
*/
|
||||
#define assert(e) do { \
|
||||
if (!(e)) { \
|
||||
malloc_printf( \
|
||||
"<jemalloc>: %s:%d: Failed assertion: \"%s\"\n", \
|
||||
__FILE__, __LINE__, #e); \
|
||||
abort(); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define not_reached() do { \
|
||||
malloc_printf( \
|
||||
"<jemalloc>: %s:%d: Unreachable code reached\n", \
|
||||
__FILE__, __LINE__); \
|
||||
abort(); \
|
||||
} while (0)
|
||||
|
||||
#define not_implemented() do { \
|
||||
malloc_printf("<jemalloc>: %s:%d: Not implemented\n", \
|
||||
__FILE__, __LINE__); \
|
||||
abort(); \
|
||||
} while (0)
|
||||
|
||||
#define assert_not_implemented(e) do { \
|
||||
if (!(e)) \
|
||||
not_implemented(); \
|
||||
} while (0)
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* For unit tests, expose all public and private interfaces.
|
||||
*/
|
||||
#ifdef JEMALLOC_UNIT_TEST
|
||||
# define JEMALLOC_JET
|
||||
# include "jemalloc/internal/jemalloc_internal.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* For integration tests, expose the public jemalloc interfaces, but only
|
||||
* expose the minimum necessary internal utility code (to avoid re-implementing
|
||||
* essentially identical code within the test infrastructure).
|
||||
*/
|
||||
#elif defined(JEMALLOC_INTEGRATION_TEST)
|
||||
# define JEMALLOC_MANGLE
|
||||
# include "jemalloc/jemalloc@install_suffix@.h"
|
||||
# include "jemalloc/internal/jemalloc_internal_defs.h"
|
||||
|
||||
# define JEMALLOC_N(n) @private_namespace@##n
|
||||
# include "jemalloc/internal/private_namespace.h"
|
||||
|
||||
# include <inttypes.h>
|
||||
# include <stdarg.h>
|
||||
# include <errno.h>
|
||||
# define JEMALLOC_CC_SILENCE
|
||||
# define JEMALLOC_H_TYPES
|
||||
# define JEMALLOC_H_STRUCTS
|
||||
# define JEMALLOC_H_EXTERNS
|
||||
# define JEMALLOC_H_INLINES
|
||||
# include "jemalloc/internal/util.h"
|
||||
# undef JEMALLOC_H_TYPES
|
||||
# undef JEMALLOC_H_STRUCTS
|
||||
# undef JEMALLOC_H_EXTERNS
|
||||
# undef JEMALLOC_H_INLINES
|
||||
# undef JEMALLOC_CC_SILENCE
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* For stress tests, expose the public jemalloc interfaces with name mangling
|
||||
* so that they can be tested as e.g. malloc() and free(). Also expose the
|
||||
* public jemalloc interfaces with jet_ prefixes, so that stress tests can use
|
||||
* a separate allocator for their internal data structures.
|
||||
*/
|
||||
#elif defined(JEMALLOC_STRESS_TEST)
|
||||
# define JEMALLOC_NO_DEMANGLE
|
||||
# include "jemalloc/jemalloc@install_suffix@.h"
|
||||
# include "jemalloc/internal/public_unnamespace.h"
|
||||
# undef JEMALLOC_NO_DEMANGLE
|
||||
|
||||
# include "jemalloc/jemalloc_protos_jet.h"
|
||||
|
||||
# define JEMALLOC_JET
|
||||
# include "jemalloc/internal/jemalloc_internal.h"
|
||||
# include "jemalloc/internal/public_unnamespace.h"
|
||||
# undef JEMALLOC_JET
|
||||
|
||||
# define JEMALLOC_MANGLE
|
||||
# include "jemalloc/jemalloc_mangle@install_suffix@.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* This header does dangerous things, the effects of which only test code
|
||||
* should be subject to.
|
||||
*/
|
||||
#else
|
||||
# error "This header cannot be included outside a testing context"
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Common test utilities.
|
||||
*/
|
||||
#include "test/test.h"
|
||||
#include "test/thread.h"
|
2
test/include/test/test.h
Normal file
2
test/include/test/test.h
Normal file
@@ -0,0 +1,2 @@
|
||||
void test_fail(const char *format, ...) JEMALLOC_ATTR(format(printf, 1, 2));
|
||||
void test_skip(const char *format, ...) JEMALLOC_ATTR(format(printf, 1, 2));
|
12
test/include/test/thread.h
Normal file
12
test/include/test/thread.h
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
/* Abstraction layer for threading in tests */
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
typedef HANDLE je_thread_t;
|
||||
#else
|
||||
#include <pthread.h>
|
||||
typedef pthread_t je_thread_t;
|
||||
#endif
|
||||
|
||||
void je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg);
|
||||
void je_thread_join(je_thread_t thread, void **ret);
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define NTHREADS 10
|
||||
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define CHUNK 0x400000
|
||||
/* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */
|
||||
@@ -96,10 +95,9 @@ main(void)
|
||||
char buf[BUFERROR_BUF];
|
||||
|
||||
buferror(buf, sizeof(buf));
|
||||
malloc_printf(
|
||||
test_fail(
|
||||
"Error for size %zu (%#zx): %s\n",
|
||||
size, size, buf);
|
||||
exit(1);
|
||||
}
|
||||
total += malloc_usable_size(ps[i]);
|
||||
if (total >= (MAXALIGN << 1))
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
void *
|
||||
je_thread_start(void *arg)
|
||||
@@ -18,9 +17,8 @@ je_thread_start(void *arg)
|
||||
#endif
|
||||
goto label_return;
|
||||
}
|
||||
malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
|
||||
test_fail("%s(): Error in mallctl(): %s\n", __func__,
|
||||
strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
sz = sizeof(ap0);
|
||||
if ((err = mallctl("thread.allocatedp", &ap0, &sz, NULL, 0))) {
|
||||
@@ -30,9 +28,8 @@ je_thread_start(void *arg)
|
||||
#endif
|
||||
goto label_return;
|
||||
}
|
||||
malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
|
||||
test_fail("%s(): Error in mallctl(): %s\n", __func__,
|
||||
strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
assert(*ap0 == a0);
|
||||
|
||||
@@ -44,9 +41,8 @@ je_thread_start(void *arg)
|
||||
#endif
|
||||
goto label_return;
|
||||
}
|
||||
malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
|
||||
test_fail("%s(): Error in mallctl(): %s\n", __func__,
|
||||
strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
sz = sizeof(dp0);
|
||||
if ((err = mallctl("thread.deallocatedp", &dp0, &sz, NULL, 0))) {
|
||||
@@ -56,17 +52,14 @@ je_thread_start(void *arg)
|
||||
#endif
|
||||
goto label_return;
|
||||
}
|
||||
malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
|
||||
test_fail("%s(): Error in mallctl(): %s\n", __func__,
|
||||
strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
assert(*dp0 == d0);
|
||||
|
||||
p = malloc(1);
|
||||
if (p == NULL) {
|
||||
malloc_printf("%s(): Error in malloc()\n", __func__);
|
||||
exit(1);
|
||||
}
|
||||
if (p == NULL)
|
||||
test_fail("%s(): Error in malloc()\n", __func__);
|
||||
|
||||
sz = sizeof(a1);
|
||||
mallctl("thread.allocated", &a1, &sz, NULL, 0);
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define CHUNK 0x400000
|
||||
/* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */
|
||||
@@ -144,21 +143,19 @@ main(void)
|
||||
r = nallocm(&nsz, sz,
|
||||
ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
|
||||
if (r != ALLOCM_SUCCESS) {
|
||||
malloc_printf(
|
||||
test_fail(
|
||||
"nallocm() error for size %zu"
|
||||
" (%#zx): %d\n",
|
||||
sz, sz, r);
|
||||
exit(1);
|
||||
}
|
||||
rsz = 0;
|
||||
r = allocm(&ps[i], &rsz, sz,
|
||||
ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
|
||||
if (r != ALLOCM_SUCCESS) {
|
||||
malloc_printf(
|
||||
test_fail(
|
||||
"allocm() error for size %zu"
|
||||
" (%#zx): %d\n",
|
||||
sz, sz, r);
|
||||
exit(1);
|
||||
}
|
||||
if (rsz < sz) {
|
||||
malloc_printf(
|
@@ -6,7 +6,7 @@
|
||||
#include "jemalloc/jemalloc@install_suffix@.h"
|
||||
#include "jemalloc/internal/jemalloc_internal.h"
|
||||
|
||||
/* Abstraction layer for threading in tests */
|
||||
/* Abstraction layer for threading in tests. */
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
||||
@@ -17,15 +17,14 @@ je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
LPTHREAD_START_ROUTINE routine = (LPTHREAD_START_ROUTINE)proc;
|
||||
*thread = CreateThread(NULL, 0, routine, arg, 0, NULL);
|
||||
if (*thread == NULL) {
|
||||
malloc_printf("Error in CreateThread()\n");
|
||||
exit(1);
|
||||
}
|
||||
if (*thread == NULL)
|
||||
test_fail("Error in CreateThread()\n");
|
||||
}
|
||||
|
||||
void
|
||||
je_thread_join(je_thread_t thread, void **ret)
|
||||
{
|
||||
|
||||
WaitForSingleObject(thread, INFINITE);
|
||||
}
|
||||
|
||||
@@ -38,10 +37,8 @@ void
|
||||
je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
|
||||
if (pthread_create(thread, NULL, proc, arg) != 0) {
|
||||
malloc_printf("Error in pthread_create()\n");
|
||||
exit(1);
|
||||
}
|
||||
if (pthread_create(thread, NULL, proc, arg) != 0)
|
||||
test_fail("Error in pthread_create()\n");
|
||||
}
|
||||
|
||||
void
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
int
|
||||
main(void)
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define CHUNK 0x400000
|
||||
/* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */
|
||||
@@ -92,10 +91,9 @@ main(void)
|
||||
err = posix_memalign(&ps[i],
|
||||
alignment, size);
|
||||
if (err) {
|
||||
malloc_printf(
|
||||
test_fail(
|
||||
"Error for size %zu (%#zx): %s\n",
|
||||
size, size, strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
total += malloc_usable_size(ps[i]);
|
||||
if (total >= (MAXALIGN << 1))
|
@@ -1,5 +1,6 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
int
|
||||
main(void)
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define NTHREADS 10
|
||||
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
void *
|
||||
je_thread_start(void *arg)
|
28
test/src/test.c
Normal file
28
test/src/test.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define JEMALLOC_TEST_EXIT_FAIL 1
|
||||
#define JEMALLOC_TEST_EXIT_SKIP 2
|
||||
|
||||
JEMALLOC_ATTR(format(printf, 1, 2))
|
||||
void
|
||||
test_fail(const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
malloc_vcprintf(NULL, NULL, format, ap);
|
||||
va_end(ap);
|
||||
exit(JEMALLOC_TEST_EXIT_FAIL);
|
||||
}
|
||||
|
||||
JEMALLOC_ATTR(format(printf, 1, 2))
|
||||
void
|
||||
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_SKIP);
|
||||
}
|
35
test/src/thread.c
Normal file
35
test/src/thread.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
void
|
||||
je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
LPTHREAD_START_ROUTINE routine = (LPTHREAD_START_ROUTINE)proc;
|
||||
*thread = CreateThread(NULL, 0, routine, arg, 0, NULL);
|
||||
if (*thread == NULL)
|
||||
test_fail("Error in CreateThread()\n");
|
||||
}
|
||||
|
||||
void
|
||||
je_thread_join(je_thread_t thread, void **ret)
|
||||
{
|
||||
|
||||
WaitForSingleObject(thread, INFINITE);
|
||||
}
|
||||
|
||||
#else
|
||||
void
|
||||
je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
|
||||
if (pthread_create(thread, NULL, proc, arg) != 0)
|
||||
test_fail("Error in pthread_create()\n");
|
||||
}
|
||||
|
||||
void
|
||||
je_thread_join(je_thread_t thread, void **ret)
|
||||
{
|
||||
|
||||
pthread_join(thread, ret);
|
||||
}
|
||||
#endif
|
37
test/test.sh.in
Normal file
37
test/test.sh.in
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
|
||||
case @abi@ in
|
||||
macho)
|
||||
export DYLD_FALLBACK_LIBRARY_PATH="@objroot@lib"
|
||||
;;
|
||||
pecoff)
|
||||
export PATH="${PATH}:@objroot@lib"
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
|
||||
total=0
|
||||
failures=0
|
||||
echo "========================================="
|
||||
for t in $@; do
|
||||
total=`expr $total + 1`
|
||||
/bin/echo -n "${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
|
||||
done
|
||||
echo "========================================="
|
||||
echo "Failures: ${failures}/${total}"
|
@@ -1,5 +1,4 @@
|
||||
#define JEMALLOC_MANGLE
|
||||
#include "jemalloc_test.h"
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#if (LG_BITMAP_MAXBITS > 12)
|
||||
# define MAXBITS 4500
|
||||
@@ -37,7 +36,6 @@ test_bitmap_init(void)
|
||||
for (j = 0; j < i; j++)
|
||||
assert(bitmap_get(bitmap, &binfo, j) == false);
|
||||
free(bitmap);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user