]>
git.ipfire.org Git - people/stevee/pakfire.git/blob - tests/testsuite.h
1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2017 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #ifndef PAKFIRE_TESTSUITE_H
22 #define PAKFIRE_TESTSUITE_H
29 #include <pakfire/pakfire.h>
33 #define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/"
37 int (*func
)(const struct test
* t
);
38 struct pakfire
* pakfire
;
42 struct test tests
[MAX_TESTS
];
46 extern struct testsuite ts
;
48 int __testsuite_add_test(const char* name
, int (*func
)(const struct test
* t
));
49 int testsuite_run(int argc
, const char* argv
[]);
51 #define _LOG(prefix, fmt, ...) fprintf(stderr, "TESTS: " prefix fmt, ## __VA_ARGS__);
52 #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__);
53 #define LOG_WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__);
54 #define LOG_ERROR(fmt, ...) _LOG("ERROR: ", fmt, ## __VA_ARGS__);
56 #define testsuite_add_test(func) __testsuite_add_test(#func, func)
58 static inline size_t testsuite_array_length(char* array
[]) {
61 for (char** line
= array
; *line
; line
++)
67 static inline int testsuite_compare_string_arrays(char* array1
[], char* array2
[]) {
68 const size_t length1
= testsuite_array_length(array1
);
69 const size_t length2
= testsuite_array_length(array2
);
71 // Check if length matches
72 if (length1
!= length2
) {
73 LOG_ERROR("Arrays differ in length (%zu != %zu)\n", length1
, length2
);
77 char** line1
= array1
;
78 char** line2
= array2
;
81 // Check if all lines match
82 for (; *line1
&& *line2
; line1
++, line2
++, num
++) {
83 if (strcmp(*line1
, *line2
) != 0) {
84 LOG_ERROR("Line %zu does not match\n", num
);
94 for (line1
= array1
, line2
= array2
, num
= 0; *line1
&& *line2
; line1
++, line2
++, num
++) {
95 printf("Line %zu:\n");
96 printf(" 1: %s", *line1
);
97 printf(" 2: %s", *line2
);
103 #define ASSERT(expr) \
106 LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
107 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
112 #define ASSERT_NULL(expr) \
114 if (!((expr) == NULL)) { \
115 LOG_ERROR("Failed assertion: " #expr " == NULL %s:%d %s\n", \
116 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
121 #define ASSERT_SUCCESS(expr) \
124 LOG_ERROR("Failed assertion: %s (errno = %s) at %s:%d %s\n", \
125 #expr, strerror(errno), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
130 #define ASSERT_FAILURE(expr) \
133 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
134 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
139 #define ASSERT_TRUE(expr) \
142 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return true in %s:%d: %m\n", \
143 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
148 #define ASSERT_FALSE(expr) \
151 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return false in %s:%d: %m\n", \
152 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
157 #define ASSERT_ERRNO(expr, e) \
160 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
161 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
165 LOG_ERROR("Failed assertion: " #expr " failed with (%d - %s) " \
166 "but was expected to fail with (%d - %s) in %s:%d\n", \
167 errno, strerror(errno), e, strerror(e), __FILE__, __LINE__); \
172 #define ASSERT_EQUALS(value1, value2) \
174 if (value1 != value2) { \
175 LOG_ERROR("Failed assertion: " #value1 " (%lld) != " #value2 " (%lld) %s:%d %s\n", \
176 value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
181 #define ASSERT_STRING_EQUALS(value, string) \
184 LOG_ERROR("Failed assertion: %s is NULL, expected %s at %s:%d %s\n", \
185 #value, #string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
188 if (strcmp(string, value) != 0) { \
189 LOG_ERROR("Failed assertion: " #value " (%s) != " #string " (%s) %s:%d %s\n", \
190 value, string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
195 #define ASSERT_STRING_ARRAY_EQUALS(array1, array2) \
197 if (testsuite_compare_string_arrays(array1, array2)) { \
198 LOG_ERROR("Failed assertion: " #array1 " != " #array2 " %s:%d %s\n", \
199 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
204 #define ASSERT_STRING_NOT_EQUALS(value1, value2) \
206 if (strcmp(value1, value2) == 0) { \
207 LOG_ERROR("Failed assertion: " #value1 " (%s) != " #value2 " (%s) %s:%d %s\n", \
208 value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
213 #define ASSERT_STRING_STARTSWITH(string, start) \
215 if (strncmp(string, start, strlen(start)) != 0) { \
216 LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \
217 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
222 #define ASSERT_COMPARE(value1, value2, length) \
225 LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
226 #value1, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
230 LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
231 #value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
235 LOG_ERROR("Failed assertion: %s is zero at %s:%d %s\n", \
236 #length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
239 if (memcmp(value1, value2, length) != 0) { \
240 LOG_ERROR("Failed assertion: %s != %s (length = %zu) at %s:%d %s\n", \
241 #value1, #value2, length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
247 FILE* test_mktemp(char** path
);
248 char* test_mkdtemp(void);
250 #endif /* PAKFIRE_TESTSUITE_H */