aboutsummaryrefslogtreecommitdiffstats
path: root/allocn.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-08-28 12:36:31 +0200
committerMattias Andrée <maandree@kth.se>2018-08-28 12:36:31 +0200
commit6fda2a0697d9d49db823e1569edf27378cf31daf (patch)
tree9c088ca8932b98a33bfe92c2300d2940b5ca8353 /allocn.c
parentAdd test function to all .c files (diff)
downloadlibsimple-6fda2a0697d9d49db823e1569edf27378cf31daf.tar.gz
libsimple-6fda2a0697d9d49db823e1569edf27378cf31daf.tar.bz2
libsimple-6fda2a0697d9d49db823e1569edf27378cf31daf.tar.xz
Add tests for allocation functions
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'allocn.c')
-rw-r--r--allocn.c84
1 files changed, 81 insertions, 3 deletions
diff --git a/allocn.c b/allocn.c
index 671f8a3..cc27dc5 100644
--- a/allocn.c
+++ b/allocn.c
@@ -4,7 +4,7 @@
static inline size_t
-alloc_size_product(size_t n, va_list ap) /* TODO test */
+alloc_size_product(size_t n, va_list ap)
{
size_t prod = n;
if (!n) {
@@ -25,14 +25,14 @@ alloc_size_product(size_t n, va_list ap) /* TODO test */
}
void *
-libsimple_vmalloczn(int clear, size_t n, va_list ap) /* TODO test */
+libsimple_vmalloczn(int clear, size_t n, va_list ap)
{
n = alloc_size_product(n, ap);
return !n ? NULL : clear ? calloc(1, n) : malloc(n);
}
void *
-libsimple_vreallocn(void *ptr, size_t n, va_list ap) /* TODO test */
+libsimple_vreallocn(void *ptr, size_t n, va_list ap)
{
n = alloc_size_product(n, ap);
return !n ? NULL : realloc(ptr, n);
@@ -45,6 +45,84 @@ libsimple_vreallocn(void *ptr, size_t n, va_list ap) /* TODO test */
int
main(void)
{
+ struct allocinfo *info;
+ void *ptr, *old;
+
+ assert(!libsimple_malloczn(0, 0) && errno == EINVAL);
+ errno = 0;
+ assert(!libsimple_malloczn(1, 0) && errno == EINVAL);
+ errno = 0;
+ assert(!libsimple_mallocn(0) && errno == EINVAL);
+ errno = 0;
+ assert(!libsimple_callocn(0) && errno == EINVAL);
+ errno = 0;
+ assert(!libsimple_reallocn(NULL, 0) && errno == EINVAL);
+ errno = 0;
+
+ assert(!libsimple_malloczn(0, SIZE_MAX, 2, 0) && errno == ENOMEM);
+ errno = 0;
+ assert(!libsimple_malloczn(1, SIZE_MAX, 2, 0) && errno == ENOMEM);
+ errno = 0;
+ assert(!libsimple_mallocn(SIZE_MAX, 2, 0) && errno == ENOMEM);
+ errno = 0;
+ assert(!libsimple_callocn(SIZE_MAX, 2, 0) && errno == ENOMEM);
+ errno = 0;
+ assert(!libsimple_reallocn(NULL, SIZE_MAX, 2, 0) && errno == ENOMEM);
+ errno = 0;
+
+ assert((ptr = libsimple_malloczn(0, 10, 10, 0)));
+ if (have_custom_malloc()) {
+ assert((info = get_allocinfo(ptr)));
+ assert(info->size == 100);
+ assert(!info->zeroed);
+ }
+ free(ptr);
+
+ assert((ptr = libsimple_malloczn(1, 20, 20, 0)));
+ if (have_custom_malloc()) {
+ assert((info = get_allocinfo(ptr)));
+ assert(info->size == 400);
+ assert(info->zeroed == 400);
+ }
+ free(ptr);
+
+ assert((ptr = libsimple_mallocn(11, 11, 0)));
+ if (have_custom_malloc()) {
+ assert((info = get_allocinfo(ptr)));
+ assert(info->size == 121);
+ assert(!info->zeroed);
+ }
+ free(ptr);
+
+ assert((ptr = libsimple_callocn(22, 22, 0)));
+ if (have_custom_malloc()) {
+ assert((info = get_allocinfo(ptr)));
+ assert(info->size == 484);
+ assert(info->zeroed == 484);
+ }
+ free(ptr);
+
+ assert((ptr = libsimple_reallocn(NULL, 5, 0)));
+ if (have_custom_malloc()) {
+ assert((info = get_allocinfo(ptr)));
+ assert(info->size == 5);
+ assert(!info->zeroed);
+ info->refcount += 1;
+ }
+ stpcpy(ptr, "test");
+ assert((ptr = libsimple_reallocn(old = ptr, 10, 0)));
+ assert(!strcmp(ptr, "test"));
+ if (have_custom_malloc()) {
+ assert((info = get_allocinfo(ptr)));
+ assert(info->size == 10);
+ assert(!info->zeroed);
+ assert(ptr != old);
+ free(old);
+ }
+ free(ptr);
+
+ assert(!errno);
+
return 0;
}