Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / invalid-matrix.c
1 /*
2  * Copyright © 2007 Red Hat, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Author: Carl Worth <cworth@cworth.org>
25  */
26
27 #define _ISOC99_SOURCE  /* for INFINITY */
28 #define _GNU_SOURCE 1   /* for fedisableeexcept() et al */
29
30 #include "cairo-test.h"
31
32 #if !defined(INFINITY)
33 #define INFINITY HUGE_VAL
34 #endif
35
36 #if HAVE_FENV_H
37 # include <fenv.h>
38 #endif
39
40 static cairo_test_status_t
41 draw (cairo_t *cr, int width, int height)
42 {
43     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
44     cairo_status_t status;
45     cairo_surface_t *target;
46     cairo_font_face_t *font_face;
47     cairo_font_options_t *font_options;
48     cairo_scaled_font_t *scaled_font;
49     cairo_pattern_t *pattern;
50     cairo_t *cr2;
51     cairo_matrix_t identity, bogus, inf, invalid = {
52         4.0, 4.0,
53         4.0, 4.0,
54         4.0, 4.0
55     };
56
57 #define CHECK_STATUS(status, function_name)                                             \
58 if ((status) == CAIRO_STATUS_SUCCESS) {                                                 \
59     cairo_test_log (ctx, "Error: %s with invalid matrix passed\n",                              \
60                     (function_name));                                                   \
61     return CAIRO_TEST_FAILURE;                                                          \
62 } else if ((status) != CAIRO_STATUS_INVALID_MATRIX) {                                   \
63     cairo_test_log (ctx, "Error: %s with invalid matrix returned unexpected status "    \
64                     "(%d): %s\n",                                                       \
65                     (function_name),                                                    \
66                     status,                                                             \
67                     cairo_status_to_string (status));                                   \
68     return CAIRO_TEST_FAILURE;                                                          \
69 }
70
71     /* clear floating point exceptions (added by cairo_test_init()) */
72 #if HAVE_FEDISABLEEXCEPT
73     fedisableexcept (FE_INVALID);
74 #endif
75
76     /* create a bogus matrix and check results of attempted inversion */
77     bogus.x0 = bogus.xy = bogus.xx = cairo_test_NaN ();
78     bogus.y0 = bogus.yx = bogus.yy = bogus.xx;
79     status = cairo_matrix_invert (&bogus);
80     CHECK_STATUS (status, "cairo_matrix_invert(NaN)");
81
82     inf.x0 = inf.xy = inf.xx = INFINITY;
83     inf.y0 = inf.yx = inf.yy = inf.xx;
84     status = cairo_matrix_invert (&inf);
85     CHECK_STATUS (status, "cairo_matrix_invert(infinity)");
86
87     /* test cairo_matrix_invert with invalid matrix */
88     status = cairo_matrix_invert (&invalid);
89     CHECK_STATUS (status, "cairo_matrix_invert(invalid)");
90
91
92     cairo_matrix_init_identity (&identity);
93
94     target = cairo_get_group_target (cr);
95
96     /* test cairo_transform with invalid matrix */
97     cr2 = cairo_create (target);
98     cairo_transform (cr2, &invalid);
99
100     status = cairo_status (cr2);
101     cairo_destroy (cr2);
102     CHECK_STATUS (status, "cairo_transform(invalid)");
103
104     /* test cairo_transform with bogus matrix */
105     cr2 = cairo_create (target);
106     cairo_transform (cr2, &bogus);
107
108     status = cairo_status (cr2);
109     cairo_destroy (cr2);
110     CHECK_STATUS (status, "cairo_transform(NaN)");
111
112     /* test cairo_transform with ∞ matrix */
113     cr2 = cairo_create (target);
114     cairo_transform (cr2, &inf);
115
116     status = cairo_status (cr2);
117     cairo_destroy (cr2);
118     CHECK_STATUS (status, "cairo_transform(infinity)");
119
120
121     /* test cairo_set_matrix with invalid matrix */
122     cr2 = cairo_create (target);
123     cairo_set_matrix (cr2, &invalid);
124
125     status = cairo_status (cr2);
126     cairo_destroy (cr2);
127     CHECK_STATUS (status, "cairo_set_matrix(invalid)");
128
129     /* test cairo_set_matrix with bogus matrix */
130     cr2 = cairo_create (target);
131     cairo_set_matrix (cr2, &bogus);
132
133     status = cairo_status (cr2);
134     cairo_destroy (cr2);
135     CHECK_STATUS (status, "cairo_set_matrix(NaN)");
136
137     /* test cairo_set_matrix with ∞ matrix */
138     cr2 = cairo_create (target);
139     cairo_set_matrix (cr2, &inf);
140
141     status = cairo_status (cr2);
142     cairo_destroy (cr2);
143     CHECK_STATUS (status, "cairo_set_matrix(infinity)");
144
145
146     /* test cairo_set_font_matrix with invalid matrix */
147     cr2 = cairo_create (target);
148     cairo_set_font_matrix (cr2, &invalid);
149
150     /* draw some text to force the font to be resolved */
151     cairo_show_text (cr2, "hello");
152
153     status = cairo_status (cr2);
154     cairo_destroy (cr2);
155     CHECK_STATUS (status, "cairo_set_font_matrix(invalid)");
156
157     /* test cairo_set_font_matrix with bogus matrix */
158     cr2 = cairo_create (target);
159     cairo_set_font_matrix (cr2, &bogus);
160
161     /* draw some text to force the font to be resolved */
162     cairo_show_text (cr2, "hello");
163
164     status = cairo_status (cr2);
165     cairo_destroy (cr2);
166     CHECK_STATUS (status, "cairo_set_font_matrix(NaN)");
167
168     /* test cairo_set_font_matrix with ∞ matrix */
169     cr2 = cairo_create (target);
170     cairo_set_font_matrix (cr2, &inf);
171
172     /* draw some text to force the font to be resolved */
173     cairo_show_text (cr2, "hello");
174
175     status = cairo_status (cr2);
176     cairo_destroy (cr2);
177     CHECK_STATUS (status, "cairo_set_font_matrix(infinity)");
178
179
180     /* test cairo_scaled_font_create with invalid matrix */
181     cr2 = cairo_create (target);
182     font_face = cairo_get_font_face (cr2);
183     font_options = cairo_font_options_create ();
184     cairo_get_font_options (cr, font_options);
185     scaled_font = cairo_scaled_font_create (font_face,
186                                             &invalid,
187                                             &identity,
188                                             font_options);
189     status = cairo_scaled_font_status (scaled_font);
190     CHECK_STATUS (status, "cairo_scaled_font_create(invalid)");
191
192     cairo_scaled_font_destroy (scaled_font);
193
194     scaled_font = cairo_scaled_font_create (font_face,
195                                             &identity,
196                                             &invalid,
197                                             font_options);
198     status = cairo_scaled_font_status (scaled_font);
199     CHECK_STATUS (status, "cairo_scaled_font_create(invalid)");
200
201     cairo_scaled_font_destroy (scaled_font);
202     cairo_font_options_destroy (font_options);
203     cairo_destroy (cr2);
204
205     /* test cairo_scaled_font_create with bogus matrix */
206     cr2 = cairo_create (target);
207     font_face = cairo_get_font_face (cr2);
208     font_options = cairo_font_options_create ();
209     cairo_get_font_options (cr, font_options);
210     scaled_font = cairo_scaled_font_create (font_face,
211                                             &bogus,
212                                             &identity,
213                                             font_options);
214     status = cairo_scaled_font_status (scaled_font);
215     CHECK_STATUS (status, "cairo_scaled_font_create(NaN)");
216
217     cairo_scaled_font_destroy (scaled_font);
218
219     scaled_font = cairo_scaled_font_create (font_face,
220                                             &identity,
221                                             &bogus,
222                                             font_options);
223     status = cairo_scaled_font_status (scaled_font);
224     CHECK_STATUS (status, "cairo_scaled_font_create(NaN)");
225
226     cairo_scaled_font_destroy (scaled_font);
227     cairo_font_options_destroy (font_options);
228     cairo_destroy (cr2);
229
230     /* test cairo_scaled_font_create with ∞ matrix */
231     cr2 = cairo_create (target);
232     font_face = cairo_get_font_face (cr2);
233     font_options = cairo_font_options_create ();
234     cairo_get_font_options (cr, font_options);
235     scaled_font = cairo_scaled_font_create (font_face,
236                                             &inf,
237                                             &identity,
238                                             font_options);
239     status = cairo_scaled_font_status (scaled_font);
240     CHECK_STATUS (status, "cairo_scaled_font_create(infinity)");
241
242     cairo_scaled_font_destroy (scaled_font);
243
244     scaled_font = cairo_scaled_font_create (font_face,
245                                             &identity,
246                                             &inf,
247                                             font_options);
248     status = cairo_scaled_font_status (scaled_font);
249     CHECK_STATUS (status, "cairo_scaled_font_create(infinity)");
250
251     cairo_scaled_font_destroy (scaled_font);
252     cairo_font_options_destroy (font_options);
253     cairo_destroy (cr2);
254
255
256     /* test cairo_pattern_set_matrix with invalid matrix */
257     pattern = cairo_pattern_create_rgb (1.0, 1.0, 1.0);
258     cairo_pattern_set_matrix (pattern, &invalid);
259     status = cairo_pattern_status (pattern);
260     CHECK_STATUS (status, "cairo_pattern_set_matrix(invalid)");
261     cairo_pattern_destroy (pattern);
262
263     /* test cairo_pattern_set_matrix with bogus matrix */
264     pattern = cairo_pattern_create_rgb (1.0, 1.0, 1.0);
265     cairo_pattern_set_matrix (pattern, &bogus);
266     status = cairo_pattern_status (pattern);
267     CHECK_STATUS (status, "cairo_pattern_set_matrix(NaN)");
268     cairo_pattern_destroy (pattern);
269
270     /* test cairo_pattern_set_matrix with ∞ matrix */
271     pattern = cairo_pattern_create_rgb (1.0, 1.0, 1.0);
272     cairo_pattern_set_matrix (pattern, &inf);
273     status = cairo_pattern_status (pattern);
274     CHECK_STATUS (status, "cairo_pattern_set_matrix(infinity)");
275     cairo_pattern_destroy (pattern);
276
277
278     /* test invalid transformations */
279     cr2 = cairo_create (target);
280     cairo_translate (cr2, bogus.xx, bogus.yy);
281     CHECK_STATUS (status, "cairo_translate(NaN, NaN)");
282     cairo_destroy (cr2);
283
284     cr2 = cairo_create (target);
285     cairo_translate (cr2, 0, bogus.yy);
286     CHECK_STATUS (status, "cairo_translate(0, NaN)");
287     cairo_destroy (cr2);
288
289     cr2 = cairo_create (target);
290     cairo_translate (cr2, bogus.xx, 0);
291     CHECK_STATUS (status, "cairo_translate(NaN, 0)");
292     cairo_destroy (cr2);
293
294     cr2 = cairo_create (target);
295     cairo_translate (cr2, inf.xx, inf.yy);
296     CHECK_STATUS (status, "cairo_translate(∞, ∞)");
297     cairo_destroy (cr2);
298
299     cr2 = cairo_create (target);
300     cairo_translate (cr2, 0, inf.yy);
301     CHECK_STATUS (status, "cairo_translate(0, ∞)");
302     cairo_destroy (cr2);
303
304     cr2 = cairo_create (target);
305     cairo_translate (cr2, inf.xx, 0);
306     CHECK_STATUS (status, "cairo_translate(∞, 0)");
307     cairo_destroy (cr2);
308
309
310     cr2 = cairo_create (target);
311     cairo_scale (cr2, bogus.xx, bogus.yy);
312     CHECK_STATUS (status, "cairo_scale(NaN, NaN)");
313     cairo_destroy (cr2);
314
315     cr2 = cairo_create (target);
316     cairo_scale (cr2, 1, bogus.yy);
317     CHECK_STATUS (status, "cairo_scale(1, NaN)");
318     cairo_destroy (cr2);
319
320     cr2 = cairo_create (target);
321     cairo_scale (cr2, bogus.xx, 1);
322     CHECK_STATUS (status, "cairo_scale(NaN, 1)");
323     cairo_destroy (cr2);
324
325     cr2 = cairo_create (target);
326     cairo_scale (cr2, inf.xx, inf.yy);
327     CHECK_STATUS (status, "cairo_scale(∞, ∞)");
328     cairo_destroy (cr2);
329
330     cr2 = cairo_create (target);
331     cairo_scale (cr2, 1, inf.yy);
332     CHECK_STATUS (status, "cairo_scale(1, ∞)");
333     cairo_destroy (cr2);
334
335     cr2 = cairo_create (target);
336     cairo_scale (cr2, inf.xx, 1);
337     CHECK_STATUS (status, "cairo_scale(∞, 1)");
338     cairo_destroy (cr2);
339
340     cr2 = cairo_create (target);
341     cairo_scale (cr2, bogus.xx, bogus.yy);
342     CHECK_STATUS (status, "cairo_scale(0, 0)");
343     cairo_destroy (cr2);
344
345     cr2 = cairo_create (target);
346     cairo_scale (cr2, 1, bogus.yy);
347     CHECK_STATUS (status, "cairo_scale(1, 0)");
348     cairo_destroy (cr2);
349
350     cr2 = cairo_create (target);
351     cairo_scale (cr2, bogus.xx, 1);
352     CHECK_STATUS (status, "cairo_scale(0, 1)");
353     cairo_destroy (cr2);
354
355
356     cr2 = cairo_create (target);
357     cairo_rotate (cr2, bogus.xx);
358     CHECK_STATUS (status, "cairo_rotate(NaN)");
359     cairo_destroy (cr2);
360
361     cr2 = cairo_create (target);
362     cairo_rotate (cr2, inf.xx);
363     CHECK_STATUS (status, "cairo_rotate(∞)");
364     cairo_destroy (cr2);
365
366 #if HAVE_FECLEAREXCEPT
367     feclearexcept (FE_INVALID);
368 #endif
369
370     return CAIRO_TEST_SUCCESS;
371 }
372
373 CAIRO_TEST (invalid_matrix,
374             "Test that all relevant public functions return CAIRO_STATUS_INVALID_MATRIX as appropriate",
375             "api, matrix", /* keywords */
376             NULL, /* requirements */
377             0, 0,
378             NULL, draw)