dpkg 1.21.11
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 bool
87test_is_verbose(void)
88{
89 const char *verbose = getenv("TEST_VERBOSE");
90 return verbose != DPKG_NULL && strcmp(verbose, "1") == 0;
91}
92
93#ifndef TEST_OMIT_VARIABLES
94static int test_id = 1;
95static int test_skip_code;
96static const char *test_skip_prefix;
97static const char *test_skip_reason;
98#endif
99
100#define test_plan(n) \
101 printf("1..%d\n", n);
102
103#define test_skip_all(reason) \
104 do { \
105 printf("1..0 # SKIP %s\n", (reason)); \
106 exit(0); \
107 } while (0)
108#define test_skip(reason) \
109 printf("ok %d # SKIP %s\n", test_id++, (reason))
110#define test_skip_block(cond) \
111 for (test_skip_prefix = " # SKIP ", \
112 test_skip_reason = cond ? #cond : DPKG_NULL, \
113 test_skip_code = 1; \
114 test_skip_prefix; \
115 test_skip_prefix = test_skip_reason = DPKG_NULL, \
116 test_skip_code = 0)
117
118#define test_todo(a, reason, desc) \
119 do { \
120 test_skip_prefix = " # TODO "; \
121 test_skip_reason = reason; \
122 test_case(a, "%s", desc); \
123 test_skip_prefix = test_skip_reason = DPKG_NULL; \
124 } while(0)
125#define test_todo_block(reason) \
126 for (test_skip_prefix = " # TODO ", test_skip_reason = reason; \
127 test_skip_prefix; \
128 test_skip_prefix = test_skip_reason = DPKG_NULL)
129
130#define test_case(a, fmt, ...) \
131 printf("%sok %d - " fmt "%s%s\n", \
132 test_skip_code || (a) ? "" : "not ", \
133 test_id++, __VA_ARGS__, \
134 test_skip_reason ? test_skip_prefix : "", \
135 test_skip_reason ? test_skip_reason : "")
136
137#define test_pass(a) \
138 test_case((a), "pass %s", #a)
139#define test_fail(a) \
140 test_case(!(a), "fail %s", #a)
141#define test_str(a, op, b) \
142 test_case(strcmp((a), (b)) op 0, "strcmp '%s' %s '%s'", a, #op, b)
143#define test_mem(a, op, b, size) \
144 test_case(memcmp((a), (b), (size)) op 0, "memcmp %p %s %p", a, #op, b)
145
146/* Specific test macros. */
147#define test_warn(e) \
148 do { \
149 test_pass((e).type == DPKG_MSG_WARN); \
150 dpkg_error_destroy(&(e)); \
151 } while (0)
152#define test_error(e) \
153 do { \
154 test_pass((e).type == DPKG_MSG_ERROR); \
155 dpkg_error_destroy(&(e)); \
156 } while (0)
157
161
162#define TEST_ENTRY(name) \
163static void name(void); \
164int \
165main(int argc, char **argv) \
166{ \
167 setvbuf(stdout, DPKG_NULL, _IOLBF, 0); \
168 \
169 TEST_MAIN_CTOR; \
170 name(); \
171 TEST_MAIN_DTOR; \
172 return 0; \
173} \
174static void \
175name(void)
176
177#endif
#define test_alloc(ptr)
Definition: test.h:63
#define test_bail(reason)
Definition: test.h:44
#define DPKG_BEGIN_DECLS
Definition: macros.h:86
#define DPKG_NULL
A null pointer constant that works on C or C++.
Definition: macros.h:102
#define DPKG_END_DECLS
Definition: macros.h:87