From 0260961fe3280a1bf5ceeecb98762abe2495de54 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Sat, 6 Nov 2021 16:52:13 +0100 Subject: [PATCH] Add test for EDA_RECT::Inflate --- qa/common/CMakeLists.txt | 1 + qa/common/test_eda_rect.cpp | 122 ++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 qa/common/test_eda_rect.cpp diff --git a/qa/common/CMakeLists.txt b/qa/common/CMakeLists.txt index 518a31897f..565f222190 100644 --- a/qa/common/CMakeLists.txt +++ b/qa/common/CMakeLists.txt @@ -34,6 +34,7 @@ set( common_srcs test_bitmap_base.cpp test_color4d.cpp test_coroutine.cpp + test_eda_rect.cpp test_lib_table.cpp test_kicad_string.cpp test_property.cpp diff --git a/qa/common/test_eda_rect.cpp b/qa/common/test_eda_rect.cpp new file mode 100644 index 0000000000..b9118b91b5 --- /dev/null +++ b/qa/common/test_eda_rect.cpp @@ -0,0 +1,122 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2021 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 + */ + +#include "math/util.h" +#include +#include + +// Code under test +#include + + +/** + * Declare the test suite + */ +BOOST_AUTO_TEST_SUITE( EdaRect ) + +/** + * Check inflation and deflation + */ +BOOST_AUTO_TEST_CASE( Inflate ) +{ + int width = 20; + int height = 30; + wxSize sizes[] = { wxSize( width, height ), wxSize( width, -height ), wxSize( -width, height ), + wxSize( -width, -height ) }; + + // We need also to check what happens when deflation exceeds the size. + int deltas[] = { width, width / 2, width / 4 }; + + for( int delta : deltas ) + { + for( wxSize size : sizes ) + { + wxPoint origin = wxPoint( 100, 100 ); + EDA_RECT rect( origin, size ); + + // The four corners of the rectangle. + wxPoint corners[] = { origin + wxSize( 0, 0 ), origin + wxSize( 0, size.y ), + origin + wxSize( size.x, 0 ), origin + wxSize( size.x, size.y ) }; + + // Inflation + + wxPoint inflation_corners[] = { + corners[0] + wxSize( -delta * sign( size.x ), -delta * sign( size.y ) ), + corners[1] + wxSize( -delta * sign( size.x ), delta * sign( size.y ) ), + corners[2] + wxSize( delta * sign( size.x ), -delta * sign( size.y ) ), + corners[3] + wxSize( delta * sign( size.x ), delta * sign( size.y ) ) + }; + + for( wxPoint corner : inflation_corners ) + { + EDA_RECT inflated_rect = rect; + inflated_rect.Inflate( delta ); + + BOOST_CHECK( !rect.Contains( corner ) ); + BOOST_CHECK( inflated_rect.Contains( corner ) ); + } + + // Deflation + + wxPoint deflation_corners[] = { + corners[0] + + wxSize( ( delta - 1 ) * sign( size.x ), ( delta - 1 ) * sign( size.y ) ), + corners[1] + + wxSize( ( delta - 1 ) * sign( size.x ), -( delta - 1 ) * sign( size.y ) ), + corners[2] + + wxSize( -( delta - 1 ) * sign( size.x ), ( delta - 1 ) * sign( size.y ) ), + corners[3] + + wxSize( -( delta - 1 ) * sign( size.x ), -( delta - 1 ) * sign( size.y ) ) + }; + + for( wxPoint corner : deflation_corners ) + { + EDA_RECT deflated_rect = rect; + deflated_rect.Inflate( -delta ); + + // If true, deflation exceeds the size. + bool zeroed = false; + + if( abs( rect.GetSize().x ) < 2 * delta ) + { + BOOST_CHECK_EQUAL( deflated_rect.GetSize().x, 0 ); + zeroed = true; + } + + if( abs( rect.GetSize().y ) < 2 * delta ) + { + BOOST_CHECK_EQUAL( deflated_rect.GetSize().y, 0 ); + zeroed = true; + } + + if( !zeroed ) + { + BOOST_CHECK( rect.Contains( corner ) ); + BOOST_CHECK( !deflated_rect.Contains( corner ) ); + } + } + } + } +} + +BOOST_AUTO_TEST_SUITE_END()