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 _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);
48 _adg_method_copy(void)
50 cairo_matrix_t original
= { 1, 2, 3, 4, 5, 6 };
51 cairo_matrix_t matrix
= { 0 };
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
);
77 cairo_matrix_t original
= { 1, 2, 3, 4, 5, 6 };
78 cairo_matrix_t
*matrix
;
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
);
96 _adg_method_equal(void)
98 cairo_matrix_t original
= { 1, 2, 3, 4, 5, 6 };
99 cairo_matrix_t matrix
= { 0 };
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
));
109 g_assert_false(adg_matrix_equal(&matrix
, &original
));
113 _adg_method_normalize(void)
115 cairo_matrix_t matrix
;
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());
126 g_assert_false(adg_matrix_normalize(&matrix
));
127 adg_matrix_copy(&matrix
, adg_matrix_identity());
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());
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);
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);
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);
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
);
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);
228 _adg_method_dump(gint i
)
232 adg_matrix_dump(adg_matrix_identity());
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);