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