From 1d2f58909cd55914f6592ec1562ac11b2bd05abd Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Fri, 30 Jan 2015 20:08:52 +0100 Subject: [PATCH] tests: enhance test-segment --- src/cpml/tests/test-segment.c | 196 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 162 insertions(+), 34 deletions(-) diff --git a/src/cpml/tests/test-segment.c b/src/cpml/tests/test-segment.c index 62df2de8..22facca7 100644 --- a/src/cpml/tests/test-segment.c +++ b/src/cpml/tests/test-segment.c @@ -27,13 +27,13 @@ static cairo_path_data_t data[] = { { .header = { CPML_MOVE, 2 }}, { .point = { 0, 0 }}, - /* First segment: a couple of lines */ + /* First segment: a couple of lines of length 2 */ { .header = { CPML_MOVE, 2 }}, - { .point = { 0, 1 }}, + { .point = { 0, 0 }}, { .header = { CPML_LINE, 2 }}, - { .point = { 2, 3 }}, + { .point = { 2, 0 }}, { .header = { CPML_LINE, 2 }}, - { .point = { 4, 5 }}, + { .point = { 2, 2 }}, /* Another useless CPML_MOVE with useless embedded data */ { .header = { CPML_MOVE, 3 }}, @@ -42,7 +42,7 @@ static cairo_path_data_t data[] = { /* Second segment: a Bézier curve with a trailing CPML_CLOSE */ { .header = { CPML_MOVE, 2 }}, - { .point = { 6, 7 }}, + { .point = { 10, 13 }}, { .header = { CPML_CURVE, 4 }}, { .point = { 8, 9 }}, { .point = { 10, 11 }}, @@ -50,9 +50,9 @@ static cairo_path_data_t data[] = { { .header = { CPML_CLOSE, 1 }}, /* A valid cairo segment considered invalid by CPML - * because does not have a heading CPML_MOVE */ + * because does not have a leading CPML_MOVE */ { .header = { CPML_LINE, 2 }}, - { .point = { 0, 0 }}, + { .point = { 10, 0 }}, { .header = { CPML_CLOSE, 1 }}, /* Another valid cairo segment invalid in CPML */ @@ -86,6 +86,13 @@ static cairo_path_data_t noop_data[] = { { .point = { 8, 9 }} }; +static cairo_path_data_t y1_data[] = { + { .header = { CPML_MOVE, 2 }}, + { .point = { 0, 1 }}, + { .header = { CPML_LINE, 2 }}, + { .point = { 5, 1 }} +}; + cairo_path_t path = { CAIRO_STATUS_SUCCESS, data, @@ -98,52 +105,167 @@ cairo_path_t noop_path = { G_N_ELEMENTS(noop_data) }; +cairo_path_t y1_path = { + CAIRO_STATUS_SUCCESS, + y1_data, + G_N_ELEMENTS(y1_data) +}; + static void -_cpml_test_misc(void) +_cpml_test_browsing(void) { - cairo_bool_t found; - CpmlSegment segment, segment_copy; - - /* Checking APIs */ - found = cpml_segment_from_cairo(&segment, &noop_path); - g_assert_false(found); - - /* CPML entry point */ - found = cpml_segment_from_cairo(&segment, &path); - g_assert_true(found); + CpmlSegment segment; + cpml_segment_from_cairo(&segment, &path); /* First segment */ g_assert_cmpint(segment.data[0].header.type, ==, CPML_MOVE); g_assert_cmpint(segment.data[2].header.type, ==, CPML_LINE); + cpml_segment_reset(&segment); + g_assert_cmpint(segment.data[0].header.type, ==, CPML_MOVE); + g_assert_cmpint(segment.data[2].header.type, ==, CPML_LINE); + /* Second segment */ - found = cpml_segment_next(&segment); - g_assert_true(found); + g_assert_cmpint(cpml_segment_next(&segment), ==, 1); g_assert_cmpint(segment.data[0].header.type, ==, CPML_MOVE); g_assert_cmpint(segment.data[2].header.type, ==, CPML_CURVE); /* Third segment */ - found = cpml_segment_next(&segment); - g_assert_true(found); + g_assert_cmpint(cpml_segment_next(&segment), ==, 1); g_assert_cmpint(segment.data[0].header.type, ==, CPML_MOVE); g_assert_cmpint(segment.data[2].header.type, ==, CPML_ARC); /* Forth segment */ - found = cpml_segment_next(&segment); - g_assert_true(found); + g_assert_cmpint(cpml_segment_next(&segment), ==, 1); g_assert_cmpint(segment.data[0].header.type, ==, CPML_MOVE); g_assert_cmpint(segment.data[2].header.type, ==, CPML_CLOSE); - /* Copy and boundaries check */ - cpml_segment_copy(&segment_copy, &segment); - found = cpml_segment_next(&segment); - g_assert_false(found); - found = cpml_segment_next(&segment_copy); - g_assert_false(found); - cpml_segment_reset(&segment_copy); - g_assert_cmpint(segment_copy.data[0].header.type, ==, CPML_MOVE); - g_assert_cmpint(segment_copy.data[2].header.type, ==, CPML_LINE); + g_assert_cmpint(cpml_segment_next(&segment), ==, 0); + + cpml_segment_reset(&segment); + g_assert_cmpint(segment.data[0].header.type, ==, CPML_MOVE); + g_assert_cmpint(segment.data[2].header.type, ==, CPML_LINE); +} + +static void +_cpml_test_from_cairo(void) +{ + CpmlSegment segment; + g_assert_cmpint(cpml_segment_from_cairo(NULL, &path), ==, 0); + g_assert_cmpint(cpml_segment_from_cairo(&segment, NULL), ==, 0); + g_assert_cmpint(cpml_segment_from_cairo(NULL, NULL), ==, 0); + + g_assert_cmpint(cpml_segment_from_cairo(&segment, &noop_path), ==, 0); + g_assert_cmpint(cpml_segment_from_cairo(&segment, &path), ==, 1); +} + +static void +_cpml_test_get_length(void) +{ + CpmlSegment segment; + cpml_segment_from_cairo(&segment, &path); + + g_assert_cmpfloat(cpml_segment_get_length(NULL), ==, 0); + + /* First segment */ + g_assert_cmpfloat(cpml_segment_get_length(&segment), ==, 4); + g_assert_cmpfloat(cpml_segment_get_length(&segment), ==, 4); + + /* Second segment */ + cpml_segment_next(&segment); + /* TODO: Bézier curve length not yet implemented + * g_assert_cmpfloat(cpml_segment_get_length(&segment), ==, ???); */ + + /* Third segment */ + cpml_segment_next(&segment); + g_assert_cmpfloat(cpml_segment_get_length(&segment), ==, 0); + + /* Forth segment */ + cpml_segment_next(&segment); + g_assert_cmpfloat(cpml_segment_get_length(&segment), ==, 0); +} + +static void +_cpml_test_put_intersections(void) +{ + CpmlSegment segment1, segment2; + CpmlPair pair[2]; + + cpml_segment_from_cairo(&segment1, &path); + cpml_segment_from_cairo(&segment2, &y1_path); + + g_assert_cmpuint(cpml_segment_put_intersections(NULL, &segment2, 2, pair), ==, 0); + g_assert_cmpuint(cpml_segment_put_intersections(&segment1, NULL, 2, pair), ==, 0); + g_assert_cmpuint(cpml_segment_put_intersections(&segment1, &segment2, 0, pair), ==, 0); + g_assert_cmpuint(cpml_segment_put_intersections(&segment1, &segment2, 2, NULL), ==, 0); + + /* The first segment of path intersects y1_path in (2, 1) */ + g_assert_cmpuint(cpml_segment_put_intersections(&segment1, &segment2, 2, pair), ==, 1); + g_assert_cmpfloat(pair[0].x, ==, 2); + g_assert_cmpfloat(pair[0].y, ==, 1); + + /* The third segment of path does not intersect y1_path */ + cpml_segment_next(&segment1); + cpml_segment_next(&segment1); + g_assert_cmpuint(cpml_segment_put_intersections(&segment1, &segment2, 2, pair), ==, 0); +} + +static void +_cpml_test_to_cairo(void) +{ + cairo_t *cr; + CpmlSegment segment; + int length, last_length; + + cr = adg_test_cairo_context(); + cpml_segment_from_cairo(&segment, &path); + + g_assert_cmpint(adg_test_cairo_num_data(cr), ==, 0); + cpml_segment_to_cairo(NULL, cr); + g_assert_cmpint(adg_test_cairo_num_data(cr), ==, 0); + cpml_segment_to_cairo(&segment, NULL); + g_assert_cmpint(adg_test_cairo_num_data(cr), ==, 0); + + length = 0; + do { + last_length = length; + cpml_segment_to_cairo(&segment, cr); + length = adg_test_cairo_num_data(cr); + g_assert_cmpint(length, >, last_length); + } while (cpml_segment_next(&segment)); + + cairo_destroy(cr); +} + +static void +_cpml_test_dump(void) +{ +#if GLIB_CHECK_VERSION(2, 38, 0) + if (g_test_subprocess()) { + CpmlSegment segment; + + /* This should not crash the process */ + cpml_segment_dump(NULL); + + cpml_segment_from_cairo(&segment, &path); + cpml_segment_dump(&segment); + + cpml_segment_next(&segment); + cpml_segment_dump(&segment); + + return; + } + + g_test_trap_subprocess(NULL, 0, 0); + g_test_trap_assert_passed(); + g_test_trap_assert_stderr_unmatched("?"); + g_test_trap_assert_stdout("*NULL*"); + g_test_trap_assert_stdout("*move*"); + g_test_trap_assert_stdout("*line*"); + g_test_trap_assert_stdout("*curve*"); + g_test_trap_assert_stdout_unmatched("*arc*"); +#endif } @@ -152,7 +274,13 @@ main(int argc, char *argv[]) { adg_test_init(&argc, &argv); - adg_test_add_func("/cpml/segment/behavior/misc", _cpml_test_misc); + adg_test_add_func("/cpml/segment/behavior/browsing", _cpml_test_browsing); + + adg_test_add_func("/cpml/segment/method/from-cairo", _cpml_test_from_cairo); + adg_test_add_func("/cpml/segment/method/get-length", _cpml_test_get_length); + adg_test_add_func("/cpml/segment/method/put-intersections", _cpml_test_put_intersections); + adg_test_add_func("/cpml/segment/method/to-cairo", _cpml_test_to_cairo); + adg_test_add_func("/cpml/segment/method/dump", _cpml_test_dump); return g_test_run(); } -- 2.11.4.GIT