]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[list] Add test code of the sort function
authorCharlie Brej <cbrej@cs.man.ac.uk>
Sat, 28 Nov 2009 18:45:49 +0000 (18:45 +0000)
committerCharlie Brej <cbrej@cs.man.ac.uk>
Sat, 28 Nov 2009 18:45:49 +0000 (18:45 +0000)
src/libply/ply-list.c

index 5ecc5a73c11dee4800ba981c8c6798cbc4988ec7..66d1cf6fe2a4b0c783f0661057ac281eb03f0537 100644 (file)
@@ -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