diff options
author | Benjamin Otte <otte@redhat.com> | 2020-02-13 05:23:31 +0100 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2020-02-13 07:36:38 +0100 |
commit | 75b130ed6be14950f17d771e246505649d8ffa08 (patch) | |
tree | b3500ce0d39041043ea28426b74e1bc1d693c220 | |
parent | 77d7c713d4be60ec45a78668b6667aa24b5c6eb5 (diff) | |
download | gtk+-75b130ed6be14950f17d771e246505649d8ffa08.tar.gz |
testsuite: Add tests for rounded rect code
... including the bug that was fixed in the parent commit.
-rw-r--r-- | testsuite/gsk/meson.build | 1 | ||||
-rw-r--r-- | testsuite/gsk/rounded-rect.c | 100 |
2 files changed, 101 insertions, 0 deletions
diff --git a/testsuite/gsk/meson.build b/testsuite/gsk/meson.build index f303dd5f79..2b53f219e5 100644 --- a/testsuite/gsk/meson.build +++ b/testsuite/gsk/meson.build @@ -184,6 +184,7 @@ foreach test : node_parser_tests endforeach tests = [ + ['rounded-rect'], ['transform'], ] diff --git a/testsuite/gsk/rounded-rect.c b/testsuite/gsk/rounded-rect.c new file mode 100644 index 0000000000..ea969323a6 --- /dev/null +++ b/testsuite/gsk/rounded-rect.c @@ -0,0 +1,100 @@ +/* + * Copyright © 2020 Benjamin Otte + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: Benjamin Otte <otte@gnome.org> + */ + +#include "config.h" + +#include <gtk/gtk.h> + +static void +test_contains_rect (void) +{ + static double points[] = { -5, 0, 5, 10, 15, 85, 90, 95, 100, 105 }; +#define LAST (G_N_ELEMENTS(points) - 1) + GskRoundedRect rounded; + guint x1, x2, y1, y2; + + gsk_rounded_rect_init_from_rect (&rounded, &GRAPHENE_RECT_INIT (0, 0, 100, 100), 10); + + for (x1 = 0; x1 < G_N_ELEMENTS (points); x1++) + for (x2 = x1 + 1; x2 < G_N_ELEMENTS (points); x2++) + for (y1 = 0; y1 < G_N_ELEMENTS (points); y1++) + for (y2 = y1 + 1; y2 < G_N_ELEMENTS (points); y2++) + { + graphene_rect_t rect; + gboolean inside; + + /* check all points are in the bounding box */ + inside = x1 > 0 && y1 > 0 && x2 < LAST && y2 < LAST; + /* now check all the corners */ + inside &= x1 > 2 || y1 > 2 || (x1 == 2 && y1 == 2); + inside &= x2 < LAST - 2 || y1 > 2 || (x2 == LAST - 2 && y1 == 2); + inside &= x2 < LAST - 2 || y2 < LAST - 2 || (x2 == LAST - 2 && y2 == LAST - 2); + inside &= x1 > 2 || y2 < LAST - 2 || (x1 == 2 && y2 == LAST - 2); + + graphene_rect_init (&rect, points[x1], points[y1], points[x2] - points[x1], points[y2] - points[y1]); + if (inside) + g_assert_true (gsk_rounded_rect_contains_rect (&rounded, &rect)); + else + g_assert_false (gsk_rounded_rect_contains_rect (&rounded, &rect)); + } +#undef LAST +} + +static void +test_intersects_rect (void) +{ + static double points[] = { -1, 0, 1, 99, 100, 101 }; +#define ALL_THE_POINTS (G_N_ELEMENTS(points)) +#define HALF_THE_POINTS (ALL_THE_POINTS / 2) + GskRoundedRect rounded; + guint x1, x2, y1, y2; + + gsk_rounded_rect_init_from_rect (&rounded, &GRAPHENE_RECT_INIT (0, 0, 100, 100), 10); + + for (x1 = 0; x1 < ALL_THE_POINTS; x1++) + for (x2 = x1 + 1; x2 < ALL_THE_POINTS; x2++) + for (y1 = 0; y1 < ALL_THE_POINTS; y1++) + for (y2 = y1 + 1; y2 < ALL_THE_POINTS; y2++) + { + graphene_rect_t rect; + gboolean should_contain_x, should_contain_y; + + graphene_rect_init (&rect, points[x1], points[y1], points[x2] - points[x1], points[y2] - points[y1]); + should_contain_x = x1 < HALF_THE_POINTS && x2 >= HALF_THE_POINTS && y2 > 1 && y1 < ALL_THE_POINTS - 2; + should_contain_y = y1 < HALF_THE_POINTS && y2 >= HALF_THE_POINTS && x2 > 1 && x1 < ALL_THE_POINTS - 2; + if (should_contain_x || should_contain_y) + g_assert_true (gsk_rounded_rect_intersects_rect (&rounded, &rect)); + else + g_assert_false (gsk_rounded_rect_intersects_rect (&rounded, &rect)); + } +#undef ALL_THE_POINTS +#undef HALF_THE_POINTS +} + +int +main (int argc, + char *argv[]) +{ + gtk_test_init (&argc, &argv, NULL); + + g_test_add_func ("/rounded-rect/contains-rect", test_contains_rect); + g_test_add_func ("/rounded-rect/intersects-rect", test_intersects_rect); + + return g_test_run (); +} |