KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
24
26
27// Code under test
28#include <string_utils.h>
29
33BOOST_AUTO_TEST_SUITE( KicadString )
34
35
36
40{
41 using CASE = std::pair<std::string, int>;
42
43 const std::vector<CASE> cases = {
44 { "", 0 }, { "foo", 0 }, // no int
45 { "0", 0 }, // only int
46 { "42", 42 }, // only int
47 { "1001", 1001 }, // only int
48 { "Foo42", 42 }, { "12Foo42", 42 }, // only the trailing
49 { "12Foo4.2", 2 }, // no dots
50 };
51
52 for( const auto& c : cases )
53 {
54 BOOST_CHECK_EQUAL( GetTrailingInt( c.first ), c.second );
55 }
56}
57
58
62BOOST_AUTO_TEST_CASE( NaturalNumberCompare )
63{
64 using CASE = std::pair<std::pair<std::string, std::string>, std::pair<int, int>>;
65
66 const std::vector<CASE> cases = {
67 { { "a", "b" }, { -1, -1 } },
68 { { "b", "a" }, { 1, 1 } },
69 { { "a", "a" }, { 0, 0 } },
70 { { "a", "A" }, { 1, 0 } },
71 { { "A", "a" }, { -1, 0 } },
72 { { "a", "" }, { 1, 1 } },
73 { { "", "a" }, { -1, -1 } },
74 { { "1", "" }, { 1, 1 } },
75 { { "", "1" }, { -1, -1 } },
76 { { "10", "2" }, { 1, 1 } },
77 { { "2", "10" }, { -1, -1 } },
78 { { "2", "2" }, { 0, 0 } },
79 { { "10", "10" }, { 0, 0 } },
80 { { "01", "1" }, { 0, 0 } },
81 { { "01a", "1a" }, { 0, 0 } },
82 { { "01a", "1b" }, { -1, -1 } },
83 { { "01b", "1a" }, { 1, 1 } },
84 { { "10 ten", "2 two" }, { 1, 1 } },
85 { { "SYM1", "sym2" }, { -1, -1 } },
86 { { "sym2", "SYM1" }, { 1, 1 } },
87 { { "a10b20c30", "a10b20c31" }, { -1, -1 } },
88 { { "a10b20c31", "a10b20c30" }, { 1, 1 } },
89 { { "10UF", "10UF" }, { 0, 0 } },
90 { { "10uF", "10uF" }, { 0, 0 } },
91 { { "u10", "u10" }, { 0, 0 } },
92 { { "U10", "U10" }, { 0, 0 } },
93 { { "u10", "U10" }, { 1, 0 } },
94 { { "U10", "u10" }, { -1, 0 } },
95 { { "U10.1", "U10.10" }, { -1, -1 } },
96 { { "U10-1", "U10-10" }, { -1, -1 } },
97 { { "U10,1", "U10,10" }, { -1, -1 } },
98 { { "U10.A", "U10.a" }, { -1, 0 } },
99 { { "U10-A", "U10-a" }, { -1, 0 } },
100 { { "U10,A", "U10,a" }, { -1, 0 } },
101 };
102
103 for( const auto& c : cases )
104 {
105 BOOST_CHECK_MESSAGE( StrNumCmp( c.first.first, c.first.second ) == c.second.first,
106 c.first.first + " AND " + c.first.second + " failed for case sensitive" );
107
109 StrNumCmp( c.first.first, c.first.second, true ) == c.second.second,
110 c.first.first + " AND " + c.first.second + " failed for case insensitive" );
111 }
112}
113
114
115BOOST_AUTO_TEST_CASE( ValueCompare )
116{
117 using CASE = std::pair<std::pair<wxString, wxString>, int>;
118
119 const std::vector<CASE> cases = {
120 { { wxT( "100" ), wxT( "10" ) }, 1 },
121 { { wxT( "10K" ), wxT( "1K" ) }, 1 },
122 { { wxT( "10K" ), wxT( "1K5" ) }, 1 },
123 { { wxT( "10K" ), wxT( "10,000" ) }, 0 },
124 { { wxT( "1K5" ), wxT( "1.5K" ) }, 0 },
125 { { wxT( "1K5" ), wxT( "1,5K" ) }, 0 },
126 { { wxT( "K5" ), wxT( "1K" ) }, -1 },
127 { { wxT( "1K5" ), wxT( "K55" ) }, 1 },
128 { { wxT( "1R5" ), wxT( "1.5" ) }, 0 },
129 { { wxT( "1u5F" ), wxT( "1.5uF" ) }, 0 },
130 { { wxT( "1µ5" ), wxT( "1u5" ) }, 0 },
131 };
132
133 for( const auto& c : cases )
134 {
135 BOOST_CHECK_MESSAGE( ValueStringCompare( c.first.first, c.first.second ) == c.second,
136 c.first.first + " AND " + c.first.second + " failed" );
137 }
138}
139
140
145{
146 using CASE = std::pair<double, std::string>;
147
148 // conceptually a little quirky because doubles do have all those pesky additional values
149 const std::vector<CASE> cases = {
150 { 0, "0" },
151 { 1.000, "1" },
152 { 1.050, "1.05" }, // single trailing zero
153 { 0.00001523, "0.00001523" }, // value less than the magic 0.0001 threshold
154 { 0.00000000000000001523, "0" }, // really small decimal that gets cut off
155 { 623523, "623523" }, // large whole number
156 };
157
158 for( const auto& c : cases )
159 {
160 // Test both of these functions that work the same but the innards are different
161 BOOST_CHECK_EQUAL( FormatDouble2Str( c.first ), c.second );
162 BOOST_CHECK_EQUAL( UIDouble2Str( c.first ), c.second );
163 }
164}
165
166
171{
172 using CASE = std::pair<wxString, wxString>;
173
174 const std::vector<CASE> cases = {
175 { wxS( "I will display € €" ), wxS( "I will display &#8364; &#x20AC;" ) },
176 { wxS( "&lt;" ), wxS( "&amp;lt;" ) },
177 { wxS( "Don't Ω" ), wxS( "Don&apos;t Ω" ) },
178 };
179
180 for( const auto& c : cases )
181 {
182 wxString original( c.first );
183 wxString escaped = EscapeHTML( original );
184 wxString unescaped = UnescapeHTML( escaped );
185
186 wxString unescapedTest = UnescapeHTML( c.second );
187
188 BOOST_CHECK_EQUAL( original.utf8_string(), unescaped.utf8_string() );
189 BOOST_CHECK_EQUAL( original.utf8_string(), unescapedTest.utf8_string() );
190 }
191}
192
193
197BOOST_AUTO_TEST_CASE( VariantNameSort )
198{
199 std::vector<wxString> variantNames;
200
201 // Verify default variant name is always sorted to the beginning of the list.
202 variantNames.emplace_back( wxS( "Variant1" ) );
203 variantNames.emplace_back( GetDefaultVariantName() );
204 std::sort( variantNames.begin(), variantNames.end(), SortVariantNames );
205
206 BOOST_CHECK_EQUAL( variantNames[0], GetDefaultVariantName() );
207 BOOST_CHECK_EQUAL( variantNames[1], wxS( "Variant1" ) );
208}
209
210
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
wxString GetDefaultVariantName()
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
int SortVariantNames(const wxString &aLhs, const wxString &aRhs)
std::string UIDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
int ValueStringCompare(const wxString &strFWord, const wxString &strSWord)
Compare strings like the strcmp function but handle numbers and modifiers within the string text corr...
wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(TrailingInt)
Declare the test suite.
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_CHECK_EQUAL(result, "25.4")