[check] Filter programlistings for check-doc-syntax.sh
[cairo/haiku.git] / test / pattern-getters.c
blobaa420bf3f55e145b3487f4c86ca649b1511b3a1a
1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /*
3 * Copyright © 2005 Mozilla Corporation, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without
7 * fee, provided that the above copyright notice appear in all copies
8 * and that both that copyright notice and this permission notice
9 * appear in supporting documentation, and that the name of
10 * Mozilla Corporation not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. Mozilla Corporation makes no representations about the
13 * suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION BE LIABLE FOR ANY SPECIAL,
19 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Author: Vladimir Vukicevic <vladimir@pobox.com>
27 #include <stdlib.h>
28 #include "cairo-test.h"
30 static cairo_test_draw_function_t draw;
32 cairo_test_t test = {
33 "pattern-getters",
34 "Tests calls to pattern getter functions",
35 1, 1,
36 draw
39 #define CHECK_SUCCESS do { if (status) return CAIRO_TEST_FAILURE; } while (0)
41 static int
42 double_buf_equal (double *a, double *b, int nc)
44 int i;
45 for (i = 0; i < nc; i++) {
46 if (!CAIRO_TEST_DOUBLE_EQUALS(a[i],b[i])) {
47 cairo_test_log ("Error: doubles not equal: %g, %g\n",
48 a[i], b[i]);
49 return 0;
52 return 1;
55 static cairo_test_status_t
56 draw (cairo_t *cr, int width, int height)
58 cairo_status_t status;
59 cairo_pattern_t *pat;
61 /* Test pattern_get_rgba */
63 double r, g, b, a;
64 pat = cairo_pattern_create_rgba (0.2, 0.3, 0.4, 0.5);
66 status = cairo_pattern_get_rgba (pat, &r, &g, &b, &a);
67 CHECK_SUCCESS;
69 if (!CAIRO_TEST_DOUBLE_EQUALS(r,0.2) ||
70 !CAIRO_TEST_DOUBLE_EQUALS(g,0.3) ||
71 !CAIRO_TEST_DOUBLE_EQUALS(b,0.4) ||
72 !CAIRO_TEST_DOUBLE_EQUALS(a,0.5)) {
73 cairo_test_log ("Error: cairo_pattern_get_rgba returned unexepcted results: %g, %g, %g, %g\n",
74 r, g, b, a);
75 return CAIRO_TEST_FAILURE;
78 cairo_pattern_destroy (pat);
81 /* Test pattern_get_surface */
83 cairo_surface_t *surf;
85 pat = cairo_pattern_create_for_surface (cairo_get_target (cr));
87 status = cairo_pattern_get_surface (pat, &surf);
88 CHECK_SUCCESS;
90 if (surf != cairo_get_target (cr)) {
91 cairo_test_log ("Error: cairo_pattern_get_resurface returned wrong surface\n");
92 return CAIRO_TEST_FAILURE;
95 cairo_pattern_destroy (pat);
98 /* Test get_color_stops & linear_get_points */
100 int i;
101 double x0, y0, x1, y1;
102 double expected_values[15] = { 0.0, 0.2, 0.4, 0.2, 1.0,
103 0.5, 0.4, 0.5, 0.2, 0.5,
104 1.0, 0.2, 0.4, 0.5, 0.2 };
105 double new_buf[15];
107 pat = cairo_pattern_create_linear (1.0, 2.0, 3.0, 4.0);
109 for (i = 0; i < 3; i++) {
110 cairo_pattern_add_color_stop_rgba (pat,
111 expected_values[i*5+0],
112 expected_values[i*5+1],
113 expected_values[i*5+2],
114 expected_values[i*5+3],
115 expected_values[i*5+4]);
118 status = cairo_pattern_get_linear_points (pat, &x0, &y0, &x1, &y1);
119 CHECK_SUCCESS;
121 if (!CAIRO_TEST_DOUBLE_EQUALS(x0,1.0) ||
122 !CAIRO_TEST_DOUBLE_EQUALS(y0,2.0) ||
123 !CAIRO_TEST_DOUBLE_EQUALS(x1,3.0) ||
124 !CAIRO_TEST_DOUBLE_EQUALS(y1,4.0))
125 return CAIRO_TEST_FAILURE;
127 status = cairo_pattern_get_color_stop_count (pat, &i);
128 CHECK_SUCCESS;
130 if (i != 3)
131 return CAIRO_TEST_FAILURE;
133 for (i = 0; i < 3; i++) {
134 status = cairo_pattern_get_color_stop_rgba (pat, i,
135 &new_buf[i*5+0],
136 &new_buf[i*5+1],
137 &new_buf[i*5+2],
138 &new_buf[i*5+3],
139 &new_buf[i*5+4]);
140 CHECK_SUCCESS;
143 status = cairo_pattern_get_color_stop_rgba (pat, 5, NULL, NULL, NULL, NULL, NULL);
144 if (status != CAIRO_STATUS_INVALID_INDEX)
145 return CAIRO_TEST_FAILURE;
147 if (!double_buf_equal (new_buf, expected_values, sizeof(expected_values)/sizeof(double)) != 0)
148 return CAIRO_TEST_FAILURE;
150 cairo_pattern_destroy (pat);
153 /* Test radial_get_circles */
155 double a, b, c, d, e, f;
156 pat = cairo_pattern_create_radial (1, 2, 3,
157 4, 5, 6);
159 status = cairo_pattern_get_radial_circles (pat, &a, &b, &c, &d, &e, &f);
160 CHECK_SUCCESS;
162 if (!CAIRO_TEST_DOUBLE_EQUALS(a,1.0) ||
163 !CAIRO_TEST_DOUBLE_EQUALS(b,2.0) ||
164 !CAIRO_TEST_DOUBLE_EQUALS(c,3.0) ||
165 !CAIRO_TEST_DOUBLE_EQUALS(d,4.0) ||
166 !CAIRO_TEST_DOUBLE_EQUALS(e,5.0) ||
167 !CAIRO_TEST_DOUBLE_EQUALS(f,6.0))
168 return CAIRO_TEST_FAILURE;
170 cairo_pattern_destroy (pat);
173 cairo_set_source_rgb (cr, 0, 1, 0);
174 cairo_paint (cr);
176 return CAIRO_TEST_SUCCESS;
180 main (void)
182 return cairo_test (&test);