1 /*
2 * Copyright © 2004 Red Hat, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of
9 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Red Hat, Inc. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Author: Carl D. Worth <cworth@cworth.org>
24 */
25
26 #ifndef _CAIRO_TEST_H_
27 #define _CAIRO_TEST_H_
28
29 #include "cairo-boilerplate.h"
30
31 #include <stdarg.h>
32
33 CAIRO_BEGIN_DECLS
34
35 #if HAVE_STDINT_H
36 # include <stdint.h>
37 #elif HAVE_INTTYPES_H
38 # include <inttypes.h>
39 #elif HAVE_SYS_INT_TYPES_H
40 # include <sys/int_types.h>
41 #elif defined(_MSC_VER)
42 typedef __int8 int8_t;
43 typedef unsigned __int8 uint8_t;
44 typedef __int16 int16_t;
45 typedef unsigned __int16 uint16_t;
46 typedef __int32 int32_t;
47 typedef unsigned __int32 uint32_t;
48 typedef __int64 int64_t;
49 typedef unsigned __int64 uint64_t;
50 # ifndef HAVE_UINT64_T
51 # define HAVE_UINT64_T 1
52 # endif
53 #else
54 #error Cannot find definitions for fixed-width integral types (uint8_t, uint32_t, \etc.)
55 #endif
56
57 #ifdef _MSC_VER
58 #define _USE_MATH_DEFINES
59
60 #include <float.h>
61 #if _MSC_VER <= 1600
62 #define isnan(x) _isnan(x)
63 #endif
64
65 #endif
66
67 #if HAVE_FENV_H
68 # include <fenv.h>
69 #endif
70 /* The following are optional in C99, so define them if they aren't yet */
71 #ifndef FE_DIVBYZERO
72 #define FE_DIVBYZERO 0
73 #endif
74 #ifndef FE_INEXACT
75 #define FE_INEXACT 0
76 #endif
77 #ifndef FE_INVALID
78 #define FE_INVALID 0
79 #endif
80 #ifndef FE_OVERFLOW
81 #define FE_OVERFLOW 0
82 #endif
83 #ifndef FE_UNDERFLOW
84 #define FE_UNDERFLOW 0
85 #endif
86
87 #include <math.h>
88
89 static inline double
cairo_test_NaN(void)90 cairo_test_NaN (void)
91 {
92 #ifdef _MSC_VER
93 /* MSVC strtod("NaN", NULL) returns 0.0 */
94 union {
95 uint32_t i[2];
96 double d;
97 } nan = {{0xffffffff, 0x7fffffff}};
98 return nan.d;
99 #else
100 return strtod("NaN", NULL);
101 #endif
102 }
103
104 #ifndef MIN
105 #define MIN(a, b) ((a) < (b) ? (a) : (b))
106 #endif
107
108 #ifndef MAX
109 #define MAX(a, b) ((a) > (b) ? (a) : (b))
110 #endif
111
112 #define CAIRO_TEST_OUTPUT_DIR "output"
113
114 #define CAIRO_TEST_LOG_SUFFIX ".log"
115
116 #define CAIRO_TEST_FONT_FAMILY "DejaVu"
117
118 /* What is a fail and what isn't?
119 * When running the test suite we want to detect unexpected output. This
120 * can be caused by a change we have made to cairo itself, or a change
121 * in our environment. To capture this we classify the expected output into 3
122 * classes:
123 *
124 * REF -- Perfect output.
125 * Might be different for each backend, due to slight implementation
126 * differences.
127 *
128 * NEW -- A new failure. We have uncovered a bug within cairo and have
129 * recorded the current failure (along with the expected output
130 * if possible!) so we can detect any changes in our attempt to
131 * fix the bug.
132 *
133 * XFAIL -- An external failure. We believe the cairo output is perfect,
134 * but an external renderer is causing gross failure.
135 * (We also use this to capture current WONTFIX issues within cairo,
136 * such as overflow in internal coordinates, so as not to distract
137 * us when regression testing.)
138 *
139 * If no REF is given for a test, then it is assumed to be XFAIL.
140 */
141 #define CAIRO_TEST_REF_SUFFIX ".ref"
142 #define CAIRO_TEST_XFAIL_SUFFIX ".xfail"
143 #define CAIRO_TEST_NEW_SUFFIX ".new"
144
145 #define CAIRO_TEST_OUT_SUFFIX ".out"
146 #define CAIRO_TEST_DIFF_SUFFIX ".diff"
147
148 #define CAIRO_TEST_PNG_EXTENSION ".png"
149 #define CAIRO_TEST_OUT_PNG CAIRO_TEST_OUT_SUFFIX CAIRO_TEST_PNG_EXTENSION
150 #define CAIRO_TEST_REF_PNG CAIRO_TEST_REF_SUFFIX CAIRO_TEST_PNG_EXTENSION
151 #define CAIRO_TEST_DIFF_PNG CAIRO_TEST_DIFF_SUFFIX CAIRO_TEST_PNG_EXTENSION
152
153 typedef enum cairo_test_status {
154 CAIRO_TEST_SUCCESS = 0,
155 CAIRO_TEST_NO_MEMORY,
156 CAIRO_TEST_FAILURE,
157 CAIRO_TEST_NEW,
158 CAIRO_TEST_XFAILURE,
159 CAIRO_TEST_ERROR,
160 CAIRO_TEST_CRASHED,
161 CAIRO_TEST_UNTESTED = 77 /* match automake's skipped exit status */
162 } cairo_test_status_t;
163
164 typedef struct _cairo_test_context cairo_test_context_t;
165 typedef struct _cairo_test cairo_test_t;
166
167 typedef cairo_test_status_t
168 (cairo_test_preamble_function_t) (cairo_test_context_t *ctx);
169
170 typedef cairo_test_status_t
171 (cairo_test_draw_function_t) (cairo_t *cr, int width, int height);
172
173 struct _cairo_test {
174 const char *name;
175 const char *description;
176 const char *keywords;
177 const char *requirements;
178 double width;
179 double height;
180 cairo_test_preamble_function_t *preamble;
181 cairo_test_draw_function_t *draw;
182 };
183
184 /* The standard test interface which works by examining result image.
185 *
186 * CAIRO_TEST() constructs a test which will be called once before (the
187 * preamble callback), and then once for each testable backend (the draw
188 * callback). The following checks will be performed for each backend:
189 *
190 * 1) If preamble() returns CAIRO_TEST_UNTESTED, the test is skipped.
191 *
192 * 2) If preamble() does not return CAIRO_TEST_SUCCESS, the test fails.
193 *
194 * 3) If draw() does not return CAIRO_TEST_SUCCESS then this backend
195 * fails.
196 *
197 * 4) Otherwise, if cairo_status(cr) indicates an error then this
198 * backend fails.
199 *
200 * 5) Otherwise, if the image size is 0, then this backend passes.
201 *
202 * 6) Otherwise, if every channel of every pixel exactly matches the
203 * reference image then this backend passes. If not, this backend
204 * fails.
205 *
206 * The overall test result is PASS if and only if there is at least
207 * one backend that is tested and if all tested backend pass according
208 * to the four criteria above.
209 */
210 #define CAIRO_TEST(name, description, keywords, requirements, width, height, preamble, draw) \
211 void _register_##name (void); \
212 void _register_##name (void) { \
213 static cairo_test_t test = { \
214 #name, description, \
215 keywords, requirements, \
216 width, height, \
217 preamble, draw \
218 }; \
219 cairo_test_register (&test); \
220 }
221
222 void
223 cairo_test_register (cairo_test_t *test);
224
225 /* The full context for the test.
226 * For ordinary tests (using the CAIRO_TEST()->draw interface) the context
227 * is passed to the draw routine via user_data on the cairo_t.
228 * The reason why the context is not passed as an explicit parameter is that
229 * it is rarely required by the test itself and by removing the parameter
230 * we can keep the draw routines simple and serve as example code.
231 *
232 * In contrast, for the preamble phase the context is passed as the only
233 * parameter.
234 */
235 struct _cairo_test_context {
236 const cairo_test_t *test;
237 const char *test_name;
238
239 FILE *log_file;
240 const char *output;
241 const char *srcdir; /* directory containing sources and input data */
242 const char *refdir; /* directory containing reference images */
243
244 char *ref_name; /* cache of the current reference image */
245 cairo_surface_t *ref_image;
246 cairo_surface_t *ref_image_flattened;
247
248 size_t num_targets;
249 cairo_bool_t limited_targets;
250 const cairo_boilerplate_target_t **targets_to_test;
251 cairo_bool_t own_targets;
252
253 int malloc_failure;
254 int last_fault_count;
255
256 int timeout;
257 };
258
259 /* Retrieve the test context from the cairo_t, used for logging, paths etc */
260 const cairo_test_context_t *
261 cairo_test_get_context (cairo_t *cr);
262
263
264 /* Print a message to the log file, ala printf. */
265 void
266 cairo_test_log (const cairo_test_context_t *ctx,
267 const char *fmt, ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 3);
268 void
269 cairo_test_logv (const cairo_test_context_t *ctx,
270 const char *fmt, va_list ap) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 0);
271
272 /* Helper functions that take care of finding source images even when
273 * building in a non-srcdir manner, (i.e. the tests will be run in a
274 * directory that is different from the one where the source image
275 * exists). */
276 cairo_surface_t *
277 cairo_test_create_surface_from_png (const cairo_test_context_t *ctx,
278 const char *filename);
279
280 cairo_pattern_t *
281 cairo_test_create_pattern_from_png (const cairo_test_context_t *ctx,
282 const char *filename);
283
284 void
285 cairo_test_paint_checkered (cairo_t *cr);
286
287 #define CAIRO_TEST_DOUBLE_EQUALS(a,b) (fabs((a)-(b)) < 0.00001)
288
289 cairo_bool_t
290 cairo_test_is_target_enabled (const cairo_test_context_t *ctx,
291 const char *target);
292
293 char *
294 cairo_test_get_name (const cairo_test_t *test);
295
296 cairo_bool_t
297 cairo_test_malloc_failure (const cairo_test_context_t *ctx,
298 cairo_status_t status);
299
300 cairo_test_status_t
301 cairo_test_status_from_status (const cairo_test_context_t *ctx,
302 cairo_status_t status);
303
304 char *
305 cairo_test_reference_filename (const cairo_test_context_t *ctx,
306 const char *base_name,
307 const char *test_name,
308 const char *target_name,
309 const char *base_target_name,
310 const char *format,
311 const char *suffix,
312 const char *extension);
313
314 cairo_surface_t *
315 cairo_test_get_reference_image (cairo_test_context_t *ctx,
316 const char *filename,
317 cairo_bool_t flatten);
318
319 cairo_bool_t
320 cairo_test_mkdir (const char *path);
321
322 cairo_t *
323 cairo_test_create (cairo_surface_t *surface,
324 const cairo_test_context_t *ctx);
325
326 CAIRO_END_DECLS
327
328 #endif
329