Skip to content

Commit

Permalink
cuint: add additional read/delete test
Browse files Browse the repository at this point in the history
  • Loading branch information
brong committed Jan 10, 2025
1 parent 60f0c61 commit 17f965c
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions cunit/aaa-db.testc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ static char *make_basedir(const char * const *reldirs)
CU_ASSERT_EQUAL(r, CYRUSDB_OK); \
CU_ASSERT_PTR_NOT_NULL(txn);

#define CANDELETE(key, keylen) \
r = cyrusdb_delete(db, key, keylen, &txn, 1); \
CU_ASSERT_EQUAL(r, CYRUSDB_OK); \
CU_ASSERT_PTR_NOT_NULL(txn);

#define ISCONSISTENT() \
r = cyrusdb_consistent(db); \
CU_ASSERT_EQUAL(r, CYRUSDB_OK);

#define BADDATA ((const char *)0xdeadbeef)
#define BADLEN ((int)0xcafebabe)

Expand Down Expand Up @@ -150,6 +159,18 @@ static char *make_basedir(const char * const *reldirs)
CU_ASSERT_NOT_EQUAL(_datalen, BADLEN); \
}

#define CANNOTFETCH_NOTXN(key, keylen, experror) \
{ \
const char *_data = BADDATA; \
size_t _datalen = BADLEN; \
r = cyrusdb_fetch(db, key, keylen, &_data, &_datalen, NULL); \
CU_ASSERT_EQUAL(r, experror); \
CU_ASSERT_PTR_NULL(_data); \
CU_ASSERT_PTR_NOT_EQUAL(_data, BADDATA); \
CU_ASSERT_EQUAL(_datalen, 0); \
CU_ASSERT_NOT_EQUAL(_datalen, BADLEN); \
}

#define CANNOTFETCHNEXT(key, keylen, experror) \
{ \
r = cyrusdb_fetchnext(db, key, keylen, NULL, 0, NULL, 0, &txn); \
Expand Down Expand Up @@ -329,6 +350,63 @@ static void test_multiopen(void)
CU_ASSERT_EQUAL(fexists(filename), 0);
}

static void test_read_and_delete(void)
{
struct db *db = NULL;
struct txn *txn = NULL;
int r;
/* data courtesy hipsteripsum.me */
static const char KEY1[] = "mustache";
static const char DATA1[] = "blog lomo";
static const char KEY2[] = "cred";
static const char DATA2[] = "beard ethical";
static const char KEY3[] = "leggings";
static const char DATA3[] = "tumblr salvia";
static const char KEY3CHILD[] = "leggings.biodiesel";
static const char KEY4[] = "occupy";
static const char DATA4[] = "etsy tote bag";

if (skiptest()) return;

CU_ASSERT_EQUAL(fexists(filename), -ENOENT);

/* open() with _CREATE succeeds and creates the db */
r = cyrusdb_open(backend, filename, CYRUSDB_CREATE, &db);
CU_ASSERT_EQUAL(r, CYRUSDB_OK);
CU_ASSERT_PTR_NOT_NULL(db);
CU_ASSERT_EQUAL(fexists(filename), 0);

/* 1st txn starts */
CANSTORE(KEY1, strlen(KEY1), DATA1, strlen(DATA1));
CANSTORE(KEY2, strlen(KEY2), DATA2, strlen(DATA2));
CANSTORE(KEY3, strlen(KEY3), DATA3, strlen(DATA3));
CANSTORE(KEY4, strlen(KEY4), DATA4, strlen(DATA4));
CANCOMMIT();
ISCONSISTENT();
/* 1st txn ends */

/* 2nd txn starts - try to fetch the immediately preceding value */
CANNOTFETCH(KEY3CHILD, strlen(KEY3CHILD), CYRUSDB_NOTFOUND);
CANFETCH(KEY3, strlen(KEY3), DATA3, strlen(DATA3));
CANDELETE(KEY3, strlen(KEY3));
CANFETCH(KEY1, strlen(KEY1), DATA1, strlen(DATA1));
CANDELETE(KEY1, strlen(KEY1));
CANCOMMIT();
ISCONSISTENT();

/* what is left? */
CANNOTFETCH_NOTXN(KEY1, strlen(KEY1), CYRUSDB_NOTFOUND);
CANFETCH_NOTXN(KEY2, strlen(KEY2), DATA2, strlen(DATA2));
CANNOTFETCH_NOTXN(KEY3, strlen(KEY3), CYRUSDB_NOTFOUND);
CANFETCH_NOTXN(KEY4, strlen(KEY4), DATA4, strlen(DATA4));
ISCONSISTENT();

/* closing succeeds and leaves the file in place */
r = cyrusdb_close(db);
CU_ASSERT_EQUAL(r, CYRUSDB_OK);
CU_ASSERT_EQUAL(fexists(filename), 0);
}

static void test_opentwo(void)
{
struct db *db1 = NULL;
Expand Down

0 comments on commit 17f965c

Please sign in to comment.