summaryrefslogtreecommitdiff
path: root/tests/str_table.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-19 21:44:34 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-19 21:44:34 +0200
commitdb652d246d7aa391107f1ede4693008dbf36c084 (patch)
tree94d66abc1997008eeac603e5cf40a7f14d34b542 /tests/str_table.c
parent00def9de1a38f0429b4c2c66127de00869622e1a (diff)
Add test case for string table
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/str_table.c')
-rw-r--r--tests/str_table.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/str_table.c b/tests/str_table.c
new file mode 100644
index 0000000..fad21b7
--- /dev/null
+++ b/tests/str_table.c
@@ -0,0 +1,103 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include <stdlib.h>
+#include <assert.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "str_table.h"
+
+#define STR(x) #x
+#define STRVALUE(x) STR(x)
+
+#define TEST_PATH STRVALUE(TESTPATH)
+
+static char *strings[1000];
+
+static int read_strings(void)
+{
+ ssize_t ret;
+ char *line;
+ size_t n;
+ FILE *fp;
+ int i;
+
+ fp = fopen("words.txt", "r");
+
+ if (fp == NULL) {
+ perror("words.txt");
+ return -1;
+ }
+
+ for (i = 0; i < 1000; ++i) {
+ line = NULL;
+ n = 0;
+
+ ret = getline(&line, &n, fp);
+ if (ret < 0) {
+ perror("reading words");
+ goto fail;
+ }
+
+ strings[i] = line;
+ }
+
+ fclose(fp);
+ return 0;
+fail:
+ for (i = 0; i < 1000; ++i)
+ free(strings[i]);
+ fclose(fp);
+ return -1;
+}
+
+int main(void)
+{
+ str_table_t table;
+ size_t i, j, idx;
+ const char *str;
+
+ assert(chdir(TEST_PATH) == 0);
+
+ if (read_strings())
+ return EXIT_FAILURE;
+
+ assert(str_table_init(&table, 64) == 0);
+
+ for (i = 0; i < 1000; ++i) {
+ assert(str_table_get_index(&table, strings[i], &idx) == 0);
+
+ assert(idx == i);
+
+ for (j = 0; j <= i; ++j) {
+ str = str_table_get_string(&table, j);
+
+ assert(str != NULL);
+ assert(str != strings[i]);
+ assert(strcmp(str, strings[j]) == 0);
+ }
+
+ for (; j < 1000; ++j) {
+ str = str_table_get_string(&table, j);
+ assert(str == NULL);
+ }
+ }
+
+ for (i = 0; i < 1000; ++i) {
+ assert(str_table_get_index(&table, strings[i], &idx) == 0);
+ assert(idx == i);
+
+ str = str_table_get_string(&table, i);
+
+ assert(str != NULL);
+ assert(str != strings[i]);
+ assert(strcmp(str, strings[i]) == 0);
+ }
+
+ str_table_cleanup(&table);
+
+ for (i = 0; i < 1000; ++i)
+ free(strings[i]);
+
+ return EXIT_SUCCESS;
+}