tdb2: change API to return the error value.
[ccan] / ccan / tdb2 / test / run-firstkey-nextkey.c
index a71e1a7ae82c1fa0ac8a269bfe14c4c6c309c143..b8a12bd38c8ae0bbb79a19855ac6c4f44834ec7c 100644 (file)
@@ -38,14 +38,24 @@ static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
        return 0;
 }
 
+/* Since tdb_nextkey frees dptr, we need to clone it. */
+static TDB_DATA dup_key(TDB_DATA key)
+{
+       void *p = malloc(key.dsize);
+       memcpy(p, key.dptr, key.dsize);
+       key.dptr = p;
+       return key;
+}
+
 int main(int argc, char *argv[])
 {
        unsigned int i, j;
        int num;
        struct trav_data td;
-       TDB_DATA k, k2;
+       TDB_DATA k;
        struct tdb_context *tdb;
        union tdb_attribute seed_attr;
+       enum TDB_ERROR ecode;
 
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
@@ -56,7 +66,7 @@ int main(int argc, char *argv[])
        seed_attr.seed.seed = 6334326220117065685ULL;
 
        plan_tests(sizeof(flags) / sizeof(flags[0])
-                  * (NUM_RECORDS*4 + (NUM_RECORDS-1)*2 + 20) + 1);
+                  * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1);
        for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
                tdb = tdb_open("run-traverse.tdb", flags[i],
                               O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr);
@@ -64,39 +74,33 @@ int main(int argc, char *argv[])
                if (!tdb)
                        continue;
 
-               ok1(tdb_firstkey(tdb).dptr == NULL);
-               ok1(tdb_error(tdb) == TDB_SUCCESS);
+               ok1(tdb_firstkey(tdb, &k) == TDB_ERR_NOEXIST);
 
                /* One entry... */
                k.dptr = (unsigned char *)&num;
                k.dsize = sizeof(num);
                num = 0;
                ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
-               k = tdb_firstkey(tdb);
+               ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
                ok1(k.dsize == sizeof(num));
                ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
-               k2 = tdb_nextkey(tdb, k);
-               ok1(k2.dsize == 0 && k2.dptr == NULL);
-               free(k.dptr);
+               ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
 
                /* Two entries. */
                k.dptr = (unsigned char *)&num;
                k.dsize = sizeof(num);
                num = 1;
                ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
-               k = tdb_firstkey(tdb);
+               ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
                ok1(k.dsize == sizeof(num));
                memcpy(&num, k.dptr, sizeof(num));
                ok1(num == 0 || num == 1);
-               k2 = tdb_nextkey(tdb, k);
-               ok1(k2.dsize == sizeof(j));
-               free(k.dptr);
-               memcpy(&j, k2.dptr, sizeof(j));
+               ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
+               ok1(k.dsize == sizeof(j));
+               memcpy(&j, k.dptr, sizeof(j));
                ok1(j == 0 || j == 1);
                ok1(j != num);
-               k = tdb_nextkey(tdb, k2);
-               ok1(k.dsize == 0 && k.dptr == NULL);
-               free(k2.dptr);
+               ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
 
                /* Clean up. */
                k.dptr = (unsigned char *)&num;
@@ -115,34 +119,35 @@ int main(int argc, char *argv[])
                ok1(td.calls == NUM_RECORDS);
 
                /* Simple loop should match tdb_traverse */
-               for (j = 0, k = tdb_firstkey(tdb); j < td.calls; j++) {
+               for (j = 0, ecode = tdb_firstkey(tdb, &k); j < td.calls; j++) {
                        int val;
 
+                       ok1(ecode == TDB_SUCCESS);
                        ok1(k.dsize == sizeof(val));
                        memcpy(&val, k.dptr, k.dsize);
                        ok1(td.records[j] == val);
-                       k2 = tdb_nextkey(tdb, k);
-                       free(k.dptr);
-                       k = k2;
+                       ecode = tdb_nextkey(tdb, &k);
                }
 
                /* But arbitrary orderings should work too. */
                for (j = td.calls-1; j > 0; j--) {
                        k.dptr = (unsigned char *)&td.records[j-1];
                        k.dsize = sizeof(td.records[j-1]);
-                       k = tdb_nextkey(tdb, k);
+                       k = dup_key(k);
+                       ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
                        ok1(k.dsize == sizeof(td.records[j]));
                        ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
                        free(k.dptr);
                }
 
                /* Even delete should work. */
-               for (j = 0, k = tdb_firstkey(tdb); k.dptr; j++) {
+               for (j = 0, ecode = tdb_firstkey(tdb, &k);
+                    ecode != TDB_ERR_NOEXIST;
+                    j++) {
+                       ok1(ecode == TDB_SUCCESS);
                        ok1(k.dsize == 4);
                        ok1(tdb_delete(tdb, k) == 0);
-                       k2 = tdb_nextkey(tdb, k);
-                       free(k.dptr);
-                       k = k2;
+                       ecode = tdb_nextkey(tdb, &k);
                }
 
                diag("delete using first/nextkey gave %u of %u records",