From 519d9b72a68f074eb32f24fe8587716a768026b2 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 15 Dec 2018 18:32:50 +0100 Subject: Add tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libsimple.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) (limited to 'libsimple.c') diff --git a/libsimple.c b/libsimple.c index a2ae4bc..5232833 100644 --- a/libsimple.c +++ b/libsimple.c @@ -88,7 +88,7 @@ int main(void) { struct allocinfo *volatile info; - void *ptr; + void *ptr, *old; struct timespec ts, ts1, ts2; struct timeval tv1, tv2; const char *cs; @@ -2248,6 +2248,66 @@ main(void) assert_stderr("%s: libsimple_vmemalignz: %s\n", argv0, strerror(EINVAL)); libsimple_default_failure_exit = 1; +#ifdef LIBSIMPLE_HAVE_ALIGNED_REALLOC + assert((ptr = libsimple_aligned_realloc(NULL, 16, 5))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 5 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 16); + info->refcount += 1; + } + stpcpy(ptr, "test"); + assert((ptr = libsimple_aligned_realloc(old = ptr, 32, 10))); + assert(!strcmp(ptr, "test")); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 10 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 32); + assert(ptr != old); + free(old); + } + free(ptr); + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert(!libsimple_aligned_realloc(NULL, 8, 1) && errno == ENOMEM); + assert(!alloc_fail_in); + } +#else + assert(libsimple_aligned_realloc(NULL, 8, 1) && errno == ENOSYS); +#endif + +#ifdef LIBSIMPLE_HAVE_ALIGNED_REALLOC + assert((ptr = libsimple_aligned_reallocarray(NULL, 16, 5, 3))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 15 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 16); + info->refcount += 1; + } + stpcpy(ptr, "test"); + assert((ptr = libsimple_aligned_reallocarray(old = ptr, 32, 10, 2))); + assert(!strcmp(ptr, "test")); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 20 || info->size == info->alignment); + assert(!info->zeroed); + ASSERT_ALIGNMENT(info, 32); + assert(ptr != old); + free(old); + } + free(ptr); + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert(!libsimple_aligned_reallocarray(NULL, 8, 1, 1) && errno == ENOMEM); + assert(!alloc_fail_in); + } +#else + assert(libsimple_aligned_reallocarray(NULL, 8, 1, 1) && errno == ENOSYS); +#endif + assert(libsimple_memeq("abcxyz", "abc123", 3)); assert(!libsimple_memeq("abcxyz", "abc123", 4)); assert(libsimple_memeq("abcxyz", "abcx23", 4)); -- cgit v1.2.3-70-g09d2