1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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.
21 #include "test-internal.h"
25 static CpmlPair org
= { .x
= 0, .y
= 0 };
26 static CpmlPair nord
= { .x
= 0, .y
= 1 };
27 static CpmlPair diag
= { .x
= -3, .y
= -4 };
28 static CpmlPair diag3
= { .x
= -9, .y
= -12 };
29 static CpmlPair junk
= { .x
= -12345.54321, .y
= 9876543210.123456789 };
33 _cpml_test_pair_basic(void)
36 cairo_path_data_t cairo_pair
;
39 equals
= cpml_pair_equal(&org
, &junk
);
42 equals
= cpml_pair_equal(&org
, NULL
);
45 equals
= cpml_pair_equal(NULL
, NULL
);
48 /* Just check the following calls will not crash */
49 cpml_pair_copy(&pair
, NULL
);
50 cpml_pair_copy(&pair
, &pair
);
52 cpml_pair_copy(&pair
, &org
);
53 equals
= cpml_pair_equal(&pair
, &nord
);
56 cpml_pair_to_cairo(&nord
, &cairo_pair
);
57 cpml_pair_from_cairo(&pair
, &cairo_pair
);
58 equals
= cpml_pair_equal(&pair
, &nord
);
61 cpml_pair_copy(&pair
, NULL
);
62 equals
= cpml_pair_equal(&pair
, &nord
);
65 cpml_pair_copy(&pair
, &diag
);
66 equals
= cpml_pair_equal(&pair
, &nord
);
69 cpml_pair_to_cairo(&org
, &cairo_pair
);
70 cpml_pair_from_cairo(&pair
, &cairo_pair
);
71 equals
= cpml_pair_equal(&pair
, &org
);
76 _cpml_test_pair_transform(void)
79 cairo_matrix_t matrix
;
81 g_test_message("Checking for translation transformations...");
82 cairo_matrix_init_translate(&matrix
, junk
.x
, junk
.y
);
83 cpml_pair_copy(&pair
, &org
);
84 cpml_pair_transform(&pair
, &matrix
);
85 g_assert_cmpfloat(pair
.x
, ==, junk
.x
);
86 g_assert_cmpfloat(pair
.y
, ==, junk
.y
);
88 g_test_message("Checking for scaling transformations...");
89 cairo_matrix_init_scale(&matrix
, 3, 3);
90 cpml_pair_copy(&pair
, &diag
);
91 cpml_pair_transform(&pair
, &matrix
);
92 g_assert_cmpfloat(pair
.x
, ==, diag3
.x
);
93 g_assert_cmpfloat(pair
.y
, ==, diag3
.y
);
95 g_test_message("Checking for assorted transformations...");
96 cairo_matrix_init_scale(&matrix
, 3, 3);
97 cairo_matrix_translate(&matrix
, diag
.x
, diag
.y
);
98 cairo_matrix_translate(&matrix
, -junk
.x
, -junk
.y
);
99 cpml_pair_copy(&pair
, &junk
);
100 cpml_pair_transform(&pair
, &matrix
);
101 g_assert_cmpfloat(pair
.x
, ==, diag3
.x
);
102 g_assert_cmpfloat(pair
.y
, ==, diag3
.y
);
106 _cpml_test_pair_distance(void)
108 double distance
, squared_distance
;
110 distance
= cpml_pair_distance(NULL
, NULL
);
111 g_assert_cmpfloat(distance
, ==, 0);
113 distance
= cpml_pair_distance(&org
, NULL
);
114 g_assert_cmpfloat(distance
, ==, 0);
116 distance
= cpml_pair_distance(NULL
, &org
);
117 g_assert_cmpfloat(distance
, ==, 0);
119 distance
= cpml_pair_distance(&nord
, &org
);
120 g_assert_cmpfloat(distance
, ==, 1);
122 squared_distance
= cpml_pair_squared_distance(NULL
, NULL
);
123 g_assert_cmpfloat(squared_distance
, ==, 0);
125 squared_distance
= cpml_pair_squared_distance(&org
, NULL
);
126 g_assert_cmpfloat(squared_distance
, ==, 0);
128 squared_distance
= cpml_pair_squared_distance(NULL
, &org
);
129 g_assert_cmpfloat(squared_distance
, ==, 0);
131 squared_distance
= cpml_pair_squared_distance(&nord
, &org
);
132 g_assert_cmpfloat(squared_distance
, ==, 1);
136 _cpml_test_vector_angle(void)
138 double angle
, angle2
;
141 angle
= cpml_vector_angle(&nord
);
142 g_assert_cmpfloat(angle
, ==, M_PI_2
);
145 cpml_vector_from_angle(&vector
, angle
);
146 g_assert_cmpfloat(vector
.x
, ==, nord
.x
);
147 g_assert_cmpfloat(vector
.y
, ==, nord
.y
);
149 angle
= cpml_vector_angle(&diag
);
150 angle2
= cpml_vector_angle(&diag3
);
151 g_assert_cmpfloat(angle
, ==, angle2
);
154 cpml_vector_from_angle(&vector
, angle
);
155 angle2
= cpml_vector_angle(&vector
);
156 g_assert_cmpfloat(angle
, ==, angle2
);
158 g_test_message("By convention, the vector (0,0) is considered a 0° angle");
159 angle
= cpml_vector_angle(&org
);
160 g_assert_cmpfloat(angle
, ==, 0);
162 cpml_pair_copy(&vector
, &org
);
163 cpml_vector_normal(&vector
);
164 g_assert_cmpfloat(vector
.x
, ==, org
.x
);
165 g_assert_cmpfloat(vector
.y
, ==, org
.y
);
167 g_test_message("Checking cpml_vector_normal() API...");
170 cpml_vector_normal(&vector
);
171 g_assert_cmpfloat(vector
.x
, ==, -1);
172 g_assert_cmpfloat(vector
.y
, ==, 1);
174 cpml_vector_normal(&vector
);
175 g_assert_cmpfloat(vector
.x
, ==, -1);
176 g_assert_cmpfloat(vector
.y
, ==, -1);
178 cpml_vector_normal(&vector
);
179 g_assert_cmpfloat(vector
.x
, ==, 1);
180 g_assert_cmpfloat(vector
.y
, ==, -1);
182 cpml_vector_normal(&vector
);
183 g_assert_cmpfloat(vector
.x
, ==, 1);
184 g_assert_cmpfloat(vector
.y
, ==, 1);
188 _cpml_test_vector_length(void)
193 cpml_pair_copy(&vector
, &junk
);
194 cpml_vector_set_length(&vector
, 0);
195 g_assert_cmpfloat(vector
.x
, ==, org
.x
);
196 g_assert_cmpfloat(vector
.y
, ==, org
.y
);
198 cpml_vector_set_length(&vector
, 1234);
199 g_assert_cmpfloat(vector
.x
, ==, org
.x
);
200 g_assert_cmpfloat(vector
.y
, ==, org
.y
);
202 cpml_pair_copy(&vector
, &diag3
);
203 cpml_vector_set_length(&vector
, 5);
204 g_assert_cmpfloat(vector
.x
, ==, diag
.x
);
205 g_assert_cmpfloat(vector
.y
, ==, diag
.y
);
207 g_test_message("Using integer comparison to overcome rounding errors");
209 cpml_vector_set_length(&vector
, 10);
210 length
= cpml_pair_distance(&vector
, NULL
);
211 g_assert_cmpint(round(length
), ==, 10);
213 cpml_vector_set_length(&vector
, 5);
214 g_assert_cmpint(round(vector
.x
), ==, diag
.x
);
215 g_assert_cmpint(round(vector
.y
), ==, diag
.y
);
219 _cpml_test_vector_transform(void)
222 cairo_matrix_t matrix
;
224 g_test_message("Vectors are not affected by translations");
225 cairo_matrix_init_translate(&matrix
, junk
.x
, junk
.y
);
226 cpml_pair_copy(&vector
, &org
);
227 cpml_vector_transform(&vector
, &matrix
);
228 g_assert_cmpfloat(vector
.x
, ==, org
.x
);
229 g_assert_cmpfloat(vector
.y
, ==, org
.y
);
231 g_test_message("Checking scaling transformations...");
232 cairo_matrix_init_scale(&matrix
, 3, 3);
233 cpml_pair_copy(&vector
, &diag
);
234 cpml_vector_transform(&vector
, &matrix
);
235 g_assert_cmpfloat(vector
.x
, ==, diag3
.x
);
236 g_assert_cmpfloat(vector
.y
, ==, diag3
.y
);
238 g_test_message("Checking assorted transformations...");
239 cairo_matrix_init_scale(&matrix
, 3, 3);
240 cairo_matrix_translate(&matrix
, diag
.x
, diag
.y
);
241 cairo_matrix_translate(&matrix
, -junk
.x
, -junk
.y
);
242 cpml_pair_copy(&vector
, &diag
);
243 cpml_vector_transform(&vector
, &matrix
);
244 g_assert_cmpfloat(vector
.x
, ==, diag3
.x
);
245 g_assert_cmpfloat(vector
.y
, ==, diag3
.y
);
250 main(int argc
, char *argv
[])
252 cpml_test_init(&argc
, &argv
);
254 cpml_test_add_func("/cpml/pair/basic", _cpml_test_pair_basic
);
255 cpml_test_add_func("/cpml/pair/transform", _cpml_test_pair_transform
);
256 cpml_test_add_func("/cpml/pair/distance", _cpml_test_pair_distance
);
257 cpml_test_add_func("/cpml/vector/angle", _cpml_test_vector_angle
);
258 cpml_test_add_func("/cpml/vector/length", _cpml_test_vector_length
);
259 cpml_test_add_func("/cpml/vector/transform", _cpml_test_vector_transform
);