test: update tests to use new API

This commit is contained in:
Jef Roosens 2022-12-07 12:56:31 +01:00
parent 50ebf86589
commit a153c4e22d
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 124 additions and 75 deletions

View file

@ -26,9 +26,17 @@ TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) {
// Allocate & initialize trie
Trie *trie = calloc(1, sizeof(Trie));
trie->root = tnode_init();
trie->file_path = strdup(file_path);
pthread_rwlock_init(&trie->lock, NULL);
*trie_ptr = trie;
if (file_path == NULL) {
trie->file_path = NULL;
return Ok;
}
trie->file_path = strdup(file_path);
// Populate trie with data from file
FILE *fp = fopen(file_path, "r");
@ -75,8 +83,6 @@ TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) {
fclose(fp);
*trie_ptr = trie;
return Ok;
}
@ -265,28 +271,30 @@ TrieExitCode trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
}
TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry) {
// Easiest way to make sure we don't add duplicate entries
// We use an internal function that doesn't require a read lock, as we're
// already inside a write lock
if (trie_search_node(trie, key).child != NULL) {
return AlreadyPresent;
if (trie->file_path != NULL) {
// Easiest way to make sure we don't add duplicate entries
// We use an internal function that doesn't require a read lock, as we're
// already inside a write lock
if (trie_search_node(trie, key).child != NULL) {
return AlreadyPresent;
}
FILE *fp = fopen(trie->file_path, "a");
if (fp == NULL) {
return FileError;
}
fputs(key, fp);
fputs(" ", fp);
fputc(entry_type_to_char(entry->type), fp);
fputs(" ", fp);
fputs(entry->string, fp);
fputs("\n", fp);
fclose(fp);
}
FILE *fp = fopen(trie->file_path, "a");
if (fp == NULL) {
return FileError;
}
fputs(key, fp);
fputs(" ", fp);
fputc(entry_type_to_char(entry->type), fp);
fputs(" ", fp);
fputs(entry->string, fp);
fputs("\n", fp);
fclose(fp);
// This function *should* always return Ok. Otherwise, the function would've
// exited because the string was found in the trie.
return trie_add_no_lock(trie, key, entry);