From db652d246d7aa391107f1ede4693008dbf36c084 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 19 Jul 2019 21:44:34 +0200 Subject: Add test case for string table Signed-off-by: David Oberhollenzer --- tests/str_table.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tests/str_table.c (limited to 'tests/str_table.c') 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 +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3