From ba41edf53188dc3f4f714046f64e6f6310a26a1c Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 6 Aug 2023 17:16:09 -0400 Subject: [PATCH] gsk: Add tests for GskBoundingBox --- testsuite/gsk/boundingbox.c | 92 +++++++++++++++++++++++++++++++++++++ testsuite/gsk/meson.build | 1 + 2 files changed, 93 insertions(+) create mode 100644 testsuite/gsk/boundingbox.c diff --git a/testsuite/gsk/boundingbox.c b/testsuite/gsk/boundingbox.c new file mode 100644 index 0000000000..52169d3979 --- /dev/null +++ b/testsuite/gsk/boundingbox.c @@ -0,0 +1,92 @@ +/* + * Copyright © 2023 Red Hat, Inc. + * + * 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 . + * + * Authors: Matthias Clasen + */ + +#include +#include + +static void +init_random_rect (graphene_rect_t *r) +{ + r->origin.x = g_test_rand_double_range (0, 1000); + r->origin.y = g_test_rand_double_range (0, 1000); + r->size.width = g_test_rand_double_range (0, 1000); + r->size.height = g_test_rand_double_range (0, 1000); +} + +static void +test_to_rect (void) +{ + graphene_rect_t rect, rect2; + GskBoundingBox bb; + graphene_point_t p, p2; + + for (unsigned int i = 0; i < 100; i++) + { + init_random_rect (&rect); + + gsk_bounding_box_init_from_rect (&bb, &rect); + gsk_bounding_box_to_rect (&bb, &rect2); + + graphene_rect_get_top_left (&rect, &p); + graphene_rect_get_top_left (&rect2, &p2); + + /* Note: that we can't assert equality here is the reason + * GskBoundingBox exists. + */ + g_assert_true (graphene_point_near (&p, &p2, 0.001)); + + graphene_rect_get_bottom_right (&rect, &p); + graphene_rect_get_bottom_right (&rect2, &p2); + + g_assert_true (graphene_point_near (&p, &p2, 0.001)); + } +} + +static void +test_contains (void) +{ + graphene_rect_t rect; + GskBoundingBox bb; + graphene_point_t p; + + for (unsigned int i = 0; i < 100; i++) + { + init_random_rect (&rect); + + gsk_bounding_box_init_from_rect (&bb, &rect); + + g_assert_true (gsk_bounding_box_contains_point (&bb, &bb.min)); + g_assert_true (gsk_bounding_box_contains_point (&bb, &bb.max)); + + graphene_point_interpolate (&bb.min, &bb.max, 0.5, &p); + g_assert_true (gsk_bounding_box_contains_point (&bb, &p)); + } +} + +int +main (int argc, + char *argv[]) +{ + gtk_test_init (&argc, &argv, NULL); + + g_test_add_func ("/bounding-box/to-rect", test_to_rect); + g_test_add_func ("/bounding-box/contains", test_contains); + + return g_test_run (); +} diff --git a/testsuite/gsk/meson.build b/testsuite/gsk/meson.build index e48e9a3a45..9e104e664e 100644 --- a/testsuite/gsk/meson.build +++ b/testsuite/gsk/meson.build @@ -393,6 +393,7 @@ internal_tests = [ [ 'half-float' ], ['rounded-rect'], ['misc'], + ['boundingbox'], ] foreach t : internal_tests