diff options
author | Mattias Andrée <maandree@kth.se> | 2018-08-29 00:35:12 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-08-29 00:35:12 +0200 |
commit | ac15d5da43275997dc871fc1128c6d01c3c5a4cc (patch) | |
tree | 0ff763e83f1541610329a825079eb78a8b3a9b87 /libsimple.c | |
parent | Warn about unran tests under valgrind (diff) | |
download | libsimple-ac15d5da43275997dc871fc1128c6d01c3c5a4cc.tar.gz libsimple-ac15d5da43275997dc871fc1128c6d01c3c5a4cc.tar.bz2 libsimple-ac15d5da43275997dc871fc1128c6d01c3c5a4cc.tar.xz |
Add tests
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libsimple.c | 89 |
1 files changed, 88 insertions, 1 deletions
diff --git a/libsimple.c b/libsimple.c index 24c5dd3..9a182c6 100644 --- a/libsimple.c +++ b/libsimple.c @@ -75,6 +75,8 @@ test_timeval(double d, time_t sec, long int usec, double rd, const char *s, cons int main(void) { + struct allocinfo *info; + void *ptr; struct timespec ts, ts1, ts2; struct timeval tv1, tv2; const char *cs; @@ -82,6 +84,8 @@ main(void) int intarray[10]; size_t i, n; + assert(libsimple_default_failure_exit == 1); + assert(MIN(1, 3) == 1); assert(MIN(1, 1) == 1); assert(MIN(3, 1) == 1); @@ -990,12 +994,95 @@ main(void) tv2.tv_sec = 1, tv2.tv_usec = 0L; assert(libsimple_cmptimeval(&tv1, &tv2) == -1); + assert((ptr = libsimple_mallocz(0, 11))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 11); + assert(!info->zeroed); + } + free(ptr); + + assert((ptr = libsimple_mallocz(1, 12))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 12); + assert(info->zeroed == 12); + } + free(ptr); + + assert((ptr = libsimple_emallocz(0, 13))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 13); + assert(!info->zeroed); + } + free(ptr); + + assert((ptr = libsimple_emallocz(1, 14))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 14); + assert(info->zeroed == 14); + } + free(ptr); + + assert((ptr = libsimple_enmallocz(10, 0, 15))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 15); + assert(!info->zeroed); + } + free(ptr); + + assert((ptr = libsimple_enmallocz(10, 1, 16))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 16); + assert(info->zeroed == 16); + } + free(ptr); + + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enmallocz(5, 0, 20)); + assert(exit_status == 5); + assert_stderr("%s: malloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enmallocz(6, 1, 20)); + assert(exit_status == 6); + assert_stderr("%s: calloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 7; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_emallocz(0, 20)); + assert(exit_status == 7); + assert_stderr("%s: malloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 8; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_emallocz(1, 20)); + assert(exit_status == 8); + assert_stderr("%s: calloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert(!(ptr = libsimple_mallocz(0, 20)) && errno == ENOMEM); + assert(!alloc_fail_in); + + alloc_fail_in = 1; + assert(!(ptr = libsimple_mallocz(1, 20)) && errno == ENOMEM); + assert(!alloc_fail_in); + } + if (!have_custom_malloc()) { stderr_real = 1; fprintf(stderr, "\nSome tests have not been ran because malloc(3) was not " "replaced, this is normal if running under valgrind(1).\n\n"); } - return 0; } |