new mempool tests

Mon, 20 Feb 2012 15:30:45 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Feb 2012 15:30:45 +0100
changeset 28
1666cbeb1db8
parent 27
22644e2572bc
child 29
bce0d7f2912b

new mempool tests

test/main.c file | annotate | diff | comparison | revisions
test/mpool_tests.c file | annotate | diff | comparison | revisions
test/mpool_tests.h file | annotate | diff | comparison | revisions
ucx/mempool.c file | annotate | diff | comparison | revisions
--- a/test/main.c	Sat Feb 18 18:36:30 2012 +0100
+++ b/test/main.c	Mon Feb 20 15:30:45 2012 +0100
@@ -64,7 +64,7 @@
 int main(int argc, char **argv) {
     printf("UCX Tests\n---------\n");
 
-    printf("\nUcxTestSuite Tests (1 failure is intended!)\n");
+    printf("\nUcxTestSuite tests (1 failure is intended!)\n");
     UcxTestSuite* suite = ucx_test_suite_new();
     ucx_test_register(suite, testTestSuitePositive);
     ucx_test_register(suite, testTestSuiteNegative);
@@ -72,6 +72,7 @@
     if (suite->failure == 1 && suite->success == 1) {
         ucx_test_suite_free(suite);
 
+        printf("\nLibrary function tests\n");
         suite = ucx_test_suite_new();
         /* UcxList Tests */
         ucx_test_register(suite, test_ucx_list_append);
@@ -96,11 +97,14 @@
         ucx_test_register(suite, test_ucx_dlist_remove);
         ucx_test_register(suite, test_ucx_dlist_clone);
 
-        /* TODO: replace these tests with "real" tests */
-        printf("\nUcxMemPool Tests\n");
-        if(mpool_tests()) {
-            fprintf(stderr, "mpool_tests failed\n");
-        }
+        /* UcxMemPool Tests */
+        ucx_test_register(suite, test_ucx_mempool_new);
+        ucx_test_register(suite, test_ucx_mempool_malloc);
+        ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
+        ucx_test_register(suite, test_ucx_mempool_calloc);
+        ucx_test_register(suite, test_ucx_mempool_set_destr);
+        ucx_test_register(suite, test_ucx_mempool_reg_destr);
+        ucx_test_register(suite, test_ucx_mempool_realloc);
 
         printf("\nUcxMap Tests\n");
         if(map_tests()) {
--- a/test/mpool_tests.c	Sat Feb 18 18:36:30 2012 +0100
+++ b/test/mpool_tests.c	Mon Feb 20 15:30:45 2012 +0100
@@ -2,82 +2,169 @@
  *
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #include "mpool_tests.h"
 
-int cmp_value = 2;
+UCX_TEST_BEGIN(test_ucx_mempool_new) {
+    UcxMempool *pool = ucx_mempool_new(16);
+    
+    UCX_TEST_ASSERT(pool->size == 16, "wrong size")
+    UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter")
+    UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed")
+    
+    ucx_mempool_free(pool);
+    
+    UCX_TEST_END
+}
 
-void int_destructor(int *ptr) {
-    if(*ptr != cmp_value) {
-        fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
-        return;
-    }
-    cmp_value += 2;
+UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
+    
+    UcxMempool *pool = ucx_mempool_new(1);
+    
+    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
+    
+    UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
+    UCX_TEST_ASSERT(pool->size == 1, "chcap called")
+    
+    int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor));
+    *pooladdr = 5;
+    
+    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
+    
+    ucx_mempool_free(pool);
+    
+    UCX_TEST_END
 }
 
-void hello_destructor(char *str) {
-    if(strcmp(str, "Hello World!") != 0) {
-        fprintf(stderr, "ucx_mempool_reg_destr failed\n");
-    }
+UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
+    
+    UcxMempool *pool = ucx_mempool_new(1);
+    
+    ucx_mempool_malloc(pool, sizeof(int));
+    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
+    
+    UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented")
+    UCX_TEST_ASSERT(pool->size == 17, "chcap not called")
+    
+    int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor));
+    *pooladdr = 5;
+    
+    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
+    
+    ucx_mempool_free(pool);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
+    
+    UcxMempool *pool = ucx_mempool_new(1);
+    
+    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
+    
+    UCX_TEST_ASSERT(test != NULL, "no memory for test data")
+    UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
+    
+    ucx_mempool_free(pool);
+    
+    UCX_TEST_END
+}
+
+void test_setdestr(void* elem) {
+    int *cb = (int*) ((int*) elem)[1];
+    *cb = 42;
 }
 
