From 0366952bae967a1b69c510f8e633c0567ccab4a9 Mon Sep 17 00:00:00 2001 From: axtloss Date: Mon, 15 Jul 2024 03:24:14 +0200 Subject: add function join_str --- tests/test.c | 83 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 29 deletions(-) (limited to 'tests') diff --git a/tests/test.c b/tests/test.c index b58dfe4..0b19f97 100644 --- a/tests/test.c +++ b/tests/test.c @@ -23,6 +23,7 @@ test_malloc_free_secure (size_t size) int test_fcopy() { + int errno = 0; char *buf_a; char *buf_b; size_t len_a; @@ -38,74 +39,91 @@ test_fcopy() fcopy (file_test_a, file_test_b); fclose (file_test_b); - if (memcmp(buf_a, buf_b, len_a) != 0) { - free_secure ((void**)&buf_a, len_a); - free_secure ((void **)&buf_b, len_b); - return 1; - } + if (memcmp(buf_a, buf_b, len_a) != 0) + errno = 1; + free_secure ((void**)&buf_a, len_a); free_secure ((void **)&buf_b, len_b); - return 0; + return errno; } int test_strlwr_strupr () { - + int errno = 0; char *test_string = strdup ("hello"); char *uppr_string = strupr (test_string); char *lower_string = strlwr (uppr_string); - if (strcmp (uppr_string, "HELLO") != 0) { - free_secure ((void **)&test_string, strlen (test_string)); - return 1; - } - if (strcmp (lower_string, "hello") != 0) { - free_secure ((void **)&test_string, strlen (test_string)); - return 2; - } + if (strcmp (uppr_string, "HELLO") != 0) + errno = 1; + + if (strcmp (lower_string, "hello") != 0) + errno = 2; + + EXIT: free_secure ((void **)&test_string, strlen (test_string)); - return 0; + return errno; } int test_trim() { #undef free + int errno = 0; char *test_string = strdup ("\t\thi\t\t"); int rem_front = 0; int rem_back = 0; char *trimmed_string = trim (test_string, &rem_front, &rem_back); if (strstr (trimmed_string, "\t") != NULL) { - free (trimmed_string-rem_front); - free_secure ((void**)&test_string, strlen (test_string)); - return 1; + errno = 1; + goto EXIT; } if (rem_front != 2 && rem_back != 2) { - free (trimmed_string-rem_front); - free_secure ((void**)&test_string, strlen (test_string)); - return 2; + errno = 2; + goto EXIT; } + + EXIT: free (trimmed_string-rem_front); free_secure ((void**)&test_string, strlen (test_string)); - return 0; + return errno; } int test_replace_str () { + int errno = 0; char *test_string = strdup ("replace world!"); char *replaced_string = replace_str (test_string, "replace", "hello"); - if (strcmp (replaced_string, "hello world!") != 0) { - free_secure ((void**)&test_string, strlen (test_string)); - free_secure ((void**)&replaced_string, strlen (replaced_string)); - return 1; - } + if (strcmp (replaced_string, "hello world!") != 0) + errno = 1; + free_secure ((void**)&test_string, strlen (test_string)); free_secure ((void**)&replaced_string, strlen (replaced_string)); - return 0; + return errno; } +int +test_join_str () +{ + int errno = 0; + char **test_string = malloc (sizeof (char*) * 4); + for (int i = 0; i < 3; i++) + test_string [i] = strdup ("hello"); + + char *joined_string = join_str (test_string, 3, ' '); + + if (strcmp (joined_string, "hello hello hello") != 0) + errno = 1; + + free_secure ((void**)&test_string[0], strlen (test_string[0])); + free_secure ((void**)&test_string[1], strlen (test_string[1])); + free_secure ((void**)&test_string[2], strlen (test_string[2])); + free (test_string); + free_secure ((void**)&joined_string, strlen (joined_string)); + return errno; +} int main (void) @@ -147,5 +165,12 @@ main (void) printf ("Test Case 5: test_replace_str -- SUCCESS\n\n"); else printf ("Test Case 5: test_replace_str -- FAILED %d\n\n", test_5_result); + + printf ("Test Case 6: test_join_str -- join_str\n"); + int test_6_result = test_join_str(); + if (test_6_result == 0) + printf ("Test Case 6: test_join_str -- SUCCESS\n\n"); + else + printf ("Test Case 6: test_join_str -- FAILED %d\n\n", test_6_result); return 0; } -- cgit v1.2.3