rbtree: vary insert and delete orders in test
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 10 Jan 2011 05:55:48 +0000 (16:25 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 10 Jan 2011 05:55:48 +0000 (16:25 +1030)
This covers more code than simply doing an ordered delete/insert.

ccan/rbtree/test/run-many.c

index ff32f4f18675dc62b56018f64c1f1daf18776507..a023403064ca4015103b783631d4ee79bb6f51ae 100644 (file)
@@ -24,20 +24,27 @@ static bool lookup_all(trbt_tree_t *rb, bool exist)
        return true;
 }
 
        return true;
 }
 
+static bool add_one(trbt_tree_t *rb, bool exist, unsigned int i)
+{
+       int *p = trbt_insert32(rb, i, talloc_memdup(rb, &i, sizeof(i)));
+       if (p) {
+               if (!exist)
+                       return false;
+               if (*p != i)
+                       return false;
+       } else
+               if (exist)
+                       return false;
+       return true;
+}
+
 static bool insert_all(trbt_tree_t *rb, bool exist)
 {
        unsigned int i;
 
        for (i = 0; i < NUM_ELEMS; i++) {
 static bool insert_all(trbt_tree_t *rb, bool exist)
 {
        unsigned int i;
 
        for (i = 0; i < NUM_ELEMS; i++) {
-               int *p = trbt_insert32(rb, i, talloc_memdup(rb, &i, sizeof(i)));
-               if (p) {
-                       if (!exist)
-                               return false;
-                       if (*p != i)
-                               return false;
-               } else
-                       if (exist)
-                               return false;
+               if (!add_one(rb, exist, i))
+                       return false;
        }
        return true;
 }
        }
        return true;
 }
@@ -46,7 +53,12 @@ static void delete_all(trbt_tree_t *rb)
 {
        unsigned int i;
 
 {
        unsigned int i;
 
-       for (i = 0; i < NUM_ELEMS; i++) {
+       /* Don't delete them in the obvious order. */
+       for (i = 0; i < NUM_ELEMS / 2; i++) {
+               trbt_delete32(rb, i);
+       }
+
+       for (i = NUM_ELEMS-1; i >= NUM_ELEMS / 2; i--) {
                trbt_delete32(rb, i);
        }
 }
                trbt_delete32(rb, i);
        }
 }