aboutsummaryrefslogtreecommitdiff
path: root/tests/str_table.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-04-17 09:38:43 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-04-17 09:38:43 +0200
commit15250710c63a2c3d230304e46a03532f787759fb (patch)
tree9fda47ad94248d133b7b48c67eba20d357c6694e /tests/str_table.c
parent6b3e6d299d5298a5936dbba57f67cdfc4a406789 (diff)
tests: improve diagnostics output & make "release builds" work
This commit adds a few macros and helper functions for the unit test programs. Those are used instead of asserts to provide more fine grained diagnostics on the one hand and on the other hand because they also work if NDEBUG is defined, unlike asserts that get eliminated in that case. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/str_table.c')
-rw-r--r--tests/str_table.c43
1 files changed, 16 insertions, 27 deletions
diff --git a/tests/str_table.c b/tests/str_table.c
index 4e306b5..0935c89 100644
--- a/tests/str_table.c
+++ b/tests/str_table.c
@@ -6,13 +6,9 @@
*/
#include "config.h"
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-
#include "str_table.h"
#include "compat.h"
+#include "test.h"
#define STR(x) #x
#define STRVALUE(x) STR(x)
@@ -29,12 +25,7 @@ static int read_strings(void)
FILE *fp;
int i;
- fp = fopen("words.txt", "r");
-
- if (fp == NULL) {
- perror("words.txt");
- return -1;
- }
+ fp = test_open_read("words.txt");
for (i = 0; i < 1000; ++i) {
line = NULL;
@@ -64,41 +55,39 @@ int main(void)
size_t i, j, idx;
const char *str;
- assert(chdir(TEST_PATH) == 0);
+ TEST_ASSERT(chdir(TEST_PATH) == 0);
if (read_strings())
return EXIT_FAILURE;
- assert(str_table_init(&table, 64) == 0);
+ TEST_ASSERT(str_table_init(&table, 64) == 0);
for (i = 0; i < 1000; ++i) {
- assert(str_table_get_index(&table, strings[i], &idx) == 0);
+ TEST_ASSERT(str_table_get_index(&table, strings[i], &idx) == 0);
- assert(idx == i);
+ TEST_EQUAL_UI(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);
+ TEST_NOT_NULL(str);
+ TEST_ASSERT(str != strings[i]);
+ TEST_STR_EQUAL(str, strings[j]);
}
- for (; j < 1000; ++j) {
- str = str_table_get_string(&table, j);
- assert(str == NULL);
- }
+ for (; j < 1000; ++j)
+ TEST_NULL(str_table_get_string(&table, j));
}
for (i = 0; i < 1000; ++i) {
- assert(str_table_get_index(&table, strings[i], &idx) == 0);
- assert(idx == i);
+ TEST_ASSERT(str_table_get_index(&table, strings[i], &idx) == 0);
+ TEST_EQUAL_UI(idx, i);
str = str_table_get_string(&table, i);
- assert(str != NULL);
- assert(str != strings[i]);
- assert(strcmp(str, strings[i]) == 0);
+ TEST_NOT_NULL(str);
+ TEST_ASSERT(str != strings[i]);
+ TEST_STR_EQUAL(str, strings[i]);
}
str_table_cleanup(&table);