diff --git a/libs/kimath/include/trigo.h b/libs/kimath/include/trigo.h index 60eab83b09..fa319993f8 100644 --- a/libs/kimath/include/trigo.h +++ b/libs/kimath/include/trigo.h @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2018-2019 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2018-2020 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -33,20 +33,22 @@ #include // For wxPoint /** - * Function IsPointOnSegment - * @param aSegStart The first point of the segment S. - * @param aSegEnd The second point of the segment S. - * @param aTestPoint The point P to test. - * @return true if the point P is on the segment S. - * faster than TestSegmentHit() because P should be exactly on S - * therefore works fine only for H, V and 45 deg segm. - * suitable for buses and wires in Eeschema, otherwise use TestSegmentHit() + * Test if \a aTestPoint is on line defined by \a aSegStart and \a aSegEnd. + * + * This function is faster than #TestSegmentHit() because \a aTestPoint should be exactly on + * the line. This works fine only for H, V and 45 degree line segments. + * + * @param aSegStart The first point of the line segment. + * @param aSegEnd The second point of the line segment. + * @param aTestPoint The point to test. + * + * @return true if the point is on the line segment. */ bool IsPointOnSegment( const wxPoint& aSegStart, const wxPoint& aSegEnd, const wxPoint& aTestPoint ); /** - * Function SegmentIntersectsSegment + * Test if two lines intersect. * * @param a_p1_l1 The first point of the first line. * @param a_p2_l1 The second point of the first line. @@ -99,7 +101,8 @@ void RotatePoint( double *pX, double *pY, double angle ); void RotatePoint( double *pX, double *pY, double cx, double cy, double angle ); /** - * Determine the center of an arc/circle, given three points on its circumference + * Determine the center of an arc or circle given three points on its circumference. + * * @param aStart The starting point of the circle (equivalent to aEnd) * @param aMid The point on the arc, half-way between aStart and aEnd * @param aEnd The ending point of the circle (equivalent to aStart) @@ -183,9 +186,8 @@ inline double CrossProduct( const wxPoint &vectorA, const wxPoint &vectorB ) } /** - * Function TestSegmentHit - * test for hit on line segment - * i.e. a reference point is within a given distance from segment + * Test if \a aRefPoint is with \a aDistance on the line defined by \a aStart and \a aEnd.. + * * @param aRefPoint = reference point to test * @param aStart is the first end-point of the line segment * @param aEnd is the second end-point of the line segment @@ -195,10 +197,10 @@ bool TestSegmentHit( const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist ); /** - * Function GetLineLength - * returns the length of a line segment defined by \a aPointA and \a aPointB. - * See also EuclideanNorm and Distance for the single vector or four - * scalar versions + * Return the length of a line segment defined by \a aPointA and \a aPointB. + * + * See also EuclideanNorm and Distance for the single vector or four scalar versions. + * * @return Length of a line (as double) */ inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB ) @@ -359,8 +361,6 @@ template inline void NORMALIZE_ANGLE_180( T& Angle ) * * Testing is performed in the quadrant 1 to quadrant 4 direction (counter-clockwise). * - * @warning Do not use this function, it has not been tested. - * * @param aStartAngle The arc start angle in degrees. * @param aEndAngle The arc end angle in degrees. */ @@ -368,10 +368,10 @@ inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle ) { double end = aEndAngle; - if( aStartAngle < aEndAngle ) + if( aStartAngle > aEndAngle ) end += 360.0; - return aStartAngle < 360.0 && aEndAngle > 360.0; + return aStartAngle < 360.0 && end > 360.0; } /** @@ -379,8 +379,6 @@ inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle ) * * Testing is performed in the quadrant 1 to quadrant 4 direction (counter-clockwise). * - * @warning Do not use this function, it has not been tested. - * * @param aStartAngle The arc start angle in degrees. * @param aEndAngle The arc end angle in degrees. */ @@ -388,10 +386,10 @@ inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle ) { double end = aEndAngle; - if( aStartAngle < aEndAngle ) + if( aStartAngle > aEndAngle ) end += 360.0; - return aStartAngle < 180.0 && aEndAngle > 180.0; + return aStartAngle < 180.0 && end > 180.0; } /** diff --git a/qa/libs/CMakeLists.txt b/qa/libs/CMakeLists.txt index a0d3a88943..9836c2585b 100644 --- a/qa/libs/CMakeLists.txt +++ b/qa/libs/CMakeLists.txt @@ -1,6 +1,6 @@ # This program source code file is part of KiCad, a free EDA CAD application. # -# Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. +# Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -21,4 +21,5 @@ # QA tests and tools for common libraries -add_subdirectory( sexpr ) \ No newline at end of file +add_subdirectory( sexpr ) +add_subdirectory( kimath ) diff --git a/qa/libs/kimath/CMakeLists.txt b/qa/libs/kimath/CMakeLists.txt new file mode 100644 index 0000000000..ab41eff648 --- /dev/null +++ b/qa/libs/kimath/CMakeLists.txt @@ -0,0 +1,43 @@ +# This program source code file is part of KiCad, a free EDA CAD application. +# +# Copyright (C) 2020 KiCad Developers, see AUTHORS.TXT for contributors. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, you may find one here: +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# or you may search the http://www.gnu.org website for the version 2 license, +# or you may write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +# +# Unit tests for KiCad math routines. + +set( KIMATH_SRCS + kimath_test_module.cpp + + test_kimath.cpp +) + +add_executable( qa_kimath ${KIMATH_SRCS} ) + +target_link_libraries( qa_kimath + kimath + unit_test_utils + ${wxWidgets_LIBRARIES} +) + +target_include_directories( qa_kimath PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} +) + +kicad_add_boost_test( qa_kimath kmath ) diff --git a/qa/libs/kimath/kimath_test_module.cpp b/qa/libs/kimath/kimath_test_module.cpp new file mode 100644 index 0000000000..02faf54221 --- /dev/null +++ b/qa/libs/kimath/kimath_test_module.cpp @@ -0,0 +1,28 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 KiCad Developers, see CHANGELOG.TXT for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/** + * Main file for the Sexpr tests + */ +#define BOOST_TEST_MODULE KiMath +#include diff --git a/qa/libs/kimath/test_kimath.cpp b/qa/libs/kimath/test_kimath.cpp new file mode 100644 index 0000000000..07518e83e1 --- /dev/null +++ b/qa/libs/kimath/test_kimath.cpp @@ -0,0 +1,63 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 KiCad Developers, see CHANGELOG.TXT for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/** + * Test suite for KiCad math code. + */ + +#include + +// Code under test +#include + + +/** + * Declare the test suite + */ +BOOST_AUTO_TEST_SUITE( KiMath ) + + +BOOST_AUTO_TEST_CASE( TestInterceptsPositiveX ) +{ + BOOST_CHECK( !InterceptsPositiveX( 10.0, 20.0 ) ); + BOOST_CHECK( !InterceptsPositiveX( 10.0, 120.0 ) ); + BOOST_CHECK( !InterceptsPositiveX( 10.0, 220.0 ) ); + BOOST_CHECK( !InterceptsPositiveX( 10.0, 320.0 ) ); + BOOST_CHECK( InterceptsPositiveX( 20.0, 10.0 ) ); + BOOST_CHECK( InterceptsPositiveX( 345.0, 15.0 ) ); +} + + +BOOST_AUTO_TEST_CASE( TestInterceptsNegativeX ) +{ + BOOST_CHECK( !InterceptsNegativeX( 10.0, 20.0 ) ); + BOOST_CHECK( !InterceptsNegativeX( 10.0, 120.0 ) ); + BOOST_CHECK( InterceptsNegativeX( 10.0, 220.0 ) ); + BOOST_CHECK( InterceptsNegativeX( 10.0, 320.0 ) ); + BOOST_CHECK( InterceptsNegativeX( 20.0, 10.0 ) ); + BOOST_CHECK( !InterceptsNegativeX( 345.0, 15.0 ) ); + BOOST_CHECK( InterceptsNegativeX( 145.0, 225.0 ) ); +} + + +BOOST_AUTO_TEST_SUITE_END()