]> git.ipfire.org Git - pakfire.git/commitdiff
tests: Add check to compare string arrays
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 15:34:32 +0000 (15:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 15:34:32 +0000 (15:34 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/testsuite.h

index 3535967610f975de6a73dce8f2a08b6321236327..fe9c3f3a7f797f4a69e24d05d457289a184906f9 100644 (file)
@@ -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) { \