test: updated test units
[adg.git] / src / cpml / tests / test-primitive.c
blobde310367367fdb946aa208b2d63a4c84ffc38d19
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 <string.h>
25 static cairo_path_data_t data[] = {
26 { .header = { CPML_MOVE, 2 }},
27 { .point = { 0, 1 }},
29 /* Line */
30 { .header = { CPML_LINE, 2 }},
31 { .point = { 2, 3 }},
33 /* Arc */
34 { .header = { CPML_ARC, 3 }},
35 { .point = { 4, 5 }},
36 { .point = { 6, 7 }},
38 /* Curve */
39 { .header = { CPML_CURVE, 4 }},
40 { .point = { 8, 9 }},
41 { .point = { 10, 11 }},
42 { .point = { 12, 13 }},
44 /* Closing primive */
45 { .header = { CPML_CLOSE, 1 }}
48 cairo_path_t path = {
49 CAIRO_STATUS_SUCCESS,
50 data,
51 G_N_ELEMENTS(data)
55 static void
56 _cpml_test_basic(void)
58 cairo_bool_t found;
59 CpmlSegment segment;
60 CpmlPrimitive primitive, primitive_copy;
62 found = cpml_segment_from_cairo(&segment, &path);
63 g_assert(found);
65 /* Browsing API */
66 cpml_primitive_from_segment(&primitive, &segment);
67 g_assert(primitive.data != NULL);
68 found = cpml_primitive_next(&primitive);
69 g_assert(found);
70 found = cpml_primitive_next(&primitive);
71 g_assert(found);
72 found = cpml_primitive_next(&primitive);
73 g_assert(found);
75 /* Checking primitive copy and corner case browsing */
76 cpml_primitive_copy(&primitive_copy, &primitive);
77 found = cpml_primitive_next(&primitive);
78 g_assert(! found);
79 cpml_primitive_reset(&primitive);
80 g_assert(primitive.data != NULL);
81 g_assert_cmpint((primitive.data)->header.type, ==, CPML_LINE);
82 found = cpml_primitive_next(&primitive_copy);
83 g_assert(! found);
84 cpml_primitive_reset(&primitive_copy);
85 g_assert(primitive_copy.data != NULL);
86 g_assert_cmpint((primitive_copy.data)->header.type, ==, CPML_LINE);
89 static void
90 _cpml_test_type_get_n_points(void)
92 size_t n_points;
94 n_points = cpml_primitive_type_get_n_points(CPML_MOVE);
95 g_assert_cmpuint(n_points, ==, 0);
97 n_points = cpml_primitive_type_get_n_points(CPML_LINE);
98 g_assert_cmpuint(n_points, ==, 2);
100 n_points = cpml_primitive_type_get_n_points(CPML_ARC);
101 g_assert_cmpuint(n_points, ==, 3);
103 n_points = cpml_primitive_type_get_n_points(CPML_CURVE);
104 g_assert_cmpuint(n_points, ==, 4);
106 n_points = cpml_primitive_type_get_n_points(CPML_CLOSE);
107 g_assert_cmpuint(n_points, ==, 2);
110 static void
111 _cpml_test_get_n_points(void)
113 CpmlSegment segment;
114 CpmlPrimitive primitive;
115 size_t n_points;
117 cpml_segment_from_cairo(&segment, &path);
119 /* Line */
120 cpml_primitive_from_segment(&primitive, &segment);
121 n_points = cpml_primitive_get_n_points(&primitive);
122 g_assert_cmpuint(n_points, ==, 2);
124 /* Arc */
125 cpml_primitive_next(&primitive);
126 n_points = cpml_primitive_get_n_points(&primitive);
127 g_assert_cmpuint(n_points, ==, 3);
129 /* Curve */
130 cpml_primitive_next(&primitive);
131 n_points = cpml_primitive_get_n_points(&primitive);
132 g_assert_cmpuint(n_points, ==, 4);
134 /* Close: although the end point is not needed, the CPML API
135 * returns 2 points to treat this primitive as a CPML_LINE */
136 cpml_primitive_next(&primitive);
137 n_points = cpml_primitive_get_n_points(&primitive);
138 g_assert_cmpuint(n_points, ==, 2);
141 static void
142 _cpml_test_set_point(void)
144 gsize size;
145 cairo_path_data_t data_copy[G_N_ELEMENTS(data)];
146 cairo_path_t path_copy = {
147 CAIRO_STATUS_SUCCESS,
148 data_copy,
149 G_N_ELEMENTS(data)
151 CpmlSegment segment;
152 CpmlPrimitive primitive;
153 CpmlPair pair, pair2;
154 int equality;
156 size = sizeof(data_copy);
157 memcpy(data_copy, data, size);
159 cpml_segment_from_cairo(&segment, &path_copy);
161 /* Line */
162 cpml_primitive_from_segment(&primitive, &segment);
164 equality = memcmp(data_copy, data, size);
165 g_assert_cmpint(equality, ==, 0);
166 cpml_primitive_put_point(&primitive, 0, &pair);
167 pair.x += 1;
168 cpml_primitive_set_point(&primitive, 0, &pair);
169 equality = memcmp(data_copy, data, size);
170 g_assert_cmpint(equality, !=, 0);
171 pair.x -= 1;
172 cpml_primitive_set_point(&primitive, 0, &pair);
173 equality = memcmp(data_copy, data, size);
174 g_assert_cmpint(equality, ==, 0);
175 cpml_primitive_put_point(&primitive, 1, &pair);
176 pair.y += 1;
177 cpml_primitive_set_point(&primitive, 1, &pair);
178 equality = memcmp(data_copy, data, size);
179 g_assert_cmpint(equality, !=, 0);
180 /* On a CPML_LINE primitives, -1 and 1 indices are equals */
181 cpml_primitive_put_point(&primitive, -1, &pair2);
182 g_assert_cmpfloat(pair.x, ==, pair2.x);
183 g_assert_cmpfloat(pair.y, ==, pair2.y);
184 memcpy(data_copy, data, size);
185 equality = memcmp(data_copy, data, size);
186 g_assert_cmpint(equality, ==, 0);
187 cpml_primitive_put_point(&primitive, 2, &pair);
188 pair.x += 1;
189 pair.y += 1;
190 /* This should be a NOP without segfaults */
191 cpml_primitive_set_point(&primitive, 2, &pair);
192 equality = memcmp(data_copy, data, size);
193 g_assert_cmpint(equality, ==, 0);
195 /* From now on, memcpy() is assumed to force equality (as yet
196 * proved by the previous assertions) and pair2 is used as a
197 * different-from-everything pair, that is setting pair2 on any
198 * point will break the equality between data and data_copy
200 pair2.x = 12345;
201 pair2.y = 54321;
203 /* Arc */
204 cpml_primitive_next(&primitive);
206 cpml_primitive_set_point(&primitive, 0, &pair2);
207 equality = memcmp(data_copy, data, size);
208 g_assert_cmpint(equality, !=, 0);
209 memcpy(data_copy, data, size);
210 cpml_primitive_set_point(&primitive, 1, &pair2);
211 equality = memcmp(data_copy, data, size);
212 g_assert_cmpint(equality, !=, 0);
213 memcpy(data_copy, data, size);
214 cpml_primitive_set_point(&primitive, 2, &pair2);
215 equality = memcmp(data_copy, data, size);
216 g_assert_cmpint(equality, !=, 0);
217 memcpy(data_copy, data, size);
218 cpml_primitive_set_point(&primitive, 3, &pair2);
219 equality = memcmp(data_copy, data, size);
220 g_assert_cmpint(equality, ==, 0);
222 /* Curve */
223 cpml_primitive_next(&primitive);
225 cpml_primitive_set_point(&primitive, 0, &pair2);
226 equality = memcmp(data_copy, data, size);
227 g_assert_cmpint(equality, !=, 0);
228 memcpy(data_copy, data, size);
229 cpml_primitive_set_point(&primitive, 1, &pair2);
230 equality = memcmp(data_copy, data, size);
231 g_assert_cmpint(equality, !=, 0);
232 memcpy(data_copy, data, size);
233 cpml_primitive_set_point(&primitive, 2, &pair2);
234 equality = memcmp(data_copy, data, size);
235 g_assert_cmpint(equality, !=, 0);
236 memcpy(data_copy, data, size);
237 cpml_primitive_set_point(&primitive, 3, &pair2);
238 equality = memcmp(data_copy, data, size);
239 g_assert_cmpint(equality, !=, 0);
240 memcpy(data_copy, data, size);
241 cpml_primitive_set_point(&primitive, 4, &pair2);
242 equality = memcmp(data_copy, data, size);
243 g_assert_cmpint(equality, ==, 0);
245 /* Close */
246 cpml_primitive_next(&primitive);
248 cpml_primitive_set_point(&primitive, 0, &pair2);
249 equality = memcmp(data_copy, data, size);
250 g_assert_cmpint(equality, !=, 0);
251 memcpy(data_copy, data, size);
252 cpml_primitive_set_point(&primitive, 1, &pair2);
253 equality = memcmp(data_copy, data, size);
254 g_assert_cmpint(equality, !=, 0);
255 memcpy(data_copy, data, size);
256 cpml_primitive_set_point(&primitive, 2, &pair2);
257 equality = memcmp(data_copy, data, size);
258 g_assert_cmpint(equality, ==, 0);
261 static void
262 _cpml_test_put_point(void)
264 CpmlSegment segment;
265 CpmlPrimitive primitive;
266 CpmlPair pair;
268 cpml_segment_from_cairo(&segment, &path);
270 /* Line */
271 cpml_primitive_from_segment(&primitive, &segment);
273 cpml_primitive_put_point(&primitive, 0, &pair);
274 g_assert_cmpfloat(pair.x, ==, 0);
275 g_assert_cmpfloat(pair.y, ==, 1);
276 cpml_primitive_put_point(&primitive, 1, &pair);
277 g_assert_cmpfloat(pair.x, ==, 2);
278 g_assert_cmpfloat(pair.y, ==, 3);
279 cpml_primitive_put_point(&primitive, 2, &pair);
280 g_assert_cmpfloat(pair.x, ==, 2);
281 g_assert_cmpfloat(pair.y, ==, 3);
282 /* The negative indices are checked only against CPML_LINE */
283 cpml_primitive_put_point(&primitive, -1, &pair);
284 g_assert_cmpfloat(pair.x, ==, 2);
285 g_assert_cmpfloat(pair.y, ==, 3);
286 cpml_primitive_put_point(&primitive, -2, &pair);
287 g_assert_cmpfloat(pair.x, ==, 0);
288 g_assert_cmpfloat(pair.y, ==, 1);
289 cpml_primitive_put_point(&primitive, -3, &pair);
290 g_assert_cmpfloat(pair.x, ==, 0);
291 g_assert_cmpfloat(pair.y, ==, 1);
293 /* Arc */
294 cpml_primitive_next(&primitive);
296 cpml_primitive_put_point(&primitive, 0, &pair);
297 g_assert_cmpfloat(pair.x, ==, 2);
298 g_assert_cmpfloat(pair.y, ==, 3);
299 cpml_primitive_put_point(&primitive, 1, &pair);
300 g_assert_cmpfloat(pair.x, ==, 4);
301 g_assert_cmpfloat(pair.y, ==, 5);
302 cpml_primitive_put_point(&primitive, 2, &pair);
303 g_assert_cmpfloat(pair.x, ==, 6);
304 g_assert_cmpfloat(pair.y, ==, 7);
305 cpml_primitive_put_point(&primitive, 3, &pair);
306 g_assert_cmpfloat(pair.x, ==, 6);
307 g_assert_cmpfloat(pair.y, ==, 7);
309 /* Curve */
310 cpml_primitive_next(&primitive);
312 cpml_primitive_put_point(&primitive, 0, &pair);
313 g_assert_cmpfloat(pair.x, ==, 6);
314 g_assert_cmpfloat(pair.y, ==, 7);
315 cpml_primitive_put_point(&primitive, 1, &pair);
316 g_assert_cmpfloat(pair.x, ==, 8);
317 g_assert_cmpfloat(pair.y, ==, 9);
318 cpml_primitive_put_point(&primitive, 2, &pair);
319 g_assert_cmpfloat(pair.x, ==, 10);
320 g_assert_cmpfloat(pair.y, ==, 11);
321 cpml_primitive_put_point(&primitive, 3, &pair);
322 g_assert_cmpfloat(pair.x, ==, 12);
323 g_assert_cmpfloat(pair.y, ==, 13);
324 cpml_primitive_put_point(&primitive, 4, &pair);
325 g_assert_cmpfloat(pair.x, ==, 12);
326 g_assert_cmpfloat(pair.y, ==, 13);
328 /* Close */
329 cpml_primitive_next(&primitive);
331 cpml_primitive_put_point(&primitive, 0, &pair);
332 g_assert_cmpfloat(pair.x, ==, 12);
333 g_assert_cmpfloat(pair.y, ==, 13);
334 cpml_primitive_put_point(&primitive, 1, &pair);
335 g_assert_cmpfloat(pair.x, ==, 0);
336 g_assert_cmpfloat(pair.y, ==, 1);
337 cpml_primitive_put_point(&primitive, 2, &pair);
338 g_assert_cmpfloat(pair.x, ==, 0);
339 g_assert_cmpfloat(pair.y, ==, 1);
344 main(int argc, char *argv[])
346 cpml_test_init(&argc, &argv);
348 cpml_test_add_func("/cpml/primitive/basic", _cpml_test_basic);
349 cpml_test_add_func("/cpml/primitive/type_get_n_points", _cpml_test_type_get_n_points);
350 cpml_test_add_func("/cpml/primitive/get_n_points", _cpml_test_get_n_points);
351 cpml_test_add_func("/cpml/primitive/set_point", _cpml_test_set_point);
352 cpml_test_add_func("/cpml/primitive/put_point", _cpml_test_put_point);
354 return g_test_run();