diff options
author | Charlie Brej <cbrej@cs.man.ac.uk> | 2009-11-28 18:45:49 +0000 |
---|---|---|
committer | Charlie Brej <cbrej@cs.man.ac.uk> | 2009-11-28 18:45:49 +0000 |
commit | a8c9973d67e0157206009f438a3ed515da705012 (patch) | |
tree | 7a10f9c6f83066156c5459832dc334a092d99d38 | |
parent | 5ab755153356b3f685afe87c5926969389665bb2 (diff) |
[list] Add test code of the sort function
-rw-r--r-- | src/libply/ply-list.c | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/src/libply/ply-list.c b/src/libply/ply-list.c index 5ecc5a73..66d1cf6f 100644 --- a/src/libply/ply-list.c +++ b/src/libply/ply-list.c @@ -315,13 +315,27 @@ ply_list_node_set_data (ply_list_node_t *node, void *data) #ifdef PLY_LIST_ENABLE_TEST #include <stdio.h> +static int +compare_int_ptr (void *element_a, + void *element_b) +{ + int *int_a = element_a; + int *int_b = element_b; + return *int_a - *int_b; + +} + int main (int argc, char **argv) { ply_list_t *list; ply_list_node_t *node; - int i; + int i, lastval; + int *value; + int errors; + + errors = 0; list = ply_list_new (); @@ -343,8 +357,39 @@ main (int argc, i++; } + printf ("\n"); + ply_list_remove_all_nodes (list); + srandom(1); + + for (i = 0; i<100; i++) + { + value = malloc(sizeof(int)); + *value = random() %100; + ply_list_append_data (list, (void *) value); + } + + ply_list_sort (list, compare_int_ptr); + + node = ply_list_get_first_node (list); + i = 0; + lastval = 0; + + while (node != NULL) + { + value = (int *) ply_list_node_get_data (node); + if (*value < lastval) + { + printf ("ERROR: incorrect order\n"); + errors = 1; + } + lastval = *value; + printf ("node '%d' has data '%d'\n", i, *value); + node = ply_list_get_next_node (list, node); + i++; + } + ply_list_free (list); - return 0; + return errors; } #endif |