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:
Jason Evans
2013-11-30 15:25:42 -08:00
parent 6668853596
commit 86abd0dcd8
48 changed files with 1335 additions and 938 deletions

View 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
View 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));

View 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);