Generic SCH_ITEM tests

This commit is contained in:
Mikolaj Wielgus 2021-11-27 19:39:32 +01:00
parent c159bd79f0
commit 152a583863
3 changed files with 293 additions and 0 deletions

View File

@ -53,6 +53,7 @@ set( QA_EESCHEMA_SRCS
test_eagle_plugin.cpp test_eagle_plugin.cpp
test_lib_part.cpp test_lib_part.cpp
test_netlists.cpp test_netlists.cpp
test_sch_item.cpp
test_sch_pin.cpp test_sch_pin.cpp
test_sch_rtree.cpp test_sch_rtree.cpp
test_sch_sheet.cpp test_sch_sheet.cpp

View File

@ -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 <qa_utils/wx_utils/unit_test_utils.h>
#include <convert_to_biu.h>
#include <eda_item.h>
template <typename T>
static void IterateOverPositionsAndReferences( T* aItem, void ( *aCallback )( T*, wxPoint ) )
{
constexpr int XSTEP = static_cast<int>( Millimeter2iu( 100 ) );
constexpr int YSTEP = static_cast<int>( 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

View File

@ -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 <qa_utils/wx_utils/unit_test_utils.h>
#include <eda_item_test_utils.h>
#include <core/typeinfo.h>
// Code under test
#include <sch_item.h>
//#include <sch_marker.h>
#include <sch_junction.h>
#include <sch_no_connect.h>
#include <sch_bus_entry.h>
#include <sch_line.h>
#include <sch_bitmap.h>
#include <sch_text.h>
#include <sch_field.h>
#include <sch_symbol.h>
#include <sch_sheet_pin.h>
#include <sch_sheet.h>
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<KICAD_T>( i );
auto item = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
auto originalItem = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
if( item == nullptr || originalItem == nullptr )
continue;
BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
{
IterateOverPositionsAndReferences<SCH_ITEM>(
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<KICAD_T>( i );
auto item = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
auto originalItem = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
if( item == nullptr || originalItem == nullptr )
continue;
BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
{
IterateOverPositionsAndReferences<SCH_ITEM>(
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<KICAD_T>( i );
auto item = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
auto originalItem = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
if( item == nullptr || originalItem == nullptr )
continue;
BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
{
IterateOverPositionsAndReferences<SCH_ITEM>(
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<KICAD_T>( i );
auto item = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
auto originalItem = std::unique_ptr<SCH_ITEM>( Instatiate( type ) );
if( item == nullptr || originalItem == nullptr )
continue;
BOOST_TEST_CONTEXT( "Class: " << item->GetClass() )
{
IterateOverPositionsAndReferences<SCH_ITEM>(
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()