diff options
author | Mattias Andrée <maandree@kth.se> | 2018-08-29 12:55:52 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-08-29 12:55:52 +0200 |
commit | 280258050d3c129614d60bca4d6df31288dc2573 (patch) | |
tree | a72ecb0feaf0ca0659383ff2eb3707a9e4340eb6 /libsimple.c | |
parent | Add tests (diff) | |
download | libsimple-280258050d3c129614d60bca4d6df31288dc2573.tar.gz libsimple-280258050d3c129614d60bca4d6df31288dc2573.tar.bz2 libsimple-280258050d3c129614d60bca4d6df31288dc2573.tar.xz |
Add tests
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libsimple.c')
-rw-r--r-- | libsimple.c | 59 |
1 files changed, 57 insertions, 2 deletions
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 " |