1 /* 2 * test-api.h 3 * test API macros and functions. 4 * 5 * SPDX-License-Identifier: pkgconf 6 * 7 * Copyright (c) 2026 pkgconf authors (see AUTHORS). 8 * 9 * Permission to use, copy, modify, and/or distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * This software is provided 'as is' and without any warranty, express or 14 * implied. In no event shall the authors be liable for any damages arising 15 * from the use of this software. 16 */ 17 18 #ifndef TEST_API_H 19 #define TEST_API_H 20 21 #include <libpkgconf/stdinc.h> 22 #include <libpkgconf/libpkgconf.h> 23 #include <libpkgconf/path.h> 24 #include <tests/win-shim.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 29 #if defined(__GNUC__) && !defined(__clang__) 30 # pragma GCC diagnostic push 31 # pragma GCC diagnostic ignored "-Wpragmas" // older gcc may not know the next one 32 // Stop false-positive warnings for test cases 33 # pragma GCC diagnostic ignored "-Wanalyzer-tainted-assertion" 34 #endif 35 36 #define TEST_FAIL_(fmt, ...) \ 37 do { \ 38 fprintf(stderr, "FAIL: %s:%d: " fmt "\n", \ 39 __FILE__, __LINE__, __VA_ARGS__); \ 40 exit(EXIT_FAILURE); \ 41 } while (0) 42 43 #define TEST_ASSERT_TRUE(expr) \ 44 do { \ 45 if (!(expr)) \ 46 TEST_FAIL_("TEST_ASSERT_TRUE(%s) was false", #expr); \ 47 } while (0) 48 49 #define TEST_ASSERT_FALSE(expr) \ 50 do { \ 51 if ((expr)) \ 52 TEST_FAIL_("TEST_ASSERT_FALSE(%s) was true", #expr); \ 53 } while (0) 54 55 #define TEST_ASSERT_NULL(expr) \ 56 do { \ 57 const void *_v = (const void *)(expr); \ 58 if (_v != NULL) \ 59 TEST_FAIL_("TEST_ASSERT_NULL(%s) was %p", #expr, _v); \ 60 } while (0) 61 62 #define TEST_ASSERT_NONNULL(expr) \ 63 do { \ 64 if ((expr) == NULL) \ 65 TEST_FAIL_("TEST_ASSERT_NONNULL(%s) was NULL", #expr); \ 66 } while (0) 67 68 #define TEST_ASSERT_EQ(a, b) \ 69 do { \ 70 long long _a = (long long)(a); \ 71 long long _b = (long long)(b); \ 72 if (_a != _b) \ 73 TEST_FAIL_("TEST_ASSERT_EQ(%s, %s): %lld != %lld", \ 74 #a, #b, _a, _b); \ 75 } while (0) 76 77 #define TEST_ASSERT_LT(a, b) \ 78 do { \ 79 long long _a = (long long)(a); \ 80 long long _b = (long long)(b); \ 81 if (_a >= _b) \ 82 TEST_FAIL_("TEST_ASSERT_LT(%s, %s): %lld >= %lld", \ 83 #a, #b, _a, _b); \ 84 } while (0) 85 86 #define TEST_ASSERT_LE(a, b) \ 87 do { \ 88 long long _a = (long long)(a); \ 89 long long _b = (long long)(b); \ 90 if (_a > _b) \ 91 TEST_FAIL_("TEST_ASSERT_LE(%s, %s): %lld > %lld", \ 92 #a, #b, _a, _b); \ 93 } while (0) 94 95 #define TEST_ASSERT_GT(a, b) \ 96 do { \ 97 long long _a = (long long)(a); \ 98 long long _b = (long long)(b); \ 99 if (_a <= _b) \ 100 TEST_FAIL_("TEST_ASSERT_GT(%s, %s): %lld <= %lld", \ 101 #a, #b, _a, _b); \ 102 } while (0) 103 104 #define TEST_ASSERT_GE(a, b) \ 105 do { \ 106 long long _a = (long long)(a); \ 107 long long _b = (long long)(b); \ 108 if (_a < _b) \ 109 TEST_FAIL_("TEST_ASSERT_GE(%s, %s): %lld < %lld", \ 110 #a, #b, _a, _b); \ 111 } while (0) 112 113 #define TEST_ASSERT_NE(a, b) \ 114 do { \ 115 long long _a = (long long)(a); \ 116 long long _b = (long long)(b); \ 117 if (_a == _b) \ 118 TEST_FAIL_("TEST_ASSERT_NE(%s, %s): both %lld", \ 119 #a, #b, _a); \ 120 } while (0) 121 122 #define TEST_ASSERT_STRCMP_EQ(actual, expected) \ 123 do { \ 124 const char *_a = (actual); \ 125 const char *_e = (expected); \ 126 if (_a == NULL || _e == NULL || strcmp(_a, _e) != 0) \ 127 TEST_FAIL_("TEST_ASSERT_STRCMP_EQ(%s, %s): [%s] != [%s]", \ 128 #actual, #expected, \ 129 _a ? _a : "(null)", _e ? _e : "(null)"); \ 130 } while (0) 131 132 #define TEST_ASSERT_STRCASECMP_EQ(actual, expected) \ 133 do { \ 134 const char *_a = (actual); \ 135 const char *_e = (expected); \ 136 if (_a == NULL || _e == NULL || strcasecmp(_a, _e) != 0) \ 137 TEST_FAIL_("TEST_ASSERT_STRCASECMP_EQ(%s, %s): [%s] !~ [%s]", \ 138 #actual, #expected, \ 139 _a ? _a : "(null)", _e ? _e : "(null)"); \ 140 } while (0) 141 142 #define TEST_ASSERT_STRNCMP_EQ(actual, expected, n) \ 143 do { \ 144 const char *_a = (actual); \ 145 const char *_e = (expected); \ 146 size_t _n = (size_t)(n); \ 147 if (_a == NULL || _e == NULL || strncmp(_a, _e, _n) != 0) \ 148 TEST_FAIL_("TEST_ASSERT_STRNCMP_EQ(%s, %s, %zu): [%s] != [%s]", \ 149 #actual, #expected, _n, \ 150 _a ? _a : "(null)", _e ? _e : "(null)"); \ 151 } while (0) 152 153 #define TEST_ASSERT_STRSTR(haystack, needle) \ 154 do { \ 155 const char *_h = (haystack); \ 156 const char *_n = (needle); \ 157 if (_h == NULL || _n == NULL || strstr(_h, _n) == NULL) \ 158 TEST_FAIL_("TEST_ASSERT_STRSTR(%s, %s): [%s] does not contain [%s]", \ 159 #haystack, #needle, \ 160 _h ? _h : "(null)", _n ? _n : "(null)"); \ 161 } while (0) 162 163 #define TEST_ASSERT_EMPTY_STRING(expr) \ 164 do { \ 165 const char *_s = (expr); \ 166 if (_s == NULL) \ 167 TEST_FAIL_("TEST_ASSERT_EMPTY_STRING(%s) was NULL", #expr); \ 168 if (_s[0] != '\0') \ 169 TEST_FAIL_("TEST_ASSERT_EMPTY_STRING(%s) was [%s]", #expr, _s); \ 170 } while (0) 171 172 #define TEST_RUN(name, fn) \ 173 do { \ 174 fprintf(stderr, "%s -> %s:", name, #fn); \ 175 fn(); \ 176 fprintf(stderr, " PASS\n"); \ 177 } while (0) 178 179 static inline pkgconf_client_t * 180 test_client_new(void) 181 { 182 pkgconf_cross_personality_t *pers = pkgconf_cross_personality_default(); 183 return pkgconf_client_new(NULL, NULL, pers, NULL, NULL); 184 } 185 186 #endif // TEST_API_H 187