diff options
author | bpc2003 <wpesfriendnva@gmail.com> | 2025-04-22 16:45:36 -0400 |
---|---|---|
committer | bpc2003 <wpesfriendnva@gmail.com> | 2025-04-22 16:45:36 -0400 |
commit | ba5422d8ac64ca864dca168780a957a92f22a5d0 (patch) | |
tree | 7dbc2c9dd25038cb1af8f153331cd0bd417aa3d5 /src | |
parent | dc83060f785031f1607dc4152e678472bff06b8a (diff) |
Add utils.h and moved batch operations into their own files
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/batch.c | 184 | ||||
-rw-r--r-- | src/lib/delkeys.c | 95 | ||||
-rw-r--r-- | src/lib/getkeys.c | 16 | ||||
-rw-r--r-- | src/lib/keytab.c | 119 | ||||
-rw-r--r-- | src/lib/setkeys.c | 132 | ||||
-rw-r--r-- | src/lib/utils.c | 78 | ||||
-rw-r--r-- | src/lib/utils.h | 19 | ||||
-rw-r--r-- | src/test.c | 52 |
8 files changed, 378 insertions, 317 deletions
diff --git a/src/lib/batch.c b/src/lib/batch.c deleted file mode 100644 index 0534daf..0000000 --- a/src/lib/batch.c +++ /dev/null @@ -1,184 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <threads.h> - -#include "mdb.h" - -// TODO: move these functions into seperate files - -static int setkey_helper(void *thr_data); -static int delkey_helper(void *thr_data); -static int *clone(int n); -static tablist_t *copytab(tablist_t *dst, tablist_t *src); -static void dellist(tablist_t *list); - -extern int setkey(tablist_t **, int, char *); -extern int delkey(tablist_t *, int, char *); - -tablist_t *setkey_copy; -char *setkey_str; - -mtx_t setkey_mtx; -int setkeys(tablist_t **list, int id, char **pairs, int len) -{ - if (id < -1 || pairs == NULL || len <= 0) - return -1; - if (mtx_init(&setkey_mtx, mtx_plain) != thrd_success) - return -2; - int rc = 0; - setkey_copy = calloc((*list)[0].len, sizeof(tablist_t)); - copytab(setkey_copy, *list); - - for (int i = 0; i < len; ++i) { - setkey_str = pairs[i]; - if (id == -1) { - thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t)); - for (int i = 0; i < setkey_copy[0].len; ++i) - thrd_create(&thrds[i], setkey_helper, clone(i)); - for (int i = 0; i < setkey_copy[0].len; ++i) { - if (rc) - thrd_join(thrds[i], NULL); - else - thrd_join(thrds[i], &rc); - } - free(thrds); - } - else - rc = setkey_helper(clone(id)); - } - - if (!rc) { - if (setkey_copy[0].len > (*list)[0].len) - *list = realloc(*list, setkey_copy[0].len * sizeof(tablist_t)); - dellist(*list); - copytab(*list, setkey_copy); - } - mtx_destroy(&setkey_mtx); - dellist(setkey_copy); - free(setkey_copy); - return rc; -} - -tablist_t *delkey_copy; -char **delkeys_keys; -int keys_len; - -mtx_t delkey_mtx; -int delkeys(tablist_t *list, int id, char **keys, int len) -{ - if (id < -1 || len < 0) - return -1; - if (mtx_init(&delkey_mtx, mtx_plain) != thrd_success) - return -2; - int rc = 0; - delkey_copy = calloc(list[0].len, sizeof(tablist_t)); - copytab(delkey_copy, list); - delkeys_keys = keys; - keys_len = len; - - if (id == -1) { - thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t)); - for (int i = 0; i < delkey_copy[0].len; ++i) - thrd_create(&thrds[i], delkey_helper, clone(i)); - for (int i = 0; i < delkey_copy[0].len; ++i) { - if (rc) - thrd_join(thrds[i], NULL); - else - thrd_join(thrds[i], &rc); - } - free(thrds); - } else - rc = delkey_helper(clone(id)); - - if (!rc) { - dellist(list); - memmove(list, delkey_copy, delkey_copy[0].len * sizeof(tablist_t)); - } else - dellist(delkey_copy); - mtx_destroy(&delkey_mtx); - free(delkey_copy); - return rc; -} - -static int setkey_helper(void *thr_data) -{ - int rc; - int *id = (int *) thr_data; - - mtx_lock(&setkey_mtx); - rc = setkey(&setkey_copy, *id, setkey_str); - mtx_unlock(&setkey_mtx); - - free(id); - return rc; -} - -static int delkey_helper(void *thr_data) -{ - int rc = 0; - int *id = (int *) thr_data; - - mtx_lock(&delkey_mtx); - if (keys_len > 0 && delkeys_keys != NULL) - for (int i = 0; i < keys_len; ++i) { - if (delkeys_keys[i] == NULL) - return -3; - rc = delkey(delkey_copy, *id, delkeys_keys[i]); - } - else { - tablist_t *indexes = getkeys(delkey_copy, *id, NULL, 0); - for (int i = 0; indexes[0].tab[i].flag; ++i) - rc = delkey(delkey_copy, *id, indexes[0].tab[i].key); - free(indexes); - } - mtx_unlock(&delkey_mtx); - - free(id); - return rc; -} - -static int *clone(int n) -{ - int *c = calloc(1, sizeof(int)); - *c = n; - return c; -} - -static tablist_t *copytab(tablist_t *dst, tablist_t *src) -{ - if (dst == NULL) - return NULL; - dst[0].len = src[0].len; - for (int i = 0; i < src[0].len; ++i) { - for (int j = 0; j < TABLEN; ++j) { - dst[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } }; - if (src[i].tab[j].flag) { - switch (src[i].tab[j].flag) { - case 3: - dst[i].tab[j].value.str = - calloc(strlen(src[i].tab[j].value.str) + 1, sizeof(char)); - strcpy(dst[i].tab[j].value.str, src[i].tab[j].value.str); - break; - default: - dst[i].tab[j].value = src[i].tab[j].value; - break; - } - dst[i].tab[j].flag = src[i].tab[j].flag; - dst[i].tab[j].key = calloc(strlen(src[i].tab[j].key) + 1, sizeof(char)); - strcpy(dst[i].tab[j].key, src[i].tab[j].key); - } - } - } - return dst; -} - -static void dellist(tablist_t *list) -{ - for (int i = 0; i < list[0].len; ++i) { - tablist_t *indexes = getkeys(list, i, NULL, 0); - for (int j = 0; indexes[0].tab[j].flag; ++j) - delkey(list, i, indexes[0].tab[j].key); - free(indexes); - } -} diff --git a/src/lib/delkeys.c b/src/lib/delkeys.c index e69de29..b1ee83a 100644 --- a/src/lib/delkeys.c +++ b/src/lib/delkeys.c @@ -0,0 +1,95 @@ +#include <threads.h> +#include <stdlib.h> +#include <string.h> + +#include "mdb.h" +#include "utils.h" + +static int delkey_helper(void *thr_data); +static int delkey(tablist_t *, int, char *); + +tablist_t *delkey_copy; +char **delkeys_keys; +int keys_len; + +mtx_t delkey_mtx; + + +int delkeys(tablist_t *list, int id, char **keys, int len) +{ + if (id < -1 || len < 0) + return -1; + if (mtx_init(&delkey_mtx, mtx_plain) != thrd_success) + return -2; + int rc = 0; + delkey_copy = calloc(list[0].len, sizeof(tablist_t)); + copytab(delkey_copy, list); + delkeys_keys = keys; + keys_len = len; + + if (id == -1) { + thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t)); + for (int i = 0; i < delkey_copy[0].len; ++i) + thrd_create(&thrds[i], delkey_helper, clone(i)); + for (int i = 0; i < delkey_copy[0].len; ++i) { + if (rc) + thrd_join(thrds[i], NULL); + else + thrd_join(thrds[i], &rc); + } + free(thrds); + } else + rc = delkey_helper(clone(id)); + + if (!rc) { + dellist(list); + memmove(list, delkey_copy, delkey_copy[0].len * sizeof(tablist_t)); + } else + dellist(delkey_copy); + mtx_destroy(&delkey_mtx); + free(delkey_copy); + return rc; +} + +static int delkey_helper(void *thr_data) +{ + int rc = 0; + int *id = (int *) thr_data; + + mtx_lock(&delkey_mtx); + if (keys_len > 0 && delkeys_keys != NULL) + for (int i = 0; i < keys_len; ++i) { + if (delkeys_keys[i] == NULL) + return -3; + rc = delkey(delkey_copy, *id, delkeys_keys[i]); + } + else { + tablist_t *indexes = getkeys(delkey_copy, *id, NULL, 0); + for (int i = 0; indexes[0].tab[i].flag; ++i) + rc = delkey(delkey_copy, *id, indexes[0].tab[i].key); + free(indexes); + } + mtx_unlock(&delkey_mtx); + + free(id); + return rc; +} + +static int delkey(tablist_t *list, int id, char *key) +{ + int idx = hash(key); + if (list[id].tab[idx].key == NULL) + return 1; + while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + idx++; + if (idx >= TABLEN) + return 2; + free(list[id].tab[idx].key); + list[id].tab[idx].key = NULL; + if (list[id].tab[idx].flag == 3) { + free(list[id].tab[idx].value.str); + list[id].tab[idx].value.str = NULL; + } + list[id].tab[idx].flag = 0; + return 0; +} diff --git a/src/lib/getkeys.c b/src/lib/getkeys.c index 98442a3..cdba202 100644 --- a/src/lib/getkeys.c +++ b/src/lib/getkeys.c @@ -4,8 +4,9 @@ #include <threads.h> #include "mdb.h" +#include "utils.h" -extern tabidx_t getkey(tablist_t *list, int id, char *key); +static tabidx_t getkey(tablist_t *list, int id, char *key); static int getkeys_helper(void *data); @@ -89,3 +90,16 @@ static struct params *pass(tablist_t *list, tablist_t *ret, char **keys, int len p->pid = pid; return p; } + +static tabidx_t getkey(tablist_t *list, int id, char *key) +{ + int idx = hash(key); + if (list[id].tab[idx].key == NULL) + return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; + + while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + idx++; + if (idx >= TABLEN) + return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; + return list[id].tab[idx]; +} diff --git a/src/lib/keytab.c b/src/lib/keytab.c deleted file mode 100644 index e9f744a..0000000 --- a/src/lib/keytab.c +++ /dev/null @@ -1,119 +0,0 @@ -#include <stdlib.h> -#include <string.h> -#include <ctype.h> - -#include "mdb.h" - -// TODO: move all these functions into seperate files - -static int hash(char *key); -static char **getkv(char *pair); - -// getkey - gets a single key from a keytable -// if it can't find the key it will return an empty table index -tabidx_t getkey(tablist_t *list, int id, char *key) -{ - int idx = hash(key); - if (list[id].tab[idx].key == NULL) - return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; - - while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) - idx++; - if (idx >= TABLEN) - return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; - return list[id].tab[idx]; -} - -// setkey - adds the given pair to the given key table -// if setkey fails it will return 1 otherwise 0 -int setkey(tablist_t **list, int id, char *pair) -{ - if (pair == NULL) - return 1; - if (id >= (*list)[0].len) { - *list = realloc(*list, (id + 1) * sizeof(tablist_t)); - for (int i = (*list)[0].len; i <= id; ++i) { - for (int j = 0; j < TABLEN; ++j) - (*list)[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } }; - } - (*list)[0].len = id + 1; - } - char **kv = getkv(pair); - if (kv == NULL) - return 2; - - int idx = hash(kv[0]); - while ((*list)[id].tab[idx].key != NULL && - strcmp((*list)[id].tab[idx].key, kv[0]) && - idx < TABLEN) idx++; - if (idx >= TABLEN) - return 2; - if (!(*list)[id].tab[idx].key) - (*list)[id].tab[idx].key = kv[0]; - else { - free(kv[0]); - if ((*list)[id].tab[idx].flag == 3) - free((*list)[id].tab[idx].value.str); - } - char *end; - double num = strtod(kv[1], &end); - if (*end == '\0') { - (*list)[id].tab[idx].flag = 1; - (*list)[id].tab[idx].value.num = num; - } else if (!strcmp(kv[1], "true") || !strcmp(kv[1], "false")) { - (*list)[id].tab[idx].flag = 2; - (*list)[id].tab[idx].value.boolean = !strcmp(kv[1], "true"); - } else { - (*list)[id].tab[idx].flag = 3; - (*list)[id].tab[idx].value.str = calloc(strlen(kv[1]) + 1, sizeof(char)); - strcpy((*list)[id].tab[idx].value.str, kv[1]); - } - free(kv[1]); - free(kv); - return 0; -} - -// delkey - removes the given key from the given key table -int delkey(tablist_t *list, int id, char *key) -{ - int idx = hash(key); - if (list[id].tab[idx].key == NULL) - return 1; - while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) - idx++; - if (idx >= TABLEN) - return 2; - free(list[id].tab[idx].key); - list[id].tab[idx].key = NULL; - if (list[id].tab[idx].flag == 3) { - free(list[id].tab[idx].value.str); - list[id].tab[idx].value.str = NULL; - } - list[id].tab[idx].flag = 0; - return 0; -} - -static int hash(char *key) -{ - unsigned long h = 5381; - for (int i = 0; i < strlen(key); ++i) - h = ((h << 5) + h) + key[i]; - return h % TABLEN; -} - -static char **getkv(char *pair) -{ - char **kv = calloc(2, sizeof(char *)); - int i = 0; - while (pair[i] != ':' && i < strlen(pair)) - i++; - if (i >= strlen(pair)) { - free(kv); - return NULL; - } - kv[0] = calloc(i + 1, sizeof(char)); - strncpy(kv[0], pair, i); - kv[1] = calloc(strlen(pair) - i, sizeof(char)); - strcpy(kv[1], pair + i + 1); - return kv; -} diff --git a/src/lib/setkeys.c b/src/lib/setkeys.c index e69de29..7e9ef09 100644 --- a/src/lib/setkeys.c +++ b/src/lib/setkeys.c @@ -0,0 +1,132 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <threads.h> + +#include "mdb.h" +#include "utils.h" + +static int setkey_helper(void *thr_data); +static int setkey(tablist_t **, int, char *); +static char **getkv(char *pair); + +tablist_t *setkey_copy; +char *setkey_str; + +mtx_t setkey_mtx; +int setkeys(tablist_t **list, int id, char **pairs, int len) +{ + if (id < -1 || pairs == NULL || len <= 0) + return -1; + if (mtx_init(&setkey_mtx, mtx_plain) != thrd_success) + return -2; + int rc = 0; + setkey_copy = calloc((*list)[0].len, sizeof(tablist_t)); + copytab(setkey_copy, *list); + + for (int i = 0; i < len; ++i) { + setkey_str = pairs[i]; + if (id == -1) { + thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t)); + for (int i = 0; i < setkey_copy[0].len; ++i) + thrd_create(&thrds[i], setkey_helper, clone(i)); + for (int i = 0; i < setkey_copy[0].len; ++i) { + if (rc) + thrd_join(thrds[i], NULL); + else + thrd_join(thrds[i], &rc); + } + free(thrds); + } + else + rc = setkey_helper(clone(id)); + } + + if (!rc) { + if (setkey_copy[0].len > (*list)[0].len) + *list = realloc(*list, setkey_copy[0].len * sizeof(tablist_t)); + dellist(*list); + copytab(*list, setkey_copy); + } + mtx_destroy(&setkey_mtx); + dellist(setkey_copy); + free(setkey_copy); + return rc; +} + +static int setkey_helper(void *thr_data) +{ + int rc; + int *id = (int *) thr_data; + + mtx_lock(&setkey_mtx); + rc = setkey(&setkey_copy, *id, setkey_str); + mtx_unlock(&setkey_mtx); + + free(id); + return rc; +} + +static int setkey(tablist_t **list, int id, char *pair) +{ + if (pair == NULL) + return 1; + if (id >= (*list)[0].len) { + *list = realloc(*list, (id + 1) * sizeof(tablist_t)); + for (int i = (*list)[0].len; i <= id; ++i) { + for (int j = 0; j < TABLEN; ++j) + (*list)[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } }; + } + (*list)[0].len = id + 1; + } + char **kv = getkv(pair); + if (kv == NULL) + return 2; + + int idx = hash(kv[0]); + while ((*list)[id].tab[idx].key != NULL && + strcmp((*list)[id].tab[idx].key, kv[0]) && + idx < TABLEN) idx++; + if (idx >= TABLEN) + return 2; + if (!(*list)[id].tab[idx].key) + (*list)[id].tab[idx].key = kv[0]; + else { + free(kv[0]); + if ((*list)[id].tab[idx].flag == 3) + free((*list)[id].tab[idx].value.str); + } + char *end; + double num = strtod(kv[1], &end); + if (*end == '\0') { + (*list)[id].tab[idx].flag = 1; + (*list)[id].tab[idx].value.num = num; + } else if (!strcmp(kv[1], "true") || !strcmp(kv[1], "false")) { + (*list)[id].tab[idx].flag = 2; + (*list)[id].tab[idx].value.boolean = !strcmp(kv[1], "true"); + } else { + (*list)[id].tab[idx].flag = 3; + (*list)[id].tab[idx].value.str = calloc(strlen(kv[1]) + 1, sizeof(char)); + strcpy((*list)[id].tab[idx].value.str, kv[1]); + } + free(kv[1]); + free(kv); + return 0; +} + +static char **getkv(char *pair) +{ + char **kv = calloc(2, sizeof(char *)); + int i = 0; + while (pair[i] != ':' && i < strlen(pair)) + i++; + if (i >= strlen(pair)) { + free(kv); + return NULL; + } + kv[0] = calloc(i + 1, sizeof(char)); + strncpy(kv[0], pair, i); + kv[1] = calloc(strlen(pair) - i, sizeof(char)); + strcpy(kv[1], pair + i + 1); + return kv; +} diff --git a/src/lib/utils.c b/src/lib/utils.c new file mode 100644 index 0000000..9961c11 --- /dev/null +++ b/src/lib/utils.c @@ -0,0 +1,78 @@ +#include <stdlib.h> +#include <string.h> + +#include "utils.h" + +static int delkey(tablist_t *, int, char *); + +void dellist(tablist_t *list) +{ + for (int i = 0; i < list[0].len; ++i) { + tablist_t *indexes = getkeys(list, i, NULL, 0); + for (int j = 0; indexes[0].tab[j].flag; ++j) + delkey(list, i, indexes[0].tab[j].key); + free(indexes); + } +} + +int hash(char *key) +{ + unsigned long h = 5381; + for (int i = 0; i < strlen(key); ++i) + h = ((h << 5) + h) + key[i]; + return h % TABLEN; +} + +int *clone(int n) +{ + int *c = calloc(1, sizeof(int)); + *c = n; + return c; +} + +tablist_t *copytab(tablist_t *dst, tablist_t *src) +{ + if (dst == NULL) + return NULL; + dst[0].len = src[0].len; + for (int i = 0; i < src[0].len; ++i) { + for (int j = 0; j < TABLEN; ++j) { + dst[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } }; + if (src[i].tab[j].flag) { + switch (src[i].tab[j].flag) { + case 3: + dst[i].tab[j].value.str = + calloc(strlen(src[i].tab[j].value.str) + 1, sizeof(char)); + strcpy(dst[i].tab[j].value.str, src[i].tab[j].value.str); + break; + default: + dst[i].tab[j].value = src[i].tab[j].value; + break; + } + dst[i].tab[j].flag = src[i].tab[j].flag; + dst[i].tab[j].key = calloc(strlen(src[i].tab[j].key) + 1, sizeof(char)); + strcpy(dst[i].tab[j].key, src[i].tab[j].key); + } + } + } + return dst; +} + +static int delkey(tablist_t *list, int id, char *key) +{ + int idx = hash(key); + if (list[id].tab[idx].key == NULL) + return 1; + while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + idx++; + if (idx >= TABLEN) + return 2; + free(list[id].tab[idx].key); + list[id].tab[idx].key = NULL; + if (list[id].tab[idx].flag == 3) { + free(list[id].tab[idx].value.str); + list[id].tab[idx].value.str = NULL; + } + list[id].tab[idx].flag = 0; + return 0; +} diff --git a/src/lib/utils.h b/src/lib/utils.h new file mode 100644 index 0000000..64e2787 --- /dev/null +++ b/src/lib/utils.h @@ -0,0 +1,19 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "mdb.h" + +// dellist: deletes the provided table +void dellist(tablist_t *list); + +/* hash: calculates the DJB2 hash of a string, + * and returns that mod TABLEN */ +int hash(char *str); + +// clone: clones and returns a pointer to int +int *clone(int n); + +// copytab: copys table src into table dst +tablist_t *copytab(tablist_t *dst, tablist_t *src); + +#endif @@ -7,8 +7,10 @@ void test_setkeys(void) { tablist_t *list = readdb("dbs/test.db"); char *pairs[] = { "name:John" }; - if (setkeys(&list, -1, pairs, 1)) + if (setkeys(&list, -1, pairs, 1)) { fprintf(stderr, "test_setkeys: failed\n"); + return; + } for (int i = 0; i < list[0].len; ++i) { printf("id: %d\n", i); tablist_t *indexes = getkeys(list, i, NULL, 0); @@ -24,8 +26,10 @@ void test_setkeys_fail(void) { tablist_t *list = readdb("dbs/test.db"); char *pairs[] = { "namejohn" }; - if (!setkeys(&list, -1, pairs, 1)) + if (!setkeys(&list, -1, pairs, 1)) { fprintf(stderr, "test_setkeys_fail: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -34,8 +38,10 @@ void test_setkeys_multi_fail(void) { tablist_t *list = readdb("dbs/test.db"); char *pairs[] = { "name:John", NULL }; - if (!setkeys(&list, -1, pairs, 2)) + if (!setkeys(&list, -1, pairs, 2)) { fprintf(stderr, "test_setkeys_multi_fail: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -44,8 +50,10 @@ void test_setkeys_single(void) { tablist_t *list = readdb("dbs/test.db"); char *pairs[] = { "name:Alice" }; - if (setkeys(&list, 101, pairs, 1)) + if (setkeys(&list, 101, pairs, 1)) { fprintf(stderr, "test_setkeys_single: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -54,8 +62,10 @@ void test_setkeys_multipairs(void) { tablist_t *list = readdb("dbs/test.db"); char *pairs[] = { "name:Bob", "active:true" }; - if (setkeys(&list, 0, pairs, 2)) + if (setkeys(&list, 0, pairs, 2)) { fprintf(stderr, "test_setkeys_multipairs: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -64,8 +74,10 @@ void test_delkeys(void) { tablist_t *list = readdb("dbs/test.db"); char *keys[] = { "Row_1" }; - if (delkeys(list, -1, keys, 1)) + if (delkeys(list, -1, keys, 1)) { fprintf(stderr, "test_delkeys: failed\n"); + return; + } for (int i = 0; i < list[0].len; ++i) { printf("id: %d\n", i); tablist_t *indexes = getkeys(list, i, NULL, 0); @@ -80,8 +92,10 @@ void test_delkeys(void) void test_delkeys_all(void) { tablist_t *list = readdb("dbs/test.db"); - if (delkeys(list, -1, NULL, 0)) + if (delkeys(list, -1, NULL, 0)) { fprintf(stderr, "test_delkeys_all: failed\n"); + return; + } free(list); } @@ -89,8 +103,10 @@ void test_delkeys_fail(void) { tablist_t *list = readdb("dbs/test.db"); char *keys[] = { "Row_4" }; - if (!delkeys(list, -1, keys, 1)) + if (!delkeys(list, -1, keys, 1)) { fprintf(stderr, "test_delkeys_fail: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -98,8 +114,10 @@ void test_delkeys_fail(void) void test_delkeys_single(void) { tablist_t *list = readdb("dbs/test.db"); - if (delkeys(list, 0, NULL, 0)) + if (delkeys(list, 0, NULL, 0)) { fprintf(stderr, "test_delkeys_single: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -108,8 +126,10 @@ void test_delkeys_multi(void) { tablist_t *list = readdb("dbs/test.db"); char *keys[] = { "Row_1", "Row_2" }; - if (delkeys(list, -1, keys, 2)) + if (delkeys(list, -1, keys, 2)) { fprintf(stderr, "test_delkeys_multi: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -119,8 +139,10 @@ void test_getkeys_multi(void) tablist_t *list = readdb("dbs/test.db"); char *keys[] = { "Row_1", "Row_2" }; tablist_t *ret = getkeys(list, 0, keys, 2); - if (ret == NULL) + if (ret == NULL) { fprintf(stderr, "test_getkeys_multi: failed\n"); + return; + } for (int i = 0; ret[0].tab[i].flag; ++i) printf("%s\n", ret[0].tab[i].key); free(ret); @@ -133,8 +155,10 @@ void test_getkeys_multi_fail(void) tablist_t *list = readdb("dbs/test.db"); char *keys[] = { "Row_1", "Row_4" }; tablist_t *ret = getkeys(list, 0, keys, 2); - if (ret) + if (ret) { fprintf(stderr, "test_getkeys_multi_fail: failed\n"); + return; + } delkeys(list, -1, NULL, 0); free(list); } @@ -143,8 +167,10 @@ void test_getkeys(void) { tablist_t *list = readdb("dbs/test.db"); tablist_t *ret = getkeys(list, -1, NULL, 0); - if (!ret) + if (!ret) { fprintf(stderr, "test_getkeys: failed\n"); + return; + } free(ret); delkeys(list, -1, NULL, 0); free(list); |