kicad/qa/tests/libs/kimath/math/test_vector2.cpp

186 lines
6.4 KiB
C++
Raw Permalink Normal View History

/*
* 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
*/
/**
* Test suite for KiCad math code.
*/
2021-02-06 22:20:31 +00:00
#include <qa_utils/wx_utils/unit_test_utils.h>
// Code under test
#include <math/vector2d.h>
/**
* Declare the test suite
*/
BOOST_AUTO_TEST_SUITE( VECTOR2TESTS )
BOOST_AUTO_TEST_CASE( test_cross_product, *boost::unit_test::tolerance( 0.000001 ) )
{
VECTOR2I v1(0, 1);
VECTOR2I v2(1, 0);
BOOST_CHECK( v2.Cross( v1 ) == 1 );
}
BOOST_AUTO_TEST_CASE( test_dot_product, *boost::unit_test::tolerance( 0.000001 ) )
{
VECTOR2I v1( 0, 1 );
VECTOR2I v2( 1, 0 );
BOOST_CHECK( v2.Dot( v1 ) == 0 );
}
BOOST_AUTO_TEST_CASE( test_resize, *boost::unit_test::tolerance( 0.000001 ) )
{
// just some arbitrary vectors
VECTOR2I v1( 4, 3 );
VECTOR2I v2( 5, -1 );
VECTOR2I v3( -2, 1 );
VECTOR2I v4( 1, 1 );
VECTOR2I v5( -70, -70 );
BOOST_CHECK( v1.Resize( 8 ) == VECTOR2I( 6, 5 ) );
BOOST_CHECK( v2.Resize( 10 ) == VECTOR2I( 10, -2 ) );
BOOST_CHECK( v3.Resize( 4 ) == VECTOR2I( -4, 2 ) );
BOOST_CHECK( v4.Resize( 1 ) == VECTOR2I( 1, 1 ) );
BOOST_CHECK( v5.Resize( 100 ) == VECTOR2I( -71, -71 ) );
}
2024-06-10 22:12:57 +00:00
BOOST_AUTO_TEST_CASE( test_casting )
{
VECTOR2I vint( 4, 3 );
VECTOR2D vdouble( 4.0, 3.0 );
VECTOR2L vlong( 4, 3 );
VECTOR2<float> vfloat( 4.0f, 3.0f );
VECTOR2<unsigned> vunsigned( 4, 3 );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vdouble ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vlong ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vfloat ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vunsigned ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vint ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vlong ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vfloat ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vunsigned ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vint ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vdouble ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vfloat ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vunsigned ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vint ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vdouble ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vlong ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vunsigned ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vint ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vdouble ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vlong ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vfloat ) );
2024-06-10 22:12:57 +00:00
// Check that negative values are handled correctly
vint = vint - 1;
vdouble = vdouble - 1;
vlong = vlong - 1;
vfloat = vfloat - 1;
vunsigned = vunsigned - 1;
BOOST_CHECK_EQUAL( vint, VECTOR2I( 3, 2 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( 3.0, 2.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( 3, 2 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( 3.0f, 2.0f ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 3, 2 ) );
// Check that subtracting unsigned values works correctly
vint = vint - (unsigned)1;
vdouble = vdouble - (unsigned)1;
vlong = vlong - (unsigned)1;
vfloat = vfloat - (unsigned)1;
vunsigned = vunsigned - (unsigned)1;
BOOST_CHECK_EQUAL( vint, VECTOR2I( 2, 1 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( 2.0, 1.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( 2, 1 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( 2.0f, 1.0f ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 2, 1 ) );
vint = vint - 5.0;
vdouble = vdouble - 5.0;
vlong = vlong - 5.0;
vfloat = vfloat - 5.0;
vunsigned = vunsigned - 5.0;
BOOST_CHECK_EQUAL( vint, VECTOR2I( -3, -4 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -3.0, -4.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( -3, -4 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -3.0f, -4.0f ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 4294967293, 4294967292 ) ); // roll over unsigned when explicitly subtracting.
2024-06-10 22:12:57 +00:00
// Check that negative initial values are handled correctly
vint = VECTOR2I( -4, -3 );
vdouble = VECTOR2D( -4.0, -3.0 );
vlong = VECTOR2L( -4, -3 );
vfloat = VECTOR2<float>( -4.0f, -3.0f );
vunsigned = VECTOR2<unsigned>( -4, -3 );
2024-06-10 22:12:57 +00:00
vint = vint - 1;
vdouble = vdouble - 1;
vlong = vlong - 1;
vfloat = vfloat - 1;
vunsigned = vunsigned - 1;
2024-06-10 22:12:57 +00:00
BOOST_CHECK_EQUAL( vint, VECTOR2I( -5, -4 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -5.0, -4.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( -5, -4 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -5.0f, -4.0f ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 4294967291, 4294967292 ) );
2024-06-10 22:12:57 +00:00
vint = vint - 1u;
vdouble = vdouble - 1u;
vlong = vlong - 1u;
vfloat = vfloat - 1u;
BOOST_CHECK_EQUAL( vint, VECTOR2I( -6, -5 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -6.0, -5.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( -6, -5 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -6.0f, -5.0f ) );
2024-06-10 22:12:57 +00:00
auto add = vint + vdouble;
BOOST_CHECK_EQUAL( add, VECTOR2D( -12.0, -10.0 ) );
2024-06-10 22:12:57 +00:00
auto sub = vint - 2 * vlong;
BOOST_CHECK_EQUAL( sub.x, 6 );
BOOST_CHECK_EQUAL( sub.y, 5 );
vunsigned = VECTOR2<unsigned>( std::numeric_limits<unsigned>::max(), std::numeric_limits<unsigned>::max() );
vint = VECTOR2I( vunsigned );
BOOST_CHECK_EQUAL( vint.x, std::numeric_limits<int>::max() );
vunsigned += 1;
BOOST_CHECK_EQUAL( vunsigned.x, 0 );
2024-06-10 22:12:57 +00:00
}
BOOST_AUTO_TEST_SUITE_END()