test/mpool_tests.c

Sat, 14 Jan 2012 13:07:18 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 14 Jan 2012 13:07:18 +0100
changeset 21
d599fefc7620
parent 19
cdd7a3173249
child 28
1666cbeb1db8
permissions
-rw-r--r--

merge

/*
 *
 */

#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() {
	int r = 0;

    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+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));

    *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';

    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--;
    }
    
    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 r;
}

mercurial