1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 static CpmlPair org
= { .x
= 0, .y
= 0 };
27 static CpmlPair nord
= { .x
= 0, .y
= 1 };
28 static CpmlPair diag
= { .x
= -3, .y
= -4 };
29 static CpmlPair diag3
= { .x
= -9, .y
= -12 };
30 static CpmlPair junk
= { .x
= -12345.54321, .y
= 9876543210.123456789 };
34 _cmpl_behavior_misc(void)
37 cairo_path_data_t cairo_pair
;
40 equals
= cpml_pair_equal(&org
, &junk
);
41 g_assert_false(equals
);
43 equals
= cpml_pair_equal(&org
, NULL
);
44 g_assert_false(equals
);
46 equals
= cpml_pair_equal(NULL
, NULL
);
47 g_assert_true(equals
);
49 /* Just check the following calls will not crash */
50 cpml_pair_copy(&pair
, NULL
);
51 cpml_pair_copy(&pair
, &pair
);
53 cpml_pair_copy(&pair
, &org
);
54 equals
= cpml_pair_equal(&pair
, &nord
);
55 g_assert_false(equals
);
57 cpml_pair_to_cairo(&nord
, &cairo_pair
);
58 cpml_pair_from_cairo(&pair
, &cairo_pair
);
59 equals
= cpml_pair_equal(&pair
, &nord
);
60 g_assert_true(equals
);
62 cpml_pair_copy(&pair
, NULL
);
63 equals
= cpml_pair_equal(&pair
, &nord
);
64 g_assert_true(equals
);
66 cpml_pair_copy(&pair
, &diag
);
67 equals
= cpml_pair_equal(&pair
, &nord
);
68 g_assert_false(equals
);
70 cpml_pair_to_cairo(&org
, &cairo_pair
);
71 cpml_pair_from_cairo(&pair
, &cairo_pair
);
72 equals
= cpml_pair_equal(&pair
, &org
);
73 g_assert_true(equals
);
77 _cpml_method_pair_transform(void)
80 cairo_matrix_t matrix
;
82 g_test_message("Checking for translation transformations...");
83 cairo_matrix_init_translate(&matrix
, junk
.x
, junk
.y
);
84 cpml_pair_copy(&pair
, &org
);
85 cpml_pair_transform(&pair
, &matrix
);
86 adg_assert_isapprox(pair
.x
, junk
.x
);
87 adg_assert_isapprox(pair
.y
, junk
.y
);
89 g_test_message("Checking for scaling transformations...");
90 cairo_matrix_init_scale(&matrix
, 3, 3);
91 cpml_pair_copy(&pair
, &diag
);
92 cpml_pair_transform(&pair
, &matrix
);
93 adg_assert_isapprox(pair
.x
, diag3
.x
);
94 adg_assert_isapprox(pair
.y
, diag3
.y
);
96 g_test_message("Checking for assorted transformations...");
97 cairo_matrix_init_scale(&matrix
, 3, 3);
98 cairo_matrix_translate(&matrix
, diag
.x
, diag
.y
);
99 cairo_matrix_translate(&matrix
, -junk
.x
, -junk
.y
);
100 cpml_pair_copy(&pair
, &junk
);
101 cpml_pair_transform(&pair
, &matrix
);
102 adg_assert_isapprox(pair
.x
, diag3
.x
);
103 adg_assert_isapprox(pair
.y
, diag3
.y
);
107 _cpml_method_distance(void)
109 double distance
, squared_distance
;
111 distance
= cpml_pair_distance(NULL
, NULL
);
112 adg_assert_isapprox(distance
, 0);
114 distance
= cpml_pair_distance(&org
, NULL
);
115 adg_assert_isapprox(distance
, 0);
117 distance
= cpml_pair_distance(NULL
, &org
);
118 adg_assert_isapprox(distance
, 0);
120 distance
= cpml_pair_distance(&nord
, &org
);
121 adg_assert_isapprox(distance
, 1);
123 squared_distance
= cpml_pair_squared_distance(NULL
, NULL
);
124 adg_assert_isapprox(squared_distance
, 0);
126 squared_distance
= cpml_pair_squared_distance(&org
, NULL
);
127 adg_assert_isapprox(squared_distance
, 0);
129 squared_distance
= cpml_pair_squared_distance(NULL
, &org
);
130 adg_assert_isapprox(squared_distance
, 0);
132 squared_distance
= cpml_pair_squared_distance(&nord
, &org
);
133 adg_assert_isapprox(squared_distance
, 1);
137 _cpml_method_angle(void)
139 double angle
, angle2
;
142 angle
= cpml_vector_angle(&nord
);
143 adg_assert_isapprox(angle
, M_PI_2
);
146 cpml_vector_from_angle(&vector
, angle
);
147 adg_assert_isapprox(vector
.x
, nord
.x
);
148 adg_assert_isapprox(vector
.y
, nord
.y
);
150 angle
= cpml_vector_angle(&diag
);
151 angle2
= cpml_vector_angle(&diag3
);
152 adg_assert_isapprox(angle
, angle2
);
155 cpml_vector_from_angle(&vector
, angle
);
156 angle2
= cpml_vector_angle(&vector
);
157 adg_assert_isapprox(angle
, angle2
);
159 g_test_message("By convention, the vector (0,0) is considered a 0° angle");
160 angle
= cpml_vector_angle(&org
);
161 adg_assert_isapprox(angle
, 0);
163 cpml_pair_copy(&vector
, &org
);
164 cpml_vector_normal(&vector
);
165 adg_assert_isapprox(vector
.x
, org
.x
);
166 adg_assert_isapprox(vector
.y
, org
.y
);
168 g_test_message("Checking cpml_vector_normal() API...");
171 cpml_vector_normal(&vector
);
172 adg_assert_isapprox(vector
.x
, -1);
173 adg_assert_isapprox(vector
.y
, 1);
175 cpml_vector_normal(&vector
);
176 adg_assert_isapprox(vector
.x
, -1);
177 adg_assert_isapprox(vector
.y
, -1);
179 cpml_vector_normal(&vector
);
180 adg_assert_isapprox(vector
.x
, 1);
181 adg_assert_isapprox(vector
.y
, -1);
183 cpml_vector_normal(&vector
);
184 adg_assert_isapprox(vector
.x
, 1);
185 adg_assert_isapprox(vector
.y
, 1);
189 _cpml_method_length(void)
194 cpml_pair_copy(&vector
, &junk
);
195 cpml_vector_set_length(&vector
, 0);
196 adg_assert_isapprox(vector
.x
, org
.x
);
197 adg_assert_isapprox(vector
.y
, org
.y
);
199 cpml_vector_set_length(&vector
, 1234);
200 adg_assert_isapprox(vector
.x
, org
.x
);
201 adg_assert_isapprox(vector
.y
, org
.y
);
203 cpml_pair_copy(&vector
, &diag3
);
204 cpml_vector_set_length(&vector
, 5);
205 adg_assert_isapprox(vector
.x
, diag
.x
);
206 adg_assert_isapprox(vector
.y
, diag
.y
);
208 g_test_message("Using integer comparison to overcome rounding errors");
210 cpml_vector_set_length(&vector
, 10);
211 length
= cpml_pair_distance(&vector
, NULL
);
212 g_assert_cmpint(round(length
), ==, 10);
214 cpml_vector_set_length(&vector
, 5);
215 g_assert_cmpint(round(vector
.x
), ==, diag
.x
);
216 g_assert_cmpint(round(vector
.y
), ==, diag
.y
);
220 _cpml_method_vector_transform(void)
223 cairo_matrix_t matrix
;
225 g_test_message("Vectors are not affected by translations");
226 cairo_matrix_init_translate(&matrix
, junk
.x
, junk
.y
);
227 cpml_pair_copy(&vector
, &org
);
228 cpml_vector_transform(&vector
, &matrix
);
229 adg_assert_isapprox(vector
.x
, org
.x
);
230 adg_assert_isapprox(vector
.y
, org
.y
);
232 g_test_message("Checking scaling transformations...");
233 cairo_matrix_init_scale(&matrix
, 3, 3);
234 cpml_pair_copy(&vector
, &diag
);
235 cpml_vector_transform(&vector
, &matrix
);
236 adg_assert_isapprox(vector
.x
, diag3
.x
);
237 adg_assert_isapprox(vector
.y
, diag3
.y
);
239 g_test_message("Checking assorted transformations...");
240 cairo_matrix_init_scale(&matrix
, 3, 3);
241 cairo_matrix_translate(&matrix
, diag
.x
, diag
.y
);
242 cairo_matrix_translate(&matrix
, -junk
.x
, -junk
.y
);
243 cpml_pair_copy(&vector
, &diag
);
244 cpml_vector_transform(&vector
, &matrix
);
245 adg_assert_isapprox(vector
.x
, diag3
.x
);
246 adg_assert_isapprox(vector
.y
, diag3
.y
);
251 main(int argc
, char *argv
[])
253 adg_test_init(&argc
, &argv
);
255 g_test_add_func("/cpml/pair/behavior/misc", _cmpl_behavior_misc
);
257 g_test_add_func("/cpml/pair/method/transform", _cpml_method_pair_transform
);
258 g_test_add_func("/cpml/pair/method/distance", _cpml_method_distance
);
259 g_test_add_func("/cpml/vector/method/angle", _cpml_method_angle
);
260 g_test_add_func("/cpml/vector/method/length", _cpml_method_length
);
261 g_test_add_func("/cpml/vector/method/transform", _cpml_method_vector_transform
);