From: Michael Tremer Date: Tue, 9 Aug 2022 15:34:32 +0000 (+0000) Subject: tests: Add check to compare string arrays X-Git-Tag: 0.9.28~541 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9edef5eec198bd5ac04baa51343f179c1529f367;p=pakfire.git tests: Add check to compare string arrays Signed-off-by: Michael Tremer --- diff --git a/tests/testsuite.h b/tests/testsuite.h index 353596761..fe9c3f3a7 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -55,6 +55,51 @@ int testsuite_run(int argc, const char* argv[]); #define testsuite_add_test(func) __testsuite_add_test(#func, func) +static inline size_t testsuite_array_length(char* array[]) { + size_t length = 0; + + for (char** line = array; *line; line++) + length++; + + return length; +} + +static inline int testsuite_compare_string_arrays(char* array1[], char* array2[]) { + const size_t length1 = testsuite_array_length(array1); + const size_t length2 = testsuite_array_length(array2); + + // Check if length matches + if (length1 != length2) { + LOG_ERROR("Arrays differ in length (%zu != %zu)\n", length1, length2); + goto ERROR; + } + + char** line1 = array1; + char** line2 = array2; + size_t num = 0; + + // Check if all lines match + for (; *line1 && *line2; line1++, line2++, num++) { + if (strcmp(*line1, *line2) != 0) { + LOG_ERROR("Line %zu does not match\n", num); + goto ERROR; + } + } + + // Success + return 0; + +ERROR: + // Dump both arrays + for (line1 = array1, line2 = array2, num = 0; *line1 && *line2; line1++, line2++, num++) { + printf("Line %zu:\n"); + printf(" 1: %s", *line1); + printf(" 2: %s", *line2); + } + + return 1; +} + #define ASSERT(expr) \ do { \ if ((!(expr))) { \ @@ -138,6 +183,15 @@ int testsuite_run(int argc, const char* argv[]); } \ } while (0) +#define ASSERT_STRING_ARRAY_EQUALS(array1, array2) \ + do { \ + if (testsuite_compare_string_arrays(array1, array2)) { \ + LOG_ERROR("Failed assertion: " #array1 " != " #array2 " %s:%d %s\n", \ + __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + goto FAIL; \ + } \ + } while(0) + #define ASSERT_STRING_NOT_EQUALS(value1, value2) \ do { \ if (strcmp(value1, value2) == 0) { \