update test macros; add realloc tests

This commit is contained in:
Daan Leijen 2022-04-19 10:21:41 -07:00
parent 5b172280b0
commit 8713959498
2 changed files with 57 additions and 71 deletions

View File

@ -56,77 +56,77 @@ int main(void) {
// Malloc
// ---------------------------------------------------
CHECK_BODY("malloc-zero",{
CHECK_BODY("malloc-zero") {
void* p = mi_malloc(0);
result = (p != NULL);
mi_free(p);
});
CHECK_BODY("malloc-nomem1",{
};
CHECK_BODY("malloc-nomem1") {
result = (mi_malloc((size_t)PTRDIFF_MAX + (size_t)1) == NULL);
});
CHECK_BODY("malloc-null",{
};
CHECK_BODY("malloc-null") {
mi_free(NULL);
});
CHECK_BODY("calloc-overflow",{
};
CHECK_BODY("calloc-overflow") {
// use (size_t)&mi_calloc to get some number without triggering compiler warnings
result = (mi_calloc((size_t)&mi_calloc,SIZE_MAX/1000) == NULL);
});
CHECK_BODY("calloc0",{
};
CHECK_BODY("calloc0") {
result = (mi_usable_size(mi_calloc(0,1000)) <= 16);
});
CHECK_BODY("malloc-large",{ // see PR #544.
};
CHECK_BODY("malloc-large") { // see PR #544.
void* p = mi_malloc(67108872);
mi_free(p);
});
};
// ---------------------------------------------------
// Extended
// ---------------------------------------------------
CHECK_BODY("posix_memalign1", {
CHECK_BODY("posix_memalign1") {
void* p = &p;
int err = mi_posix_memalign(&p, sizeof(void*), 32);
result = ((err==0 && (uintptr_t)p % sizeof(void*) == 0) || p==&p);
mi_free(p);
});
CHECK_BODY("posix_memalign_no_align", {
};
CHECK_BODY("posix_memalign_no_align") {
void* p = &p;
int err = mi_posix_memalign(&p, 3, 32);
result = (err==EINVAL && p==&p);
});
CHECK_BODY("posix_memalign_zero", {
};
CHECK_BODY("posix_memalign_zero") {
void* p = &p;
int err = mi_posix_memalign(&p, sizeof(void*), 0);
mi_free(p);
result = (err==0);
});
CHECK_BODY("posix_memalign_nopow2", {
};
CHECK_BODY("posix_memalign_nopow2") {
void* p = &p;
int err = mi_posix_memalign(&p, 3*sizeof(void*), 32);
result = (err==EINVAL && p==&p);
});
CHECK_BODY("posix_memalign_nomem", {
};
CHECK_BODY("posix_memalign_nomem") {
void* p = &p;
int err = mi_posix_memalign(&p, sizeof(void*), SIZE_MAX);
result = (err==ENOMEM && p==&p);
});
};
// ---------------------------------------------------
// Aligned API
// ---------------------------------------------------
CHECK_BODY("malloc-aligned1", {
CHECK_BODY("malloc-aligned1") {
void* p = mi_malloc_aligned(32,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p);
});
CHECK_BODY("malloc-aligned2", {
};
CHECK_BODY("malloc-aligned2") {
void* p = mi_malloc_aligned(48,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p);
});
CHECK_BODY("malloc-aligned3", {
};
CHECK_BODY("malloc-aligned3") {
void* p1 = mi_malloc_aligned(48,32); bool result1 = (p1 != NULL && (uintptr_t)(p1) % 32 == 0);
void* p2 = mi_malloc_aligned(48,32); bool result2 = (p2 != NULL && (uintptr_t)(p2) % 32 == 0);
mi_free(p2);
mi_free(p1);
result = (result1&&result2);
});
CHECK_BODY("malloc-aligned4", {
};
CHECK_BODY("malloc-aligned4") {
void* p;
bool ok = true;
for (int i = 0; i < 8 && ok; i++) {
@ -134,11 +134,11 @@ int main(void) {
ok = (p != NULL && (uintptr_t)(p) % 16 == 0); mi_free(p);
}
result = ok;
});
CHECK_BODY("malloc-aligned5", {
};
CHECK_BODY("malloc-aligned5") {
void* p = mi_malloc_aligned(4097,4096); size_t usable = mi_usable_size(p); result = usable >= 4097 && usable < 10000; mi_free(p);
});
CHECK_BODY("malloc-aligned6", {
};
CHECK_BODY("malloc-aligned6") {
bool ok = true;
for (size_t align = 1; align <= MI_ALIGNMENT_MAX && ok; align *= 2) {
void* ps[8];
@ -153,20 +153,20 @@ int main(void) {
}
}
result = ok;
});
CHECK_BODY("malloc-aligned7", {
};
CHECK_BODY("malloc-aligned7") {
void* p = mi_malloc_aligned(1024,MI_ALIGNMENT_MAX); mi_free(p);
});
CHECK_BODY("malloc-aligned8", {
};
CHECK_BODY("malloc-aligned8") {
void* p = mi_malloc_aligned(1024,2*MI_ALIGNMENT_MAX); mi_free(p);
});
CHECK_BODY("malloc-aligned-at1", {
};
CHECK_BODY("malloc-aligned-at1") {
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);
});
CHECK_BODY("malloc-aligned-at2", {
};
CHECK_BODY("malloc-aligned-at2") {
void* p = mi_malloc_aligned_at(50,32,8); result = (p != NULL && ((uintptr_t)(p) + 8) % 32 == 0); mi_free(p);
});
CHECK_BODY("memalign1", {
};
CHECK_BODY("memalign1") {
void* p;
bool ok = true;
for (int i = 0; i < 8 && ok; i++) {
@ -174,23 +174,30 @@ int main(void) {
ok = (p != NULL && (uintptr_t)(p) % 16 == 0); mi_free(p);
}
result = ok;
});
};
// ---------------------------------------------------
// Reallocation
// ---------------------------------------------------
CHECK_BODYX("realloc-null1") {
CHECK_BODY("realloc-null") {
void* p = mi_realloc(NULL,4);
result = (p != NULL);
mi_free(p);
};
CHECK_BODYX("realloc-null2") {
CHECK_BODY("realloc-null-sizezero") {
void* p = mi_realloc(NULL,0); // <https://en.cppreference.com/w/c/memory/realloc> "If ptr is NULL, the behavior is the same as calling malloc(new_size)."
result = (p != NULL);
mi_free(p);
};
CHECK_BODY("realloc-sizezero") {
void* p = mi_malloc(4);
void* q = mi_realloc(p, 0);
result = (q != NULL);
mi_free(q);
};
// ---------------------------------------------------
// Heaps
// ---------------------------------------------------
@ -202,11 +209,11 @@ int main(void) {
// ---------------------------------------------------
// various
// ---------------------------------------------------
CHECK_BODY("realpath", {
CHECK_BODY("realpath") {
char* s = mi_realpath( ".", NULL );
// printf("realpath: %s\n",s);
mi_free(s);
});
};
CHECK("stl_allocator1", test_stl_allocator1());
CHECK("stl_allocator2", test_stl_allocator2());

View File

@ -28,32 +28,11 @@ static bool check_result(bool result, const char* testname, const char* fname, l
return true;
}
#define CHECK_BODYX(name) \
#define CHECK_BODY(name) \
fprintf(stderr,"test: %s... ", name ); \
for(bool done = false, result = true; !done; done = check_result(result,name,__FILE__,__LINE__))
#define CHECK_BODY(name,body) \
do { \
fprintf(stderr,"test: %s... ", name ); \
bool result = true; \
do { body } while(false); \
if (!(result)) { \
failed++; \
fprintf(stderr, \
"\n FAILED: %s:%d:\n %s\n", \
__FILE__, \
__LINE__, \
#body); \
/* exit(1); */ \
} \
else { \
ok++; \
fprintf(stderr,"ok.\n"); \
} \
} while (false)
#define CHECK(name,expr) CHECK_BODYX(name){ result = (expr); }
#define CHECK(name,expr) CHECK_BODY(name){ result = (expr); }
// Print summary of test. Return value can be directly use as a return value for main().
static inline int print_test_summary(void)