ntdb: remove hash table trees.
[Samba/id10ts.git] / lib / ntdb / test / run-30-exhaust-before-expand.c
blobcc9ea3fa3dd6f19da8ea9bb0d8b3f04a0b3b5627
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
5 static bool empty_freetable(struct ntdb_context *ntdb)
7 struct ntdb_freetable ftab;
8 unsigned int i;
10 /* Now, free table should be completely exhausted in zone 0 */
11 if (ntdb_read_convert(ntdb, ntdb->ftable_off, &ftab, sizeof(ftab)) != 0)
12 abort();
14 for (i = 0; i < sizeof(ftab.buckets)/sizeof(ftab.buckets[0]); i++) {
15 if (ftab.buckets[i])
16 return false;
18 return true;
22 int main(int argc, char *argv[])
24 unsigned int i, j;
25 struct ntdb_context *ntdb;
26 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
27 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
28 NTDB_NOMMAP|NTDB_CONVERT };
30 plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
32 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
33 NTDB_DATA k, d;
34 uint64_t size;
35 bool was_empty = false;
37 k.dptr = (void *)&j;
38 k.dsize = sizeof(j);
40 ntdb = ntdb_open("run-30-exhaust-before-expand.ntdb", flags[i],
41 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
42 ok1(ntdb);
43 if (!ntdb)
44 continue;
46 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
47 /* There's one empty record in initial db. */
48 ok1(!empty_freetable(ntdb));
50 size = ntdb->file->map_size;
52 /* Create one record to chew up most space. */
53 d.dsize = size - NEW_DATABASE_HDR_SIZE(ntdb->hash_bits) - 32;
54 d.dptr = calloc(d.dsize, 1);
55 j = 0;
56 ok1(ntdb_store(ntdb, k, d, NTDB_INSERT) == 0);
57 ok1(ntdb->file->map_size == size);
58 free(d.dptr);
60 /* Now insert minimal-length records until we expand. */
61 for (j = 1; ntdb->file->map_size == size; j++) {
62 was_empty = empty_freetable(ntdb);
63 if (ntdb_store(ntdb, k, k, NTDB_INSERT) != 0)
64 err(1, "Failed to store record %i", j);
67 /* Would have been empty before expansion, but no longer. */
68 ok1(was_empty);
69 ok1(!empty_freetable(ntdb));
70 ntdb_close(ntdb);
73 ok1(tap_log_messages == 0);
74 return exit_status();