* MoonlightTypeConverter.cs: Convert CacheMode's from strings.
[moon.git] / cairo / test / a1-mask.c
blobe4638fee3da26bdf9366818dc135725695408e06
1 /*
2 * Copyright © Jeff Muizelaar
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 * JEFF MUIZELAAR 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 * Authors: Jeff Muizelaar <jeff@infidigm.net>
24 * Carl Worth <cworth@cworth.org>
25 * Chris Wilson <chris@chris-wilson.co.uk>
28 #include "cairo-test.h"
30 static cairo_test_draw_function_t draw;
32 #define MASK_WIDTH 10
33 #define MASK_HEIGHT 8
35 #ifdef WORDS_BIGENDIAN
36 #define MASK 0x28, 0x55
37 #else
38 #define MASK 0x14, 0xAA
39 #endif
40 static unsigned char mask[(MASK_WIDTH + 7) / 8 * MASK_HEIGHT] = {
41 MASK,
42 MASK,
43 MASK,
44 MASK,
45 MASK,
46 MASK,
47 MASK,
48 MASK,
51 static const cairo_test_t test = {
52 "a1-mask",
53 "test masks of CAIRO_FORMAT_A1",
54 MASK_WIDTH, MASK_HEIGHT,
55 draw
59 static cairo_test_status_t
60 check_status (const cairo_test_context_t *ctx,
61 cairo_status_t status,
62 cairo_status_t expected)
64 if (status == expected)
65 return CAIRO_TEST_SUCCESS;
67 cairo_test_log (ctx,
68 "Error: Expected status value %d (%s), received %d (%s)\n",
69 expected,
70 cairo_status_to_string (expected),
71 status,
72 cairo_status_to_string (status));
73 return CAIRO_TEST_FAILURE;
76 static cairo_test_status_t
77 test_surface_with_width_and_stride (const cairo_test_context_t *ctx,
78 int width, int stride,
79 cairo_status_t expected)
81 cairo_test_status_t status;
82 cairo_surface_t *surface;
83 cairo_t *cr;
84 int len;
85 unsigned char *data;
87 cairo_test_log (ctx,
88 "Creating surface with width %d and stride %d\n",
89 width, stride);
91 len = stride;
92 if (len < 0)
93 len = -len;
94 data = xmalloc (len);
96 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
97 width, 1, stride);
98 cr = cairo_create (surface);
100 cairo_paint (cr);
102 status = check_status (ctx, cairo_surface_status (surface), expected);
103 if (status)
104 goto BAIL;
106 status = check_status (ctx, cairo_status (cr), expected);
107 if (status)
108 goto BAIL;
110 BAIL:
111 cairo_destroy (cr);
112 cairo_surface_destroy (surface);
113 free (data);
114 return status;
117 static cairo_test_status_t
118 draw (cairo_t *cr, int dst_width, int dst_height)
120 unsigned char *mask_aligned;
121 cairo_surface_t *surface;
123 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
124 MASK_WIDTH,
125 MASK_HEIGHT);
127 mask_aligned = cairo_image_surface_get_data (surface);
128 if (mask_aligned != NULL) {
129 int stride = cairo_image_surface_get_stride (surface), row;
130 const unsigned char *src = mask;
131 unsigned char *dst = mask_aligned;
132 for (row = 0; row < MASK_HEIGHT; row++) {
133 memcpy (dst, src, (MASK_WIDTH + 7) / 8);
134 src += (MASK_WIDTH + 7) / 8;
135 dst += stride;
139 /* Paint background blue */
140 cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
141 cairo_paint (cr);
143 /* Then paint red through our mask */
144 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
145 cairo_mask_surface (cr, surface, 0, 0);
146 cairo_surface_destroy (surface);
148 return CAIRO_TEST_SUCCESS;
152 main (void)
154 cairo_test_context_t ctx;
155 int test_width;
157 cairo_test_init (&ctx, "a1-mask");
159 /* first check the API strictness */
160 for (test_width = 0; test_width < 40; test_width++) {
161 int test_stride = (test_width + 7) / 8;
162 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_A1,
163 test_width);
164 cairo_test_status_t status;
165 cairo_status_t expected;
167 /* First create a surface using the width as the stride,
168 * (most of these should fail).
170 expected = (stride == test_stride) ?
171 CAIRO_STATUS_SUCCESS : CAIRO_STATUS_INVALID_STRIDE;
173 status = test_surface_with_width_and_stride (&ctx,
174 test_width,
175 test_stride,
176 expected);
177 if (status)
178 return status;
180 status = test_surface_with_width_and_stride (&ctx,
181 test_width,
182 -test_stride,
183 expected);
184 if (status)
185 return status;
188 /* Then create a surface using the correct stride,
189 * (should always succeed).
191 status = test_surface_with_width_and_stride (&ctx,
192 test_width,
193 stride,
194 CAIRO_STATUS_SUCCESS);
195 if (status)
196 return status;
198 status = test_surface_with_width_and_stride (&ctx,
199 test_width,
200 -stride,
201 CAIRO_STATUS_SUCCESS);
202 if (status)
203 return status;
206 cairo_test_fini (&ctx);
208 return cairo_test (&test);