tdb2: get rid of zones
[ccan] / ccan / tdb2 / test / run-30-exhaust-before-expand.c
index eb656954f83b64b25862130151f7b17abad6c5f7..e2e27295edd48f04ab59c13c98cff1ab4a406e65 100644 (file)
@@ -8,6 +8,23 @@
 #include <err.h>
 #include "logging.h"
 
+static bool empty_freelist(struct tdb_context *tdb)
+{
+       struct tdb_freelist free;
+       unsigned int i;
+
+       /* Now, free list should be completely exhausted in zone 0 */
+       if (tdb_read_convert(tdb, tdb->flist_off, &free, sizeof(free)) != 0)
+               abort();
+
+       for (i = 0; i < sizeof(free.buckets)/sizeof(free.buckets[0]); i++) {
+               if (free.buckets[i])
+                       return false;
+       }
+       return true;
+}
+
+
 int main(int argc, char *argv[])
 {
        unsigned int i, j;
@@ -16,12 +33,12 @@ int main(int argc, char *argv[])
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
                        TDB_NOMMAP|TDB_CONVERT };
 
-       plan_tests(sizeof(flags) / sizeof(flags[0]) * 5 + 1);
+       plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
 
        for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
-               tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1];
-               bool all_empty;
-               TDB_DATA k, d;
+               TDB_DATA k;
+               uint64_t size;
+               bool was_empty = false;
 
                k.dptr = (void *)&j;
                k.dsize = sizeof(j);
@@ -32,38 +49,23 @@ int main(int argc, char *argv[])
                if (!tdb)
                        continue;
 
-               /* We don't want the hash to expand, so we use one alloc to
-                * chew up over most of the space first. */
-               j = -1;
-               d.dsize = (1 << INITIAL_ZONE_BITS) - 500;
-               d.dptr = malloc(d.dsize);
-               ok1(tdb_store(tdb, k, d, TDB_INSERT) == 0);
-               ok1(tdb->map_size == sizeof(struct tdb_header)
-                   + (1 << INITIAL_ZONE_BITS));
+               ok1(empty_freelist(tdb));
+               /* Create some free space. */
+               ok1(tdb_expand(tdb, 1) == 0);
+               ok1(tdb_check(tdb, NULL, NULL) == 0);
+               ok1(!empty_freelist(tdb));
 
-               /* Insert minimal-length records until we add a zone. */ 
-               for (j = 0;
-                    tdb->map_size == sizeof(struct tdb_header)
-                            + (1 << INITIAL_ZONE_BITS);
-                    j++) {
+               size = tdb->map_size;
+               /* Insert minimal-length records until we expand. */
+               for (j = 0; tdb->map_size == size; j++) {
+                       was_empty = empty_freelist(tdb);
                        if (tdb_store(tdb, k, k, TDB_INSERT) != 0)
                                err(1, "Failed to store record %i", j);
                }
 
-               /* Now, free list should be completely exhausted in zone 0 */
-               ok1(tdb_read_convert(tdb,
-                                    sizeof(struct tdb_header)
-                                    + sizeof(struct free_zone_header),
-                                    &free, sizeof(free)) == 0);
-
-               all_empty = true;
-               for (j = 0; j < sizeof(free)/sizeof(free[0]); j++) {
-                       if (free[j]) {
-                               diag("Free bucket %i not empty", j);
-                               all_empty = false;
-                       }
-               }
-               ok1(all_empty);
+               /* Would have been empty before expansion, but no longer. */
+               ok1(was_empty);
+               ok1(!empty_freelist(tdb));
                tdb_close(tdb);
        }