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 (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
37BOOST_AUTO_TEST_SUITE( KicadString )
38
39
40
44{
45 using CASE = std::pair<std::string, int>;
46
47 const std::vector<CASE> cases = {
48 { "", 0 }, { "foo", 0 }, // no int
49 { "0", 0 }, // only int
50 { "42", 42 }, // only int
51 { "1001", 1001 }, // only int
52 { "Foo42", 42 }, { "12Foo42", 42 }, // only the trailing
53 { "12Foo4.2", 2 }, // no dots
54 };
55
56 for( const auto& c : cases )
57 {
58 BOOST_CHECK_EQUAL( GetTrailingInt( c.first ), c.second );
59 }
60}
61
62
66BOOST_AUTO_TEST_CASE( NaturalNumberCompare )
67{
68 using CASE = std::pair<std::pair<std::string, std::string>, std::pair<int, int>>;
69
70 const std::vector<CASE> cases = {
71 { { "a", "b" }, { -1, -1 } },
72 { { "b", "a" }, { 1, 1 } },
73 { { "a", "a" }, { 0, 0 } },
74 { { "a", "A" }, { 1, 0 } },
75 { { "A", "a" }, { -1, 0 } },
76 { { "a", "" }, { 1, 1 } },
77 { { "", "a" }, { -1, -1 } },
78 { { "1", "" }, { 1, 1 } },
79 { { "", "1" }, { -1, -1 } },
80 { { "10", "2" }, { 1, 1 } },
81 { { "2", "10" }, { -1, -1 } },
82 { { "2", "2" }, { 0, 0 } },
83 { { "10", "10" }, { 0, 0 } },
84 { { "01", "1" }, { 0, 0 } },
85 { { "01a", "1a" }, { 0, 0 } },
86 { { "01a", "1b" }, { -1, -1 } },
87 { { "01b", "1a" }, { 1, 1 } },
88 { { "10 ten", "2 two" }, { 1, 1 } },
89 { { "SYM1", "sym2" }, { -1, -1 } },
90 { { "sym2", "SYM1" }, { 1, 1 } },
91 { { "a10b20c30", "a10b20c31" }, { -1, -1 } },
92 { { "a10b20c31", "a10b20c30" }, { 1, 1 } },
93 { { "10UF", "10UF" }, { 0, 0 } },
94 { { "10uF", "10uF" }, { 0, 0 } },
95 { { "u10", "u10" }, { 0, 0 } },
96 { { "U10", "U10" }, { 0, 0 } },
97 { { "u10", "U10" }, { 1, 0 } },
98 { { "U10", "u10" }, { -1, 0 } },
99 { { "U10.1", "U10.10" }, { -1, -1 } },
100 { { "U10-1", "U10-10" }, { -1, -1 } },
101 { { "U10,1", "U10,10" }, { -1, -1 } },
102 { { "U10.A", "U10.a" }, { -1, 0 } },
103 { { "U10-A", "U10-a" }, { -1, 0 } },
104 { { "U10,A", "U10,a" }, { -1, 0 } },
105 };
106
107 for( const auto& c : cases )
108 {
109 BOOST_CHECK_MESSAGE( StrNumCmp( c.first.first, c.first.second ) == c.second.first,
110 c.first.first + " AND " + c.first.second + " failed for case sensitive" );
111
112 BOOST_CHECK_MESSAGE(
113 StrNumCmp( c.first.first, c.first.second, true ) == c.second.second,
114 c.first.first + " AND " + c.first.second + " failed for case insensitive" );
115 }
116}
117
118
123{
124 using CASE = std::pair<double, std::string>;
125
126 // conceptually a little quirky because doubles do have all those pesky additional values
127 const std::vector<CASE> cases = {
128 { 0, "0" },
129 { 1.000, "1" },
130 { 1.050, "1.05" }, // single trailing zero
131 { 0.00001523, "0.00001523" }, // value less than the magic 0.0001 threshold
132 { 0.00000000000000001523, "0" }, // really small decimal that gets cut off
133 { 623523, "623523" }, // large whole number
134 };
135
136 for( const auto& c : cases )
137 {
138 // Test both of these functions that work the same but the innards are different
139 BOOST_CHECK_EQUAL( FormatDouble2Str( c.first ), c.second );
140 BOOST_CHECK_EQUAL( UIDouble2Str( c.first ), c.second );
141 }
142}
143
144
149{
150 using CASE = std::pair<wxString, wxString>;
151
152 const std::vector<CASE> cases = {
153 { wxS( "I will display € €" ), wxS( "I will display &#8364; &#x20AC;" ) },
154 { wxS( "&lt;" ), wxS( "&amp;lt;" ) },
155 { wxS( "Don't Ω" ), wxS( "Don&apos;t Ω" ) },
156 };
157
158 for( const auto& c : cases )
159 {
160 wxString original( c.first );
161 wxString escaped = EscapeHTML( original );
162 wxString unescaped = UnescapeHTML( escaped );
163
164 wxString unescapedTest = UnescapeHTML( c.second );
165
166 BOOST_CHECK_EQUAL( original.utf8_string(), unescaped.utf8_string() );
167 BOOST_CHECK_EQUAL( original.utf8_string(), unescapedTest.utf8_string() );
168 }
169}
170
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.
int GetTrailingInt(const wxString &aStr)
Gets the trailing int, if any, from a string.
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...
wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(TrailingInt)
Declare the test suite.