s3:include: remove typedef user_struct
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-03-coalesce.c
blobad39719bcbd965eb51eca130d04056d233586cc8
1 #include "tdb2-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4 #include "layout.h"
6 static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off)
8 struct tdb_free_record f;
9 enum TDB_ERROR ecode;
11 ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
12 if (ecode != TDB_SUCCESS)
13 return ecode;
14 if (frec_magic(&f) != TDB_FREE_MAGIC)
15 return TDB_ERR_CORRUPT;
16 return frec_len(&f);
19 int main(int argc, char *argv[])
21 tdb_off_t b_off, test;
22 struct tdb_context *tdb;
23 struct tdb_layout *layout;
24 struct tdb_data data, key;
25 tdb_len_t len;
27 /* FIXME: Test TDB_CONVERT */
28 /* FIXME: Test lock order fail. */
30 plan_tests(42);
31 data = tdb_mkdata("world", 5);
32 key = tdb_mkdata("hello", 5);
34 /* No coalescing can be done due to EOF */
35 layout = new_tdb_layout();
36 tdb_layout_add_freetable(layout);
37 len = 1024;
38 tdb_layout_add_free(layout, len, 0);
39 tdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.tdb");
40 /* NOMMAP is for lockcheck. */
41 tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
42 &tap_log_attr);
43 ok1(tdb_check(tdb, NULL, NULL) == 0);
44 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
46 /* Figure out which bucket free entry is. */
47 b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(len));
48 /* Lock and fail to coalesce. */
49 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
50 test = layout->elem[1].base.off;
51 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, len, &test)
52 == 0);
53 tdb_unlock_free_bucket(tdb, b_off);
54 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
55 ok1(test == layout->elem[1].base.off);
56 ok1(tdb_check(tdb, NULL, NULL) == 0);
57 tdb_close(tdb);
58 tdb_layout_free(layout);
60 /* No coalescing can be done due to used record */
61 layout = new_tdb_layout();
62 tdb_layout_add_freetable(layout);
63 tdb_layout_add_free(layout, 1024, 0);
64 tdb_layout_add_used(layout, key, data, 6);
65 tdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.tdb");
66 /* NOMMAP is for lockcheck. */
67 tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
68 &tap_log_attr);
69 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
70 ok1(tdb_check(tdb, NULL, NULL) == 0);
72 /* Figure out which bucket free entry is. */
73 b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
74 /* Lock and fail to coalesce. */
75 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
76 test = layout->elem[1].base.off;
77 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
78 == 0);
79 tdb_unlock_free_bucket(tdb, b_off);
80 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
81 ok1(test == layout->elem[1].base.off);
82 ok1(tdb_check(tdb, NULL, NULL) == 0);
83 tdb_close(tdb);
84 tdb_layout_free(layout);
86 /* Coalescing can be done due to two free records, then EOF */
87 layout = new_tdb_layout();
88 tdb_layout_add_freetable(layout);
89 tdb_layout_add_free(layout, 1024, 0);
90 tdb_layout_add_free(layout, 2048, 0);
91 tdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.tdb");
92 /* NOMMAP is for lockcheck. */
93 tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
94 &tap_log_attr);
95 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
96 ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
97 ok1(tdb_check(tdb, NULL, NULL) == 0);
99 /* Figure out which bucket (first) free entry is. */
100 b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
101 /* Lock and coalesce. */
102 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
103 test = layout->elem[2].base.off;
104 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
105 == 1024 + sizeof(struct tdb_used_record) + 2048);
106 /* Should tell us it's erased this one... */
107 ok1(test == TDB_ERR_NOEXIST);
108 ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
109 ok1(free_record_length(tdb, layout->elem[1].base.off)
110 == 1024 + sizeof(struct tdb_used_record) + 2048);
111 ok1(tdb_check(tdb, NULL, NULL) == 0);
112 tdb_close(tdb);
113 tdb_layout_free(layout);
115 /* Coalescing can be done due to two free records, then data */
116 layout = new_tdb_layout();
117 tdb_layout_add_freetable(layout);
118 tdb_layout_add_free(layout, 1024, 0);
119 tdb_layout_add_free(layout, 512, 0);
120 tdb_layout_add_used(layout, key, data, 6);
121 tdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.tdb");
122 /* NOMMAP is for lockcheck. */
123 tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
124 &tap_log_attr);
125 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
126 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
127 ok1(tdb_check(tdb, NULL, NULL) == 0);
129 /* Figure out which bucket free entry is. */
130 b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
131 /* Lock and coalesce. */
132 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
133 test = layout->elem[2].base.off;
134 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
135 == 1024 + sizeof(struct tdb_used_record) + 512);
136 ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
137 ok1(free_record_length(tdb, layout->elem[1].base.off)
138 == 1024 + sizeof(struct tdb_used_record) + 512);
139 ok1(test == TDB_ERR_NOEXIST);
140 ok1(tdb_check(tdb, NULL, NULL) == 0);
141 tdb_close(tdb);
142 tdb_layout_free(layout);
144 /* Coalescing can be done due to three free records, then EOF */
145 layout = new_tdb_layout();
146 tdb_layout_add_freetable(layout);
147 tdb_layout_add_free(layout, 1024, 0);
148 tdb_layout_add_free(layout, 512, 0);
149 tdb_layout_add_free(layout, 256, 0);
150 tdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.tdb");
151 /* NOMMAP is for lockcheck. */
152 tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
153 &tap_log_attr);
154 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
155 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
156 ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
157 ok1(tdb_check(tdb, NULL, NULL) == 0);
159 /* Figure out which bucket free entry is. */
160 b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
161 /* Lock and coalesce. */
162 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
163 test = layout->elem[2].base.off;
164 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
165 == 1024 + sizeof(struct tdb_used_record) + 512
166 + sizeof(struct tdb_used_record) + 256);
167 ok1(tdb->file->allrecord_lock.count == 0
168 && tdb->file->num_lockrecs == 0);
169 ok1(free_record_length(tdb, layout->elem[1].base.off)
170 == 1024 + sizeof(struct tdb_used_record) + 512
171 + sizeof(struct tdb_used_record) + 256);
172 ok1(tdb_check(tdb, NULL, NULL) == 0);
173 tdb_close(tdb);
174 tdb_layout_free(layout);
176 ok1(tap_log_messages == 0);
177 return exit_status();