dpkg 1.22.7-3-g89f48
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1/*
2 * libdpkg - Debian packaging suite library routines
3 * test.h - test suite support
4 *
5 * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef LIBDPKG_TEST_H
22#define LIBDPKG_TEST_H
23
24#include <string.h>
25#include <stdbool.h>
26#include <stdlib.h>
27#include <stdio.h>
28
29#include <dpkg/macros.h>
30#ifndef TEST_MAIN_CTOR
31#include <dpkg/ehandle.h>
32#define TEST_MAIN_CTOR push_error_context()
33#define TEST_MAIN_DTOR pop_error_context(ehflag_normaltidy)
34#endif
35
37
44#define test_bail(reason) \
45 do { \
46 printf("Bail out! %s\n", (reason)); \
47 exit(255); \
48 } while (0)
49
50#define test_xstringify(str) \
51 #str
52#define test_stringify(str) \
53 test_xstringify(str)
54
55static inline void *
56test_alloc(void *ptr, const char *reason)
57{
58 if (ptr == DPKG_NULL)
59 test_bail(reason);
60 return ptr;
61}
62
63#define test_alloc(ptr) \
64 test_alloc((ptr), "cannot allocate memory for " #ptr " in " __FILE__ ":" test_stringify(__LINE__))
65
66#define test_try(jmp) \
67 push_error_context_jump(&(jmp), DPKG_NULL, "test try"); \
68 if (!setjmp((jmp)))
69#define test_catch \
70 else
71#define test_finally \
72 pop_error_context(ehflag_normaltidy);
73
74static inline const char *
75test_get_envdir(const char *envvar)
76{
77 const char *envdir = getenv(envvar);
78 return envdir ? envdir : ".";
79}
80
81#define test_get_srcdir() \
82 test_get_envdir("srcdir")
83#define test_get_builddir() \
84 test_get_envdir("builddir")
85
86static inline char *
87test_data_file(const char *filename)
88{
89 char *pathname;
90 int rc;
91
92 rc = asprintf(&pathname, "%s/t/data/%s", test_get_srcdir(), filename);
93 if (rc < 0)
94 test_bail("cannot allocate data filename");
95
96 return pathname;
97}
98
99static inline bool
100test_is_verbose(void)
101{
102 const char *verbose = getenv("TEST_VERBOSE");
103 return verbose != DPKG_NULL && strcmp(verbose, "1") == 0;
104}
105
106#ifndef TEST_OMIT_VARIABLES
107static int test_id = 1;
108static int test_skip_code;
109static const char *test_skip_prefix;
110static const char *test_skip_reason;
111#endif
112
113#define test_plan(n) \
114 printf("1..%d\n", n);
115
116#define test_skip_all(reason) \
117 do { \
118 printf("1..0 # SKIP %s\n", (reason)); \
119 exit(0); \
120 } while (0)
121#define test_skip(reason) \
122 printf("ok %d # SKIP %s\n", test_id++, (reason))
123#define test_skip_block(cond) \
124 for (test_skip_prefix = " # SKIP ", \
125 test_skip_reason = cond ? #cond : DPKG_NULL, \
126 test_skip_code = 1; \
127 test_skip_prefix; \
128 test_skip_prefix = test_skip_reason = DPKG_NULL, \
129 test_skip_code = 0)
130
131#define test_todo(a, reason, desc) \
132 do { \
133 test_skip_prefix = " # TODO "; \
134 test_skip_reason = reason; \
135 test_case(a, "%s", desc); \
136 test_skip_prefix = test_skip_reason = DPKG_NULL; \
137 } while(0)
138#define test_todo_block(reason) \
139 for (test_skip_prefix = " # TODO ", test_skip_reason = reason; \
140 test_skip_prefix; \
141 test_skip_prefix = test_skip_reason = DPKG_NULL)
142
143#define test_case(a, fmt, ...) \
144 printf("%sok %d - " fmt "%s%s\n", \
145 test_skip_code || (a) ? "" : "not ", \
146 test_id++, __VA_ARGS__, \
147 test_skip_reason ? test_skip_prefix : "", \
148 test_skip_reason ? test_skip_reason : "")
149
150#define test_pass(a) \
151 test_case((a), "pass %s", #a)
152#define test_fail(a) \
153 test_case(!(a), "fail %s", #a)
154#define test_str(a, op, b) \
155 test_case(strcmp((a), (b)) op 0, "strcmp '%s' %s '%s'", a, #op, b)
156#define test_mem(a, op, b, size) \
157 test_case(memcmp((a), (b), (size)) op 0, "memcmp %p %s %p", a, #op, b)
158
159/* Specific test macros. */
160#define test_warn(e) \
161 do { \
162 test_pass((e).type == DPKG_MSG_WARN); \
163 dpkg_error_destroy(&(e)); \
164 } while (0)
165#define test_error(e) \
166 do { \
167 test_pass((e).type == DPKG_MSG_ERROR); \
168 dpkg_error_destroy(&(e)); \
169 } while (0)
170
174
175#define TEST_ENTRY(name) \
176static void name(void); \
177int \
178main(int argc, char **argv) \
179{ \
180 setvbuf(stdout, DPKG_NULL, _IOLBF, 0); \
181 \
182 TEST_MAIN_CTOR; \
183 name(); \
184 TEST_MAIN_DTOR; \
185 return 0; \
186} \
187static void \
188name(void)
189
190#endif
#define test_alloc(ptr)
Definition test.h:63
#define test_get_srcdir()
Definition test.h:81
#define test_bail(reason)
Definition test.h:44
#define DPKG_BEGIN_DECLS
Definition macros.h:164
#define DPKG_NULL
A null pointer constant that works on C or C++.
Definition macros.h:180
#define DPKG_END_DECLS
Definition macros.h:165