From 152a583863d9b38e25e284d0c1d2895f7658b646 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Sat, 27 Nov 2021 19:39:32 +0100 Subject: [PATCH] Generic SCH_ITEM tests --- qa/eeschema/CMakeLists.txt | 1 + qa/eeschema/eda_item_test_utils.h | 62 ++++++++ qa/eeschema/test_sch_item.cpp | 230 ++++++++++++++++++++++++++++++ 3 files changed, 293 insertions(+) create mode 100644 qa/eeschema/eda_item_test_utils.h create mode 100644 qa/eeschema/test_sch_item.cpp diff --git a/qa/eeschema/CMakeLists.txt b/qa/eeschema/CMakeLists.txt index 552097d214..25d097356b 100644 --- a/qa/eeschema/CMakeLists.txt +++ b/qa/eeschema/CMakeLists.txt @@ -53,6 +53,7 @@ set( QA_EESCHEMA_SRCS test_eagle_plugin.cpp test_lib_part.cpp test_netlists.cpp + test_sch_item.cpp test_sch_pin.cpp test_sch_rtree.cpp test_sch_sheet.cpp diff --git a/qa/eeschema/eda_item_test_utils.h b/qa/eeschema/eda_item_test_utils.h new file mode 100644 index 0000000000..5d62b7f728 --- /dev/null +++ b/qa/eeschema/eda_item_test_utils.h @@ -0,0 +1,62 @@ +/* + * 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 + */ + +#ifndef EDA_ITEM_TEST_UTILS_H +#define EDA_ITEM_TEST_UTILS_H + +#include +#include +#include + + +template +static void IterateOverPositionsAndReferences( T* aItem, void ( *aCallback )( T*, wxPoint ) ) +{ + constexpr int XSTEP = static_cast( Millimeter2iu( 100 ) ); + constexpr int YSTEP = static_cast( Millimeter2iu( 50 ) ); + constexpr int XMIN = -1 * XSTEP; + constexpr int XMAX = 1 * XSTEP; + constexpr int YMIN = -1 * YSTEP; + constexpr int YMAX = 1 * YSTEP; + + for( int posX = XMIN; posX <= XMAX; posX += XSTEP ) + { + for( int posY = YMIN; posY <= YMAX; posY += YSTEP ) + { + for( int refX = XMIN; refX <= XMAX; refX += XSTEP ) + { + for( int refY = YMIN; refY <= YMAX; refY += YSTEP ) + { + BOOST_TEST_CONTEXT( wxString::Format( "Position: %d %d, Reference: %d %d", + posX, posY, refX, refY ) ) + { + aItem->SetPosition( wxPoint( posX, posY ) ); + aCallback( aItem, wxPoint( refX, refY ) ); + } + } + } + } + } +} + +#endif // EDA_ITEM_TEST_UTILS_H diff --git a/qa/eeschema/test_sch_item.cpp b/qa/eeschema/test_sch_item.cpp new file mode 100644 index 0000000000..50cd8a463e --- /dev/null +++ b/qa/eeschema/test_sch_item.cpp @@ -0,0 +1,230 @@ +/* + * 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 +#include +#include + +// Code under test +#include +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static SCH_ITEM* Instatiate( KICAD_T aType ) +{ + if( !IsEeschemaType( aType ) ) + return nullptr; + + if( !IsInstatiableType( aType ) ) + return nullptr; + + switch( aType ) + { + case SCH_MARKER_T: return nullptr; + case SCH_JUNCTION_T: return new SCH_JUNCTION(); + case SCH_NO_CONNECT_T: return new SCH_NO_CONNECT(); + case SCH_BUS_WIRE_ENTRY_T: return new SCH_BUS_WIRE_ENTRY(); + case SCH_BUS_BUS_ENTRY_T: return new SCH_BUS_BUS_ENTRY(); + case SCH_LINE_T: return new SCH_LINE(); + case SCH_BITMAP_T: return new SCH_BITMAP(); + case SCH_TEXT_T: return new SCH_TEXT( wxPoint( 0, 0 ), "test" ); + case SCH_LABEL_T: return new SCH_LABEL( wxPoint( 0, 0 ), "test" ); + case SCH_GLOBAL_LABEL_T: return new SCH_GLOBALLABEL(); + case SCH_HIER_LABEL_T: return new SCH_HIERLABEL(); + case SCH_FIELD_T: return new SCH_FIELD( wxPoint( 0, 0 ), 0, nullptr ); + case SCH_SYMBOL_T: return new SCH_SYMBOL(); + + case SCH_SHEET_PIN_T: + case SCH_SHEET_T: + case SCH_PIN_T: + case SCHEMATIC_T: + // TODO + return nullptr; + + // `LIB_ITEM`s aren't handled in this module. + case LIB_SYMBOL_T: + case LIB_ALIAS_T: + case LIB_SHAPE_T: + case LIB_TEXT_T: + case LIB_PIN_T: + case LIB_FIELD_T: + return nullptr; + + default: + BOOST_FAIL( wxString::Format( "Unhandled type: %d", aType ) ); + return nullptr; + } +} + + +static void CompareItems( SCH_ITEM* aItem, SCH_ITEM* aOriginalItem ) +{ + BOOST_CHECK_EQUAL( aItem->GetPosition(), aOriginalItem->GetPosition() ); + BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetOrigin(), + aOriginalItem->GetBoundingBox().GetOrigin() ); + BOOST_CHECK_EQUAL( aItem->GetBoundingBox().GetSize(), + aOriginalItem->GetBoundingBox().GetSize() ); +} + + +/** + * Declare the test suite + */ +BOOST_AUTO_TEST_SUITE( SchItem ) + + +BOOST_AUTO_TEST_CASE( Move ) +{ + for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ ) + { + KICAD_T type = static_cast( i ); + + auto item = std::unique_ptr( Instatiate( type ) ); + auto originalItem = std::unique_ptr( Instatiate( type ) ); + + if( item == nullptr || originalItem == nullptr ) + continue; + + BOOST_TEST_CONTEXT( "Class: " << item->GetClass() ) + { + IterateOverPositionsAndReferences( + item.get(), + []( SCH_ITEM* aOriginalItem, wxPoint aOffset ) + { + SCH_ITEM* aItem = aOriginalItem->Duplicate(); + + aItem->Move( aOffset ); + aItem->Move( -aOffset ); + + CompareItems( aItem, aOriginalItem ); + } ); + } + } +} + + +BOOST_AUTO_TEST_CASE( Rotate ) +{ + for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ ) + { + KICAD_T type = static_cast( i ); + + auto item = std::unique_ptr( Instatiate( type ) ); + auto originalItem = std::unique_ptr( Instatiate( type ) ); + + if( item == nullptr || originalItem == nullptr ) + continue; + + BOOST_TEST_CONTEXT( "Class: " << item->GetClass() ) + { + IterateOverPositionsAndReferences( + item.get(), + []( SCH_ITEM* aOriginalItem, wxPoint aRef ) + { + SCH_ITEM* aItem = aOriginalItem->Duplicate(); + + // Four rotations are an identity. + aItem->Rotate( aRef ); + aItem->Rotate( aRef ); + aItem->Rotate( aRef ); + aItem->Rotate( aRef ); + + CompareItems( aItem, aOriginalItem ); + } ); + } + } +} + + +BOOST_AUTO_TEST_CASE( MirrorHorizontally ) +{ + for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ ) + { + KICAD_T type = static_cast( i ); + + auto item = std::unique_ptr( Instatiate( type ) ); + auto originalItem = std::unique_ptr( Instatiate( type ) ); + + if( item == nullptr || originalItem == nullptr ) + continue; + + BOOST_TEST_CONTEXT( "Class: " << item->GetClass() ) + { + IterateOverPositionsAndReferences( + item.get(), + []( SCH_ITEM* aOriginalItem, wxPoint aRef ) + { + SCH_ITEM* aItem = aOriginalItem->Duplicate(); + + // Two mirrorings are an identity. + aItem->MirrorHorizontally( aRef.x ); + aItem->MirrorHorizontally( aRef.x ); + + CompareItems( aItem, aOriginalItem ); + } ); + } + } +} + + +BOOST_AUTO_TEST_CASE( MirrorVertically ) +{ + for( int i = 0; i < MAX_STRUCT_TYPE_ID; i++ ) + { + KICAD_T type = static_cast( i ); + + auto item = std::unique_ptr( Instatiate( type ) ); + auto originalItem = std::unique_ptr( Instatiate( type ) ); + + if( item == nullptr || originalItem == nullptr ) + continue; + + BOOST_TEST_CONTEXT( "Class: " << item->GetClass() ) + { + IterateOverPositionsAndReferences( + item.get(), + []( SCH_ITEM* aOriginalItem, wxPoint aRef ) + { + SCH_ITEM* aItem = aOriginalItem->Duplicate(); + + // Two mirrorings are an identity. + aItem->MirrorVertically( aRef.x ); + aItem->MirrorVertically( aRef.x ); + + CompareItems( aItem, aOriginalItem ); + } ); + } + } +} + +BOOST_AUTO_TEST_SUITE_END()