<p dir="ltr">Thanks. We should probably have a discussion at some point as to the best guidelines on how to split the code up for unit testing purposes.</p>
<p dir="ltr">For now I'll move ahead with getting the vertex clipping code to support N vertices while still passing these tests. I probably won't be able to start that for a few weeks though.</p>
<p dir="ltr">Sam.</p>
<div class="gmail_quote">On 17/09/2013 12:41 PM, "Kristian Høgsberg" <<a href="mailto:hoegsberg@gmail.com">hoegsberg@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Fri, Sep 13, 2013 at 10:01:22AM +0800, Sam Spilsbury wrote:<br>
> This tests (via the table-driven testing method) that the correct<br>
> number of vertices and also the correct vertices themselves<br>
> are generated for an clip box and polygon of up to eight vertices.<br>
><br>
> Also add a <a href="http://libshared-test.la" target="_blank">libshared-test.la</a> so that we don't have to build weston-test-runner<br>
> all the time<br>
<br>
Hi Sam,<br>
<br>
I've committed these two, let's not get stuck on how to split it.<br>
<br>
Kristian<br>
<br>
> ---<br>
> tests/Makefile.am | 37 +++++---<br>
> tests/vertex-clip-test.c | 219 +++++++++++++++++++++++++++++++++++++++++++++++<br>
> 2 files changed, 245 insertions(+), 11 deletions(-)<br>
> create mode 100644 tests/vertex-clip-test.c<br>
><br>
> diff --git a/tests/Makefile.am b/tests/Makefile.am<br>
> index 3a36619..5be52c6 100644<br>
> --- a/tests/Makefile.am<br>
> +++ b/tests/Makefile.am<br>
> @@ -1,7 +1,8 @@<br>
> TESTS = $(shared_tests) $(module_tests) $(weston_tests)<br>
><br>
> shared_tests = \<br>
> - config-parser.test<br>
> + config-parser.test \<br>
> + vertex-clip.test<br>
><br>
> module_tests = \<br>
> <a href="http://surface-test.la" target="_blank">surface-test.la</a> \<br>
> @@ -48,13 +49,6 @@ AM_CPPFLAGS = \<br>
> -DUNIT_TEST \<br>
> $(COMPOSITOR_CFLAGS)<br>
><br>
> -<br>
> -config_parser_test_LDADD = \<br>
> - ../shared/<a href="http://libshared.la" target="_blank">libshared.la</a> \<br>
> - $(COMPOSITOR_LIBS)<br>
> -config_parser_test_SOURCES = \<br>
> - config-parser-test.c<br>
> -<br>
> surface_global_test_la_SOURCES = surface-global-test.c<br>
> surface_global_test_la_LDFLAGS = -module -avoid-version -rpath $(libdir)<br>
> surface_test_la_SOURCES = surface-test.c<br>
> @@ -72,17 +66,38 @@ weston_test_la_SOURCES = \<br>
> weston_test_runner_src = \<br>
> weston-test-runner.c \<br>
> weston-test-runner.h<br>
> +<br>
> +check_LTLIBRARIES = <a href="http://libshared-test.la" target="_blank">libshared-test.la</a><br>
> +<br>
> +libshared_test_la_SOURCES = \<br>
> + $(weston_test_runner_src)<br>
> +libshared_test_la_CFLAGS = $(GCC_CFLAGS) $(COMPOSITOR_CFLAGS)<br>
> +<br>
> +config_parser_test_LDADD = \<br>
> + ../shared/<a href="http://libshared.la" target="_blank">libshared.la</a> \<br>
> + <a href="http://libshared-test.la" target="_blank">libshared-test.la</a> \<br>
> + $(COMPOSITOR_LIBS)<br>
> +config_parser_test_SOURCES = \<br>
> + config-parser-test.c<br>
> +vertex_clip_test_SOURCES = \<br>
> + vertex-clip-test.c \<br>
> + ../src/vertex-clipping.c \<br>
> + ../src/vertex-clipping.h<br>
> +vertex_clip_test_LDADD = \<br>
> + <a href="http://libshared-test.la" target="_blank">libshared-test.la</a> \<br>
> + -lm -lrt<br>
> +<br>
> weston_test_client_src = \<br>
> weston-test-client-helper.c \<br>
> weston-test-client-helper.h \<br>
> wayland-test-protocol.c \<br>
> wayland-test-client-protocol.h \<br>
> subsurface-protocol.c \<br>
> - subsurface-client-protocol.h \<br>
> - $(weston_test_runner_src)<br>
> + subsurface-client-protocol.h<br>
> weston_test_client_libs = \<br>
> $(SIMPLE_CLIENT_LIBS) \<br>
> - ../shared/<a href="http://libshared.la" target="_blank">libshared.la</a><br>
> + ../shared/<a href="http://libshared.la" target="_blank">libshared.la</a> \<br>
> + <a href="http://libshared-test.la" target="_blank">libshared-test.la</a><br>
><br>
> keyboard_weston_SOURCES = keyboard-test.c $(weston_test_client_src)<br>
> keyboard_weston_LDADD = $(weston_test_client_libs)<br>
> diff --git a/tests/vertex-clip-test.c b/tests/vertex-clip-test.c<br>
> new file mode 100644<br>
> index 0000000..5b2e08c<br>
> --- /dev/null<br>
> +++ b/tests/vertex-clip-test.c<br>
> @@ -0,0 +1,219 @@<br>
> +/*<br>
> + * Copyright © 2013 Sam Spilsbury <<a href="mailto:smspillaz@gmail.com">smspillaz@gmail.com</a>><br>
> + *<br>
> + * Permission to use, copy, modify, distribute, and sell this software and<br>
> + * its documentation for any purpose is hereby granted without fee, provided<br>
> + * that the above copyright notice appear in all copies and that both that<br>
> + * copyright notice and this permission notice appear in supporting<br>
> + * documentation, and that the name of the copyright holders not be used in<br>
> + * advertising or publicity pertaining to distribution of the software<br>
> + * without specific, written prior permission. The copyright holders make<br>
> + * no representations about the suitability of this software for any<br>
> + * purpose. It is provided "as is" without express or implied warranty.<br>
> + *<br>
> + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS<br>
> + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND<br>
> + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY<br>
> + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER<br>
> + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF<br>
> + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN<br>
> + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.<br>
> + */<br>
> +#include <assert.h><br>
> +#include <stdio.h><br>
> +#include <stdlib.h><br>
> +#include <string.h><br>
> +#include <math.h><br>
> +<br>
> +#include "weston-test-runner.h"<br>
> +<br>
> +#include "../src/vertex-clipping.h"<br>
> +<br>
> +#define BOUNDING_BOX_TOP_Y 100.0f<br>
> +#define BOUNDING_BOX_LEFT_X 50.0f<br>
> +#define BOUNDING_BOX_RIGHT_X 100.0f<br>
> +#define BOUNDING_BOX_BOTTOM_Y 50.0f<br>
> +<br>
> +#define INSIDE_X1 (BOUNDING_BOX_LEFT_X + 1.0f)<br>
> +#define INSIDE_X2 (BOUNDING_BOX_RIGHT_X - 1.0f)<br>
> +#define INSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y + 1.0f)<br>
> +#define INSIDE_Y2 (BOUNDING_BOX_TOP_Y - 1.0f)<br>
> +<br>
> +#define OUTSIDE_X1 (BOUNDING_BOX_LEFT_X - 1.0f)<br>
> +#define OUTSIDE_X2 (BOUNDING_BOX_RIGHT_X + 1.0f)<br>
> +#define OUTSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y - 1.0f)<br>
> +#define OUTSIDE_Y2 (BOUNDING_BOX_TOP_Y + 1.0f)<br>
> +<br>
> +static void<br>
> +populate_clip_context (struct clip_context *ctx)<br>
> +{<br>
> + ctx->clip.x1 = BOUNDING_BOX_LEFT_X;<br>
> + ctx->clip.y1 = BOUNDING_BOX_BOTTOM_Y;<br>
> + ctx->clip.x2 = BOUNDING_BOX_RIGHT_X;<br>
> + ctx->clip.y2 = BOUNDING_BOX_TOP_Y;<br>
> +}<br>
> +<br>
> +static int<br>
> +clip_polygon (struct clip_context *ctx,<br>
> + struct polygon8 *polygon,<br>
> + GLfloat *vertices_x,<br>
> + GLfloat *vertices_y)<br>
> +{<br>
> + populate_clip_context(ctx);<br>
> + return clip_transformed(ctx, polygon, vertices_x, vertices_y);<br>
> +}<br>
> +<br>
> +struct vertex_clip_test_data<br>
> +{<br>
> + struct polygon8 surface;<br>
> + struct polygon8 expected;<br>
> +};<br>
> +<br>
> +const struct vertex_clip_test_data test_data[] =<br>
> +{<br>
> + /* All inside */<br>
> + {<br>
> + {<br>
> + { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },<br>
> + { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + },<br>
> + {<br>
> + { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },<br>
> + { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + }<br>
> + },<br>
> + /* Top outside */<br>
> + {<br>
> + {<br>
> + { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },<br>
> + { INSIDE_Y1, INSIDE_Y1, OUTSIDE_Y2, OUTSIDE_Y2 },<br>
> + 4<br>
> + },<br>
> + {<br>
> + { INSIDE_X1, INSIDE_X1, INSIDE_X2, INSIDE_X2 },<br>
> + { BOUNDING_BOX_TOP_Y, INSIDE_Y1, INSIDE_Y1, BOUNDING_BOX_TOP_Y },<br>
> + 4<br>
> + }<br>
> + },<br>
> + /* Bottom outside */<br>
> + {<br>
> + {<br>
> + { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },<br>
> + { OUTSIDE_Y1, OUTSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + },<br>
> + {<br>
> + { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },<br>
> + { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + }<br>
> + },<br>
> + /* Left outside */<br>
> + {<br>
> + {<br>
> + { OUTSIDE_X1, INSIDE_X2, INSIDE_X2, OUTSIDE_X1 },<br>
> + { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + },<br>
> + {<br>
> + { BOUNDING_BOX_LEFT_X, INSIDE_X2, INSIDE_X2, BOUNDING_BOX_LEFT_X },<br>
> + { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + }<br>
> + },<br>
> + /* Right outside */<br>
> + {<br>
> + {<br>
> + { INSIDE_X1, OUTSIDE_X2, OUTSIDE_X2, INSIDE_X1 },<br>
> + { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + },<br>
> + {<br>
> + { INSIDE_X1, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, INSIDE_X1 },<br>
> + { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },<br>
> + 4<br>
> + }<br>
> + },<br>
> + /* Diamond extending from bounding box edges, clip to bounding box */<br>
> + {<br>
> + {<br>
> + { BOUNDING_BOX_LEFT_X - 25, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 25, BOUNDING_BOX_RIGHT_X - 25 },<br>
> + { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 25, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 25 },<br>
> + 4<br>
> + },<br>
> + {<br>
> + { BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X },<br>
> + { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_BOTTOM_Y },<br>
> + 4<br>
> + }<br>
> + },<br>
> + /* Diamond inside of bounding box edges, clip t bounding box, 8 resulting vertices */<br>
> + {<br>
> + {<br>
> + { BOUNDING_BOX_LEFT_X - 12.5, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 12.5, BOUNDING_BOX_RIGHT_X - 25 },<br>
> + { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 12.5, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 12.5 },<br>
> + 4<br>
> + },<br>
> + {<br>
> + { BOUNDING_BOX_LEFT_X + 12.5, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X + 12.5,<br>
> + BOUNDING_BOX_RIGHT_X - 12.5, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X - 12.5 },<br>
> + { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_TOP_Y,<br>
> + BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_BOTTOM_Y },<br>
> + 8<br>
> + }<br>
> + }<br>
> +};<br>
> +<br>
> +/* clip_polygon modifies the source operand and the test data must<br>
> + * be const, so we need to deep copy it */<br>
> +static void<br>
> +deep_copy_polygon8(const struct polygon8 *src, struct polygon8 *dst)<br>
> +{<br>
> + dst->n = src->n;<br>
> + memcpy((void *) dst->x, src->x, sizeof (src->x));<br>
> + memcpy((void *) dst->y, src->y, sizeof (src->y));<br>
> +}<br>
> +<br>
> +TEST_P(clip_polygon_n_vertices_emitted, test_data)<br>
> +{<br>
> + struct vertex_clip_test_data *tdata = data;<br>
> + struct clip_context ctx;<br>
> + struct polygon8 polygon;<br>
> + GLfloat vertices_x[8];<br>
> + GLfloat vertices_y[8];<br>
> + deep_copy_polygon8(&tdata->surface, &polygon);<br>
> + int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);<br>
> +<br>
> + assert(emitted == tdata->expected.n);<br>
> +}<br>
> +<br>
> +TEST_P(clip_polygon_expected_vertices, test_data)<br>
> +{<br>
> + struct vertex_clip_test_data *tdata = data;<br>
> + struct clip_context ctx;<br>
> + struct polygon8 polygon;<br>
> + GLfloat vertices_x[8];<br>
> + GLfloat vertices_y[8];<br>
> + deep_copy_polygon8(&tdata->surface, &polygon);<br>
> + int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);<br>
> + int i = 0;<br>
> +<br>
> + for (; i < emitted; ++i)<br>
> + {<br>
> + assert(vertices_x[i] == tdata->expected.x[i]);<br>
> + assert(vertices_y[i] == tdata->expected.y[i]);<br>
> + }<br>
> +}<br>
> +<br>
> +TEST(float_difference_different)<br>
> +{<br>
> + assert(float_difference(1.0f, 0.0f) == 1.0f);<br>
> +}<br>
> +<br>
> +TEST(float_difference_same)<br>
> +{<br>
> + assert(float_difference(1.0f, 1.0f) == 0.0f);<br>
> +}<br>
> +<br>
> --<br>
> 1.8.3.2<br>
><br>
> _______________________________________________<br>
> wayland-devel mailing list<br>
> <a href="mailto:wayland-devel@lists.freedesktop.org">wayland-devel@lists.freedesktop.org</a><br>
> <a href="http://lists.freedesktop.org/mailman/listinfo/wayland-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/wayland-devel</a><br>
</blockquote></div>