KiCad PCB EDA Suite
test_kicad_string.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018 KiCad Developers, see AUTHORS.TXT for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
30 
31 // Code under test
32 #include <string_utils.h>
33 
37 BOOST_AUTO_TEST_SUITE( KicadString )
38 
39 
42 BOOST_AUTO_TEST_CASE( TrailingInt )
43 {
44  using CASE = std::pair<std::string, int>;
45 
46  const std::vector<CASE> cases = {
47  { "", 0 }, { "foo", 0 }, // no int
48  { "0", 0 }, // only int
49  { "42", 42 }, // only int
50  { "1001", 1001 }, // only int
51  { "Foo42", 42 }, { "12Foo42", 42 }, // only the trailing
52  { "12Foo4.2", 2 }, // no dots
53  };
54 
55  for( const auto& c : cases )
56  {
57  BOOST_CHECK_EQUAL( GetTrailingInt( c.first ), c.second );
58  }
59 }
60 
64 BOOST_AUTO_TEST_CASE( NaturalNumberCompare )
65 {
66  using CASE = std::pair<std::pair<std::string, std::string>, std::pair<int, int>>;
67 
68  const std::vector<CASE> cases = {
69  { { "a", "b" }, { -1, -1 } },
70  { { "b", "a" }, { 1, 1 } },
71  { { "a", "a" }, { 0, 0 } },
72  { { "a", "A" }, { 1, 0 } },
73  { { "A", "a" }, { -1, 0 } },
74  { { "a", "" }, { 1, 1 } },
75  { { "", "a" }, { -1, -1 } },
76  { { "1", "" }, { 1, 1 } },
77  { { "", "1" }, { -1, -1 } },
78  { { "10", "2" }, { 1, 1 } },
79  { { "2", "10" }, { -1, -1 } },
80  { { "2", "2" }, { 0, 0 } },
81  { { "10", "10" }, { 0, 0 } },
82  { { "01", "1" }, { 0, 0 } },
83  { { "01a", "1a" }, { 0, 0 } },
84  { { "01a", "1b" }, { -1, -1 } },
85  { { "01b", "1a" }, { 1, 1 } },
86  { { "10 ten", "2 two" }, { 1, 1 } },
87  { { "SYM1", "sym2" }, { -1, -1 } },
88  { { "sym2", "SYM1" }, { 1, 1 } },
89  { { "a10b20c30", "a10b20c31" }, { -1, -1 } },
90  { { "a10b20c31", "a10b20c30" }, { 1, 1 } },
91  { { "10UF", "10UF" }, { 0, 0 } },
92  { { "10uF", "10uF" }, { 0, 0 } },
93  { { "u10", "u10" }, { 0, 0 } },
94  { { "U10", "U10" }, { 0, 0 } },
95  { { "u10", "U10" }, { 1, 0 } },
96  { { "U10", "u10" }, { -1, 0 } },
97  { { "U10.1", "U10.10" }, { -1, -1 } },
98  { { "U10-1", "U10-10" }, { -1, -1 } },
99  { { "U10,1", "U10,10" }, { -1, -1 } },
100  { { "U10.A", "U10.a" }, { -1, 0 } },
101  { { "U10-A", "U10-a" }, { -1, 0 } },
102  { { "U10,A", "U10,a" }, { -1, 0 } },
103  };
104 
105  for( const auto& c : cases )
106  {
107  BOOST_CHECK_MESSAGE( StrNumCmp( c.first.first, c.first.second ) == c.second.first,
108  c.first.first + " AND " + c.first.second + " failed for case sensitive" );
109 
110  BOOST_CHECK_MESSAGE(
111  StrNumCmp( c.first.first, c.first.second, true ) == c.second.second,
112  c.first.first + " AND " + c.first.second + " failed for case insensitive" );
113  }
114 }
115 
116 BOOST_AUTO_TEST_SUITE_END()
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
BOOST_AUTO_TEST_CASE(TrailingInt)
Declare the test suite.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.