-int mpool_tests() {
-	int r = 0;
-
-    printf("   Test ucx_mempool_new\n");
-    UcxMempool *pool = ucx_mempool_new(16);
+UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
+    
+    UcxMempool *pool = ucx_mempool_new(2);
+    
+    ucx_mempool_malloc(pool, sizeof(int));
+    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
+    
+    int *cb = (int*) malloc(sizeof(int));
+    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
+    
+    test[0] = 5; test[1] = (int) cb;
+    *cb = 13;
+    
+    ucx_mempool_set_destr(test, test_setdestr);
+    
+    UCX_TEST_ASSERT(
+            *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
+    UCX_TEST_ASSERT(
+            test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data")
+    
+    ucx_mempool_free(pool);
+    
+    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
 
-    printf("   Test ucx_mempool_malloc\n");
-    int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
-    for(int i=0;i<256;i++) {
-        ucx_mempool_malloc(pool, i+1);
-    }
-    int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
-    int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
-    for(int i=0;i<256;i++) {
-        ucx_mempool_malloc(pool, i+1);
-    }
-    int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
+    free(cb);
+    
+    UCX_TEST_END
+}
 
-    *ptr1 = 2;
-    *ptr2 = 4;
-    *ptr3 = 6;
-    *ptr4 = 8;
-
-    printf("   Test ucx_mempool_set_destr\n");
-    ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
-    ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
-    ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
-    ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
 
-    printf("   Test ucx_mempool_calloc\n");
-    char *str = ucx_mempool_calloc(pool, 1, 3);
-    if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
-        fprintf(stderr, "ucx_mempool_calloc failed\n");
-        r--;
-    }
-    str[0] = 'O';
-    str[1] = 'K';
+UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
+    
+    UcxMempool *pool = ucx_mempool_new(1);
+    
+    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
+    
+    int *cb = (int*) malloc(sizeof(int));
+    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
+    
+    test[0] = 5; test[1] = (int) cb;
+    *cb = 13;
+    
+    ucx_mempool_reg_destr(pool, test, test_setdestr);
+    
+    ucx_destructor *pooladdr = (ucx_destructor*)
+            ((char*)pool->data[1] + sizeof(ucx_destructor));
+    
+    UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
+    
+    ucx_mempool_free(pool);
+    
+    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
+
+    free(cb);
+    
+    UCX_TEST_END
+}
 
-    printf("   Test ucx_mempool_realloc\n");
-    str = ucx_mempool_realloc(pool, str, 4);
-    str[2] = '!';
-    str[3] = 0;
-    if(strcmp(str, "OK!") != 0) {
-        fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
-        r--;
-    }
+UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
+    
+    UcxMempool *pool = ucx_mempool_new(2);
+    
+    ucx_mempool_malloc(pool, sizeof(int));
+    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
+    
+    int *cb = (int*) malloc(sizeof(int));
+    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
+    
+    test[0] = 5; test[1] = (int) cb;
+    *cb = 13;
+    
+    ucx_mempool_set_destr(test, test_setdestr);
     
-    printf("   Test ucx_mempool_reg_destr\n");
-    char *hello = "Hello World!";
-    ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
+    int *rtest, n = 2;
+    do {
+        n *= 2;
+        UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
+        rtest = ucx_mempool_realloc(pool, test, n*sizeof(int));
+    } while (rtest == test);
+    test = rtest;
+    
+    UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
+            "realloc killed destructor")
+    UCX_TEST_ASSERT(
+            test[0] == 5 && test[1] == (int) cb, "realloc destroyed data")
+    
+    ucx_mempool_free(pool);
+    
+    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
 
-    printf("   Test ucx_mempool_free\n");
-    //ucx_mempool_free(pool);
+    free(cb);
     
-
-    return r;
+    UCX_TEST_END
 }
--- a/test/mpool_tests.h	Sat Feb 18 18:36:30 2012 +0100
+++ b/test/mpool_tests.h	Mon Feb 20 15:30:45 2012 +0100
@@ -5,15 +5,20 @@
 #ifndef MPOOL_TESTS_H
 #define	MPOOL_TESTS_H
 
+#include "ucx/test.h"
 #include "ucx/mempool.h"
 
 #ifdef	__cplusplus
 extern "C" {
 #endif
 
-int mpool_tests();
-
-
+UCX_TEST_DECLARE(test_ucx_mempool_new)
+UCX_TEST_DECLARE(test_ucx_mempool_malloc)
+UCX_TEST_DECLARE(test_ucx_mempool_malloc_with_chcap)
+UCX_TEST_DECLARE(test_ucx_mempool_calloc)
+UCX_TEST_DECLARE(test_ucx_mempool_set_destr)
+UCX_TEST_DECLARE(test_ucx_mempool_reg_destr)
+UCX_TEST_DECLARE(test_ucx_mempool_realloc)
 
 #ifdef	__cplusplus
 }
--- a/ucx/mempool.c	Sat Feb 18 18:36:30 2012 +0100
+++ b/ucx/mempool.c	Mon Feb 20 15:30:45 2012 +0100
@@ -75,20 +75,20 @@
 }
 
 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
-    void *mem = ((char*)ptr) - sizeof(ucx_destructor);
+    char *mem = ((char*)ptr) - sizeof(ucx_destructor);
     char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
     if (newm == NULL) return NULL;
     if (mem != newm) {
         for(int i=0;i<pool->ndata;i++) {
             if(pool->data[i] == mem) {
                 pool->data[i] = newm;
-                return ((char*) newm) + sizeof(ucx_destructor);
+                return newm + sizeof(ucx_destructor);
             }
         }
-        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
+        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", ptr, pool);
         exit(1);
     } else {
-        return ((char*) newm) + sizeof(ucx_destructor);
+        return newm + sizeof(ucx_destructor);
     }
 }
 

mercurial