]>
git.ipfire.org Git - 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/ctx.h>
30 #include <pakfire/httpclient.h>
31 #include <pakfire/pakfire.h>
35 #define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/"
38 TEST_WANTS_PAKFIRE
= (1 << 0),
39 TEST_WANTS_HTTPCLIENT
= (1 << 1),
44 int (*func
)(const struct test
* t
);
48 struct pakfire_ctx
* ctx
;
51 struct pakfire
* pakfire
;
54 struct pakfire_httpclient
* httpclient
;
58 struct test tests
[MAX_TESTS
];
62 extern struct testsuite ts
;
64 int __testsuite_add_test(const char* name
, int (*func
)(const struct test
* t
), int flags
);
65 int testsuite_run(int argc
, const char* argv
[]);
67 #define _LOG(prefix, fmt, ...) fprintf(stderr, "TESTS: " prefix fmt, ## __VA_ARGS__);
68 #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__);
69 #define LOG_WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__);
70 #define LOG_ERROR(fmt, ...) _LOG("ERROR: ", fmt, ## __VA_ARGS__);
72 #define testsuite_add_test(func, flags) __testsuite_add_test(#func, func, flags)
74 static inline size_t testsuite_array_length(char* array
[]) {
77 for (char** line
= array
; *line
; line
++)
83 static inline int testsuite_compare_string_arrays(char* array1
[], char* array2
[]) {
84 const size_t length1
= testsuite_array_length(array1
);
85 const size_t length2
= testsuite_array_length(array2
);
87 // Check if length matches
88 if (length1
!= length2
) {
89 LOG_ERROR("Arrays differ in length (%zu != %zu)\n", length1
, length2
);
93 char** line1
= array1
;
94 char** line2
= array2
;
97 // Check if all lines match
98 for (; *line1
&& *line2
; line1
++, line2
++, num
++) {
99 if (strcmp(*line1
, *line2
) != 0) {
100 LOG_ERROR("Line %zu does not match\n", num
);
110 for (line1
= array1
, line2
= array2
, num
= 0; *line1
&& *line2
; line1
++, line2
++, num
++) {
111 printf("Line %zu:\n");
112 printf(" 1: %s", *line1
);
113 printf(" 2: %s", *line2
);
119 #define ASSERT(expr) \
122 LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
123 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
128 #define ASSERT_NULL(expr) \
130 if (!((expr) == NULL)) { \
131 LOG_ERROR("Failed assertion: " #expr " == NULL %s:%d %s\n", \
132 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
137 #define ASSERT_SUCCESS(expr) \
140 LOG_ERROR("Failed assertion: %s (errno = %s) at %s:%d %s\n", \
141 #expr, strerror(errno), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
146 #define ASSERT_FAILURE(expr) \
149 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
150 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
155 #define ASSERT_TRUE(expr) \
158 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return true in %s:%d: %m\n", \
159 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
164 #define ASSERT_FALSE(expr) \
167 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't return false in %s:%d: %m\n", \
168 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
173 #define ASSERT_ERRNO(expr, e) \
176 LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \
177 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
181 LOG_ERROR("Failed assertion: " #expr " failed with (%d - %s) " \
182 "but was expected to fail with (%d - %s) in %s:%d\n", \
183 errno, strerror(errno), e, strerror(e), __FILE__, __LINE__); \
188 #define ASSERT_EQUALS(value1, value2) \
190 if (value1 != value2) { \
191 LOG_ERROR("Failed assertion: " #value1 " (%lld) != " #value2 " (%lld) %s:%d %s\n", \
192 value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
197 #define ASSERT_STRING_EQUALS(value, string) \
200 LOG_ERROR("Failed assertion: %s is NULL, expected %s at %s:%d %s\n", \
201 #value, #string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
204 if (strcmp(string, value) != 0) { \
205 LOG_ERROR("Failed assertion: " #value " (%s) != " #string " (%s) %s:%d %s\n", \
206 value, string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
211 #define ASSERT_STRING_ARRAY_EQUALS(array1, array2) \
213 if (testsuite_compare_string_arrays(array1, array2)) { \
214 LOG_ERROR("Failed assertion: " #array1 " != " #array2 " %s:%d %s\n", \
215 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
220 #define ASSERT_STRING_NOT_EQUALS(value1, value2) \
222 if (strcmp(value1, value2) == 0) { \
223 LOG_ERROR("Failed assertion: " #value1 " (%s) != " #value2 " (%s) %s:%d %s\n", \
224 value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
229 #define ASSERT_STRING_STARTSWITH(string, start) \
231 if (strncmp(string, start, strlen(start)) != 0) { \
232 LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \
233 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
238 #define ASSERT_COMPARE(value1, value2, length) \
241 LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
242 #value1, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
246 LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
247 #value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
251 LOG_ERROR("Failed assertion: %s is zero at %s:%d %s\n", \
252 #length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
255 if (memcmp(value1, value2, length) != 0) { \
256 LOG_ERROR("Failed assertion: %s != %s (length = %zu) at %s:%d %s\n", \
257 #value1, #value2, length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
263 FILE* test_mktemp(char** path
);
264 char* test_mkdtemp(void);
266 #endif /* PAKFIRE_TESTSUITE_H */