[test] Add test case for a leaky dashed rectangle.
[cairo/haiku.git] / test / copy-path.c
blob362bb34d3ae9848b2e41ed23c4e67efe21ae269d
1 /*
2 * Copyright © 2005 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 #include <stdlib.h>
27 #include "cairo-test.h"
29 static cairo_test_draw_function_t draw;
31 cairo_test_t test = {
32 "copy-path",
33 "Tests calls to path_data functions: cairo_copy_path, cairo_copy_path_flat, and cairo_append_path",
34 45, 53,
35 draw
38 static void
39 scale_by_two (double *x, double *y)
41 *x = *x * 2.0;
42 *y = *y * 2.0;
45 typedef void (*munge_func_t) (double *x, double *y);
47 static void
48 munge_and_set_path (cairo_t *cr,
49 cairo_path_t *path,
50 munge_func_t munge)
52 int i;
53 cairo_path_data_t *p;
54 double x1, y1, x2, y2, x3, y3;
56 for (i=0; i < path->num_data; i += path->data[i].header.length) {
57 p = &path->data[i];
58 switch (p->header.type) {
59 case CAIRO_PATH_MOVE_TO:
60 x1 = p[1].point.x; y1 = p[1].point.y;
61 (munge) (&x1, &y1);
62 cairo_move_to (cr, x1, y1);
63 break;
64 case CAIRO_PATH_LINE_TO:
65 x1 = p[1].point.x; y1 = p[1].point.y;
66 (munge) (&x1, &y1);
67 cairo_line_to (cr, x1, y1);
68 break;
69 case CAIRO_PATH_CURVE_TO:
70 x1 = p[1].point.x; y1 = p[1].point.y;
71 x2 = p[2].point.x; y2 = p[2].point.y;
72 x3 = p[3].point.x; y3 = p[3].point.y;
73 (munge) (&x1, &y1);
74 (munge) (&x2, &y2);
75 (munge) (&x3, &y3);
76 cairo_curve_to (cr,
77 x1, y1,
78 x2, y2,
79 x3, y3);
80 break;
81 case CAIRO_PATH_CLOSE_PATH:
82 cairo_close_path (cr);
83 break;
88 static void
89 make_path (cairo_t *cr)
91 cairo_rectangle (cr, 0, 0, 5, 5);
92 cairo_move_to (cr, 15, 2.5);
93 cairo_arc (cr, 12.5, 2.5, 2.5, 0, 2 * M_PI);
96 static cairo_test_status_t
97 draw (cairo_t *cr, int width, int height)
99 cairo_path_t *path;
100 cairo_t *cr_error;
102 /* Ensure that calling cairo_copy_path on an in-error cairo_t will
103 * propagate the error. */
104 cr_error = cairo_create (NULL);
105 path = cairo_copy_path (cr_error);
106 if (path->status == CAIRO_STATUS_SUCCESS ||
107 path->status != cairo_status (cr_error)) {
108 cairo_test_log ("Error: cairo_copy_path returned status of %s rather than propagating %s\n",
109 cairo_status_to_string (path->status),
110 cairo_status_to_string (cairo_status (cr_error)));
111 cairo_path_destroy (path);
112 return CAIRO_TEST_FAILURE;
114 cairo_path_destroy (path);
116 path = cairo_copy_path_flat (cr_error);
117 if (path->status == CAIRO_STATUS_SUCCESS ||
118 path->status != cairo_status (cr_error)) {
119 cairo_test_log ("Error: cairo_copy_path_flat returned status of %s rather than propagating %s\n",
120 cairo_status_to_string (path->status),
121 cairo_status_to_string (cairo_status (cr_error)));
122 cairo_path_destroy (path);
123 return CAIRO_TEST_FAILURE;
125 cairo_path_destroy (path);
127 cairo_destroy (cr_error);
129 /* first check that we can copy an empty path */
130 cairo_new_path (cr);
131 path = cairo_copy_path (cr);
132 if (path->status != CAIRO_STATUS_SUCCESS) {
133 cairo_test_log ("Error: cairo_copy_path returned status of %s\n",
134 cairo_status_to_string (path->status));
135 cairo_path_destroy (path);
136 return CAIRO_TEST_FAILURE;
138 if (path->num_data != 0) {
139 cairo_test_log ("Error: cairo_copy_path did not copy an empty path, returned path contains %d elements\n",
140 path->num_data);
141 cairo_path_destroy (path);
142 return CAIRO_TEST_FAILURE;
144 cairo_append_path (cr, path);
145 cairo_path_destroy (path);
146 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
147 cairo_test_log ("Error: cairo_append_path failed with a copy of an empty path, returned status of %s\n",
148 cairo_status_to_string (cairo_status (cr)));
149 return CAIRO_TEST_FAILURE;
152 /* We draw in the default black, so paint white first. */
153 cairo_save (cr);
154 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
155 cairo_paint (cr);
156 cairo_restore (cr);
158 /* copy path, munge, and fill */
159 cairo_translate (cr, 5, 5);
160 make_path (cr);
161 path = cairo_copy_path (cr);
163 cairo_new_path (cr);
164 munge_and_set_path (cr, path, scale_by_two);
165 cairo_path_destroy (path);
166 cairo_fill (cr);
168 /* copy flattened path, munge, and fill */
169 cairo_translate (cr, 0, 15);
170 make_path (cr);
171 path = cairo_copy_path_flat (cr);
173 cairo_new_path (cr);
174 munge_and_set_path (cr, path, scale_by_two);
175 cairo_path_destroy (path);
176 cairo_fill (cr);
178 /* append two copies of path, and fill */
179 cairo_translate (cr, 0, 15);
180 cairo_scale (cr, 2.0, 2.0);
181 make_path (cr);
182 path = cairo_copy_path (cr);
184 cairo_new_path (cr);
185 cairo_append_path (cr, path);
186 cairo_translate (cr, 2.5, 2.5);
187 cairo_append_path (cr, path);
189 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
190 cairo_fill (cr);
192 cairo_path_destroy (path);
194 return CAIRO_TEST_SUCCESS;
198 main (void)
200 cairo_t *cr;
201 cairo_path_data_t data;
202 cairo_path_t path;
203 cairo_surface_t *surface;
205 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
207 /* Test a few error cases for cairo_append_path_data */
208 cr = cairo_create (surface);
209 cairo_append_path (cr, NULL);
210 if (cairo_status (cr) != CAIRO_STATUS_NULL_POINTER)
211 return 1;
212 cairo_destroy (cr);
214 cr = cairo_create (surface);
215 path.status = -1;
216 cairo_append_path (cr, &path);
217 if (cairo_status (cr) != CAIRO_STATUS_INVALID_STATUS)
218 return 1;
219 cairo_destroy (cr);
221 cr = cairo_create (surface);
222 path.status = CAIRO_STATUS_NO_MEMORY;
223 cairo_append_path (cr, &path);
224 if (cairo_status (cr) != CAIRO_STATUS_NO_MEMORY)
225 return 1;
226 cairo_destroy (cr);
228 cr = cairo_create (surface);
229 path.data = NULL;
230 path.num_data = 0;
231 path.status = CAIRO_STATUS_SUCCESS;
232 cairo_append_path (cr, &path);
233 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
234 return 1;
235 cairo_destroy (cr);
237 cr = cairo_create (surface);
238 path.data = NULL;
239 path.num_data = 1;
240 path.status = CAIRO_STATUS_SUCCESS;
241 cairo_append_path (cr, &path);
242 if (cairo_status (cr) != CAIRO_STATUS_NULL_POINTER)
243 return 1;
244 cairo_destroy (cr);
246 cr = cairo_create (surface);
247 /* Intentionally insert bogus header.length value (otherwise would be 2) */
248 data.header.type = CAIRO_PATH_MOVE_TO;
249 data.header.length = 1;
250 path.data = &data;
251 path.num_data = 1;
252 cairo_append_path (cr, &path);
253 if (cairo_status (cr) != CAIRO_STATUS_INVALID_PATH_DATA)
254 return 1;
255 cairo_destroy (cr);
257 /* And test the degnerate case */
258 cr = cairo_create (surface);
259 path.num_data = 0;
260 cairo_append_path (cr, &path);
261 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
262 return 1;
263 cairo_destroy (cr);
265 cairo_surface_destroy (surface);
267 return cairo_test (&test);