summaryrefslogtreecommitdiff
path: root/test/create-for-stream.c
blob: 9d90cf4efcfd8031be355dcd455277f7965e313e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright © 2006 Red Hat, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Red Hat, Inc. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Kristian Høgsberg <krh@redhat.com>
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <cairo.h>

#if CAIRO_HAS_PS_SURFACE
#include <cairo-ps.h>
#endif

#if CAIRO_HAS_PDF_SURFACE
#include <cairo-pdf.h>
#endif

#if CAIRO_HAS_SVG_SURFACE
#include <cairo-svg.h>
#endif

#include "cairo-test.h"

/* The main test suite doesn't test the *_create_for_stream
 * constructors for the PDF, PS and SVG surface, so we do that here.
 * We draw to an in-memory buffer using the stream constructor and
 * compare the output to the contents of a file writting using the
 * file constructor.
 */

#define MAX_OUTPUT_SIZE 4096

#define WIDTH_IN_INCHES  3
#define HEIGHT_IN_INCHES 3
#define WIDTH_IN_POINTS  (WIDTH_IN_INCHES  * 72.0)
#define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    /* Just draw a rectangle. */

    cairo_rectangle (cr, width / 10., height /10.,
		     width - 2 * width / 10.,
		     height - 2 * height /10.);
    cairo_fill (cr);

    cairo_show_page (cr);

    return CAIRO_TEST_SUCCESS;
}

static void
draw_to (cairo_surface_t *surface)
{
    cairo_t *cr;

    cr = cairo_create (surface);

    draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);

    cairo_destroy (cr);
}

typedef struct _write_closure {
    const cairo_test_context_t *ctx;
    char buffer[MAX_OUTPUT_SIZE];
    size_t index;
    cairo_test_status_t status;
} write_closure_t;

static cairo_status_t
bad_write (void		*closure,
	   const unsigned char	*data,
	   unsigned int	 length)
{
    return CAIRO_STATUS_WRITE_ERROR;
}

static cairo_status_t
test_write (void		*closure,
	    const unsigned char	*data,
	    unsigned int	 length)
{
    write_closure_t *wc = closure;

    if (wc->index + length >= sizeof wc->buffer) {
	cairo_test_log (wc->ctx, "Error: out of bounds in write callback\n");
	wc->status = CAIRO_TEST_FAILURE;
	return CAIRO_STATUS_SUCCESS;
    }

    memcpy (&wc->buffer[wc->index], data, length);
    wc->index += length;

    return CAIRO_STATUS_SUCCESS;
}


typedef cairo_surface_t *
(*file_constructor_t) (const char	       *filename,
		       double			width_in_points,
		       double			height_in_points);

typedef cairo_surface_t *
(*stream_constructor_t) (cairo_write_func_t	write_func,
			 void		       *closure,
			 double			width_in_points,
			 double			height_in_points);

