doc: update copyright line for 2021
[adg.git] / src / adg / tests / test-matrix.c
blob6297dffaed4565711a393a3cdd951f93e5dbb42a
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.
21 #include <adg-test.h>
22 #include <adg.h>
25 static void
26 _adg_behavior_misc(void)
28 const cairo_matrix_t *matrix;
30 matrix = adg_matrix_identity();
31 adg_assert_isapprox(matrix->xx, 1);
32 adg_assert_isapprox(matrix->yx, 0);
33 adg_assert_isapprox(matrix->xy, 0);
34 adg_assert_isapprox(matrix->yy, 1);
35 adg_assert_isapprox(matrix->x0, 0);
36 adg_assert_isapprox(matrix->y0, 0);
38 matrix = adg_matrix_null();
39 adg_assert_isapprox(matrix->xx, 0);
40 adg_assert_isapprox(matrix->yx, 0);
41 adg_assert_isapprox(matrix->xy, 0);
42 adg_assert_isapprox(matrix->yy, 0);
43 adg_assert_isapprox(matrix->x0, 0);
44 adg_assert_isapprox(matrix->y0, 0);
47 static void
48 _adg_method_copy(void)
50 cairo_matrix_t original = { 1, 2, 3, 4, 5, 6 };
51 cairo_matrix_t matrix = { 0 };
53 /* Check sanity */
54 adg_matrix_copy(&matrix, NULL);
55 adg_matrix_copy(NULL, &original);
57 g_assert_cmpfloat(original.xx, !=, matrix.xx);
58 g_assert_cmpfloat(original.yx, !=, matrix.yx);
59 g_assert_cmpfloat(original.xy, !=, matrix.xy);
60 g_assert_cmpfloat(original.yy, !=, matrix.yy);
61 g_assert_cmpfloat(original.x0, !=, matrix.x0);
62 g_assert_cmpfloat(original.y0, !=, matrix.y0);
64 adg_matrix_copy(&matrix, &original);
66 adg_assert_isapprox(original.xx, matrix.xx);
67 adg_assert_isapprox(original.yx, matrix.yx);
68 adg_assert_isapprox(original.xy, matrix.xy);
69 adg_assert_isapprox(original.yy, matrix.yy);
70 adg_assert_isapprox(original.x0, matrix.x0);
71 adg_assert_isapprox(original.y0, matrix.y0);
74 static void
75 _adg_method_dup(void)
77 cairo_matrix_t original = { 1, 2, 3, 4, 5, 6 };
78 cairo_matrix_t *matrix;
80 /* Check sanity */
81 g_assert_null(adg_matrix_dup(NULL));
83 matrix = adg_matrix_dup(&original);
85 adg_assert_isapprox(original.xx, matrix->xx);
86 adg_assert_isapprox(original.yx, matrix->yx);
87 adg_assert_isapprox(original.xy, matrix->xy);
88 adg_assert_isapprox(original.yy, matrix->yy);
89 adg_assert_isapprox(original.x0, matrix->x0);
90 adg_assert_isapprox(original.y0, matrix->y0);
92 g_free(matrix);
95 static void
96 _adg_method_equal(void)
98 cairo_matrix_t original = { 1, 2, 3, 4, 5, 6 };
99 cairo_matrix_t matrix = { 0 };
101 /* Check sanity */
102 adg_matrix_equal(&matrix, NULL);
103 adg_matrix_equal(NULL, &original);
105 g_assert_false(adg_matrix_equal(&matrix, &original));
106 adg_matrix_copy(&matrix, &original);
107 g_assert_true(adg_matrix_equal(&matrix, &original));
108 matrix.xy = 0;
109 g_assert_false(adg_matrix_equal(&matrix, &original));
112 static void
113 _adg_method_normalize(void)
115 cairo_matrix_t matrix;
117 /* Check sanity */
118 g_assert_false(adg_matrix_normalize(NULL));
119 adg_matrix_copy(&matrix, adg_matrix_null());
120 g_assert_false(adg_matrix_normalize(&matrix));
122 /* Normalization of anamorphic matrices is not supported */
123 adg_matrix_copy(&matrix, adg_matrix_identity());
124 matrix.xx = 2;
125 matrix.yy = 3;
126 g_assert_false(adg_matrix_normalize(&matrix));
127 adg_matrix_copy(&matrix, adg_matrix_identity());
128 matrix.xy = 2;
129 matrix.yx = 3;
130 g_assert_false(adg_matrix_normalize(&matrix));
132 adg_matrix_copy(&matrix, adg_matrix_identity());
133 g_assert_true(adg_matrix_normalize(&matrix));
134 adg_matrix_equal(&matrix, adg_matrix_identity());
135 matrix.xx = 5;
136 matrix.yy = 5;
137 g_assert_true(adg_matrix_normalize(&matrix));
138 adg_assert_isapprox(matrix.xx, 1);
139 adg_assert_isapprox(matrix.yy, 1);
140 adg_assert_isapprox(matrix.xy, 0);
141 adg_assert_isapprox(matrix.yx, 0);
143 matrix.xx = 3;
144 matrix.yy = 3;
145 matrix.xy = 4;
146 matrix.yx = -4;
147 g_assert_true(adg_matrix_normalize(&matrix));
148 adg_assert_isapprox(matrix.xx, 0.6);
149 adg_assert_isapprox(matrix.yy, 0.6);
150 adg_assert_isapprox(matrix.xy, 0.8);
151 adg_assert_isapprox(matrix.yx, -0.8);
153 g_assert_true(adg_matrix_normalize(&matrix));
154 adg_assert_isapprox(matrix.xx, 0.6);
155 adg_assert_isapprox(matrix.yy, 0.6);
156 adg_assert_isapprox(matrix.xy, 0.8);
157 adg_assert_isapprox(matrix.yx, -0.8);
159 matrix.xx = 0;
160 matrix.yy = 0;
161 matrix.xy = -3;
162 matrix.yx = 3;
163 g_assert_true(adg_matrix_normalize(&matrix));
164 adg_assert_isapprox(matrix.xx, 0);
165 adg_assert_isapprox(matrix.yy, 0);
166 adg_assert_isapprox(matrix.xy, 1);
167 adg_assert_isapprox(matrix.yx, -1);
170 static void
171 _adg_method_transform(void)
173 cairo_matrix_t original = { 1, 2, 3, 4, 5, 6 };
174 cairo_matrix_t matrix;
175 cairo_matrix_t map = { 2, 0, 0, 2, 1, 1 };
177 adg_matrix_copy(&matrix, &original);
179 /* Check sanity */
180 adg_matrix_transform(NULL, &map, ADG_TRANSFORM_NONE);
181 adg_matrix_transform(&matrix, NULL, ADG_TRANSFORM_NONE);
183 g_assert_true(adg_matrix_equal(&matrix, &original));
185 adg_matrix_transform(&matrix, &map, ADG_TRANSFORM_NONE);
186 g_assert_true(adg_matrix_equal(&matrix, &original));
188 adg_matrix_transform(&matrix, adg_matrix_identity(), ADG_TRANSFORM_BEFORE);
189 g_assert_true(adg_matrix_equal(&matrix, &original));
191 adg_matrix_transform(&matrix, &map, ADG_TRANSFORM_BEFORE);
192 adg_assert_isapprox(matrix.xx, 2);
193 adg_assert_isapprox(matrix.yx, 4);
194 adg_assert_isapprox(matrix.xy, 6);
195 adg_assert_isapprox(matrix.yy, 8);
196 adg_assert_isapprox(matrix.x0, 9);
197 adg_assert_isapprox(matrix.y0, 12);
199 adg_matrix_copy(&matrix, &original);
200 adg_matrix_transform(&matrix, &map, ADG_TRANSFORM_AFTER);
201 adg_assert_isapprox(matrix.xx, 2);
202 adg_assert_isapprox(matrix.yx, 4);
203 adg_assert_isapprox(matrix.xy, 6);
204 adg_assert_isapprox(matrix.yy, 8);
205 adg_assert_isapprox(matrix.x0, 11);
206 adg_assert_isapprox(matrix.y0, 13);
208 adg_matrix_copy(&matrix, &original);
209 adg_matrix_transform(&matrix, &map, ADG_TRANSFORM_BEFORE_NORMALIZED);
210 adg_assert_isapprox(matrix.xx, 1);
211 adg_assert_isapprox(matrix.yx, 2);
212 adg_assert_isapprox(matrix.xy, 3);
213 adg_assert_isapprox(matrix.yy, 4);
214 adg_assert_isapprox(matrix.x0, 9);
215 adg_assert_isapprox(matrix.y0, 12);
217 adg_matrix_copy(&matrix, &original);
218 adg_matrix_transform(&matrix, &map, ADG_TRANSFORM_AFTER_NORMALIZED);
219 adg_assert_isapprox(matrix.xx, 1);
220 adg_assert_isapprox(matrix.yx, 2);
221 adg_assert_isapprox(matrix.xy, 3);
222 adg_assert_isapprox(matrix.yy, 4);
223 adg_assert_isapprox(matrix.x0, 6);
224 adg_assert_isapprox(matrix.y0, 7);
227 static void
228 _adg_method_dump(gint i)
230 switch (i) {
231 case 1:
232 adg_matrix_dump(adg_matrix_identity());
233 break;
234 default:
235 g_test_trap_assert_passed();
236 g_test_trap_assert_stderr_unmatched("?");
238 /* The identity matrix has only 0 and 1 */
239 g_test_trap_assert_stdout("*1.000*");
240 g_test_trap_assert_stdout("*0.000*");
241 g_test_trap_assert_stdout_unmatched("*2*");
242 g_test_trap_assert_stdout_unmatched("*3*");
243 g_test_trap_assert_stdout_unmatched("*4*");
244 g_test_trap_assert_stdout_unmatched("*5*");
245 g_test_trap_assert_stdout_unmatched("*6*");
246 g_test_trap_assert_stdout_unmatched("*7*");
247 g_test_trap_assert_stdout_unmatched("*8*");
248 g_test_trap_assert_stdout_unmatched("*9*");
254 main(int argc, char *argv[])
256 adg_test_init(&argc, &argv);
258 adg_test_add_boxed_checks("/adg/matrix/type/boxed", CAIRO_GOBJECT_TYPE_MATRIX, g_new0(cairo_matrix_t, 1));
260 g_test_add_func("/adg/matrix/behavior/misc", _adg_behavior_misc);
262 g_test_add_func("/adg/matrix/method/dup", _adg_method_dup);
263 g_test_add_func("/adg/matrix/method/copy", _adg_method_copy);
264 g_test_add_func("/adg/matrix/method/equal", _adg_method_equal);
265 g_test_add_func("/adg/matrix/method/normalize", _adg_method_normalize);
266 g_test_add_func("/adg/matrix/method/transform", _adg_method_transform);
267 adg_test_add_traps("/adg/matrix/method/dump", _adg_method_dump, 1);
269 return g_test_run();