diff options
-rw-r--r-- | enmemdup.c | 39 | ||||
-rw-r--r-- | enstrdup.c | 39 | ||||
-rw-r--r-- | enstrndup.c | 103 | ||||
-rw-r--r-- | envputenvf.c | 47 | ||||
-rw-r--r-- | envreallocn.c | 59 | ||||
-rw-r--r-- | libsimple.c | 59 | ||||
-rw-r--r-- | libsimple.h | 16 | ||||
-rw-r--r-- | test.h | 4 |
8 files changed, 350 insertions, 16 deletions
@@ -4,7 +4,7 @@ void * -libsimple_enmemdup(int status, const void *s, size_t n) /* TODO test (libsimple_ememdup) */ +libsimple_enmemdup(int status, const void *s, size_t n) { void *ret = memdup(s, n); if (!ret) @@ -19,6 +19,43 @@ libsimple_enmemdup(int status, const void *s, size_t n) /* TODO test (libsimple_ int main(void) { + struct allocinfo *info; + void *s; + + assert((s = libsimple_enmemdup(1, "hello", 5))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 5); + assert(!info->zeroed); + } + assert(!memcmp(s, "hello", 5)); + free(s); + + assert((s = libsimple_ememdup("test", 5))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 5); + assert(!info->zeroed); + } + assert(!memcmp(s, "test", 5)); + free(s); + + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enmemdup(44, "hello", 2)); + assert(exit_status == 44); + assert_stderr("%s: memdup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 55; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_ememdup("test", 2)); + assert(exit_status == 55); + assert_stderr("%s: memdup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + } + return 0; } @@ -4,7 +4,7 @@ char * -libsimple_enstrdup(int status, const char *s) /* TODO test (libsimple_estrdup) */ +libsimple_enstrdup(int status, const char *s) { char *ret = strdup(s); if (!ret) @@ -19,6 +19,43 @@ libsimple_enstrdup(int status, const char *s) /* TODO test (libsimple_estrdup) * int main(void) { + struct allocinfo *info; + char *s; + + assert((s = libsimple_enstrdup(1, "hello"))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 6); + assert(!info->zeroed); + } + assert(!strcmp(s, "hello")); + free(s); + + assert((s = libsimple_estrdup("test"))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 5); + assert(!info->zeroed); + } + assert(!strcmp(s, "test")); + free(s); + + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enstrdup(14, "hello")); + assert(exit_status == 14); + assert_stderr("%s: strdup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 15; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_estrdup("test")); + assert(exit_status == 15); + assert_stderr("%s: strdup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + } + return 0; } diff --git a/enstrndup.c b/enstrndup.c index 367ab3c..a6bc7b3 100644 --- a/enstrndup.c +++ b/enstrndup.c @@ -4,7 +4,7 @@ char * -libsimple_enstrndup(int status, const char *s, size_t n) /* TODO test (libsimple_estrndup) */ +libsimple_enstrndup(int status, const char *s, size_t n) { void *ret = strndup(s, n); if (!ret) @@ -19,6 +19,107 @@ libsimple_enstrndup(int status, const char *s, size_t n) /* TODO test (libsimple int main(void) { + struct allocinfo *info; + char *s; + + assert((s = libsimple_enstrndup(1, "hello", 10))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 6); + assert(!info->zeroed); + } + assert(!strcmp(s, "hello")); + free(s); + + assert((s = libsimple_estrndup("test", 10))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 5); + assert(!info->zeroed); + } + assert(!strcmp(s, "test")); + free(s); + + assert((s = libsimple_enstrndup(1, "hello", 2))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 3); + assert(!info->zeroed); + } + assert(!strcmp(s, "he")); + free(s); + + assert((s = libsimple_estrndup("test", 3))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 4); + assert(!info->zeroed); + } + assert(!strcmp(s, "tes")); + free(s); + + assert((s = libsimple_enstrndup(1, "hello", 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 1); + assert(!info->zeroed); + } + assert(!strcmp(s, "")); + free(s); + + assert((s = libsimple_estrndup("test", 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(s))); + assert(info->size == 1); + assert(!info->zeroed); + } + assert(!strcmp(s, "")); + free(s); + + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enstrndup(14, "hello", 10)); + assert(exit_status == 14); + assert_stderr("%s: strndup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 15; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_estrndup("test", 10)); + assert(exit_status == 15); + assert_stderr("%s: strndup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enstrndup(16, "hello", 1)); + assert(exit_status == 16); + assert_stderr("%s: strndup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 17; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_estrndup("test", 2)); + assert(exit_status == 17); + assert_stderr("%s: strndup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enstrndup(18, "hello", 0)); + assert(exit_status == 18); + assert_stderr("%s: strndup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 19; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_estrndup("test", 0)); + assert(exit_status == 19); + assert_stderr("%s: strndup: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + } + return 0; } diff --git a/envputenvf.c b/envputenvf.c index bef9867..b4568fd 100644 --- a/envputenvf.c +++ b/envputenvf.c @@ -4,7 +4,7 @@ void -libsimple_envputenvf(int status, const char *fmt, va_list ap) /* TODO test (enputenvf, evputenvf, eputenvf) */ +libsimple_envputenvf(int status, const char *fmt, va_list ap) { if (libsimple_vputenvf(fmt, ap)) enprintf(status, "putenvf:"); @@ -17,6 +17,51 @@ libsimple_envputenvf(int status, const char *fmt, va_list ap) /* TODO test (enpu int main(void) { + eputenvf("X=xyz"); + assert(!strcmpnul(getenv("X"), "xyz")); + eputenvf("Y=xyz"); + assert(!strcmpnul(getenv("Y"), "xyz")); + + eputenvf("X=x%sz", "abc"); + assert(!strcmpnul(getenv("X"), "xabcz")); + eputenvf("Y=x%sz", "abc"); + assert(!strcmpnul(getenv("Y"), "xabcz")); + + eputenvf("X=%ix%sz%i", 10, "abc", -11); + assert(!strcmpnul(getenv("X"), "10xabcz-11")); + eputenvf("Y=%ix%sz%i", 10, "abc", -11); + assert(!strcmpnul(getenv("Y"), "10xabcz-11")); + + enputenvf(1, "X=xyz"); + assert(!strcmpnul(getenv("X"), "xyz")); + enputenvf(1, "Y=xyz"); + assert(!strcmpnul(getenv("Y"), "xyz")); + + enputenvf(1, "X=x%sz", "abc"); + assert(!strcmpnul(getenv("X"), "xabcz")); + enputenvf(1, "Y=x%sz", "abc"); + assert(!strcmpnul(getenv("Y"), "xabcz")); + + enputenvf(1, "X=%ix%sz%i", 10, "abc", -11); + assert(!strcmpnul(getenv("X"), "10xabcz-11")); + enputenvf(1, "Y=%ix%sz%i", 10, "abc", -11); + assert(!strcmpnul(getenv("Y"), "10xabcz-11")); + + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert_exit(enputenvf(100, "X=xyz")); + assert(exit_status == 100); + assert_stderr("%s: putenvf: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 102; + alloc_fail_in = 1; + assert_exit(eputenvf("X=xyz")); + assert(exit_status == 102); + assert_stderr("%s: putenvf: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + } + return 0; } diff --git a/envreallocn.c b/envreallocn.c index b6ef1c1..571d077 100644 --- a/envreallocn.c +++ b/envreallocn.c @@ -4,7 +4,7 @@ void * -libsimple_envreallocn(int status, void *ptr, size_t n, va_list ap) /* TODO test (enreallocn, ereallocn) */ +libsimple_envreallocn(int status, void *ptr, size_t n, va_list ap) { void *ret = libsimple_vreallocn(ptr, n, ap); if (!ret) @@ -19,6 +19,63 @@ libsimple_envreallocn(int status, void *ptr, size_t n, va_list ap) /* TODO test int main(void) { + struct allocinfo *info; + void *ptr, *old; + + assert((ptr = libsimple_enreallocn(1, NULL, 5, 3, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 15); + assert(!info->zeroed); + info->refcount += 1; + } + stpcpy(ptr, "test"); + assert((ptr = libsimple_enreallocn(1, old = ptr, 10, 2, 0))); + assert(!strcmp(ptr, "test")); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 20); + assert(!info->zeroed); + assert(ptr != old); + free(old); + } + free(ptr); + + assert((ptr = libsimple_ereallocn(NULL, 5, 4, 0))); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 20); + assert(!info->zeroed); + info->refcount += 1; + } + stpcpy(ptr, "test"); + assert((ptr = libsimple_ereallocn(old = ptr, 10, 11, 0))); + assert(!strcmp(ptr, "test")); + if (have_custom_malloc()) { + assert((info = get_allocinfo(ptr))); + assert(info->size == 110); + assert(!info->zeroed); + assert(ptr != old); + free(old); + } + free(ptr); + + if (have_custom_malloc()) { + alloc_fail_in = 1; + assert_exit_ptr(libsimple_enreallocn(2, NULL, 1, 0)); + assert(exit_status == 2); + assert_stderr("%s: realloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + + libsimple_default_failure_exit = 104; + alloc_fail_in = 1; + assert_exit_ptr(libsimple_ereallocn(NULL, 1, 0)); + assert(exit_status == 104); + assert_stderr("%s: realloc: %s\n", argv0, strerror(ENOMEM)); + assert(!alloc_fail_in); + libsimple_default_failure_exit = 1; + } + return 0; } diff --git a/libsimple.c b/libsimple.c index 9a182c6..58565b5 100644 --- a/libsimple.c +++ b/libsimple.c @@ -72,16 +72,31 @@ test_timeval(double d, time_t sec, long int usec, double rd, const char *s, cons return 1; } +#ifdef libsimple_vasprintfa +static int +test_vasprintfa(const char *expected, const char *format, ...) +{ + char *s; + va_list ap; + va_start(ap, format); + s = libsimple_vasprintfa(format, ap); + subassert(s); + subassert(!strcmp(s, expected)); + va_end(ap); + return 1; +} +#endif + int main(void) { struct allocinfo *info; - void *ptr; + void *ptr, *ptr2; struct timespec ts, ts1, ts2; struct timeval tv1, tv2; const char *cs; char buf[1024], *s; - int intarray[10]; + int intarray[10], fds[2], oldfd; size_t i, n; assert(libsimple_default_failure_exit == 1); @@ -1078,6 +1093,46 @@ main(void) assert(!alloc_fail_in); } + assert((ptr = malloc(1))); + ptr2 = ptr; + if (have_custom_malloc()) { + info = get_allocinfo(ptr); + info->refcount = 2; + } + FREE(ptr); + assert(ptr == NULL); + if (have_custom_malloc()) { + assert(info->refcount == 1); + free(ptr2); + } + + assert(!pipe(fds)); + assert(fds[0] > 2 && fds[1] > 2 && fds[0] != fds[1]); + oldfd = fds[1]; + assert(!CLOSE(fds[1])); + assert(fds[1] == -1); + assert(!CLOSE(fds[1])); + errno = 0; + assert(CLOSE(oldfd) == -1 && errno == EBADF); + errno = 0; + assert(oldfd == -1); + assert(!read(fds[0], buf, sizeof(buf))); + close(fds[0]); + +#ifdef libsimple_asprintfa + s = libsimple_asprintfa("%sxyz%s", "abc", "123"); + assert(s); + assert(!strcmp(s, "abcxyz123")); +#else + fprintf(stderr, "warning: libsimple_asprintfa missing\n"); +#endif + +#ifdef libsimple_vasprintfa + assert(test_vasprintfa("abcxyz123", "%sxyz%s", "abc", "123")); +#else + fprintf(stderr, "warning: libsimple_vasprintfa missing\n"); +#endif + if (!have_custom_malloc()) { stderr_real = 1; fprintf(stderr, "\nSome tests have not been ran because malloc(3) was not " diff --git a/libsimple.h b/libsimple.h index 24dda8b..6681aad 100644 --- a/libsimple.h +++ b/libsimple.h @@ -477,9 +477,9 @@ extern int libsimple_default_failure_exit; #endif -#define FREE(PTR) ((void)(free(PTR), (PTR) = NULL)) /* TODO test */ +#define FREE(PTR) ((void)(free(PTR), (PTR) = NULL)) -#define CLOSE(FD) libsimple_close(&(FD)) /* TODO test */ +#define CLOSE(FD) libsimple_close(&(FD)) _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline int @@ -627,7 +627,7 @@ int libsimple_vasprintf(char **, const char *, va_list); #endif #if defined(__GNUC__) && !defined(__clang__) -# define libsimple_asprintfa(__fmt, ...) /* TODO test */\ +# define libsimple_asprintfa(__fmt, ...)\ ({\ const char *__f = (__fmt);\ char *__ret = NULL;\ @@ -644,20 +644,22 @@ int libsimple_vasprintf(char **, const char *, va_list); #endif #if defined(__GNUC__) || defined(__clang__) -# define libsimple_vasprintfa(__fmt, __ap) /* TODO test */\ +# define libsimple_vasprintfa(__fmt, __ap)\ ({\ const char *__f = (__fmt);\ - va_list __a = (__ap);\ + va_list __a1;\ va_list __a2;\ char *__ret = NULL;\ int __r;\ - va_copy(__a2, __a);\ - __r = vsnprintf(NULL, 0, __f, __a);\ + va_copy(__a1, __ap);\ + va_copy(__a2, __a1);\ + __r = vsnprintf(NULL, 0, __f, __a1);\ if (__r >= 0) {\ __ret = alloca((size_t)__r + 1);\ vsprintf(__ret, __f, __a2);\ }\ va_end(__a2);\ + va_end(__a1);\ __ret;\ }) # ifndef vasprintfa @@ -34,14 +34,14 @@ stderr_ok = old_stderr_ok__;\ break;\ }\ - assert(EXPR);\ + EXPR;\ assert_unreached();\ } while (0) #define assert_exit_ptr(EXPR)\ do {\ void *volatile ptr__;\ - assert_exit((ptr__ = (EXPR)));\ + assert_exit((void)(ptr__ = (EXPR)));\ } while (0) #define assert_stderr(FMT, ...)\ |