From 383717a8c7f67fd321fb078350ea9063bd872133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Svensson?= Date: Tue, 12 Oct 2021 11:34:05 +0200 Subject: [PATCH] Update the faulty access macros for str_array Adds a testcase for the usage of the access macros, and also updates the testsuite to only include what str_array requires. This shows that str_array can be used separately by users. --- include/str_array.h | 6 ++--- tests/check_str_array.c | 55 +++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/include/str_array.h b/include/str_array.h index aa10906..309d39b 100644 --- a/include/str_array.h +++ b/include/str_array.h @@ -32,8 +32,8 @@ void str_array_dump_slugs(const str_array *l); void str_array_dump(const str_array *l); -#define str_array_fetch(t,i) t->tokens[i] -#define str_array_len(t) t->len -#define str_array_cap(t) t->cap +#define str_array_fetch(t,i) t->tokens.entries[i] +#define str_array_len(t) t->tokens.size +#define str_array_cap(t) t->tokens.capacity #endif /* !STR_ARRAY_H */ diff --git a/tests/check_str_array.c b/tests/check_str_array.c index d9c471d..231b0ac 100644 --- a/tests/check_str_array.c +++ b/tests/check_str_array.c @@ -8,31 +8,59 @@ #include #include #include -#include "r3.h" -#include "r3_slug.h" +#include "str_array.h" START_TEST (test_str_array) { - match_entry * entry = match_entry_create("/foo"); - ck_assert(entry); + str_array *vars = r3_mem_alloc(sizeof(str_array)); + memset(vars, 0, sizeof(*vars)); char *test_str = "abc"; - ck_assert( str_array_append(&entry->vars, test_str, strlen(test_str))); - ck_assert( entry->vars.tokens.size == 1 ); + ck_assert( str_array_append(vars, test_str, strlen(test_str))); + ck_assert( vars->tokens.size == 1 ); char *test_str1 = "foo"; - ck_assert( str_array_append(&entry->vars, test_str1, strlen(test_str))); - ck_assert( entry->vars.tokens.size == 2 ); + ck_assert( str_array_append(vars, test_str1, strlen(test_str1))); + ck_assert( vars->tokens.size == 2 ); char *test_str2 = "bar"; - ck_assert( str_array_append(&entry->vars, test_str2, strlen(test_str))); - ck_assert( entry->vars.tokens.size == 3 ); + ck_assert( str_array_append(vars, test_str2, strlen(test_str2))); + ck_assert( vars->tokens.size == 3 ); char *test_str3 = "zoo"; - ck_assert( str_array_append(&entry->vars, test_str3, strlen(test_str))); - ck_assert( entry->vars.tokens.size == 4 ); + ck_assert( str_array_append(vars, test_str3, strlen(test_str3))); + ck_assert( vars->tokens.size == 4 ); - match_entry_free(entry); + str_array_free(vars); + free(vars); +} +END_TEST + +START_TEST (test_access_macros) +{ + str_array *vars = r3_mem_alloc(sizeof(str_array)); + memset(vars, 0, sizeof(*vars)); + ck_assert( str_array_len(vars) == 0); + ck_assert( str_array_cap(vars) == 0); + + r3_vector_reserve(NULL, &vars->tokens, 4); + ck_assert( str_array_len(vars) == 0); + ck_assert( str_array_cap(vars) == 4); + + char *token1 = "first"; + char *token2 = "second"; + ck_assert( str_array_append(vars, token1, strlen(token1))); + ck_assert( str_array_append(vars, token2, strlen(token2))); + ck_assert( str_array_len(vars) == 2); + ck_assert( str_array_cap(vars) == 4); + + ck_assert( strncmp(str_array_fetch(vars,0).base, "first", 5) == 0); + ck_assert( str_array_fetch(vars,0).len == 5); + ck_assert( strncmp(str_array_fetch(vars,1).base, "second", 6) == 0); + ck_assert( str_array_fetch(vars,1).len == 6); + + str_array_free(vars); + free(vars); } END_TEST @@ -40,6 +68,7 @@ Suite* r3_suite (void) { Suite *suite = suite_create("str_array test"); TCase *tcase = tcase_create("testcase"); tcase_add_test(tcase, test_str_array); + tcase_add_test(tcase, test_access_macros); suite_add_tcase(suite, tcase); return suite; }