From 280258050d3c129614d60bca4d6df31288dc2573 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 29 Aug 2018 12:55:52 +0200 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 | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'libsimple.c') 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 " -- cgit v1.2.3-70-g09d2