quinta-feira, 28 de outubro de 2010

Testing Linked List against multiple data types

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct instance {
    int a, b;
    float f;
    char str[8];
    int (*p) (const char *);
};

int
main (void)
{
    llist mylist;
    node *bufnode;
    struct search_result sr;
    int i = -1;
    char string[] = "Hello World";
    float myf = 2.5;
    struct instance strc = {1, 2, 3.4, "Five", puts};
    struct instance otherstrc;

    init_llist (&mylist);

    bufnode = init_node ("struct", (char *)&strc, sizeof (struct instance));
    add_node (&mylist, bufnode);

    bufnode = init_node ("int", (char *)&i, sizeof (int));
    add_node (&mylist, bufnode);

    bufnode = init_node ("string", string, strlen ("Hello World") + 1); 
    add_node (&mylist, bufnode);

    bufnode = init_node ("float", (char *)&myf, sizeof (float));
    add_node (&mylist, bufnode);

    sr = search_node (&mylist, "struct");
    memcpy (&otherstrc, sr.n->data, sizeof (struct instance));
    printf ("otherstrc: %d %d %f %s\n", 
        otherstrc.a, otherstrc.b, otherstrc.f , otherstrc.str);
    otherstrc.p("Nice?");
    del_node (&mylist, sr.n, sr.prior);

    sr = search_node (&mylist, "int");
    printf ("%d\n", *(int *)sr.n->data);
    del_node (&mylist, sr.n, sr.prior);

    sr = search_node (&mylist, "float");
    printf ("%f\n", *(float *)sr.n->data);
    del_node (&mylist, sr.n, sr.prior);

    sr = search_node (&mylist, "string");
    printf ("%s\n", sr.n->data);
    del_node (&mylist, sr.n, sr.prior);

    if (! mylist.first)
        printf ("empty\n");
    return 0;
}


Tested here: http://ideone.com/lTlp9

Nenhum comentário:

Postar um comentário