Start unit tests for math library trigonometry functions.

This only tests the InterceptsPositiveX() and InterceptsNegativeX() used
for finding the midpoint of an arc.

Tidy up some of the trigo.h header Doxygen comments.
This commit is contained in:
Wayne Stambaugh 2020-03-04 08:35:33 -05:00
parent 3e78403576
commit bc7df3239c
5 changed files with 161 additions and 28 deletions

View File

@ -1,7 +1,7 @@
/* /*
* This program source code file is part of KiCad, a free EDA CAD application. * 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 * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -33,20 +33,22 @@
#include <wx/gdicmn.h> // For wxPoint #include <wx/gdicmn.h> // For wxPoint
/** /**
* Function IsPointOnSegment * Test if \a aTestPoint is on line defined by \a aSegStart and \a aSegEnd.
* @param aSegStart The first point of the segment S. *
* @param aSegEnd The second point of the segment S. * This function is faster than #TestSegmentHit() because \a aTestPoint should be exactly on
* @param aTestPoint The point P to test. * the line. This works fine only for H, V and 45 degree line segments.
* @return true if the point P is on the segment S. *
* faster than TestSegmentHit() because P should be exactly on S * @param aSegStart The first point of the line segment.
* therefore works fine only for H, V and 45 deg segm. * @param aSegEnd The second point of the line segment.
* suitable for buses and wires in Eeschema, otherwise use TestSegmentHit() * @param aTestPoint The point to test.
*
* @return true if the point is on the line segment.
*/ */
bool IsPointOnSegment( const wxPoint& aSegStart, const wxPoint& aSegEnd, bool IsPointOnSegment( const wxPoint& aSegStart, const wxPoint& aSegEnd,
const wxPoint& aTestPoint ); const wxPoint& aTestPoint );
/** /**
* Function SegmentIntersectsSegment * Test if two lines intersect.
* *
* @param a_p1_l1 The first point of the first line. * @param a_p1_l1 The first point of the first line.
* @param a_p2_l1 The second 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 ); 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 aStart The starting point of the circle (equivalent to aEnd)
* @param aMid The point on the arc, half-way between aStart and 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) * @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 if \a aRefPoint is with \a aDistance on the line defined by \a aStart and \a aEnd..
* test for hit on line segment *
* i.e. a reference point is within a given distance from segment
* @param aRefPoint = reference point to test * @param aRefPoint = reference point to test
* @param aStart is the first end-point of the line segment * @param aStart is the first end-point of the line segment
* @param aEnd is the second 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 ); wxPoint aEnd, int aDist );
/** /**
* Function GetLineLength * Return the length of a line segment defined by \a aPointA and \a aPointB.
* 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 * See also EuclideanNorm and Distance for the single vector or four scalar versions.
* scalar versions *
* @return Length of a line (as double) * @return Length of a line (as double)
*/ */
inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB ) inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
@ -359,8 +361,6 @@ template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
* *
* Testing is performed in the quadrant 1 to quadrant 4 direction (counter-clockwise). * 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 aStartAngle The arc start angle in degrees.
* @param aEndAngle The arc end 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; double end = aEndAngle;
if( aStartAngle < aEndAngle ) if( aStartAngle > aEndAngle )
end += 360.0; 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). * 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 aStartAngle The arc start angle in degrees.
* @param aEndAngle The arc end 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; double end = aEndAngle;
if( aStartAngle < aEndAngle ) if( aStartAngle > aEndAngle )
end += 360.0; end += 360.0;
return aStartAngle < 180.0 && aEndAngle > 180.0; return aStartAngle < 180.0 && end > 180.0;
} }
/** /**

View File

@ -1,6 +1,6 @@
# This program source code file is part of KiCad, a free EDA CAD application. # 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 # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -21,4 +21,5 @@
# QA tests and tools for common libraries # QA tests and tools for common libraries
add_subdirectory( sexpr ) add_subdirectory( sexpr )
add_subdirectory( kimath )

View File

@ -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 )

View File

@ -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 <boost/test/unit_test.hpp>

View File

@ -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 <unit_test_utils/unit_test_utils.h>
// Code under test
#include <trigo.h>
/**
* 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()