[check] Filter programlistings for check-doc-syntax.sh
[cairo/haiku.git] / test / create-for-stream.c
blob379412fc5c2f9f5e3380b2b472deb1b3aa3a95d7
1 /*
2 * Copyright © 2006 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: Kristian Høgsberg <krh@redhat.com>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
30 #include <cairo.h>
32 #if CAIRO_HAS_PS_SURFACE
33 #include <cairo-ps.h>
34 #endif
36 #if CAIRO_HAS_PDF_SURFACE
37 #include <cairo-pdf.h>
38 #endif
40 #if CAIRO_HAS_SVG_SURFACE
41 #include <cairo-svg.h>
42 #endif
44 #include "cairo-test.h"
46 /* The main test suite doesn't test the *_create_for_stream
47 * constructors for the PDF, PS and SVG surface, so we do that here.
48 * We draw to an in-memory buffer using the stream constructor and
49 * compare the output to the contents of a file writting using the
50 * file constructor.
53 #define MAX_OUTPUT_SIZE 4096
55 #define WIDTH_IN_INCHES 3
56 #define HEIGHT_IN_INCHES 3
57 #define WIDTH_IN_POINTS (WIDTH_IN_INCHES * 72.0)
58 #define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)
60 static cairo_test_status_t
61 draw (cairo_t *cr, int width, int height)
63 /* Just draw a rectangle. */
65 cairo_rectangle (cr, width / 10., height /10.,
66 width - 2 * width / 10.,
67 height - 2 * height /10.);
68 cairo_fill (cr);
70 cairo_show_page (cr);
72 return CAIRO_TEST_SUCCESS;
75 static void
76 draw_to (cairo_surface_t *surface)
78 cairo_t *cr;
80 cr = cairo_create (surface);
82 draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
84 cairo_destroy (cr);
87 typedef struct _write_closure {
88 char buffer[MAX_OUTPUT_SIZE];
89 size_t index;
90 cairo_test_status_t status;
91 } write_closure_t;
93 static cairo_status_t
94 bad_write (void *closure,
95 const unsigned char *data,
96 unsigned int length)
98 return CAIRO_STATUS_WRITE_ERROR;
101 static cairo_status_t
102 test_write (void *closure,
103 const unsigned char *data,
104 unsigned int length)
106 write_closure_t *wc = closure;
108 if (wc->index + length >= sizeof wc->buffer) {
109 cairo_test_log ("Error: out of bounds in write callback\n");
110 wc->status = CAIRO_TEST_FAILURE;
111 return CAIRO_STATUS_SUCCESS;
114 memcpy (&wc->buffer[wc->index], data, length);
115 wc->index += length;
117 return CAIRO_STATUS_SUCCESS;
121 typedef cairo_surface_t *
122 (*file_constructor_t) (const char *filename,
123 double width_in_points,
124 double height_in_points);
126 typedef cairo_surface_t *
127 (*stream_constructor_t) (cairo_write_func_t write_func,
128 void *closure,
129 double width_in_points,
130 double height_in_points);
132 static cairo_test_status_t
133 test_surface (const char *backend,
134 const char *filename,
135 file_constructor_t file_constructor,
136 stream_constructor_t stream_constructor)
138 cairo_surface_t *surface;
139 write_closure_t wc;
140 char file_contents[MAX_OUTPUT_SIZE];
141 cairo_status_t status;
142 FILE *fp;
144 /* test propagation of user errors */
145 surface = stream_constructor (bad_write, &wc,
146 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
148 status = cairo_surface_status (surface);
149 if (status) {
150 cairo_test_log ("%s: Failed to create surface for stream.\n", backend);
151 return CAIRO_TEST_FAILURE;
154 draw_to (surface);
156 cairo_surface_finish (surface);
157 status = cairo_surface_status (surface);
158 cairo_surface_destroy (surface);
160 if (status != CAIRO_STATUS_WRITE_ERROR) {
161 cairo_test_log ("%s: Error: expected \"write error\", but received \"%s\".\n",
162 backend, cairo_status_to_string (status));
163 return CAIRO_TEST_FAILURE;
166 /* construct the real surface */
167 wc.status = CAIRO_TEST_SUCCESS;
168 wc.index = 0;
170 surface = stream_constructor (test_write, &wc,
171 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
173 status = cairo_surface_status (surface);
174 if (status) {
175 cairo_test_log ("%s: Failed to create surface for stream.\n", backend);
176 return CAIRO_TEST_FAILURE;
179 draw_to (surface);
181 cairo_surface_destroy (surface);
183 if (wc.status != CAIRO_TEST_SUCCESS) {
184 /* Error already reported. */
185 return wc.status;
188 surface = file_constructor (filename,
189 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
191 status = cairo_surface_status (surface);
192 if (status) {
193 cairo_test_log ("%s: Failed to create surface for file %s: %s.\n",
194 backend, filename, cairo_status_to_string (status));
195 return CAIRO_TEST_FAILURE;
198 draw_to (surface);
200 cairo_surface_destroy (surface);
202 fp = fopen (filename, "r");
203 if (fp == NULL) {
204 cairo_test_log ("%s: Failed to open %s for reading: %s.\n",
205 backend, filename, strerror (errno));
206 return CAIRO_TEST_FAILURE;
209 if (fread (file_contents, 1, wc.index, fp) != wc.index) {
210 cairo_test_log ("%s: Failed to read %s: %s.\n",
211 backend, filename, strerror (errno));
212 fclose (fp);
213 return CAIRO_TEST_FAILURE;
216 if (memcmp (file_contents, wc.buffer, wc.index) != 0) {
217 cairo_test_log ("%s: Stream based output differ from file output for %s.\n",
218 backend, filename);
219 fclose (fp);
220 return CAIRO_TEST_FAILURE;
223 fclose (fp);
225 return CAIRO_TEST_SUCCESS;
229 main (void)
231 cairo_test_status_t status = CAIRO_TEST_SUCCESS;
232 cairo_test_status_t test_status;
233 const char test_name[] = "create-for-stream";
235 cairo_test_init (test_name);
237 #if CAIRO_HAS_PS_SURFACE
238 test_status = test_surface ("ps", "create-for-stream.ps",
239 cairo_ps_surface_create,
240 cairo_ps_surface_create_for_stream);
241 cairo_test_log ("TEST: %s TARGET: %s RESULT: %s\n",
242 test_name, "ps",
243 test_status ? "FAIL" : "PASS");
244 if (status == CAIRO_TEST_SUCCESS)
245 status = test_status;
246 #endif
248 #if CAIRO_HAS_PDF_SURFACE
249 test_status = test_surface ("pdf", "create-for-stream.pdf",
250 cairo_pdf_surface_create,
251 cairo_pdf_surface_create_for_stream);
252 cairo_test_log ("TEST: %s TARGET: %s RESULT: %s\n",
253 test_name, "pdf",
254 test_status ? "FAIL" : "PASS");
255 if (status == CAIRO_TEST_SUCCESS)
256 status = test_status;
257 #endif
259 #if CAIRO_HAS_SVG_SURFACE
260 test_status = test_surface ("svg", "create-for-stream.svg",
261 cairo_svg_surface_create,
262 cairo_svg_surface_create_for_stream);
263 cairo_test_log ("TEST: %s TARGET: %s RESULT: %s\n",
264 test_name, "svg",
265 test_status ? "FAIL" : "PASS");
266 if (status == CAIRO_TEST_SUCCESS)
267 status = test_status;
268 #endif
270 cairo_test_fini ();
272 return status;