1
/*
2
 * Copyright © 2008 Chris Wilson
3
 *
4
 * Permission to use, copy, modify, distribute, and sell this software
5
 * and its documentation for any purpose is hereby granted without
6
 * fee, provided that the above copyright notice appear in all copies
7
 * and that both that copyright notice and this permission notice
8
 * appear in supporting documentation, and that the name of
9
 * Chris Wilson not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Chris Wilson makes no representations about the
12
 * suitability of this software for any purpose.  It is provided "as
13
 * is" without express or implied warranty.
14
 *
15
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
18
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
 *
23
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
24
 *
25
 * Based on an example by Dirk "krit" Schulze found during WebKit integration.
26
 */
27
 
28
 /*  Copyright (C) 2021 Heiko Lewin <hlewin@gmx.de> 
29
  *  Added error margin for point comparisons */
30

            
31
#include "cairo-test.h"
32
#include "cairo-fixed-type-private.h"
33

            
34
/* we know that this is an inherent limitation in cairo */
35
#define FAIL CAIRO_TEST_XFAILURE
36

            
37
/* Test the idempotency of path construction and copying */
38

            
39

            
40
/* The error to be expected from double<->fixed conversions */
41
#define EXPECTED_ERROR (0.5 / (1<<CAIRO_FIXED_FRAC_BITS))
42

            
43
15
static int compare_points( double *p1, double *p2 ) {
44
45
    for(int i=0; i<2; ++i) {
45
30
	double error = fabs(p2[i]-p1[i]);
46
30
	if(error > EXPECTED_ERROR) {
47
	    return 1;
48
	}
49
    }
50
15
    return 0;
51
}
52

            
53
static cairo_test_status_t
54
3
draw (cairo_t *cr, int width, int height)
55
{
56
3
    cairo_path_data_t path_data[] = {
57
	{ { CAIRO_PATH_MOVE_TO, 2 }, },
58
	{ .point={ 95.000000, 40.000000 }, },
59

            
60
	{ { CAIRO_PATH_LINE_TO, 2 }, },
61
	{ .point={ 94.960533, 41.255810 }, },
62

            
63
	{ { CAIRO_PATH_LINE_TO, 2 }, },
64
	{ .point={ 94.842293, 42.50666 }, },
65

            
66
	{ { CAIRO_PATH_LINE_TO, 2 }, },
67
	{ .point={ 94.645744, 43.747627 }, },
68

            
69
	{ { CAIRO_PATH_LINE_TO, 2 }, },
70
	{ .point={ 94.371666, 44.973797 }, },
71
    };
72
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
73
    cairo_path_t path, *path_copy;
74
    int i, j, n;
75
3
    cairo_test_status_t result = CAIRO_TEST_SUCCESS;
76

            
77
3
    path.status = CAIRO_STATUS_SUCCESS;
78
3
    path.num_data = ARRAY_LENGTH (path_data);
79
3
    path.data = path_data;
80

            
81
3
    cairo_new_path (cr);
82
3
    cairo_append_path (cr, &path);
83
3
    path_copy = cairo_copy_path (cr);
84

            
85
3
    if (path_copy->status)
86
	return cairo_test_status_from_status (ctx, path_copy->status);
87

            
88
3
    for (i = j = n = 0;
89
18
	 i < path.num_data && j < path_copy->num_data;
90
15
	 i += path.data[i].header.length,
91
15
	 j += path_copy->data[j].header.length,
92
15
	 n++)
93
    {
94
	const cairo_path_data_t *src, *dst;
95

            
96
15
	src = &path.data[i];
97
15
	dst = &path_copy->data[j];
98

            
99
15
	if (src->header.type != dst->header.type) {
100
	    cairo_test_log (ctx,
101
			    "Paths differ in header type after %d operations.\n"
102
			    "Expected path operation %d, found %d.\n",
103
			    n, src->header.type, dst->header.type);
104
	    result = FAIL;
105
	    break;
106
	}
107

            
108
15
	if (compare_points ((double*)&src[1].point, (double*)&dst[1].point)) {
109
	    cairo_test_log (ctx,
110
			    "Paths differ in coordinates after %d operations.\n"
111
			    "Expected point (%f, %f), found (%f, %f).\n",
112
			    n,
113
			    src[1].point.x, src[1].point.y,
114
			    dst[1].point.x, dst[1].point.y);
115
	    result = FAIL;
116
	    break;
117
	}
118
    }
119

            
120
3
    cairo_path_destroy (path_copy);
121
3
    return result;
122
}
123

            
124
1
CAIRO_TEST (path_precision,
125
	    "Check that the path append/copy is idempotent.",
126
	    "api", /* keywords */
127
	    NULL, /* requirements */
128
	    0, 0,
129
	    NULL, draw)