When running a test, make sure it didn't leave allocation failure enabled.

Add predicates for null and non-null pointers.
Staticize some internal allocator state.
This commit is contained in:
Dag-Erling Smørgrav 2015-10-13 21:39:47 +00:00 committed by des
parent 4fa82f7c7b
commit 0a9f2c21fe
4 changed files with 41 additions and 1 deletions

View file

@ -82,6 +82,8 @@ void t_fcloseall(void);
/*
* Various utilities
*/
int t_is_null(const void *);
int t_is_not_null(const void *);
int t_compare_mem(const void *, const void *, size_t);
int t_compare_str(const char *, const char *);
#define t_compare_num(n, t) int t_compare_##n(t, t);

View file

@ -170,6 +170,16 @@ t_run_test(struct t_test *t, int n)
ret = 0;
if ((signo = setjmp(sigjmp)) == 0)
ret = (*t->func)(&desc, t->arg);
if (t_malloc_fail != 0) {
t_verbose("WARNING: test case left t_malloc_fail = %d\n",
t_malloc_fail);
t_malloc_fail = 0;
}
if (t_malloc_fail_after != 0) {
t_verbose("WARNING: test case left t_malloc_fail_after = %d\n",
t_malloc_fail_after);
t_malloc_fail_after = 0;
}
if (ret > 0)
printf("ok %d - %s\n", n, desc ? desc : "no description");
else if (ret < 0)

View file

@ -126,7 +126,7 @@ static struct bucket buckets[BUCKET_MAX_SHIFT + 1];
/* mapping metadata */
static struct mapping *mappings;
unsigned long nmapalloc, nmapfree;
static unsigned long nmapalloc, nmapfree;
/* if non-zero, all allocations fail */
int t_malloc_fail;

View file

@ -40,6 +40,34 @@
#include <cryb/test.h>
/*
* Print a verbose message if a pointer is not null.
*/
int
t_is_null(const void *ptr)
{
if (ptr != NULL) {
t_verbose("expected null pointer, got non-null pointer\n");
return (0);
}
return (1);
}
/*
* Print a verbose message if a pointer is null.
*/
int
t_is_not_null(const void *ptr)
{
if (ptr == NULL) {
t_verbose("expected non-null pointer, got null pointer\n");
return (0);
}
return (1);
}
/*
* Compare two buffers, and print a verbose message if they differ.
*/