a3d75442c6ee49c96c951080ff0815f0a9aade33
[framework/graphics/cairo.git] / test / dash-infinite-loop.c
1 /*
2  * Copyright © 2009 M Joonas Pihlaja
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: M Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
25  */
26
27 #include "cairo-test.h"
28
29 /* When faced with very small dash lengths the stroker is liable to
30  * get stuck in an infinite loop when advancing the dash offset.  This
31  * test attempts to hit each of the locations in the stroker code
32  * where the dash offset is advanced in a loop.
33  *
34  * Reported to the cairo mailing list by Hans Breuer.
35  * http://lists.cairographics.org/archives/cairo/2009-June/017506.html
36  */
37
38 #define EPS 1e-30
39 /* This should be comfortably smaller than the unit epsilon of the
40  * floating point type used to advance the dashing, yet not small
41  * enough that it underflows to zero.  1e-30 works to foil up to 80
42  * bit extended precision arithmetic.  We want to avoid zero dash
43  * lengths because those trigger special processing in the stroker. */
44
45 static void
46 do_dash (cairo_t *cr, double dx, double dy, double offset)
47 {
48     /* Set the dash pattern to be predominantly ON so that we can
49      * create a reference image by just ignoring the dashing. */
50     static double dash[] = { EPS, EPS/512 };
51     cairo_set_dash (cr, dash, 2, offset);
52     cairo_move_to (cr, 10, 10);
53     cairo_rel_line_to (cr, dx, dy);
54     cairo_stroke (cr);
55     cairo_translate (cr, dx, dy);
56 }
57
58 static cairo_test_status_t
59 draw (cairo_t *cr, int width, int height)
60 {
61     (void)width; (void)height;
62
63     cairo_set_source_rgb (cr, 1,1,1);
64     cairo_paint (cr);
65     cairo_set_source_rgb (cr, 0,0,0);
66
67     cairo_set_line_width (cr, 10);
68
69     /* The following calls will wedge in various places that try
70      * to advance the dashing in a loop inside the stroker. */
71     do_dash (cr, 30, 30, 0); /* _cairo_stroker_line_to_dashed */
72     do_dash (cr, 30,  0, 0); /* _cairo_rectilinear_stroker_line_to_dashed */
73     do_dash (cr, 30, 30, 1); /* _cairo_stroker_dash_start */
74
75     return CAIRO_TEST_SUCCESS;
76 }
77
78 CAIRO_TEST (dash_infinite_loop,
79             "Test dashing with extremely small dash lengths.",
80             "dash",
81             NULL,
82             100, 100,
83             NULL, draw);