2 * Copyright © 2004 Red Hat, Inc.
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.
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.
23 * Author: Carl D. Worth <cworth@cworth.org>
26 #ifndef _CAIRO_TEST_H_
27 #define _CAIRO_TEST_H_
29 #include "cairo-boilerplate.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
54 #error Cannot find definitions for fixed-width integral types (uint8_t, uint32_t, \etc.)
58 #define _USE_MATH_DEFINES
61 #define isnan(x) _isnan(x)
68 /* The following are optional in C99, so define them if they aren't yet */
70 #define FE_DIVBYZERO 0
82 #define FE_UNDERFLOW 0
91 /* MSVC strtod("NaN", NULL) returns 0.0 */
95 } nan = {{0xffffffff, 0x7fffffff}};
98 return strtod("NaN", NULL);
103 #define MIN(a, b) ((a) < (b) ? (a) : (b))
107 #define MAX(a, b) ((a) > (b) ? (a) : (b))
110 #define CAIRO_TEST_OUTPUT_DIR "output"
112 #define CAIRO_TEST_LOG_SUFFIX ".log"
114 #define CAIRO_TEST_FONT_FAMILY "DejaVu"
116 /* What is a fail and what isn't?
117 * When running the test suite we want to detect unexpected output. This
118 * can be caused by a change we have made to cairo itself, or a change
119 * in our environment. To capture this we classify the expected output into 3
122 * REF -- Perfect output.
123 * Might be different for each backend, due to slight implementation
126 * NEW -- A new failure. We have uncovered a bug within cairo and have
127 * recorded the current failure (along with the expected output
128 * if possible!) so we can detect any changes in our attempt to
131 * XFAIL -- An external failure. We believe the cairo output is perfect,
132 * but an external renderer is causing gross failure.
133 * (We also use this to capture current WONTFIX issues within cairo,
134 * such as overflow in internal coordinates, so as not to distract
135 * us when regression testing.)
137 * If no REF is given for a test, then it is assumed to be XFAIL.
139 #define CAIRO_TEST_REF_SUFFIX ".ref"
140 #define CAIRO_TEST_XFAIL_SUFFIX ".xfail"
141 #define CAIRO_TEST_NEW_SUFFIX ".new"
143 #define CAIRO_TEST_OUT_SUFFIX ".out"
144 #define CAIRO_TEST_DIFF_SUFFIX ".diff"
146 #define CAIRO_TEST_PNG_EXTENSION ".png"
147 #define CAIRO_TEST_OUT_PNG CAIRO_TEST_OUT_SUFFIX CAIRO_TEST_PNG_EXTENSION
148 #define CAIRO_TEST_REF_PNG CAIRO_TEST_REF_SUFFIX CAIRO_TEST_PNG_EXTENSION
149 #define CAIRO_TEST_DIFF_PNG CAIRO_TEST_DIFF_SUFFIX CAIRO_TEST_PNG_EXTENSION
151 typedef enum cairo_test_status {
152 CAIRO_TEST_SUCCESS = 0,
153 CAIRO_TEST_NO_MEMORY,
159 CAIRO_TEST_UNTESTED = 77 /* match automake's skipped exit status */
160 } cairo_test_status_t;
162 typedef struct _cairo_test_context cairo_test_context_t;
163 typedef struct _cairo_test cairo_test_t;
165 typedef cairo_test_status_t
166 (cairo_test_preamble_function_t) (cairo_test_context_t *ctx);
168 typedef cairo_test_status_t
169 (cairo_test_draw_function_t) (cairo_t *cr, int width, int height);
173 const char *description;
174 const char *keywords;
175 const char *requirements;
178 cairo_test_preamble_function_t *preamble;
179 cairo_test_draw_function_t *draw;
182 /* The standard test interface which works by examining result image.
184 * CAIRO_TEST() constructs a test which will be called once before (the
185 * preamble callback), and then once for each testable backend (the draw
186 * callback). The following checks will be performed for each backend:
188 * 1) If preamble() returns CAIRO_TEST_UNTESTED, the test is skipped.
190 * 2) If preamble() does not return CAIRO_TEST_SUCCESS, the test fails.
192 * 3) If draw() does not return CAIRO_TEST_SUCCESS then this backend
195 * 4) Otherwise, if cairo_status(cr) indicates an error then this
198 * 5) Otherwise, if the image size is 0, then this backend passes.
200 * 6) Otherwise, if every channel of every pixel exactly matches the
201 * reference image then this backend passes. If not, this backend
204 * The overall test result is PASS if and only if there is at least
205 * one backend that is tested and if all tested backend pass according
206 * to the four criteria above.
208 #define CAIRO_TEST(name, description, keywords, requirements, width, height, preamble, draw) \
209 void _register_##name (void); \
210 void _register_##name (void) { \
211 static cairo_test_t test = { \
212 #name, description, \
213 keywords, requirements, \
217 cairo_test_register (&test); \
221 cairo_test_register (cairo_test_t *test);
223 /* The full context for the test.
224 * For ordinary tests (using the CAIRO_TEST()->draw interface) the context
225 * is passed to the draw routine via user_data on the cairo_t.
226 * The reason why the context is not passed as an explicit parameter is that
227 * it is rarely required by the test itself and by removing the parameter
228 * we can keep the draw routines simple and serve as example code.
230 * In contrast, for the preamble phase the context is passed as the only
233 struct _cairo_test_context {
234 const cairo_test_t *test;
235 const char *test_name;
239 const char *srcdir; /* directory containing sources and input data */
240 const char *refdir; /* directory containing reference images */
242 char *ref_name; /* cache of the current reference image */
243 cairo_surface_t *ref_image;
244 cairo_surface_t *ref_image_flattened;
247 cairo_bool_t limited_targets;
248 const cairo_boilerplate_target_t **targets_to_test;
249 cairo_bool_t own_targets;
252 int last_fault_count;
257 /* Retrieve the test context from the cairo_t, used for logging, paths etc */
258 const cairo_test_context_t *
259 cairo_test_get_context (cairo_t *cr);
262 /* Print a message to the log file, ala printf. */
264 cairo_test_log (const cairo_test_context_t *ctx,
265 const char *fmt, ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 3);
267 cairo_test_logv (const cairo_test_context_t *ctx,
268 const char *fmt, va_list ap) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 0);
270 /* Helper functions that take care of finding source images even when
271 * building in a non-srcdir manner, (i.e. the tests will be run in a
272 * directory that is different from the one where the source image
275 cairo_test_create_surface_from_png (const cairo_test_context_t *ctx,
276 const char *filename);
279 cairo_test_create_pattern_from_png (const cairo_test_context_t *ctx,
280 const char *filename);
283 cairo_test_paint_checkered (cairo_t *cr);
285 #define CAIRO_TEST_DOUBLE_EQUALS(a,b) (fabs((a)-(b)) < 0.00001)
288 cairo_test_is_target_enabled (const cairo_test_context_t *ctx,
292 cairo_test_get_name (const cairo_test_t *test);
295 cairo_test_malloc_failure (const cairo_test_context_t *ctx,
296 cairo_status_t status);
299 cairo_test_status_from_status (const cairo_test_context_t *ctx,
300 cairo_status_t status);
303 cairo_test_reference_filename (const cairo_test_context_t *ctx,
304 const char *base_name,
305 const char *test_name,
306 const char *target_name,
307 const char *base_target_name,
310 const char *extension);
313 cairo_test_get_reference_image (cairo_test_context_t *ctx,
314 const char *filename,
315 cairo_bool_t flatten);
318 cairo_test_mkdir (const char *path);