feat(lsm): implement a simple trie remove

This commit is contained in:
Jef Roosens 2023-10-14 15:57:33 +02:00
parent 682f422e3c
commit 6938c29725
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 147 additions and 34 deletions

View file

@ -54,6 +54,8 @@ void test_insert_multiple() {
TEST_CHECK(lsm_bt_insert(bt, chars[i], (void *)(i + 1)) == lsm_error_ok);
}
TEST_CHECK(lsm_bt_size(bt) == char_count);
void *data;
for (size_t i = 0; i < char_count; i++) {
TEST_CHECK(lsm_bt_insert(bt, chars[i], (void *)(i + 1)) == lsm_error_already_present);
@ -68,11 +70,13 @@ void test_remove_root() {
BT_INIT();
TEST_CHECK(lsm_bt_insert(bt, 'a', (void *)1) == lsm_error_ok);
TEST_CHECK(lsm_bt_size(bt) == 1);
void *data;
TEST_CHECK(lsm_bt_remove(&data, bt, 'a') == lsm_error_ok);
TEST_CHECK(data == (void *)1);
TEST_CHECK(bt->root == NULL);
TEST_CHECK(lsm_bt_size(bt) == 0);
lsm_bt_free(bt);
}
@ -95,6 +99,8 @@ void test_remove_multiple() {
TEST_CHECK(data == (void *)6);
TEST_CHECK(lsm_bt_remove(&data, bt, 'e') == lsm_error_not_found);
TEST_CHECK(lsm_bt_size(bt) == char_count - 2);
lsm_bt_free(bt);
}

View file

@ -122,8 +122,7 @@ int fuzzy_test_trie_seed(FuzzyConfig conf) {
lsm_trie *trie;
lsm_trie_init(&trie);
bool changed;
lsm_error status;
lsm_error res;
// 0: success
// 1: invalid add
@ -134,13 +133,13 @@ int fuzzy_test_trie_seed(FuzzyConfig conf) {
// Add all strings to trie, checking for duplicates
for (int i = 0; i < conf.word_count; i++) {
status = lsm_trie_insert(trie, &matrix[i], (void **)1);
res = lsm_trie_insert(trie, &matrix[i], (void **)1);
// if changed is false, *contains_dedupped[i] should be true, as changed
// can only be false if the string is already contained in the trie. if
// changed is true, *contains_dedupped[i] should be false, as the string
// cannot be in the trie yet.
if (status == lsm_error_ok && *contains_dedupped[i]) {
if (res == lsm_error_ok && *contains_dedupped[i]) {
exit_code = 1;
goto END;
}
@ -159,26 +158,27 @@ int fuzzy_test_trie_seed(FuzzyConfig conf) {
}
// Remove all strings again, again taking duplicates into consideration
/* for (int i = 0; i < conf.word_count; i++) { */
/* changed = remove_func(ct, matrix[i]); */
for (int i = 0; i < conf.word_count; i++) {
res = lsm_trie_remove(NULL, trie, &matrix[i]);
/* // The string shouldn't be in the trie, yet another add operation */
/* // says it added it as well */
/* if (changed != *contains_dedupped[i]) { */
/* exit_code = 2; */
/* goto END; */
/* } */
// The string shouldn't be in the trie, yet another add operation
// says it added it as well
if (res == lsm_error_ok && !*contains_dedupped[i]) {
exit_code = 2;
goto END;
}
/* if (*contains_dedupped[i]) { */
/* *contains_dedupped[i] = false; */
/* size--; */
/* } */
/* } */
if (*contains_dedupped[i]) {
*contains_dedupped[i] = false;
size--;
}
}
// Finally, check that the trie is completely empty
/* if (size_func(ct) != 0) { */
/* exit_code = 4; */
/* } */
if (lsm_trie_size(trie) != 0) {
printf("%lu %lu\n", lsm_trie_size(trie), size);
exit_code = 4;
}
END:
/* trie_free(ct); */