summaryrefslogtreecommitdiff
path: root/test/pattern-getters.c
blob: 657159c26b78bcf9731d1b0499eaf7384b34ef68 (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
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/*
 * Copyright © 2005 Mozilla Corporation, 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
 * Mozilla Corporation not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Mozilla Corporation makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION 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: Vladimir Vukicevic <vladimir@pobox.com>
 */

#include <stdlib.h>
#include "cairo-test.h"

#define CHECK_SUCCESS do { \
    if (status) { \
	cairo_pattern_destroy (pat); \
	return cairo_test_status_from_status (ctx, status); \
    } \
} while (0)

static int
double_buf_equal (const cairo_test_context_t *ctx, double *a, double *b, int nc)
{
    int i;
    for (i = 0; i < nc; i++) {
	if (!CAIRO_TEST_DOUBLE_EQUALS(a[i],b[i])) {
	    cairo_test_log (ctx, "Error: doubles not equal: %g, %g\n",
			    a[i], b[i]);
	    return 0;
	}
    }
    return 1;
}

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
    cairo_status_t status;
    cairo_pattern_t *pat;

    /* Test pattern_get_rgba */
    {
	double r, g, b, a;
	pat = cairo_pattern_create_rgba (0.2, 0.3, 0.4, 0.5);

	status = cairo_pattern_get_rgba (pat, &r, &g, &b, &a);
	CHECK_SUCCESS;

	if (!CAIRO_TEST_DOUBLE_EQUALS(r,0.2) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(g,0.3) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(b,0.4) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(a,0.5)) {
	    cairo_test_log (ctx, "Error: cairo_pattern_get_rgba returned unexepcted results: %g, %g, %g, %g\n",
			    r, g, b, a);
	    cairo_pattern_destroy (pat);
	    return CAIRO_TEST_FAILURE;
	}

	cairo_pattern_destroy (pat);
    }

    /* Test pattern_get_surface */
    {
	cairo_surface_t *surf;

	pat = cairo_pattern_create_for_surface (cairo_get_target (cr));

	status = cairo_pattern_get_surface (pat, &surf);
	CHECK_SUCCESS;

	if (surf != cairo_get_target (cr)) {
	    cairo_test_log (ctx, "Error: cairo_pattern_get_resurface returned wrong surface\n");
	    cairo_pattern_destroy (pat);
	    return CAIRO_TEST_FAILURE;
	}

	cairo_pattern_destroy (pat);
    }

    /* Test get_color_stops & linear_get_points */
    {
	int i;
	double x0, y0, x1, y1;
	double expected_values[15] = { 0.0, 0.2, 0.4, 0.2, 1.0,
				       0.5, 0.4, 0.5, 0.2, 0.5,
				       1.0, 0.2, 0.4, 0.5, 0.2 };
	double new_buf[15];

	pat = cairo_pattern_create_linear (1.0, 2.0, 3.0, 4.0);

	for (i = 0; i < 3; i++) {
	    cairo_pattern_add_color_stop_rgba (pat,
					       expected_values[i*5+0],
					       expected_values[i*5+1],
					       expected_values[i*5+2],
					       expected_values[i*5+3],
					       expected_values[i*5+4]);
	}

	status = cairo_pattern_get_linear_points (pat, &x0, &y0, &x1, &y1);
	CHECK_SUCCESS;

	if (!CAIRO_TEST_DOUBLE_EQUALS(x0,1.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(y0,2.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(x1,3.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(y1,4.0))
	{
	    cairo_pattern_destroy (pat);
	    return CAIRO_TEST_FAILURE;
	}

	status = cairo_pattern_get_color_stop_count (pat, &i);
	CHECK_SUCCESS;

	if (i != 3) {
	    cairo_pattern_destroy (pat);
	    return CAIRO_TEST_FAILURE;
	}

	for (i = 0; i < 3; i++) {
	    status = cairo_pattern_get_color_stop_rgba (pat, i,
							&new_buf[i*5+0],
							&new_buf[i*5+1],
							&new_buf[i*5+2],
							&new_buf[i*5+3],
							&new_buf[i*5+4]);
	    CHECK_SUCCESS;
	}

	status = cairo_pattern_get_color_stop_rgba (pat, 5, NULL, NULL, NULL, NULL, NULL);
	if (status != CAIRO_STATUS_INVALID_INDEX) {
	    cairo_pattern_destroy (pat);
	    return CAIRO_TEST_FAILURE;
	}

	if (!double_buf_equal (ctx, new_buf, expected_values,
			       sizeof(expected_values)/sizeof(double)) != 0)
	{
	    cairo_pattern_destroy (pat);
	    return CAIRO_TEST_FAILURE;
	}

	cairo_pattern_destroy (pat);
    }

    /* Test radial_get_circles */
    {
	double a, b, c, d, e, f;
	pat = cairo_pattern_create_radial (1, 2, 3,
					   4, 5, 6);

	status = cairo_pattern_get_radial_circles (pat, &a, &b, &c, &d, &e, &f);
	CHECK_SUCCESS;

	if (!CAIRO_TEST_DOUBLE_EQUALS(a,1.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(b,2.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(c,3.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(d,4.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(e,5.0) ||
	    !CAIRO_TEST_DOUBLE_EQUALS(f,6.0))
	{
	    cairo_pattern_destroy (pat);
	    return CAIRO_TEST_FAILURE;
	}

	cairo_pattern_destroy (pat);
    }

    cairo_set_source_rgb (cr, 0, 1, 0);
    cairo_paint (cr);

    return CAIRO_TEST_SUCCESS;
}

CAIRO_TEST (pattern_getters,
	    "Tests calls to pattern getter functions",
	    "pattern, api", /* keywords */
	    NULL, /* requirements */
	    1, 1,
	    NULL, draw)