Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / cairo-test.h
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 #define isnan(x) _isnan(x)
62
63 #endif
64
65 #include <math.h>
66
67 static inline double
68 cairo_test_NaN (void)
69 {
70 #ifdef _MSC_VER
71     /* MSVC strtod("NaN", NULL) returns 0.0 */
72     union {
73         uint32_t i[2];
74         double d;
75     } nan = {{0xffffffff, 0x7fffffff}};
76     return nan.d;
77 #else
78     return strtod("NaN", NULL);
79 #endif
80 }
81
82 #ifndef MIN
83 #define MIN(a, b) ((a) < (b) ? (a) : (b))
84 #endif
85
86 #ifndef MAX
87 #define MAX(a, b) ((a) > (b) ? (a) : (b))
88 #endif
89
90 #ifndef ARRAY_LENGTH
91 #define ARRAY_LENGTH(__array) ((int) (sizeof (__array) / sizeof (__array[0])))
92 #endif
93
94 #define CAIRO_TEST_OUTPUT_DIR "output"
95
96 #define CAIRO_TEST_LOG_SUFFIX ".log"
97
98 #define CAIRO_TEST_FONT_FAMILY "DejaVu"
99
100 /* What is a fail and what isn't?
101  * When running the test suite we want to detect unexpected output. This
102  * can be caused by a change we have made to cairo itself, or a change
103  * in our environment. To capture this we classify the expected output into 3
104  * classes:
105  *
106  *   REF  -- Perfect output.
107  *           Might be different for each backend, due to slight implementation
108  *           differences.
109  *
110  *   NEW  -- A new failure. We have uncovered a bug within cairo and have
111  *           recorded the current failure (along with the expected output
112  *           if possible!) so we can detect any changes in our attempt to
113  *           fix the bug.
114  *
115  *  XFAIL -- An external failure. We believe the cairo output is perfect,
116  *           but an external renderer is causing gross failure.
117  *           (We also use this to capture current WONTFIX issues within cairo,
118  *           such as overflow in internal coordinates, so as not to distract
119  *           us when regression testing.)
120  *
121  *  If no REF is given for a test, then it is assumed to be XFAIL.
122  */
123 #define CAIRO_TEST_REF_SUFFIX ".ref"
124 #define CAIRO_TEST_XFAIL_SUFFIX ".xfail"
125 #define CAIRO_TEST_NEW_SUFFIX ".new"
126
127 #define CAIRO_TEST_OUT_SUFFIX ".out"
128 #define CAIRO_TEST_DIFF_SUFFIX ".diff"
129
130 #define CAIRO_TEST_PNG_EXTENSION ".png"
131 #define CAIRO_TEST_OUT_PNG CAIRO_TEST_OUT_SUFFIX CAIRO_TEST_PNG_EXTENSION
132 #define CAIRO_TEST_REF_PNG CAIRO_TEST_REF_SUFFIX CAIRO_TEST_PNG_EXTENSION
133 #define CAIRO_TEST_DIFF_PNG CAIRO_TEST_DIFF_SUFFIX CAIRO_TEST_PNG_EXTENSION
134
135 typedef enum cairo_test_status {
136     CAIRO_TEST_SUCCESS = 0,
137     CAIRO_TEST_NO_MEMORY,
138     CAIRO_TEST_FAILURE,
139     CAIRO_TEST_NEW,
140     CAIRO_TEST_XFAILURE,
141     CAIRO_TEST_ERROR,
142     CAIRO_TEST_CRASHED,
143     CAIRO_TEST_UNTESTED = 77 /* match automake's skipped exit status */
144 } cairo_test_status_t;
145
146 typedef struct _cairo_test_context cairo_test_context_t;
147 typedef struct _cairo_test cairo_test_t;
148
149 typedef cairo_test_status_t
150 (cairo_test_preamble_function_t) (cairo_test_context_t *ctx);
151
152 typedef cairo_test_status_t
153 (cairo_test_draw_function_t) (cairo_t *cr, int width, int height);
154
155 struct _cairo_test {
156     const char *name;
157     const char *description;
158     const char *keywords;
159     const char *requirements;
160     double width;
161     double height;
162     cairo_test_preamble_function_t *preamble;
163     cairo_test_draw_function_t *draw;
164 };
165
166 /* The standard test interface which works by examining result image.
167  *
168  * CAIRO_TEST() constructs a test which will be called once before (the
169  * preamble callback), and then once for each testable backend (the draw
170  * callback). The following checks will be performed for each backend:
171  *
172  * 1) If preamble() returns CAIRO_TEST_UNTESTED, the test is skipped.
173  *
174  * 2) If preamble() does not return CAIRO_TEST_SUCCESS, the test fails.
175  *
176  * 3) If draw() does not return CAIRO_TEST_SUCCESS then this backend
177  *    fails.
178  *
179  * 4) Otherwise, if cairo_status(cr) indicates an error then this
180  *    backend fails.
181  *
182  * 5) Otherwise, if the image size is 0, then this backend passes.
183  *
184  * 6) Otherwise, if every channel of every pixel exactly matches the
185  *    reference image then this backend passes. If not, this backend
186  *    fails.
187  *
188  * The overall test result is PASS if and only if there is at least
189  * one backend that is tested and if all tested backend pass according
190  * to the four criteria above.
191  */
192 #define CAIRO_TEST(name, description, keywords, requirements, width, height, preamble, draw) \
193 void _register_##name (void); \
194 void _register_##name (void) { \
195     static cairo_test_t test = { \
196         #name, description, \
197         keywords, requirements, \
198         width, height, \
199         preamble, draw \
200     }; \
201     cairo_test_register (&test); \
202 }
203
204 void
205 cairo_test_register (cairo_test_t *test);
206
207 /* The full context for the test.
208  * For ordinary tests (using the CAIRO_TEST()->draw interface) the context
209  * is passed to the draw routine via user_data on the cairo_t.
210  * The reason why the context is not passed as an explicit parameter is that
211  * it is rarely required by the test itself and by removing the parameter
212  * we can keep the draw routines simple and serve as example code.
213  *
214  * In contrast, for the preamble phase the context is passed as the only
215  * parameter.
216  */
217 struct _cairo_test_context {
218     const cairo_test_t *test;
219     const char *test_name;
220
221     FILE *log_file;
222     const char *output;
223     const char *srcdir; /* directory containing sources and input data */
224     const char *refdir; /* directory containing reference images */
225
226     char *ref_name; /* cache of the current reference image */
227     cairo_surface_t *ref_image;
228     cairo_surface_t *ref_image_flattened;
229
230     size_t num_targets;
231     cairo_bool_t limited_targets;
232     const cairo_boilerplate_target_t **targets_to_test;
233     cairo_bool_t own_targets;
234
235     int malloc_failure;
236     int last_fault_count;
237
238     int timeout;
239 };
240
241 /* Retrieve the test context from the cairo_t, used for logging, paths etc */
242 const cairo_test_context_t *
243 cairo_test_get_context (cairo_t *cr);
244
245
246 /* Print a message to the log file, ala printf. */
247 void
248 cairo_test_log (const cairo_test_context_t *ctx,
249                 const char *fmt, ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 3);
250 void
251 cairo_test_logv (const cairo_test_context_t *ctx,
252                 const char *fmt, va_list ap) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 0);
253
254 /* Helper functions that take care of finding source images even when
255  * building in a non-srcdir manner, (i.e. the tests will be run in a
256  * directory that is different from the one where the source image
257  * exists). */
258 cairo_surface_t *
259 cairo_test_create_surface_from_png (const cairo_test_context_t *ctx,
260                                     const char *filename);
261
262 cairo_pattern_t *
263 cairo_test_create_pattern_from_png (const cairo_test_context_t *ctx,
264                                     const char *filename);
265
266 void
267 cairo_test_paint_checkered (cairo_t *cr);
268
269 #define CAIRO_TEST_DOUBLE_EQUALS(a,b)  (fabs((a)-(b)) < 0.00001)
270
271 cairo_bool_t
272 cairo_test_is_target_enabled (const cairo_test_context_t *ctx,
273                               const char *target);
274
275 char *
276 cairo_test_get_name (const cairo_test_t *test);
277
278 cairo_bool_t
279 cairo_test_malloc_failure (const cairo_test_context_t *ctx,
280                            cairo_status_t status);
281
282 cairo_test_status_t
283 cairo_test_status_from_status (const cairo_test_context_t *ctx,
284                                cairo_status_t status);
285
286 char *
287 cairo_test_reference_filename (const cairo_test_context_t *ctx,
288                                const char *base_name,
289                                const char *test_name,
290                                const char *target_name,
291                                const char *base_target_name,
292                                const char *format,
293                                const char *suffix,
294                                const char *extension);
295
296 cairo_surface_t *
297 cairo_test_get_reference_image (cairo_test_context_t *ctx,
298                                 const char *filename,
299                                 cairo_bool_t flatten);
300
301 CAIRO_END_DECLS
302
303 #endif