Add hashing to and unit tests for some low level objects.

This commit is contained in:
Wayne Stambaugh 2023-04-04 15:28:34 -04:00
parent 8dfe8e595c
commit c3e6825d62
8 changed files with 139 additions and 3 deletions

View File

@ -1000,6 +1000,14 @@ wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
}
std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aText )
{
aStream << aText.GetText();
return aStream;
}
static struct EDA_TEXT_DESC
{
EDA_TEXT_DESC()

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2004-2022 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2004-2023 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
@ -95,7 +95,10 @@ public:
* for intance a title, a prefix for texts in display functions.
* False to disable any added text (for instance when writing the shown text in netlists).
*/
virtual wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const { return m_shown_text; }
virtual wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const
{
return m_shown_text;
}
/**
* Indicates the ShownText has text var references which need to be processed.
@ -330,6 +333,10 @@ public:
int Compare( const EDA_TEXT* aOther ) const;
bool operator==( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) == 0; }
bool operator<( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) < 0; }
bool operator>( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) > 0; }
virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
wxString GetHyperlink() const { return m_hyperlink; }
void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
@ -408,4 +415,21 @@ private:
};
extern std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aAttributes );
template<>
struct std::hash<EDA_TEXT>
{
std::size_t operator()( const EDA_TEXT& aText ) const
{
std::size_t seed;
hash_combine( seed, aText.GetText(), aText.GetAttributes(), aText.GetTextPos().x,
aText.GetTextPos().y );
return seed;
}
};
#endif // EDA_TEXT_H_

View File

@ -92,4 +92,22 @@ public:
extern std::ostream& operator<<( std::ostream& aStream, const TEXT_ATTRIBUTES& aAttributes );
template<>
struct std::hash<TEXT_ATTRIBUTES>
{
std::size_t operator()( const TEXT_ATTRIBUTES& aAttributes ) const
{
std::size_t seed;
hash_combine( seed, aAttributes.m_Font, aAttributes.m_Halign, aAttributes.m_Valign,
aAttributes.m_Angle.AsDegrees(), aAttributes.m_LineSpacing,
aAttributes.m_StrokeWidth, aAttributes.m_Italic, aAttributes.m_Bold,
aAttributes.m_Underlined, aAttributes.m_Color, aAttributes.m_Visible,
aAttributes.m_Mirrored, aAttributes.m_Multiline, aAttributes.m_Size.x,
aAttributes.m_Size.y );
return seed;
}
};
#endif //TEXT_ATTRIBUTES_H

View File

@ -30,6 +30,7 @@
#include <wx/debug.h>
#include <wx/colour.h>
#include <wx/string.h>
#include <hash.h>
#include <nlohmann/json_fwd.hpp>
@ -404,4 +405,17 @@ void from_json( const nlohmann::json& aJson, COLOR4D& aColor );
} // namespace KIGFX
template<>
struct std::hash<KIGFX::COLOR4D>
{
std::size_t operator()( const KIGFX::COLOR4D& aColor ) const
{
std::size_t seed;
hash_combine( seed, aColor.r, aColor.b, aColor.g, aColor.a );
return seed;
}
};
#endif /* COLOR4D_H_ */

View File

@ -35,6 +35,7 @@ set( QA_COMMON_SRCS
test_color4d.cpp
test_coroutine.cpp
test_eda_shape.cpp
test_eda_text.cpp
test_lib_table.cpp
test_markup_parser.cpp
test_kicad_string.cpp

View File

@ -307,17 +307,21 @@ BOOST_AUTO_TEST_CASE( FromWx )
/**
* Check the Compare method.
* Check the Compare method and hashing.
*/
BOOST_AUTO_TEST_CASE( Compare )
{
std::hash<COLOR4D> colorHasher;
COLOR4D a( 0.5, 0.5, 0.5, 0.5 );
COLOR4D b( 0.5, 0.5, 0.5, 0.5 );
BOOST_CHECK_EQUAL( a.Compare( b ), 0 );
BOOST_CHECK_EQUAL( colorHasher( a ), colorHasher( b ) );
b.r = 0.25;
BOOST_CHECK_GT( a.Compare( b ), 0 );
BOOST_CHECK_NE( colorHasher( a ), colorHasher( b ) );
b.r = 0.75;
BOOST_CHECK_LT( a.Compare( b ), 0 );

View File

@ -0,0 +1,64 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2023 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <boost/test/unit_test.hpp>
#include <base_units.h>
#include <eda_text.h>
BOOST_AUTO_TEST_SUITE( EdaText )
BOOST_AUTO_TEST_CASE( Compare )
{
std::hash<EDA_TEXT> hasher;
EDA_TEXT a( unityScale );
EDA_TEXT b( unityScale );
BOOST_CHECK_EQUAL( a, b );
BOOST_CHECK_EQUAL( hasher( a ), hasher( b ) );
a.SetText( wxS( "A" ) );
BOOST_CHECK_GT( a, b );
BOOST_CHECK_NE( hasher( a ), hasher( b ) );
b.SetText( wxS( "B" ) );
BOOST_CHECK_LT( a, b );
BOOST_CHECK_NE( hasher( a ), hasher( b ) );
a.SetText( wxS( "B" ) );
a.SetTextPos( VECTOR2I( 1, 0 ) );
BOOST_CHECK_GT( a, b );
a.SetTextPos( VECTOR2I( -1, 0 ) );
BOOST_CHECK_LT( a, b );
a.SetTextPos( VECTOR2I( 0, 0 ) );
b.SetTextPos( VECTOR2I( 0, 1 ) );
BOOST_CHECK_LT( a, b );
b.SetTextPos( VECTOR2I( 0, -1 ) );
BOOST_CHECK_GT( a, b );
// Text attributes are tested in the TEXT_ATTRIBUTES unit tests.
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -29,13 +29,16 @@ BOOST_AUTO_TEST_SUITE( TextAttributes )
BOOST_AUTO_TEST_CASE( Compare )
{
std::hash<TEXT_ATTRIBUTES> hasher;
TEXT_ATTRIBUTES a;
TEXT_ATTRIBUTES b;
BOOST_CHECK_EQUAL( a, b );
BOOST_CHECK_EQUAL( hasher( a ), hasher( b ) );
a.m_Font = KIFONT::FONT::GetFont();
BOOST_CHECK_GT( a, b );
BOOST_CHECK_NE( hasher( a ), hasher( b ) );
a.m_Font = nullptr;
b.m_Font = KIFONT::FONT::GetFont();