]> git.ozlabs.org Git - ccan/blobdiff - ccan/htable/test/run-type.c
htable: htable_pick helper to select a random entry.
[ccan] / ccan / htable / test / run-type.c
index 11ce54bac7fd889338893264822ac6be022159ca..c4201ed0cc5f5a6b151603f015ece212d5484d9a 100644 (file)
@@ -65,7 +65,7 @@ static void find_vals(const struct htable_obj *ht,
                        return;
                }
        }
-       pass("Found %u numbers in hash", i);
+       ok1(htable_obj_count(ht) == i);
 }
 
 static void del_vals(struct htable_obj *ht,
@@ -83,7 +83,7 @@ static void del_vals(struct htable_obj *ht,
 }
 
 static void del_vals_bykey(struct htable_obj *ht,
-                          const struct obj val[], unsigned int num)
+                          const struct obj val[] UNNEEDED, unsigned int num)
 {
        unsigned int i;
 
@@ -107,31 +107,33 @@ static bool check_mask(struct htable *ht, const struct obj val[], unsigned num)
        return true;
 }
 
-int main(int argc, char *argv[])
+int main(void)
 {
        unsigned int i;
-       struct htable_obj ht;
+       struct htable_obj ht, ht2;
        struct obj val[NUM_VALS], *result;
        unsigned int dne;
        void *p;
        struct htable_obj_iter iter;
 
-       plan_tests(26);
+       plan_tests(35);
        for (i = 0; i < NUM_VALS; i++)
                val[i].key = i;
        dne = i;
 
        htable_obj_init(&ht);
-       ok1(ht.raw.max == 0);
+       ok1(htable_obj_count(&ht) == 0);
+       ok1(ht_max(&ht.raw) == 0);
        ok1(ht.raw.bits == 0);
 
        /* We cannot find an entry which doesn't exist. */
        ok1(!htable_obj_get(&ht, &dne));
+       ok1(!htable_obj_pick(&ht, 0, NULL));
 
        /* Fill it, it should increase in size. */
        add_vals(&ht, val, NUM_VALS);
        ok1(ht.raw.bits == NUM_BITS + 1);
-       ok1(ht.raw.max < (1 << ht.raw.bits));
+       ok1(ht_max(&ht.raw) < (1 << ht.raw.bits));
 
        /* Mask should be set. */
        ok1(ht.raw.common_mask != 0);
@@ -141,12 +143,18 @@ int main(int argc, char *argv[])
        /* Find all. */
        find_vals(&ht, val, NUM_VALS);
        ok1(!htable_obj_get(&ht, &dne));
+       ok1(htable_obj_pick(&ht, 0, NULL));
+       ok1(htable_obj_pick(&ht, 0, &iter));
 
        /* Walk once, should get them all. */
        i = 0;
        for (p = htable_obj_first(&ht,&iter); p; p = htable_obj_next(&ht, &iter))
                i++;
        ok1(i == NUM_VALS);
+       i = 0;
+       for (p = htable_obj_prev(&ht,&iter); p; p = htable_obj_prev(&ht, &iter))
+               i++;
+       ok1(i == NUM_VALS);
 
        /* Delete all. */
        del_vals(&ht, val, NUM_VALS);
@@ -167,8 +175,12 @@ int main(int argc, char *argv[])
        find_vals(&ht, val, NUM_VALS);
        ok1(!htable_obj_get(&ht, &dne));
 
+       /* Check copy. */
+       ok1(htable_obj_copy(&ht2, &ht));
+
        /* Delete them all by key. */
        del_vals_bykey(&ht, val, NUM_VALS);
+       del_vals_bykey(&ht2, val, NUM_VALS);
 
        /* Write two of the same value. */
        val[1] = val[0];
@@ -197,5 +209,8 @@ int main(int argc, char *argv[])
        }
 
        htable_obj_clear(&ht);
+       ok1(htable_obj_count(&ht) == 0);
+       htable_obj_clear(&ht2);
+       ok1(htable_obj_count(&ht2) == 0);
        return exit_status();
 }