docs: updated copyright to 2012
[adg.git] / src / cpml / tests / test-pair.c
blobe0fa7cf700908907e9f47983b5b904a618f916c2
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2011,2012 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 g_test_message("Careful: manipulation APIs are not guarded against NULLs");
41 equals = cpml_pair_equal(&org, &junk);
42 g_assert(! equals);
44 equals = cpml_pair_equal(&org, NULL);
45 g_assert(! equals);
47 equals = cpml_pair_equal(NULL, NULL);
48 g_assert(equals);
50 cpml_pair_copy(&pair, &org);
51 equals = cpml_pair_equal(&pair, &nord);
52 g_assert(! equals);
54 cpml_pair_to_cairo(&nord, &cairo_pair);
55 cpml_pair_from_cairo(&pair, &cairo_pair);
56 equals = cpml_pair_equal(&pair, &nord);
57 g_assert(equals);
59 cpml_pair_copy(&pair, &diag);
60 equals = cpml_pair_equal(&pair, &nord);
61 g_assert(! equals);
63 cpml_pair_to_cairo(&org, &cairo_pair);
64 cpml_pair_from_cairo(&pair, &cairo_pair);
65 equals = cpml_pair_equal(&pair, &org);
66 g_assert(equals);
69 static void
70 _cpml_test_pair_transform(void)
72 CpmlPair pair;
73 cairo_matrix_t matrix;
75 g_test_message("Checking for translation transformations...");
76 cairo_matrix_init_translate(&matrix, junk.x, junk.y);
77 cpml_pair_copy(&pair, &org);
78 cpml_pair_transform(&pair, &matrix);
79 g_assert_cmpfloat(pair.x, ==, junk.x);
80 g_assert_cmpfloat(pair.y, ==, junk.y);
82 g_test_message("Checking for scaling transformations...");
83 cairo_matrix_init_scale(&matrix, 3, 3);
84 cpml_pair_copy(&pair, &diag);
85 cpml_pair_transform(&pair, &matrix);
86 g_assert_cmpfloat(pair.x, ==, diag3.x);
87 g_assert_cmpfloat(pair.y, ==, diag3.y);
89 g_test_message("Checking for assorted transformations...");
90 cairo_matrix_init_scale(&matrix, 3, 3);
91 cairo_matrix_translate(&matrix, diag.x, diag.y);
92 cairo_matrix_translate(&matrix, -junk.x, -junk.y);
93 cpml_pair_copy(&pair, &junk);
94 cpml_pair_transform(&pair, &matrix);
95 g_assert_cmpfloat(pair.x, ==, diag3.x);
96 g_assert_cmpfloat(pair.y, ==, diag3.y);
99 static void
100 _cpml_test_pair_distance(void)
102 double distance, squared_distance;
104 distance = cpml_pair_distance(NULL, NULL);
105 g_assert_cmpfloat(distance, ==, 0);
107 distance = cpml_pair_distance(&org, NULL);
108 g_assert_cmpfloat(distance, ==, 0);
110 distance = cpml_pair_distance(NULL, &org);
111 g_assert_cmpfloat(distance, ==, 0);
113 distance = cpml_pair_distance(&nord, &org);
114 g_assert_cmpfloat(distance, ==, 1);
116 squared_distance = cpml_pair_squared_distance(NULL, NULL);
117 g_assert_cmpfloat(squared_distance, ==, 0);
119 squared_distance = cpml_pair_squared_distance(&org, NULL);
120 g_assert_cmpfloat(squared_distance, ==, 0);
122 squared_distance = cpml_pair_squared_distance(NULL, &org);
123 g_assert_cmpfloat(squared_distance, ==, 0);
125 squared_distance = cpml_pair_squared_distance(&nord, &org);
126 g_assert_cmpfloat(squared_distance, ==, 1);
129 static void
130 _cpml_test_vector_angle(void)
132 double angle, angle2;
133 CpmlVector vector;
135 angle = cpml_vector_angle(&nord);
136 g_assert_cmpfloat(angle, ==, M_PI_2);
138 angle = M_PI_2;
139 cpml_vector_from_angle(&vector, angle);
140 g_assert_cmpfloat(vector.x, ==, nord.x);
141 g_assert_cmpfloat(vector.y, ==, nord.y);
143 angle = cpml_vector_angle(&diag);
144 angle2 = cpml_vector_angle(&diag3);
145 g_assert_cmpfloat(angle, ==, angle2);
147 angle = 1.234567;
148 cpml_vector_from_angle(&vector, angle);
149 angle2 = cpml_vector_angle(&vector);
150 g_assert_cmpfloat(angle, ==, angle2);
152 g_test_message("By convention, the vector (0,0) is considered a 0° angle");
153 angle = cpml_vector_angle(&org);
154 g_assert_cmpfloat(angle, ==, 0);
156 cpml_pair_copy(&vector, &org);
157 cpml_vector_normal(&vector);
158 g_assert_cmpfloat(vector.x, ==, org.x);
159 g_assert_cmpfloat(vector.y, ==, org.y);
161 g_test_message("Checking cpml_vector_normal() API...");
162 vector.x = 1;
163 vector.y = 1;
164 cpml_vector_normal(&vector);
165 g_assert_cmpfloat(vector.x, ==, -1);
166 g_assert_cmpfloat(vector.y, ==, 1);
168 cpml_vector_normal(&vector);
169 g_assert_cmpfloat(vector.x, ==, -1);
170 g_assert_cmpfloat(vector.y, ==, -1);
172 cpml_vector_normal(&vector);
173 g_assert_cmpfloat(vector.x, ==, 1);
174 g_assert_cmpfloat(vector.y, ==, -1);
176 cpml_vector_normal(&vector);
177 g_assert_cmpfloat(vector.x, ==, 1);
178 g_assert_cmpfloat(vector.y, ==, 1);
181 static void
182 _cpml_test_vector_length(void)
184 CpmlVector vector;
185 double length;
187 cpml_pair_copy(&vector, &junk);
188 cpml_vector_set_length(&vector, 0);
189 g_assert_cmpfloat(vector.x, ==, org.x);
190 g_assert_cmpfloat(vector.y, ==, org.y);
192 cpml_vector_set_length(&vector, 1234);
193 g_assert_cmpfloat(vector.x, ==, org.x);
194 g_assert_cmpfloat(vector.y, ==, org.y);
196 cpml_pair_copy(&vector, &diag3);
197 cpml_vector_set_length(&vector, 5);
198 g_assert_cmpfloat(vector.x, ==, diag.x);
199 g_assert_cmpfloat(vector.y, ==, diag.y);
201 g_test_message("Using integer comparison to overcome rounding errors");
203 cpml_vector_set_length(&vector, 10);
204 length = cpml_pair_distance(&vector, NULL);
205 g_assert_cmpint(round(length), ==, 10);
207 cpml_vector_set_length(&vector, 5);
208 g_assert_cmpint(round(vector.x), ==, diag.x);
209 g_assert_cmpint(round(vector.y), ==, diag.y);
212 static void
213 _cpml_test_vector_transform(void)
215 CpmlVector vector;
216 cairo_matrix_t matrix;
218 g_test_message("Vectors are not affected by translations");
219 cairo_matrix_init_translate(&matrix, junk.x, junk.y);
220 cpml_pair_copy(&vector, &org);
221 cpml_vector_transform(&vector, &matrix);
222 g_assert_cmpfloat(vector.x, ==, org.x);
223 g_assert_cmpfloat(vector.y, ==, org.y);
225 g_test_message("Checking scaling transformations...");
226 cairo_matrix_init_scale(&matrix, 3, 3);
227 cpml_pair_copy(&vector, &diag);
228 cpml_vector_transform(&vector, &matrix);
229 g_assert_cmpfloat(vector.x, ==, diag3.x);
230 g_assert_cmpfloat(vector.y, ==, diag3.y);
232 g_test_message("Checking assorted transformations...");
233 cairo_matrix_init_scale(&matrix, 3, 3);
234 cairo_matrix_translate(&matrix, diag.x, diag.y);
235 cairo_matrix_translate(&matrix, -junk.x, -junk.y);
236 cpml_pair_copy(&vector, &diag);
237 cpml_vector_transform(&vector, &matrix);
238 g_assert_cmpfloat(vector.x, ==, diag3.x);
239 g_assert_cmpfloat(vector.y, ==, diag3.y);
244 main(int argc, char *argv[])
246 cpml_test_init(&argc, &argv);
248 cpml_test_add_func("/cpml/pair/basic", _cpml_test_pair_basic);
249 cpml_test_add_func("/cpml/pair/transform", _cpml_test_pair_transform);
250 cpml_test_add_func("/cpml/pair/distance", _cpml_test_pair_distance);
251 cpml_test_add_func("/cpml/vector/angle", _cpml_test_vector_angle);
252 cpml_test_add_func("/cpml/vector/length", _cpml_test_vector_length);
253 cpml_test_add_func("/cpml/vector/transform", _cpml_test_vector_transform);
255 return g_test_run();