diff --git a/eeschema/pin_numbers.cpp b/eeschema/pin_numbers.cpp index fa081c9944..ca5907d376 100644 --- a/eeschema/pin_numbers.cpp +++ b/eeschema/pin_numbers.cpp @@ -25,9 +25,8 @@ #include "pin_numbers.h" #include -namespace { -wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor ) +wxString PIN_NUMBERS::getNextSymbol( const wxString& str, wxString::size_type& cursor ) { if( str.size() <= cursor ) return wxEmptyString; @@ -36,14 +35,15 @@ wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor ) wxChar c = str[cursor]; - if( wxIsdigit( c ) || c == '+' || c == '-' ) + // Need to check that there is a digit in the string before we parse it as a numeric + if( ( wxIsdigit( c ) || ( ( c == '+' || c == '-' ) && ( cursor < str.size() - 1 ) && wxIsdigit( str[cursor + 1] ) ) ) ) { // number, possibly with sign while( ++cursor < str.size() ) { c = str[cursor]; - if( wxIsdigit( c ) || c == 'v' || c == 'V' ) + if( wxIsdigit( c ) || c == 'v' || c == 'V' || c == '.' ) continue; else break; @@ -65,8 +65,6 @@ wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor ) return str.substr( begin, cursor - begin ); } -} - wxString PIN_NUMBERS::GetSummary() const { @@ -117,8 +115,8 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs ) for( ; ; ) { - symbol1 = GetNextSymbol( lhs, cursor1 ); - symbol2 = GetNextSymbol( rhs, cursor2 ); + symbol1 = getNextSymbol( lhs, cursor1 ); + symbol2 = getNextSymbol( rhs, cursor2 ); if( symbol1.empty() && symbol2.empty() ) return 0; @@ -129,12 +127,12 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs ) if( symbol2.empty() ) return 2; - wxChar c1 = symbol1[0]; - wxChar c2 = symbol2[0]; + bool sym1_isnumeric = symbol1.find_first_of( "0123456789" ) != wxString::npos; + bool sym2_isnumeric = symbol2.find_first_of( "0123456789" ) != wxString::npos; - if( wxIsdigit( c1 ) || c1 == '-' || c1 == '+' ) + if( sym1_isnumeric ) { - if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' ) + if( sym2_isnumeric ) { // numeric comparison wxString::size_type v1 = symbol1.find_first_of( wxT( "vV" ) ); @@ -173,7 +171,7 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs ) } else { - if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' ) + if( sym2_isnumeric ) return 2; int res = symbol1.Cmp( symbol2 ); diff --git a/eeschema/pin_numbers.h b/eeschema/pin_numbers.h index 2bc5c9e913..5a90e81eb1 100644 --- a/eeschema/pin_numbers.h +++ b/eeschema/pin_numbers.h @@ -47,6 +47,8 @@ private: typedef std::set container_type; + static wxString getNextSymbol( const wxString& str, wxString::size_type& cursor ); + public: typedef container_type::value_type value_type; typedef container_type::iterator iterator; diff --git a/qa/eeschema/CMakeLists.txt b/qa/eeschema/CMakeLists.txt index 6ddc16fed6..46041cc750 100644 --- a/qa/eeschema/CMakeLists.txt +++ b/qa/eeschema/CMakeLists.txt @@ -55,6 +55,7 @@ set( QA_EESCHEMA_SRCS test_lib_part.cpp test_netlists.cpp test_ee_item.cpp + test_pin_numbers.cpp test_sch_pin.cpp test_sch_rtree.cpp test_sch_sheet.cpp diff --git a/qa/eeschema/test_pin_numbers.cpp b/qa/eeschema/test_pin_numbers.cpp new file mode 100644 index 0000000000..a0cbe095b7 --- /dev/null +++ b/qa/eeschema/test_pin_numbers.cpp @@ -0,0 +1,134 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2022 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, you may find one here: + * https://www.gnu.org/licenses/gpl-3.0.html + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/** + * @file test_pin_numbers.cpp + * + * Test suite for Pin number comparison functions. + */ + +#include + +#include +#include + + +class TEST_PIN_NUMBERS +{ +public: + TEST_PIN_NUMBERS() + { + } +}; + + +struct TEST_PIN_NUMBER_CMP_CASE +{ + wxString m_lhs; + wxString m_rhs; + int m_return; +}; + +/** + * Declare the test suite + */ +BOOST_FIXTURE_TEST_SUITE( SchInternalUnits, TEST_PIN_NUMBERS ) + + +BOOST_AUTO_TEST_CASE( ComparePinNumbers ) +{ + + const std::vector cases = { + { + wxT( "+V" ), + wxT( "+V" ), + 0, + }, + { + wxT( "+V1234" ), + wxT( "+V" ), + 2, + }, + { + wxT( "+V" ), + wxT( "+V1234" ), + -2, + }, + { + wxT( "Pin1" ), + wxT( "Pin2" ), + -1 + }, + { + wxT( "Pin2" ), + wxT( "Pin1" ), + 1 + }, + { + wxT( "Pin1" ), + wxT( "Pin1" ), + 0 + }, + { + wxT( "1Pin" ), + wxT( "2Pin" ), + -1 + }, + { + wxT( "2Pin" ), + wxT( "1Pin" ), + 1 + }, + { + wxT( "1Pin" ), + wxT( "1Pin" ), + 0 + }, + { + wxT( "+3V3" ), + wxT( "+3.3" ), + 0 + }, + { + wxT( "+5V" ), + wxT( "+6V" ), + -1 + }, + { + wxT( "+6V" ), + wxT( "+5V" ), + 1 + } + + }; + + for( auto el : cases ) + { + int retval = PIN_NUMBERS::Compare( el.m_lhs, el.m_rhs ); + wxString msg; + + msg.Printf( "Comparing %s and %s failed [%d != %d]", el.m_lhs, el.m_rhs, retval, el.m_return ); + BOOST_CHECK_MESSAGE( retval == el.m_return, msg.ToStdString() ); + } +} + +BOOST_AUTO_TEST_SUITE_END()