From c9ca83443542710d247b5a4d67fa4bee2f73b31f Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Wed, 16 Jan 2019 16:46:14 +0100 Subject: [PATCH] cpml: add and test cpml_angle_distance --- src/cpml/cpml-utils.c | 19 ++++++++++++++ src/cpml/cpml-utils.h | 2 ++ src/cpml/tests/.gitignore | 1 + src/cpml/tests/Makefile.am | 3 +++ src/cpml/tests/test-utils.c | 63 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 src/cpml/tests/test-utils.c diff --git a/src/cpml/cpml-utils.c b/src/cpml/cpml-utils.c index 9e4db360..a99d0e46 100644 --- a/src/cpml/cpml-utils.c +++ b/src/cpml/cpml-utils.c @@ -57,3 +57,22 @@ cpml_angle(double angle) return angle; } + +/** + * cpml_angle_distance: + * @angle: first angle in radians + * @from: second angle in radians + * + * Computes the distance between the two given angles. The returned + * distance is always positive and is never greater than M_PI. + * + * Returns: the distance in radians + * + * Since: 1.0 + **/ +double +cpml_angle_distance(double angle, double from) +{ + double delta = cpml_angle(from - angle); + return fabs(delta); +} diff --git a/src/cpml/cpml-utils.h b/src/cpml/cpml-utils.h index aada3c2a..3eaa94ed 100644 --- a/src/cpml/cpml-utils.h +++ b/src/cpml/cpml-utils.h @@ -34,6 +34,8 @@ CAIRO_BEGIN_DECLS double cpml_angle (double angle); +double cpml_angle_distance (double angle, + double from); CAIRO_END_DECLS diff --git a/src/cpml/tests/.gitignore b/src/cpml/tests/.gitignore index 8101c527..28851b97 100644 --- a/src/cpml/tests/.gitignore +++ b/src/cpml/tests/.gitignore @@ -5,3 +5,4 @@ /test-pair /test-primitive /test-segment +/test-utils diff --git a/src/cpml/tests/Makefile.am b/src/cpml/tests/Makefile.am index 204834d6..7f978b24 100644 --- a/src/cpml/tests/Makefile.am +++ b/src/cpml/tests/Makefile.am @@ -10,6 +10,9 @@ LDADD= $(top_builddir)/src/tests/libadgtest.la \ $(ADG_LIBS) +TEST_PROGS+= test-utils$(EXEEXT) +test_utils_SOURCES= test-utils.c + TEST_PROGS+= test-pair$(EXEEXT) test_pair_SOURCES= test-pair.c diff --git a/src/cpml/tests/test-utils.c b/src/cpml/tests/test-utils.c new file mode 100644 index 00000000..9001c36e --- /dev/null +++ b/src/cpml/tests/test-utils.c @@ -0,0 +1,63 @@ +/* ADG - Automatic Drawing Generation + * Copyright (C) 2007-2019 Nicola Fontana + * + * 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 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, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#include +#include +#include + + +static void +_cpml_behavior_angle(void) +{ + adg_assert_isapprox(0, cpml_angle(0)); + adg_assert_isapprox(0, cpml_angle(2 * M_PI)); + adg_assert_isapprox(M_PI, cpml_angle(M_PI)); + adg_assert_isapprox(M_PI, cpml_angle(3 * M_PI)); + adg_assert_isapprox(M_PI, cpml_angle(-M_PI)); + adg_assert_isapprox(M_PI, cpml_angle(-3 * M_PI)); + adg_assert_isapprox(-M_PI + 1, cpml_angle(-3 * M_PI + 1)); +} + +static void +_cpml_behavior_angle_distance(void) +{ + adg_assert_isapprox(0, cpml_angle_distance(0, 0)); + adg_assert_isapprox(0, cpml_angle_distance(M_PI, M_PI)); + adg_assert_isapprox(0, cpml_angle_distance(M_PI, 3 * M_PI)); + adg_assert_isapprox(0, cpml_angle_distance(M_PI, -M_PI)); + adg_assert_isapprox(0, cpml_angle_distance(2 * M_PI, 0)); + adg_assert_isapprox(0, cpml_angle_distance(-2 * M_PI, 0)); + adg_assert_isapprox(M_PI, cpml_angle_distance(0, M_PI)); + adg_assert_isapprox(M_PI, cpml_angle_distance(M_PI, 0)); + adg_assert_isapprox(M_PI, cpml_angle_distance(2 * M_PI, M_PI)); + adg_assert_isapprox(M_PI, cpml_angle_distance(0, -M_PI)); +} + + +int +main(int argc, char *argv[]) +{ + adg_test_init(&argc, &argv); + + g_test_add_func("/cpml/utils/behavior/angle", _cpml_behavior_angle); + g_test_add_func("/cpml/utils/behavior/angle-distance", _cpml_behavior_angle_distance); + + return g_test_run(); +} -- 2.11.4.GIT