test/mpool_tests.c

Sat, 31 Dec 2011 22:21:45 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 22:21:45 +0100
changeset 14
b78e174b6814
parent 13
98ac89e3aa37
child 19
cdd7a3173249
permissions
-rw-r--r--

Fixed realloc

     1 /*
     2  *
     3  */
     5 #include <stdio.h>
     6 #include <stdlib.h>
     7 #include <string.h>
     9 #include "mpool_tests.h"
    11 int cmp_value = 2;
    13 void int_destructor(int *ptr) {
    14     if(*ptr != cmp_value) {
    15         fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
    16         return;
    17     }
    18     cmp_value += 2;
    19 }
    21 void hello_destructor(char *str) {
    22     if(strcmp(str, "Hello World!") != 0) {
    23         fprintf(stderr, "ucx_mempool_reg_destr failed\n");
    24     }
    25 }
    27 int mpool_tests() {
    28     printf("   Test ucx_mempool_new\n");
    29     UcxMempool *pool = ucx_mempool_new(16);
    31     printf("   Test ucx_mempool_malloc\n");
    32     int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    33     for(int i=0;i<256;i++) {
    34         ucx_mempool_malloc(pool, i+1);
    35     }
    36     int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    37     int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    38     for(int i=0;i<256;i++) {
    39         ucx_mempool_malloc(pool, i+1);
    40     }
    41     int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    43     *ptr1 = 2;
    44     *ptr2 = 4;
    45     *ptr3 = 6;
    46     *ptr4 = 8;
    48     printf("   Test ucx_mempool_set_destr\n");
    49     ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
    50     ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
    51     ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
    52     ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
    54     printf("   Test ucx_mempool_calloc\n");
    55     char *str = ucx_mempool_calloc(pool, 1, 3);
    56     if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
    57         fprintf(stderr, "ucx_mempool_calloc failed\n");
    58     }
    59     str[0] = 'O';
    60     str[1] = 'K';
    62     printf("   Test ucx_mempool_realloc\n");
    63     str = ucx_mempool_realloc(pool, str, 4);
    64     str[2] = '!';
    65     str[3] = 0;
    66     if(strcmp(str, "OK!") != 0) {
    67         fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
    68     }
    70     printf("   Test ucx_mempool_reg_destr\n");
    71     char *hello = "Hello World!";
    72     ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
    74     printf("   Test ucx_mempool_free\n");
    75     //ucx_mempool_free(pool);
    78     return 0;
    79 }

mercurial