e31ada87f5f2ada35990e6150d94c47b3ea49a35
[adg.git] / src / cpml / tests / test-pair.c
blobe31ada87f5f2ada35990e6150d94c47b3ea49a35
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"
22 #include <math.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 };
32 static void
33 _cpml_test_pair_basic(void)
35 cairo_bool_t equals;
36 cairo_path_data_t cairo_pair;
37 CpmlPair pair;
39 equals = cpml_pair_equal(&org, &junk);
40 g_assert(! equals);
42 equals = cpml_pair_equal(&org, NULL);
43 g_assert(! equals);
45 equals = cpml_pair_equal(NULL, NULL);
46 g_assert(equals);
48 /* Just check the following calls will not crash */
49 cpml_pair_copy(NULL, &pair);
50 cpml_pair_copy(NULL, NULL);
51 cpml_pair_copy(&pair, NULL);
53 cpml_pair_copy(&pair, &org);
54 equals = cpml_pair_equal(&pair, &nord);
55 g_assert(! 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(equals);
62 cpml_pair_copy(&pair, NULL);
63 equals = cpml_pair_equal(&pair, &nord);
64 g_assert(equals);
66 cpml_pair_copy(&pair, &diag);
67 equals = cpml_pair_equal(&pair, &nord);
68 g_assert(! 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(equals);
76 static void
77 _cpml_test_pair_transform(void)
79 CpmlPair pair;
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 g_assert_cmpfloat(pair.x, ==, junk.x);
87 g_assert_cmpfloat(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 g_assert_cmpfloat(pair.x, ==, diag3.x);
94 g_assert_cmpfloat(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 g_assert_cmpfloat(pair.x, ==, diag3.x);
103 g_assert_cmpfloat(pair.y, ==, diag3.y);
106 static void
107 _cpml_test_pair_distance(void)
109 double distance, squared_distance;
111 distance = cpml_pair_distance(NULL, NULL);
112 g_assert_cmpfloat(distance, ==, 0);
114 distance = cpml_pair_distance(&org, NULL);
115 g_assert_cmpfloat(distance, ==, 0);
117 distance = cpml_pair_distance(NULL, &org);
118 g_assert_cmpfloat(distance, ==, 0);
120 distance = cpml_pair_distance(&nord, &org);
121 g_assert_cmpfloat(distance, ==, 1);
123 squared_distance = cpml_pair_squared_distance(NULL, NULL);
124 g_assert_cmpfloat(squared_distance, ==, 0);
126 squared_distance = cpml_pair_squared_distance(&org, NULL);
127 g_assert_cmpfloat(squared_distance, ==, 0);
129 squared_distance = cpml_pair_squared_distance(NULL, &org);
130 g_assert_cmpfloat(squared_distance, ==, 0);
132 squared_distance = cpml_pair_squared_distance(&nord, &org);
133 g_assert_cmpfloat(squared_distance, ==, 1);
136 static void
137 _cpml_test_vector_angle(void)
139 double angle, angle2;
140 CpmlVector vector;
142 angle = cpml_vector_angle(&nord);
143 g_assert_cmpfloat(angle, ==, M_PI_2);
145 angle = M_PI_2;
146 cpml_vector_from_angle(&vector, angle);
147 g_assert_cmpfloat(vector.x, ==, nord.x);
148 g_assert_cmpfloat(vector.y, ==, nord.y);
150 angle = cpml_vector_angle(&diag);
151 angle2 = cpml_vector_angle(&diag3);
152 g_assert_cmpfloat(angle, ==, angle2);
154 angle = 1.234567;
155 cpml_vector_from_angle(&vector, angle);
156 angle2 = cpml_vector_angle(&vector);
157 g_assert_cmpfloat(angle, ==, angle2);
159 g_test_message("By convention, the vector (0,0) is considered a 0° angle");
160 angle = cpml_vector_angle(&org);
161 g_assert_cmpfloat(angle, ==, 0);
163 cpml_pair_copy(&vector, &org);
164 cpml_vector_normal(&vector);
165 g_assert_cmpfloat(vector.x, ==, org.x);
166 g_assert_cmpfloat(vector.y, ==, org.y);
168 g_test_message("Checking cpml_vector_normal() API...");
169 vector.x = 1;
170 vector.y = 1;
171 cpml_vector_normal(&vector);
172 g_assert_cmpfloat(vector.x, ==, -1);
173 g_assert_cmpfloat(vector.y, ==, 1);
175 cpml_vector_normal(&vector);
176 g_assert_cmpfloat(vector.x, ==, -1);
177 g_assert_cmpfloat(vector.y, ==, -1);
179 cpml_vector_normal(&vector);
180 g_assert_cmpfloat(vector.x, ==, 1);
181 g_assert_cmpfloat(vector.y, ==, -1);
183 cpml_vector_normal(&vector);
184 g_assert_cmpfloat(vector.x, ==, 1);
185 g_assert_cmpfloat(vector.y, ==, 1);
188 static void
189 _cpml_test_vector_length(void)
191 CpmlVector vector;
192 double length;
194 cpml_pair_copy(&vector, &junk);
195 cpml_vector_set_length(&vector, 0);
196 g_assert_cmpfloat(vector.x, ==, org.x);
197 g_assert_cmpfloat(vector.y, ==, org.y);
199 cpml_vector_set_length(&vector, 1234);
200 g_assert_cmpfloat(vector.x, ==, org.x);
201 g_assert_cmpfloat(vector.y, ==, org.y);
203 cpml_pair_copy(&vector, &diag3);
204 cpml_vector_set_length(&vector, 5);
205 g_assert_cmpfloat(vector.x, ==, diag.x);
206 g_assert_cmpfloat(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);
219 static void
220 _cpml_test_vector_transform(void)
222 CpmlVector vector;
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 g_assert_cmpfloat(vector.x, ==, org.x);
230 g_assert_cmpfloat(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 g_assert_cmpfloat(vector.x, ==, diag3.x);
237 g_assert_cmpfloat(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 g_assert_cmpfloat(vector.x, ==, diag3.x);
246 g_assert_cmpfloat(vector.y, ==, diag3.y);
251 main(int argc, char *argv[])
253 cpml_test_init(&argc, &argv);
255 cpml_test_add_func("/cpml/pair/basic", _cpml_test_pair_basic);
256 cpml_test_add_func("/cpml/pair/transform", _cpml_test_pair_transform);
257 cpml_test_add_func("/cpml/pair/distance", _cpml_test_pair_distance);
258 cpml_test_add_func("/cpml/vector/angle", _cpml_test_vector_angle);
259 cpml_test_add_func("/cpml/vector/length", _cpml_test_vector_length);
260 cpml_test_add_func("/cpml/vector/transform", _cpml_test_vector_transform);
262 return g_test_run();