tests: move common path data in libadgtest
[adg.git] / src / cpml / tests / test-primitive.c
blob588227fba0863f6f0d9b5394c857fb5f413274d5
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2015 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 <cpml.h>
23 #include <string.h>
26 static void
27 _cpml_test_browsing(void)
29 CpmlSegment segment;
30 CpmlPrimitive primitive, primitive_copy;
32 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
34 cpml_primitive_from_segment(&primitive, &segment);
35 g_assert_cmpfloat((primitive.org)->point.x, ==, 0);
36 g_assert_cmpfloat((primitive.org)->point.y, ==, 1);
37 g_assert_cmpint((primitive.data)->header.type, ==, CPML_LINE);
38 g_assert_true(cpml_primitive_next(&primitive));
39 g_assert_cmpfloat((primitive.org)->point.x, ==, 3);
40 g_assert_cmpfloat((primitive.org)->point.y, ==, 1);
41 g_assert_cmpint((primitive.data)->header.type, ==, CPML_ARC);
42 g_assert_true(cpml_primitive_next(&primitive));
43 g_assert_cmpfloat((primitive.org)->point.x, ==, 6);
44 g_assert_cmpfloat((primitive.org)->point.y, ==, 7);
45 g_assert_cmpint((primitive.data)->header.type, ==, CPML_CURVE);
46 g_assert_true(cpml_primitive_next(&primitive));
47 g_assert_cmpfloat((primitive.org)->point.x, ==, -2);
48 g_assert_cmpfloat((primitive.org)->point.y, ==, 2);
49 g_assert_cmpint((primitive.data)->header.type, ==, CPML_CLOSE);
50 g_assert_false(cpml_primitive_next(&primitive));
52 cpml_primitive_reset(&primitive);
53 g_assert_true(cpml_primitive_next(&primitive));
54 cpml_primitive_reset(&primitive);
55 cpml_primitive_reset(&primitive);
56 g_assert_true(cpml_primitive_next(&primitive));
57 g_assert_true(cpml_primitive_next(&primitive));
58 g_assert_true(cpml_primitive_next(&primitive));
59 g_assert_false(cpml_primitive_next(&primitive));
61 cpml_primitive_copy(&primitive_copy, &primitive);
62 g_assert_false(cpml_primitive_next(&primitive_copy));
63 cpml_primitive_reset(&primitive);
64 g_assert_false(cpml_primitive_next(&primitive_copy));
65 cpml_primitive_reset(&primitive_copy);
66 g_assert_true(cpml_primitive_next(&primitive_copy));
69 static void
70 _cpml_test_sanity_from_segment(gint i)
72 CpmlPrimitive primitive;
73 CpmlSegment segment;
75 switch (i) {
76 case 1:
77 cpml_primitive_from_segment(NULL, &segment);
78 break;
79 case 2:
80 cpml_primitive_from_segment(&primitive, NULL);
81 break;
82 default:
83 g_test_trap_assert_failed();
84 break;
88 static void
89 _cpml_test_sanity_get_n_points(gint i)
91 switch (i) {
92 case 1:
93 cpml_primitive_get_n_points(NULL);
94 break;
95 default:
96 g_test_trap_assert_failed();
97 break;
101 static void
102 _cpml_test_sanity_put_point(gint i)
104 CpmlSegment segment;
105 CpmlPrimitive primitive;
106 CpmlPair pair;
108 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
109 cpml_primitive_from_segment(&primitive, &segment);
111 switch (i) {
112 case 1:
113 cpml_primitive_put_point(NULL, 0, &pair);
114 break;
115 case 2:
116 cpml_primitive_put_point(&primitive, 0, NULL);
117 break;
118 default:
119 g_test_trap_assert_failed();
120 break;
124 static void
125 _cpml_test_sanity_get_length(gint i)
127 switch (i) {
128 case 1:
129 cpml_primitive_get_length(NULL);
130 break;
131 default:
132 g_test_trap_assert_failed();
133 break;
137 static void
138 _cpml_test_sanity_put_intersections(gint i)
140 CpmlPrimitive primitive;
141 CpmlPair pair;
143 switch (i) {
144 case 1:
145 cpml_primitive_put_intersections(NULL, &primitive, 2, &pair);
146 break;
147 case 2:
148 cpml_primitive_put_intersections(&primitive, NULL, 2, &pair);
149 break;
150 case 3:
151 cpml_primitive_put_intersections(&primitive, &primitive, 2, NULL);
152 break;
153 default:
154 g_test_trap_assert_failed();
155 break;
159 static void
160 _cpml_test_sanity_put_intersections_with_segment(gint i)
162 CpmlPrimitive primitive;
163 CpmlSegment segment;
164 CpmlPair pair;
166 switch (i) {
167 case 1:
168 cpml_primitive_put_intersections_with_segment(NULL, &segment, 2, &pair);
169 break;
170 case 2:
171 cpml_primitive_put_intersections_with_segment(&primitive, NULL, 2, &pair);
172 break;
173 case 3:
174 cpml_primitive_put_intersections_with_segment(&primitive, &segment, 2, NULL);
175 break;
176 default:
177 g_test_trap_assert_failed();
178 break;
182 static void
183 _cpml_test_sanity_join(gint i)
185 CpmlPrimitive primitive;
187 switch (i) {
188 case 1:
189 cpml_primitive_join(NULL, &primitive);
190 break;
191 case 2:
192 cpml_primitive_join(&primitive, NULL);
193 break;
194 default:
195 g_test_trap_assert_failed();
196 break;
200 static void
201 _cpml_test_sanity_dump(gint i)
203 switch (i) {
204 case 1:
205 cpml_segment_dump(NULL);
206 break;
207 default:
208 g_test_trap_assert_failed();
209 break;
213 static void
214 _cpml_test_sanity_to_cairo(gint i)
216 CpmlPrimitive primitive;
218 switch (i) {
219 case 1:
220 cpml_primitive_to_cairo(NULL, adg_test_cairo_context());
221 break;
222 case 2:
223 cpml_primitive_to_cairo(&primitive, NULL);
224 break;
225 default:
226 g_test_trap_assert_failed();
227 break;
231 static void
232 _cpml_test_from_segment(void)
234 CpmlSegment segment;
235 CpmlPrimitive primitive;
237 g_assert_true(cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path()));
239 cpml_primitive_from_segment(&primitive, &segment);
240 g_assert_nonnull(primitive.segment);
241 g_assert_nonnull(primitive.org);
242 g_assert_nonnull(primitive.data);
245 static void
246 _cpml_test_type_get_n_points(void)
248 size_t n_points;
250 n_points = cpml_primitive_type_get_n_points(CPML_MOVE);
251 g_assert_cmpuint(n_points, ==, 0);
253 n_points = cpml_primitive_type_get_n_points(CPML_LINE);
254 g_assert_cmpuint(n_points, ==, 2);
256 n_points = cpml_primitive_type_get_n_points(CPML_ARC);
257 g_assert_cmpuint(n_points, ==, 3);
259 n_points = cpml_primitive_type_get_n_points(CPML_CURVE);
260 g_assert_cmpuint(n_points, ==, 4);
262 n_points = cpml_primitive_type_get_n_points(CPML_CLOSE);
263 g_assert_cmpuint(n_points, ==, 2);
266 static void
267 _cpml_test_get_n_points(void)
269 CpmlSegment segment;
270 CpmlPrimitive primitive;
271 size_t n_points;
273 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
275 /* Line */
276 cpml_primitive_from_segment(&primitive, &segment);
277 n_points = cpml_primitive_get_n_points(&primitive);
278 g_assert_cmpuint(n_points, ==, 2);
280 /* Arc */
281 cpml_primitive_next(&primitive);
282 n_points = cpml_primitive_get_n_points(&primitive);
283 g_assert_cmpuint(n_points, ==, 3);
285 /* Curve */
286 cpml_primitive_next(&primitive);
287 n_points = cpml_primitive_get_n_points(&primitive);
288 g_assert_cmpuint(n_points, ==, 4);
290 /* Close: although the end point is not needed, the CPML API
291 * returns 2 points to treat this primitive as a CPML_LINE */
292 cpml_primitive_next(&primitive);
293 n_points = cpml_primitive_get_n_points(&primitive);
294 g_assert_cmpuint(n_points, ==, 2);
297 static void
298 _cpml_test_set_point(void)
300 gsize data_size;
301 CpmlSegment original, *segment;
302 CpmlPrimitive primitive;
303 CpmlPair pair, pair2;
304 int equality;
306 /* Work on a copy to not modify the original path data */
307 cpml_segment_from_cairo(&original, (cairo_path_t *) adg_test_path());
308 data_size = original.num_data * sizeof(cairo_path_data_t);
309 segment = cpml_segment_deep_dup(&original);
311 /* Line */
312 cpml_primitive_from_segment(&primitive, segment);
314 equality = memcmp(original.data, segment->data, data_size);
315 g_assert_cmpint(equality, ==, 0);
316 cpml_primitive_put_point(&primitive, 0, &pair);
317 pair.x += 1;
318 cpml_primitive_set_point(&primitive, 0, &pair);
319 equality = memcmp(original.data, segment->data, data_size);
320 g_assert_cmpint(equality, !=, 0);
321 pair.x -= 1;
322 cpml_primitive_set_point(&primitive, 0, &pair);
323 equality = memcmp(original.data, segment->data, data_size);
324 g_assert_cmpint(equality, ==, 0);
325 cpml_primitive_put_point(&primitive, 1, &pair);
326 pair.y += 1;
327 cpml_primitive_set_point(&primitive, 1, &pair);
328 equality = memcmp(original.data, segment->data, data_size);
329 g_assert_cmpint(equality, !=, 0);
330 /* On a CPML_LINE primitives, -1 and 1 indices are equals */
331 cpml_primitive_put_point(&primitive, -1, &pair2);
332 g_assert_cmpfloat(pair.x, ==, pair2.x);
333 g_assert_cmpfloat(pair.y, ==, pair2.y);
334 memcpy(segment->data, original.data, data_size);
335 equality = memcmp(original.data, segment->data, data_size);
336 g_assert_cmpint(equality, ==, 0);
337 cpml_primitive_put_point(&primitive, 2, &pair);
338 pair.x += 1;
339 pair.y += 1;
340 /* This should be a NOP without segfaults */
341 cpml_primitive_set_point(&primitive, 2, &pair);
342 equality = memcmp(original.data, segment->data, data_size);
343 g_assert_cmpint(equality, ==, 0);
345 /* From now on, memcpy() is assumed to force equality (as already
346 * proved by the previous assertions) and pair2 is used as a
347 * different-from-everything pair, that is setting pair2 on any
348 * point will break the equality between segment->data and
349 * original.data
351 pair2.x = 12345;
352 pair2.y = 54321;
354 /* Arc */
355 cpml_primitive_next(&primitive);
357 cpml_primitive_set_point(&primitive, 0, &pair2);
358 equality = memcmp(original.data, segment->data, data_size);
359 g_assert_cmpint(equality, !=, 0);
360 memcpy(segment->data, original.data, data_size);
361 cpml_primitive_set_point(&primitive, 1, &pair2);
362 equality = memcmp(original.data, segment->data, data_size);
363 g_assert_cmpint(equality, !=, 0);
364 memcpy(segment->data, original.data, data_size);
365 cpml_primitive_set_point(&primitive, 2, &pair2);
366 equality = memcmp(original.data, segment->data, data_size);
367 g_assert_cmpint(equality, !=, 0);
368 memcpy(segment->data, original.data, data_size);
369 cpml_primitive_set_point(&primitive, 3, &pair2);
370 equality = memcmp(original.data, segment->data, data_size);
371 g_assert_cmpint(equality, ==, 0);
373 /* Curve */
374 cpml_primitive_next(&primitive);
376 cpml_primitive_set_point(&primitive, 0, &pair2);
377 equality = memcmp(original.data, segment->data, data_size);
378 g_assert_cmpint(equality, !=, 0);
379 memcpy(segment->data, original.data, data_size);
380 cpml_primitive_set_point(&primitive, 1, &pair2);
381 equality = memcmp(original.data, segment->data, data_size);
382 g_assert_cmpint(equality, !=, 0);
383 memcpy(segment->data, original.data, data_size);
384 cpml_primitive_set_point(&primitive, 2, &pair2);
385 equality = memcmp(original.data, segment->data, data_size);
386 g_assert_cmpint(equality, !=, 0);
387 memcpy(segment->data, original.data, data_size);
388 cpml_primitive_set_point(&primitive, 3, &pair2);
389 equality = memcmp(original.data, segment->data, data_size);
390 g_assert_cmpint(equality, !=, 0);
391 memcpy(segment->data, original.data, data_size);
392 cpml_primitive_set_point(&primitive, 4, &pair2);
393 equality = memcmp(original.data, segment->data, data_size);
394 g_assert_cmpint(equality, ==, 0);
396 /* Close */
397 cpml_primitive_next(&primitive);
399 cpml_primitive_set_point(&primitive, 0, &pair2);
400 equality = memcmp(original.data, segment->data, data_size);
401 g_assert_cmpint(equality, !=, 0);
402 memcpy(segment->data, original.data, data_size);
403 cpml_primitive_set_point(&primitive, 1, &pair2);
404 equality = memcmp(original.data, segment->data, data_size);
405 g_assert_cmpint(equality, !=, 0);
406 memcpy(segment->data, original.data, data_size);
407 cpml_primitive_set_point(&primitive, 2, &pair2);
408 equality = memcmp(original.data, segment->data, data_size);
409 g_assert_cmpint(equality, ==, 0);
411 g_free(segment);
414 static void
415 _cpml_test_put_point(void)
417 CpmlSegment segment;
418 CpmlPrimitive primitive;
419 CpmlPair pair;
421 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
423 /* Line */
424 cpml_primitive_from_segment(&primitive, &segment);
426 cpml_primitive_put_point(&primitive, 0, &pair);
427 g_assert_cmpfloat(pair.x, ==, 0);
428 g_assert_cmpfloat(pair.y, ==, 1);
429 cpml_primitive_put_point(&primitive, 1, &pair);
430 g_assert_cmpfloat(pair.x, ==, 3);
431 g_assert_cmpfloat(pair.y, ==, 1);
432 cpml_primitive_put_point(&primitive, 2, &pair);
433 g_assert_cmpfloat(pair.x, ==, 3);
434 g_assert_cmpfloat(pair.y, ==, 1);
435 /* The negative indices are checked only against CPML_LINE */
436 cpml_primitive_put_point(&primitive, -1, &pair);
437 g_assert_cmpfloat(pair.x, ==, 3);
438 g_assert_cmpfloat(pair.y, ==, 1);
439 cpml_primitive_put_point(&primitive, -2, &pair);
440 g_assert_cmpfloat(pair.x, ==, 0);
441 g_assert_cmpfloat(pair.y, ==, 1);
442 cpml_primitive_put_point(&primitive, -3, &pair);
443 g_assert_cmpfloat(pair.x, ==, 0);
444 g_assert_cmpfloat(pair.y, ==, 1);
446 /* Arc */
447 cpml_primitive_next(&primitive);
449 cpml_primitive_put_point(&primitive, 0, &pair);
450 g_assert_cmpfloat(pair.x, ==, 3);
451 g_assert_cmpfloat(pair.y, ==, 1);
452 cpml_primitive_put_point(&primitive, 1, &pair);
453 g_assert_cmpfloat(pair.x, ==, 4);
454 g_assert_cmpfloat(pair.y, ==, 5);
455 cpml_primitive_put_point(&primitive, 2, &pair);
456 g_assert_cmpfloat(pair.x, ==, 6);
457 g_assert_cmpfloat(pair.y, ==, 7);
458 cpml_primitive_put_point(&primitive, 3, &pair);
459 g_assert_cmpfloat(pair.x, ==, 6);
460 g_assert_cmpfloat(pair.y, ==, 7);
462 /* Curve */
463 cpml_primitive_next(&primitive);
465 cpml_primitive_put_point(&primitive, 0, &pair);
466 g_assert_cmpfloat(pair.x, ==, 6);
467 g_assert_cmpfloat(pair.y, ==, 7);
468 cpml_primitive_put_point(&primitive, 1, &pair);
469 g_assert_cmpfloat(pair.x, ==, 8);
470 g_assert_cmpfloat(pair.y, ==, 9);
471 cpml_primitive_put_point(&primitive, 2, &pair);
472 g_assert_cmpfloat(pair.x, ==, 10);
473 g_assert_cmpfloat(pair.y, ==, 11);
474 cpml_primitive_put_point(&primitive, 3, &pair);
475 g_assert_cmpfloat(pair.x, ==, -2);
476 g_assert_cmpfloat(pair.y, ==, 2);
477 cpml_primitive_put_point(&primitive, 4, &pair);
478 g_assert_cmpfloat(pair.x, ==, -2);
479 g_assert_cmpfloat(pair.y, ==, 2);
481 /* Close */
482 cpml_primitive_next(&primitive);
484 cpml_primitive_put_point(&primitive, 0, &pair);
485 g_assert_cmpfloat(pair.x, ==, -2);
486 g_assert_cmpfloat(pair.y, ==, 2);
487 cpml_primitive_put_point(&primitive, 1, &pair);
488 g_assert_cmpfloat(pair.x, ==, 0);
489 g_assert_cmpfloat(pair.y, ==, 1);
490 cpml_primitive_put_point(&primitive, 2, &pair);
491 g_assert_cmpfloat(pair.x, ==, 0);
492 g_assert_cmpfloat(pair.y, ==, 1);
495 static void
496 _cpml_test_get_length(void)
498 CpmlSegment segment;
499 CpmlPrimitive primitive;
501 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
502 cpml_segment_next(&segment);
503 cpml_primitive_from_segment(&primitive, &segment);
505 g_assert_cmpfloat(cpml_primitive_get_length(&primitive), ==, 1);
507 cpml_primitive_next(&primitive);
508 g_assert_cmpfloat(cpml_primitive_get_length(&primitive), ==, 2);
511 static void
512 _cpml_test_put_intersections(void)
514 CpmlSegment segment;
515 CpmlPrimitive primitive1, primitive2;
516 CpmlPair pair[2];
518 /* Set primitive1 to 1.1 (first segment, first primitive) */
519 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
520 cpml_primitive_from_segment(&primitive1, &segment);
522 /* Set primitive2 to 2.1 (second segment, first primitive) */
523 cpml_segment_next(&segment);
524 cpml_primitive_from_segment(&primitive2, &segment);
526 /* primitive1 (1.1) does not intersect primitive2 (2.1) */
527 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1, &primitive2, 2, pair), ==, 0);
529 cpml_primitive_next(&primitive2);
531 /* primitive1 (1.1) intersects primitive2 (2.2) in (1, 1) */
532 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1, &primitive2, 2, pair), ==, 1);
533 g_assert_cmpfloat(pair[0].x, ==, 1);
534 g_assert_cmpfloat(pair[0].y, ==, 1);
535 g_assert_cmpint(cpml_primitive_is_inside(&primitive1, pair), ==, 1);
536 g_assert_cmpint(cpml_primitive_is_inside(&primitive2, pair), ==, 1);
538 /* Check the intersection is not returned when not requested */
539 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1, &primitive2, 0, pair), ==, 0);
541 cpml_primitive_next(&primitive1);
543 /* primitive1 (1.2) does not intersect primitive2 (2.2) */
544 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1, &primitive2, 2, pair), ==, 0);
546 cpml_primitive_next(&primitive1);
548 /* primitive1 (1.3) does not intersect primitive2 (2.2) */
549 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1, &primitive2, 2, pair), ==, 0);
551 cpml_primitive_next(&primitive1);
553 /* primitive1 (1.4) intersects primitive2 (2.2), but outside their boundaries */
554 g_assert_cmpuint(cpml_primitive_put_intersections(&primitive1, &primitive2, 2, pair), ==, 1);
555 g_assert_cmpfloat(pair[0].x, ==, 1);
556 g_assert_cmpfloat(pair[0].y, ==, -1);
557 g_assert_cmpint(cpml_primitive_is_inside(&primitive1, pair), ==, 0);
558 g_assert_cmpint(cpml_primitive_is_inside(&primitive2, pair), ==, 0);
561 static void
562 _cpml_test_put_intersections_with_segment(void)
564 CpmlSegment segment;
565 CpmlPrimitive primitive;
566 CpmlPair pair[4];
568 /* Set primitive to first segment, first primitive */
569 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
570 cpml_primitive_from_segment(&primitive, &segment);
572 /* Set segment to the second segment */
573 cpml_segment_next(&segment);
575 /* primitive (1.1) intersects segment (2) in (1, 1) */
576 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive, &segment, 4, pair), ==, 1);
577 g_assert_cmpfloat(pair[0].x, ==, 1);
578 g_assert_cmpfloat(pair[0].y, ==, 1);
580 cpml_primitive_next(&primitive);
582 /* primitive (1.1) does not intersect segment (2) */
583 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive, &segment, 4, pair), ==, 0);
585 /* Set primitive to second segment, first primitive */
586 cpml_primitive_from_segment(&primitive, &segment);
588 /* Set segment to the first segment */
589 cpml_segment_reset(&segment);
591 /* primitive (2.1) intersects segment (1) in extrapolation.
592 * TODO: change this behavior! They must not intersect. */
593 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive, &segment, 4, pair), ==, 1);
594 g_assert_cmpfloat(pair[0].x, ==, 2);
595 g_assert_cmpfloat(pair[0].y, ==, 0);
597 cpml_primitive_next(&primitive);
599 /* primitive (2.2) wrongly intersects segment (1) */
600 g_assert_cmpuint(cpml_primitive_put_intersections_with_segment(&primitive, &segment, 4, pair), ==, 1);
601 g_assert_cmpfloat(pair[0].x, ==, 2);
602 g_assert_cmpfloat(pair[0].y, ==, 0);
605 static void
606 _cpml_test_join(void)
608 cairo_path_data_t path_data[] = {
609 { .header = { CPML_MOVE, 2 }},
610 { .point = { 0, 0 }},
612 { .header = { CPML_LINE, 2 }},
613 { .point = { 2, 0 }},
615 { .header = { CPML_LINE, 2 }},
616 { .point = { 2, 2 }},
618 { .header = { CPML_LINE, 2 }},
619 { .point = { 1, 2 }},
621 { .header = { CPML_LINE, 2 }},
622 { .point = { 1, -2 }}
624 cairo_path_t path = {
625 CAIRO_STATUS_SUCCESS,
626 path_data,
627 G_N_ELEMENTS(path_data)
629 CpmlSegment segment;
630 CpmlPrimitive primitive1, primitive2;
632 cpml_segment_from_cairo(&segment, &path);
634 cpml_primitive_from_segment(&primitive1, &segment);
635 cpml_primitive_copy(&primitive2, &primitive1);
636 cpml_primitive_next(&primitive2);
638 /* primitive1 and primitive2 are already joint */
639 g_assert_cmpint(cpml_primitive_join(&primitive1, &primitive2), ==, 1);
640 g_assert_cmpfloat((primitive2.org)->point.x, ==, 2);
641 g_assert_cmpfloat((primitive2.org)->point.y, ==, 0);
643 cpml_primitive_next(&primitive2);
644 /* Now primitive1 and primitive2 are divergent,
645 * hence cannot be joined */
646 g_assert_cmpint(cpml_primitive_join(&primitive1, &primitive2), ==, 0);
648 cpml_primitive_next(&primitive2);
649 g_assert_cmpint(cpml_primitive_join(&primitive1, &primitive2), ==, 1);
650 g_assert_cmpfloat((primitive2.org)->point.x, ==, 1);
651 g_assert_cmpfloat((primitive2.org)->point.y, ==, 0);
654 static void
655 _cpml_test_to_cairo(void)
657 cairo_t *cr;
658 CpmlSegment segment;
659 CpmlPrimitive primitive;
660 int length, last_length;
662 cr = adg_test_cairo_context();
663 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
664 cpml_primitive_from_segment(&primitive, &segment);
666 g_assert_cmpint(adg_test_cairo_num_data(cr), ==, 0);
668 length = 0;
669 do {
670 last_length = length;
671 cpml_primitive_to_cairo(&primitive, cr);
672 length = adg_test_cairo_num_data(cr);
673 g_assert_cmpint(length, >, last_length);
674 } while (cpml_primitive_next(&primitive));
676 cairo_destroy(cr);
679 static void
680 _cpml_test_dump(gint i)
682 CpmlSegment segment;
683 CpmlPrimitive primitive;
685 switch (i) {
686 case 1:
687 cpml_segment_from_cairo(&segment, (cairo_path_t *) adg_test_path());
688 cpml_primitive_from_segment(&primitive, &segment);
689 cpml_primitive_dump(&primitive, 1);
691 cpml_primitive_next(&primitive);
692 cpml_primitive_dump(&primitive, 1);
693 break;
694 case 2:
695 g_test_trap_assert_passed();
696 g_test_trap_assert_stderr_unmatched("?");
697 g_test_trap_assert_stdout("*NULL*");
698 g_test_trap_assert_stdout("*move*");
699 g_test_trap_assert_stdout("*line*");
705 main(int argc, char *argv[])
707 adg_test_init(&argc, &argv);
709 g_test_add_func("/cpml/primitive/behavior/browsing", _cpml_test_browsing);
711 adg_test_add_traps("/cpml/primitive/sanity/from-segment", _cpml_test_sanity_from_segment, 1);
712 adg_test_add_traps("/cpml/primitive/sanity/get-n-points", _cpml_test_sanity_get_n_points, 1);
713 adg_test_add_traps("/cpml/primitive/sanity/put-point", _cpml_test_sanity_put_point, 2);
714 adg_test_add_traps("/cpml/primitive/sanity/get-length", _cpml_test_sanity_get_length, 1);
715 adg_test_add_traps("/cpml/primitive/sanity/put-intersections", _cpml_test_sanity_put_intersections, 3);
716 adg_test_add_traps("/cpml/primitive/sanity/put-intersections-with-segment", _cpml_test_sanity_put_intersections_with_segment, 3);
717 adg_test_add_traps("/cpml/primitive/sanity/join", _cpml_test_sanity_join, 2);
718 adg_test_add_traps("/cpml/primitive/sanity/to-cairo", _cpml_test_sanity_to_cairo, 2);
719 adg_test_add_traps("/cpml/primitive/sanity/dump", _cpml_test_sanity_dump, 1);
721 g_test_add_func("/cpml/primitive/method/from-segment", _cpml_test_from_segment);
722 g_test_add_func("/cpml/primitive/method/type-get-n-points", _cpml_test_type_get_n_points);
723 g_test_add_func("/cpml/primitive/method/get-n-points", _cpml_test_get_n_points);
724 g_test_add_func("/cpml/primitive/method/set-point", _cpml_test_set_point);
725 g_test_add_func("/cpml/primitive/method/put-point", _cpml_test_put_point);
726 g_test_add_func("/cpml/primitive/method/get-length", _cpml_test_get_length);
727 g_test_add_func("/cpml/primitive/method/put-intersections", _cpml_test_put_intersections);
728 g_test_add_func("/cpml/primitive/method/put-intersections-with-segment", _cpml_test_put_intersections_with_segment);
729 g_test_add_func("/cpml/primitive/method/join", _cpml_test_join);
730 g_test_add_func("/cpml/primitive/method/to-cairo", _cpml_test_to_cairo);
731 adg_test_add_traps("/cpml/primitive/method/dump", _cpml_test_dump, 1);
733 return g_test_run();