From c3e6825d62e4941f567f27842025836e1da263d8 Mon Sep 17 00:00:00 2001 From: Wayne Stambaugh Date: Tue, 4 Apr 2023 15:28:34 -0400 Subject: [PATCH] Add hashing to and unit tests for some low level objects. --- common/eda_text.cpp | 8 +++ include/eda_text.h | 28 ++++++++- include/font/text_attributes.h | 18 ++++++ include/gal/color4d.h | 14 +++++ qa/unittests/common/CMakeLists.txt | 1 + qa/unittests/common/test_color4d.cpp | 6 +- qa/unittests/common/test_eda_text.cpp | 64 ++++++++++++++++++++ qa/unittests/common/test_text_attributes.cpp | 3 + 8 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 qa/unittests/common/test_eda_text.cpp diff --git a/common/eda_text.cpp b/common/eda_text.cpp index ba276709f7..fb177e6409 100644 --- a/common/eda_text.cpp +++ b/common/eda_text.cpp @@ -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() diff --git a/include/eda_text.h b/include/eda_text.h index 37eff251fe..70fd38b7f0 100644 --- a/include/eda_text.h +++ b/include/eda_text.h @@ -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 +{ + 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_ diff --git a/include/font/text_attributes.h b/include/font/text_attributes.h index c7c5655a52..e153b4267d 100644 --- a/include/font/text_attributes.h +++ b/include/font/text_attributes.h @@ -92,4 +92,22 @@ public: extern std::ostream& operator<<( std::ostream& aStream, const TEXT_ATTRIBUTES& aAttributes ); +template<> +struct std::hash +{ + 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 diff --git a/include/gal/color4d.h b/include/gal/color4d.h index f84969db9e..5616d8b104 100644 --- a/include/gal/color4d.h +++ b/include/gal/color4d.h @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -404,4 +405,17 @@ void from_json( const nlohmann::json& aJson, COLOR4D& aColor ); } // namespace KIGFX +template<> +struct std::hash +{ + 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_ */ diff --git a/qa/unittests/common/CMakeLists.txt b/qa/unittests/common/CMakeLists.txt index 3aa2c5046f..cadafbd0ed 100644 --- a/qa/unittests/common/CMakeLists.txt +++ b/qa/unittests/common/CMakeLists.txt @@ -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 diff --git a/qa/unittests/common/test_color4d.cpp b/qa/unittests/common/test_color4d.cpp index 722cf4fb78..5139d60826 100644 --- a/qa/unittests/common/test_color4d.cpp +++ b/qa/unittests/common/test_color4d.cpp @@ -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 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 ); diff --git a/qa/unittests/common/test_eda_text.cpp b/qa/unittests/common/test_eda_text.cpp new file mode 100644 index 0000000000..f8c67d6498 --- /dev/null +++ b/qa/unittests/common/test_eda_text.cpp @@ -0,0 +1,64 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2023 Wayne Stambaugh + * 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 . + */ + +#include +#include +#include + + +BOOST_AUTO_TEST_SUITE( EdaText ) + + +BOOST_AUTO_TEST_CASE( Compare ) +{ + std::hash 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() diff --git a/qa/unittests/common/test_text_attributes.cpp b/qa/unittests/common/test_text_attributes.cpp index d5ebb42c90..efec1d90d5 100644 --- a/qa/unittests/common/test_text_attributes.cpp +++ b/qa/unittests/common/test_text_attributes.cpp @@ -29,13 +29,16 @@ BOOST_AUTO_TEST_SUITE( TextAttributes ) BOOST_AUTO_TEST_CASE( Compare ) { + std::hash 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();