pdf-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pdf-devel] List implementation tests


From: gerel
Subject: [pdf-devel] List implementation tests
Date: Thu, 06 Mar 2008 01:48:46 -0300

Hi all,

I've done a test on some functions from the list interface, it seems to work
right. Here is the test and the output.


--- test.c ---
#include "pdf-list.h"

typedef struct 
{
        int x, y;
} list_elem;


bool l_comp (const void * elemb, const void * elema)
{
        list_elem *elem, *elem2;
        elem = (list_elem*) elemb;
        elem2 = (list_elem*) elema;
        
        if (elem->x == elem2->x && elem->y == elem2->y)
                return 1;
        return 0;
}

void l_disp (const void * elema)
{
        list_elem * elem = (list_elem*) elema;
        printf ("Dispose: x: %d y: %d\n", elem->x, elem->y);
}


int main ()
{
        list_elem elem1,elem2,elem3,elem4;
        list_elem * val;
        pdf_list_node_t node;
        pdf_list_t list;
        pdf_list_iterator_t itr;
        pdf_status_t stat;

        
        list = pdf_list_create (l_comp, l_disp, 0);

        if (!list)
        {
                printf("Error list\n");
                return;
        }

        elem1.x = 1; elem1.y = 1;
        elem2.x = 2; elem2.y = 2;
        elem3.x = 3; elem3.y = 3;
        elem4.x = 4; elem4.y = 4;

        pdf_list_add_last (list, &elem1);
        pdf_list_add_last (list, &elem2);
        pdf_list_add_last (list, &elem3);
        pdf_list_add_first (list, &elem4);


        printf ("size: %d\n", pdf_list_size(list));

        val = (list_elem*) pdf_list_get_at (list, 0);
        printf ("1. x: %d y: %d\n", val->x, val->y);
        val = (list_elem*) pdf_list_get_at (list, 1);
        printf ("2. x: %d y: %d\n", val->x, val->y);
        val = (list_elem*) pdf_list_get_at (list, 2);
        printf ("3. x: %d y: %d\n", val->x, val->y);
        val = (list_elem*) pdf_list_get_at (list, 3);
        printf ("4. x: %d y: %d\n", val->x, val->y);


        node = pdf_list_search (list, &elem2);
        val = (list_elem*) pdf_list_node_value (list, node);
        printf ("search(2). x: %d y: %d\n", val->x, val->y);
   
        itr = pdf_list_iterator (list);
        stat = pdf_list_iterator_next (&itr, (void*)&val, &node);
        while (stat == PDF_OK)
        {
                printf ("elem(itr): x: %d ", val->x, val->y);
                val = (list_elem*) pdf_list_node_value (list, node);
                printf ("y: %d\n", val->y);
                stat = pdf_list_iterator_next (&itr, (void*)&val, &node);
        }
        pdf_list_iterator_free(&itr);

        node = pdf_list_search (list, &elem2);     
        pdf_list_remove_node (list, node);
        printf ("size: %d\n", pdf_list_size(list));
        pdf_list_remove (list, &elem1);
        printf ("size: %d\n", pdf_list_size(list));
        pdf_list_remove_at (list, 0);
        printf ("size: %d\n", pdf_list_size(list));


        itr = pdf_list_iterator (list);
        stat = pdf_list_iterator_next (&itr, (void*)&val, &node);
        while (stat == PDF_OK)
        {
                printf ("elem(itr): x: %d ", val->x, val->y);
                val = (list_elem*) pdf_list_node_value (list, node);
                printf ("y: %d\n", val->y);
                stat = pdf_list_iterator_next (&itr, (void*)&val, &node);
        }
        pdf_list_iterator_free(&itr);

        pdf_list_destroy(list);

}
--- EOF test.c ---

##
~/PROJECTS/libgnupdf/src/base $ gcc -I../../lib/ -I..  -I. pdf-list.c 
../../lib/gl_array_list.c ../../lib/xmalloc.c ../../lib/xalloc-die.c 
../../lib/exitfail.c  test.c 
~/PROJECTS/libgnupdf/src/base $ ./a.out 
size: 4
1. x: 4 y: 4
2. x: 1 y: 1
3. x: 2 y: 2
4. x: 3 y: 3
search(2). x: 2 y: 2
elem(itr): x: 4 y: 4
elem(itr): x: 1 y: 1
elem(itr): x: 2 y: 2
elem(itr): x: 3 y: 3
Dispose: x: 2 y: 2
size: 3
Dispose: x: 1 y: 1
size: 2
Dispose: x: 4 y: 4
size: 1
elem(itr): x: 3 y: 3
Dispose: x: 3 y: 3
~/PROJECTS/libgnupdf/src/base $ 
###

Now I need to integrate it in pdf-stm-* and the rest (should be trivial).


OTOH the interface allows the user to define a callback for element deallocation
and not for element allocation. A more consistent treatment would be to either
allocate and deallocate at the List module level or at the caller level, that
callback I feel will bring us troubles later.

We could at the _List module_ (not gl_list) do element allocation (the node is
returned anyways by pdf_list_add_* so we don't need to search for it) giving
"pdf_list_create()" the size of an element, in test.c instead of:

"list = pdf_list_create (l_comp, l_disp, 0);"

do this,

"list = pdf_list_create (l_comp, l_disp, sizeof(list_elem), 0);"

And define a simple deallocation function that calls "free()" for each element.
In case we want to keep one element after the list is destroyed a
"pdf_list_elem_copy" and/or "pdf_list_copy" function would do it.
So the new list creation interface would be:

pdf_list_t pdf_list_create (pdf_list_element_equals_fn_t equals_fn,
                            pdf_size_t elt_size, pdf_bool_t allow_duplicates);


Of course, you can strongly disagree with me and ignore all this :-)

cheers

-gerel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]