summaryrefslogtreecommitdiff
path: root/test/invalid-matrix.c
blob: 0f0ef0c563e60414afef1209e01a97bfccbd3ace (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
/*
 * Copyright © 2007 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author: Carl Worth <cworth@cworth.org>
 */

#include "cairo-test.h"

static cairo_test_draw_function_t draw;

cairo_test_t test = {
    "invalid-matrix",
    "Test that all relevant public functions return CAIRO_STATUS_INVALID_MATRIX as appropriate",
    0, 0,
    draw
};

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    cairo_status_t status;
    cairo_surface_t *target;
    cairo_font_face_t *font_face;
    cairo_font_options_t *font_options;
    cairo_scaled_font_t *scaled_font;
    cairo_pattern_t *pattern;
    cairo_t *cr2;
    cairo_matrix_t identity, bogus, invalid = {
	4.0, 4.0,
	4.0, 4.0,
	4.0, 4.0
    };

#define CHECK_STATUS(status, function_name)						\
if ((status) == CAIRO_STATUS_SUCCESS) {							\
    cairo_test_log ("Error: %s with invalid matrix passed\n",				\
		    (function_name));							\
    return CAIRO_TEST_FAILURE;								\
} else if ((status) != CAIRO_STATUS_INVALID_MATRIX) {					\
    cairo_test_log ("Warning: %s with invalid matrix returned unexpected status "	\
		    "(%d): %s\n",							\
		    (function_name),							\
		    status,								\
		    cairo_status_to_string (status));					\
}

    /* create a bogus matrix and check results of attempted inversion */
    bogus.x0 = bogus.xy = bogus.xx = strtod ("NaN", NULL);
    bogus.y0 = bogus.yx = bogus.yy = bogus.xx;
    status = cairo_matrix_invert (&bogus);
    CHECK_STATUS (status, "cairo_matrix_invert(NaN)");

    /* test cairo_matrix_invert with invalid matrix */
    status = cairo_matrix_invert (&invalid);
    CHECK_STATUS (status, "cairo_matrix_invert(invalid)");


    cairo_matrix_init_identity (&identity);

    target = cairo_get_group_target (cr);

    /* test cairo_transform with invalid matrix */
    cr2 = cairo_create (target);
    cairo_transform (cr2, &invalid);

    status = cairo_status (cr2);
    cairo_destroy (cr2);
    CHECK_STATUS (status, "cairo_transform(invalid)");

    /* test cairo_transform with bogus matrix */
    cr2 = cairo_create (target);
    cairo_transform (cr2, &bogus);

    status = cairo_status (cr2);
    cairo_destroy (cr2);
    CHECK_STATUS (status, "cairo_transform(NaN)");


    /* test cairo_set_matrix with invalid matrix */
    cr2 = cairo_create (target);
    cairo_set_matrix (cr2, &invalid);

    status = cairo_status (cr2);
    cairo_destroy (cr2);
    CHECK_STATUS (status, "cairo_set_matrix(invalid)");

    /* test cairo_set_matrix with bogus matrix */
    cr2 = cairo_create (target);
    cairo_set_matrix (cr2, &bogus);

    status = cairo_status (cr2);
    cairo_destroy (cr2);
    CHECK_STATUS (status, "cairo_set_matrix(NaN)");


    /* test cairo_set_font_matrix with invalid matrix */
    cr2 = cairo_create (target);
    cairo_set_font_matrix (cr2, &invalid);

    /* draw some text to force the font to be resolved */
    cairo_show_text (cr2, "hello");

    status = cairo_status (cr2);
    cairo_destroy (cr2);
    CHECK_STATUS (status, "cairo_set_font_matrix(invalid)");

    /* test cairo_set_font_matrix with bogus matrix */
    cr2 = cairo_create (target);
    cairo_set_font_matrix (cr2, &bogus);

    /* draw some text to force the font to be resolved */
    cairo_show_text (cr2, "hello");

    status = cairo_status (cr2);
    cairo_destroy (cr2);
    CHECK_STATUS (status, "cairo_set_font_matrix(NaN)");


    /* test cairo_scaled_font_create with invalid matrix */
    cr2 = cairo_create (target);
    font_face = cairo_get_font_face (cr2);
    font_options = cairo_font_options_create ();
    cairo_get_font_options (cr, font_options);
    scaled_font = cairo_scaled_font_create (font_face,
					    &invalid,
					    &identity,
					    font_options);
    status = cairo_scaled_font_status (scaled_font);
    CHECK_STATUS (status, "cairo_scaled_font_create(invalid)");

    cairo_scaled_font_destroy (scaled_font);

    scaled_font = cairo_scaled_font_create (font_face,
					    &identity,
					    &invalid,
					    font_options);
    status = cairo_scaled_font_status (scaled_font);
    CHECK_STATUS (status, "cairo_scaled_font_create(invalid)");

    cairo_scaled_font_destroy (scaled_font);
    cairo_font_options_destroy (font_options);
    cairo_destroy (cr2);

    /* test cairo_scaled_font_create with bogus matrix */
    cr2 = cairo_create (target);
    font_face = cairo_get_font_face (cr2);
    font_options = cairo_font_options_create ();
    cairo_get_font_options (cr, font_options);
    scaled_font = cairo_scaled_font_create (font_face,
					    &bogus,
					    &identity,
					    font_options);
    status = cairo_scaled_font_status (scaled_font);
    CHECK_STATUS (status, "cairo_scaled_font_create(NaN)");

    cairo_scaled_font_destroy (scaled_font);

    scaled_font = cairo_scaled_font_create (font_face,
					    &identity,
					    &bogus,
					    font_options);
    status = cairo_scaled_font_status (scaled_font);
    CHECK_STATUS (status, "cairo_scaled_font_create(NaN)");

    cairo_scaled_font_destroy (scaled_font);
    cairo_font_options_destroy (font_options);
    cairo_destroy (cr2);


    /* test cairo_pattern_set_matrix with invalid matrix */
    pattern = cairo_pattern_create_rgb (1.0, 1.0, 1.0);
    cairo_pattern_set_matrix (pattern, &invalid);
    status = cairo_pattern_status (pattern);
    CHECK_STATUS (status, "cairo_pattern_set_matrix(invalid)");
    cairo_pattern_destroy (pattern);

    /* test cairo_pattern_set_matrix with bogus matrix */
    pattern = cairo_pattern_create_rgb (1.0, 1.0, 1.0);
    cairo_pattern_set_matrix (pattern, &bogus);
    status = cairo_pattern_status (pattern);
    CHECK_STATUS (status, "cairo_pattern_set_matrix(NaN)");
    cairo_pattern_destroy (pattern);


    /* test invalid transformations */
    cr2 = cairo_create (target);
    cairo_translate (cr2, bogus.xx, bogus.yy);
    CHECK_STATUS (status, "cairo_translate(NaN, NaN)");
    cairo_destroy (cr2);

    cr2 = cairo_create (target);
    cairo_translate (cr2, 0, bogus.yy);
    CHECK_STATUS (status, "cairo_translate(0, NaN)");
    cairo_destroy (cr2);

    cr2 = cairo_create (target);
    cairo_translate (cr2, bogus.xx, 0);
    CHECK_STATUS (status, "cairo_translate(NaN, 0)");
    cairo_destroy (cr2);


    cr2 = cairo_create (target);
    cairo_scale (cr2, bogus.xx, bogus.yy);
    CHECK_STATUS (status, "cairo_scale(NaN, NaN)");
    cairo_destroy (cr2);

    cr2 = cairo_create (target);
    cairo_scale (cr2, 1, bogus.yy);
    CHECK_STATUS (status, "cairo_scale(1, NaN)");
    cairo_destroy (cr2);

    cr2 = cairo_create (target);
    cairo_scale (cr2, bogus.xx, 1);
    CHECK_STATUS (status, "cairo_scale(NaN, 1)");
    cairo_destroy (cr2);

    cr2 = cairo_create (target);
    cairo_rotate (cr2, bogus.xx);
    CHECK_STATUS (status, "cairo_rotate(NaN)");
    cairo_destroy (cr2);

    return CAIRO_TEST_SUCCESS;
}

int
main (void)
{
    return cairo_test (&test);
}