test/mpool_tests.c

Sat, 31 Dec 2011 21:05:59 +0100

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

Added mempool

/*
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mpool_tests.h"

int cmp_value = 2;

void int_destructor(int *ptr) {
    if(*ptr != cmp_value) {
        fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
        return;
    }
    cmp_value += 2;
}

void hello_destructor(char *str) {
    if(strcmp(str, "Hello World!") != 0) {
        fprintf(stderr, "ucx_mempool_reg_destr failed\n");
    }
}

int mpool_tests() {
    printf("   Test ucx_mempool_new\n");
    UcxMempool *pool = ucx_mempool_new(16);

    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);
    }
    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);
    }
    int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));

    *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");
    }
    str[0] = 'O';
    str[1] = 'K';

    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");
    }

    printf("   Test ucx_mempool_reg_destr\n");
    char *hello = "Hello World!";
    ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);

    printf("   Test ucx_mempool_free\n");
    ucx_mempool_free(pool);
    

    return 0;
}

mercurial