static cairo_test_status_t
test_surface (const cairo_test_context_t *ctx,
	      const char                 *backend,
	      const char		 *filename,
	      file_constructor_t	 file_constructor,
	      stream_constructor_t	 stream_constructor)
{
    cairo_surface_t *surface;
    write_closure_t wc;
    char file_contents[MAX_OUTPUT_SIZE];
    cairo_status_t status;
    FILE *fp;

    /* test propagation of user errors */
    surface = stream_constructor (bad_write, &wc,
				  WIDTH_IN_POINTS, HEIGHT_IN_POINTS);

    status = cairo_surface_status (surface);
    if (status) {
	cairo_test_log (ctx,
			"%s: Failed to create surface for stream.\n",
			backend);
	return CAIRO_TEST_FAILURE;
    }

    draw_to (surface);

    cairo_surface_finish (surface);
    status = cairo_surface_status (surface);
    cairo_surface_destroy (surface);

    if (status != CAIRO_STATUS_WRITE_ERROR) {
	cairo_test_log (ctx,
			"%s: Error: expected \"write error\", but received \"%s\".\n",
			backend, cairo_status_to_string (status));
	return CAIRO_TEST_FAILURE;
    }

    /* construct the real surface */
    wc.ctx = ctx;
    wc.status = CAIRO_TEST_SUCCESS;
    wc.index = 0;

    surface = stream_constructor (test_write, &wc,
				  WIDTH_IN_POINTS, HEIGHT_IN_POINTS);

    status = cairo_surface_status (surface);
    if (status) {
	cairo_test_log (ctx,
			"%s: Failed to create surface for stream.\n", backend);
	return CAIRO_TEST_FAILURE;
    }

    draw_to (surface);

    cairo_surface_destroy (surface);

    if (wc.status != CAIRO_TEST_SUCCESS) {
	/* Error already reported. */
	return wc.status;
    }

    surface = file_constructor (filename,
				WIDTH_IN_POINTS, HEIGHT_IN_POINTS);

    status = cairo_surface_status (surface);
    if (status) {
	cairo_test_log (ctx, "%s: Failed to create surface for file %s: %s.\n",
			backend, filename, cairo_status_to_string (status));
	return CAIRO_TEST_FAILURE;
    }

    draw_to (surface);

    cairo_surface_destroy (surface);

    fp = fopen (filename, "r");
    if (fp == NULL) {
	cairo_test_log (ctx, "%s: Failed to open %s for reading: %s.\n",
			backend, filename, strerror (errno));
	return CAIRO_TEST_FAILURE;
    }

    if (fread (file_contents, 1, wc.index, fp) != wc.index) {
	cairo_test_log (ctx, "%s: Failed to read %s: %s.\n",
			backend, filename, strerror (errno));
	fclose (fp);
	return CAIRO_TEST_FAILURE;
    }

    if (memcmp (file_contents, wc.buffer, wc.index) != 0) {
	cairo_test_log (ctx, "%s: Stream based output differ from file output for %s.\n",
			backend, filename);
	fclose (fp);
	return CAIRO_TEST_FAILURE;
    }

    fclose (fp);

    return CAIRO_TEST_SUCCESS;
}

int
main (void)
{
    cairo_test_context_t ctx;
    cairo_test_status_t status = CAIRO_TEST_SUCCESS;
    cairo_test_status_t test_status;
    const char test_name[] = "create-for-stream";

    cairo_test_init (&ctx, test_name);

#if CAIRO_HAS_PS_SURFACE
    test_status = test_surface (&ctx, "ps", "create-for-stream.ps",
			        cairo_ps_surface_create,
			        cairo_ps_surface_create_for_stream);
    cairo_test_log (&ctx, "TEST: %s TARGET: %s RESULT: %s\n",
		    test_name, "ps",
		    test_status ? "FAIL" : "PASS");
    if (status == CAIRO_TEST_SUCCESS)
	status = test_status;
#endif

#if CAIRO_HAS_PDF_SURFACE
    test_status = test_surface (&ctx, "pdf", "create-for-stream.pdf",
			        cairo_pdf_surface_create,
			        cairo_pdf_surface_create_for_stream);
    cairo_test_log (&ctx, "TEST: %s TARGET: %s RESULT: %s\n",
		    test_name, "pdf",
		    test_status ? "FAIL" : "PASS");
    if (status == CAIRO_TEST_SUCCESS)
	status = test_status;
#endif

#if CAIRO_HAS_SVG_SURFACE
    test_status = test_surface (&ctx, "svg", "create-for-stream.svg",
			        cairo_svg_surface_create,
			        cairo_svg_surface_create_for_stream);
    cairo_test_log (&ctx, "TEST: %s TARGET: %s RESULT: %s\n",
		    test_name, "svg",
		    test_status ? "FAIL" : "PASS");
    if (status == CAIRO_TEST_SUCCESS)
	status = test_status;
#endif

    cairo_test_fini (&ctx);

    return status;
}