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
108 BOOST_CHECK_MESSAGE( StrNumCmp( c.first.first, c.first.second, CMP_T::IGNORE_CASE ) == c.second.second,
109 c.first.first + " AND " + c.first.second + " failed for case insensitive" );
110 }
111}
112
113
114BOOST_AUTO_TEST_CASE( ValueCompare )
115{
116 using CASE = std::pair<std::pair<wxString, wxString>, int>;
117
118 const std::vector<CASE> cases = {
119 { { wxT( "100" ), wxT( "10" ) }, 1 },
120 { { wxT( "10K" ), wxT( "1K" ) }, 1 },
121 { { wxT( "10K" ), wxT( "1K5" ) }, 1 },
122 { { wxT( "10K" ), wxT( "10,000" ) }, 0 },
123 { { wxT( "1K5" ), wxT( "1.5K" ) }, 0 },
124 { { wxT( "1K5" ), wxT( "1,5K" ) }, 0 },
125 { { wxT( "K5" ), wxT( "1K" ) }, -1 },
126 { { wxT( "1K5" ), wxT( "K55" ) }, 1 },
127 { { wxT( "1R5" ), wxT( "1.5" ) }, 0 },
128 // Capacitor values using an SI prefix as the decimal separator.
129 { { wxT( "1u5" ), wxT( "1.5u" ) }, 0 },
130 { { wxT( "1u5F" ), wxT( "1.5uF" ) }, 0 },
131 { { wxT( "2u2F" ), wxT( "2.2uF" ) }, 0 },
132 { { wxT( "1n5F" ), wxT( "1.5nF" ) }, 0 },
133 { { wxT( "12n1F" ), wxT( "12.1nF" ) }, 0 },
134 { { wxT( "5p6F" ), wxT( "5.6pF" ) }, 0 },
135 { { wxT( "1µ5" ), wxT( "1u5" ) }, 0 },
136 };
137
138 for( const auto& c : cases )
139 {
140 BOOST_CHECK_MESSAGE( ValueStringCompare( c.first.first, c.first.second ) == c.second,
141 c.first.first + " AND " + c.first.second + " failed" );
142 }
143}
144
145
146BOOST_AUTO_TEST_CASE( ValueCompareAlphanumericIdentifiers )
147{
148 // Textual identifiers must not be parsed as numbers just because they contain SI/RKM letters.
149 // They sort after pure numbers and use normal lexical ordering relative to other identifiers.
150 BOOST_CHECK_GT( ValueStringCompare( wxT( "T9G" ), wxT( "1" ) ), 0 );
151 BOOST_CHECK_GT( ValueStringCompare( wxT( "1182T117P" ), wxT( "9999" ) ), 0 );
152 BOOST_CHECK_GT( ValueStringCompare( wxT( "G4W" ), wxT( "F1" ) ), 0 );
153}
154
155
160{
161 using CASE = std::pair<double, std::string>;
162
163 // conceptually a little quirky because doubles do have all those pesky additional values
164 const std::vector<CASE> cases = {
165 { 0, "0" },
166 { 1.000, "1" },
167 { 1.050, "1.05" }, // single trailing zero
168 { 0.00001523, "0.00001523" }, // value less than the magic 0.0001 threshold
169 { 0.00000000000000001523, "0" }, // really small decimal that gets cut off
170 { 623523, "623523" }, // large whole number
171 };
172
173 for( const auto& c : cases )
174 {
175 // Test both of these functions that work the same but the innards are different
176 BOOST_CHECK_EQUAL( FormatDouble2Str( c.first ), c.second );
177 BOOST_CHECK_EQUAL( UIDouble2Str( c.first ), c.second );
178 }
179}
180
181
186{
187 using CASE = std::pair<wxString, wxString>;
188
189 const std::vector<CASE> cases = {
190 { wxS( "I will display € €" ), wxS( "I will display &#8364; &#x20AC;" ) },
191 { wxS( "&lt;" ), wxS( "&amp;lt;" ) },
192 { wxS( "Don't Ω" ), wxS( "Don&apos;t Ω" ) },
193 };
194
195 for( const auto& c : cases )
196 {
197 wxString original( c.first );
198 wxString escaped = EscapeHTML( original );
199 wxString unescaped = UnescapeHTML( escaped );
200
201 wxString unescapedTest = UnescapeHTML( c.second );
202
203 BOOST_CHECK_EQUAL( original.utf8_string(), unescaped.utf8_string() );
204 BOOST_CHECK_EQUAL( original.utf8_string(), unescapedTest.utf8_string() );
205 }
206}
207
208
212BOOST_AUTO_TEST_CASE( VariantNameSort )
213{
214 wxArrayString variantNames;
215
216 // Verify default variant name is always sorted to the beginning of the list.
217 variantNames.Add( wxS( "Variant1" ) );
218 variantNames.Add( GetDefaultVariantName() );
219 variantNames.Sort( SortVariantNames );
220
221 BOOST_CHECK_EQUAL( variantNames[0], GetDefaultVariantName() );
222 BOOST_CHECK_EQUAL( variantNames[1], wxS( "Variant1" ) );
223}
224
225
229BOOST_AUTO_TEST_CASE( FormatTrimmedDecimalKeepsIntegerDigits )
230{
231 // Precision 0 has no decimal point and must not lose integer zeros
232 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 100.0, 0 ), wxString( "100" ) );
233 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 1000.0, 0 ), wxString( "1000" ) );
234
235 // Byte-identical with the old exporters for precision >= 1
236 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 1.5, 6 ), wxString( "1.50" ) );
237 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 1.0, 6 ), wxString( "1.0" ) );
238 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 100.0, 4 ), wxString( "100.0" ) );
239 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 0.123456, 6 ), wxString( "0.123456" ) );
240 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( -0.0001, 3 ), wxString( "0.0" ) );
241 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( -0.3, 0 ), wxString( "0" ) );
242}
243
244
245// Verbatim old ODB and IPC-2581 trim loop, compared only at precision >= 1 away from negative zero
246static wxString oldTrimLoop( double aVal, int aPrecision )
247{
248 wxString str = wxString::FromCDouble( aVal, aPrecision );
249
250 while( str.EndsWith( wxT( "00" ) ) )
251 str.RemoveLast();
252
253 return str;
254}
255
256
257BOOST_AUTO_TEST_CASE( FormatTrimmedDecimalMatchesOldTrimLoop )
258{
259 for( int ii = -300; ii <= 300; ++ii )
260 {
261 double value = ii * 0.137;
262
263 for( int precision = 1; precision <= 8; ++precision )
264 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( value, precision ), oldTrimLoop( value, precision ) );
265 }
266
267 // oldTrimLoop() reproduces the precision-0 bug this formatter fixes, so precision 0 is
268 // checked against literal values here rather than against oldTrimLoop()
269 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 100.0, 0 ), wxString( "100" ) );
270 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 1000.0, 0 ), wxString( "1000" ) );
271 BOOST_CHECK_EQUAL( FormatTrimmedDecimal( 2500.0, 0 ), wxString( "2500" ) );
272}
273
274
int StrNumCmp(const wxString &aString1, const wxString &aString2, CMP_T aCaseSensitivity)
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)
wxString FormatTrimmedDecimal(double aValue, int aPrecision)
Format a value to aPrecision decimal digits and strip trailing zeros, keeping one digit after the dec...
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.
@ IGNORE_CASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(TrailingInt)
Declare the test suite.
static wxString oldTrimLoop(double aVal, int aPrecision)