KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_altium_parser_utils.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
25
27#include <boost/test/data/test_case.hpp>
28
31
36
37
41BOOST_FIXTURE_TEST_SUITE( AltiumParserUtils, ALTIUM_PARSER_UTILS_FIXTURE )
42
43
45{
46 wxString input;
47 wxString exp_result;
48 std::map<wxString, wxString> override;
49};
50
51std::ostream & operator<<(std::ostream & strm, SPECIAL_STRINGS_TO_KICAD const & data) {
52 return strm << "[" << data.input << " -> " << data.exp_result << "]";
53}
54
58static const std::vector<SPECIAL_STRINGS_TO_KICAD> sch_special_string_to_kicad_property = {
59 // Empty
60 { "", "", {} },
61 // No Special Strings
62 { "A", "A", {} },
63 { " A", " A", {} },
64 { "A ", "A ", {} },
65 { "A=B", "A=B", {} },
66 { "A=B+C", "A=B+C", {} },
67 { "A\nB", "A\nB", {} },
68 { "A\tB", "A\tB", {} },
69 { "This is a long text with spaces", "This is a long text with spaces", {} },
70 // Text format (underscore,...), TODO: add
71 // Escaping, TODO: add
72 { "+", "+", {} },
73 { "'", "'", {} },
74 { "'A'", "'A'", {} },
75 { "A+B", "A+B", {} },
76 { "A=B", "A=B", {} },
77 { "$", "$", {} },
78 { "{", "{", {} },
79 { "}", "}", {} },
80 { "${A}", "${A}", {} }, // TODO: correct substitution
81 // Simple special strings
82 { "=A", "${A}", {} },
83 { "=A", "${C}", { { "A", "C" } } },
84 { "=A_B", "${A_B}", {} },
85 // Combined special strings
86 { "=A+B", "${A}${B}", {} },
87 { "=A+B", "${A}${B}", {} },
88 { "=A+B", "${C}${B}", { { "A", "C" } } },
89 { "=A+B", "${C}${D}", { { "A", "C" }, { "B", "D" } } },
90 // Case-insensitive special strings
91 { "=A", "${C}", { { "A", "C" } } },
92 { "=a", "${C}", { { "A", "C" } } },
93 { "=AB", "${C}", { { "AB", "C" } } },
94 { "=aB", "${C}", { { "AB", "C" } } },
95 { "=Ab", "${C}", { { "AB", "C" } } },
96 { "=ab", "${C}", { { "AB", "C" } } },
97 // Special strings with text
98 { "='A'", "A", {} },
99 { "='This is a long text with spaces'", "This is a long text with spaces", {} },
100 { "='='", "=", {} },
101 { "='+'", "+", {} },
102 { "='$'", "$", {} },
103 { "='{'", "{", {} },
104 { "='}'", "}", {} },
105 { "='${A}'", "${A}", {} }, // TODO: correct substitution
106 { "='A'+'B'", "AB", {} },
107 { "='A'+' '", "A ", {} },
108 { "=' '+'B'", " B", {} },
109 { "='A'+B", "A${B}", {} },
110 { "='A'+\"B\"", "A${B}", {} },
111 { "='A' + \"B\"", "A${B}", {} },
112 { "=\"A\"+'B'", "${A}B", {} },
113 { "=\"A\" + 'B'", "${A}B", {} },
114 { "=A+'B'", "${A}B", {} },
115 { "=A+' '+B", "${A} ${B}", {} },
116 { "='A'+B+'C'+D", "A${B}C${D}", {} },
117 // Some special cases we do not know yet how to handle correctly. But we should not crash ;)
118 { "=+", "", {} },
119 { "=++", "", {} },
120 { "=+++", "", {} },
121 { "=B+", "${B}", {} },
122 { "=+B", "${B}", {} },
123 { "=B++", "${B}", {} },
124 { "=+B+", "${B}", {} },
125 { "=++B", "${B}", {} },
126 { " =", " =", {} },
127 { "= ", "", {} },
128 { "= A", "${A}", {} },
129 { "=A ", "${A}", {} },
130 { "='A'B", "A", {} },
131 { "=A'B'", "B", {} },
132 { "=A'B", "B", {} },
133 { "=A+ 'B'", "${A}B", {} },
134};
135
136
140BOOST_DATA_TEST_CASE( AltiumSchSpecialStringsToKiCadVariablesProperties,
141 boost::unit_test::data::make(sch_special_string_to_kicad_property),
142 data )
143{
144 wxString result = AltiumSchSpecialStringsToKiCadVariables( data.input, data.override );
145
146 // These are all valid
147 BOOST_CHECK_EQUAL( result, data.exp_result );
148}
149
150
154static const std::vector<SPECIAL_STRINGS_TO_KICAD> pcb_special_string_to_kicad_property = {
155 // Empty
156 { "", "", {} },
157 // No Special Strings
158 { "A", "A", {} },
159 { " A", " A", {} },
160 { "A ", "A ", {} },
161 { "A=B", "A=B", {} },
162 { "A=B+C", "A=B+C", {} },
163 { "A\nB", "A\nB", {} },
164 { "A\tB", "A\tB", {} },
165 { "This is a long text with spaces", "This is a long text with spaces", {} },
166 // Text format (underscore,...), TODO: add
167 // Escaping, TODO: add
168 { "'", "'", {} },
169 { "'A'", "'A'", {} },
170 { "$", "$", {} },
171 { "{", "{", {} },
172 { "}", "}", {} },
173 { "${A}", "${A}", {} }, // TODO: correct substitution
174 // Simple special strings starting with dot
175 { ".A", "${A}", {} },
176 { ".A", "${C}", { { "A", "C" } } },
177 { ".A_B", "${A_B}", {} },
178 // Concatenated special strings
179 { "'.A'", "${A}", {} },
180 { "'.A''.B'", "${A}${B}", {} },
181 { "'.A''.B'", "${C}${B}", { { "A", "C" } } },
182 { "'.A''.B'", "${CC}${D}", { { "A", "CC" }, { "B", "D" } } },
183 { "A='.A', B='.B'", "A=${A}, B=${B}", {} },
184 // Case-insensitive special strings
185 { ".A", "${C}", { { "A", "C" } } },
186 { ".a", "${C}", { { "A", "C" } } },
187 { ".AB", "${C}", { { "AB", "C" } } },
188 { ".aB", "${C}", { { "AB", "C" } } },
189 { ".Ab", "${C}", { { "AB", "C" } } },
190 { ".ab", "${C}", { { "AB", "C" } } },
191 // Some special cases we do not know yet how to handle correctly. But we should not crash ;)
192 { "''", "''", {} },
193 { " .", " .", {} },
194 { ". ", "${ }", {} },
195 { ". A", "${ A}", {} },
196 { ".A ", "${A }", {} },
197 { " .A", " .A", {} },
198 { "...", "${..}", {} },
199};
200
201
205BOOST_DATA_TEST_CASE( AltiumPcbSpecialStringsToKiCadStringsProperties,
206 boost::unit_test::data::make(pcb_special_string_to_kicad_property),
207 data )
208{
209 wxString result = AltiumPcbSpecialStringsToKiCadStrings( data.input, data.override );
210
211 // These are all valid
212 BOOST_CHECK_EQUAL( result, data.exp_result );
213}
214
216{
217 wxString input;
218 wxString expected;
219};
220
221std::ostream& operator<<( std::ostream& strm, PIN_DESIGNATOR_CASE const& data )
222{
223 return strm << "[" << data.input << " -> " << data.expected << "]";
224}
225
226// Verifies GitLab issue 23709 handling: Altium encodes pins tied to multiple physical
227// pads as a comma-separated designator string; KiCad expects stacked-pin bracket notation.
228static const std::vector<PIN_DESIGNATOR_CASE> pin_designator_cases = {
229 // Single-designator inputs return after whitespace trim only
230 { wxT( "" ), wxT( "" ) },
231 { wxT( "1" ), wxT( "1" ) },
232 { wxT( "A1" ), wxT( "A1" ) },
233 { wxT( "GND" ), wxT( "GND" ) },
234 { wxT( " 1 " ), wxT( "1" ) },
235 { wxT( " A1 " ), wxT( "A1" ) },
236
237 // Basic multi-designator conversion
238 { wxT( "1,2" ), wxT( "[1,2]" ) },
239 { wxT( "1,2,3" ), wxT( "[1,2,3]" ) },
240 { wxT( "A1,A2,A3" ), wxT( "[A1,A2,A3]" ) },
241
242 // Whitespace around tokens is trimmed
243 { wxT( "1, 2, 3" ), wxT( "[1,2,3]" ) },
244 { wxT( " 1 , 2 , 3 " ), wxT( "[1,2,3]" ) },
245
246 // Stray empty tokens (trailing comma, duplicate comma) are dropped
247 { wxT( "1,2," ), wxT( "[1,2]" ) },
248 { wxT( "1,,2" ), wxT( "[1,2]" ) },
249
250 // Degenerate inputs collapse back to a single bare token or to the original
251 { wxT( "1," ), wxT( "1" ) },
252 { wxT( "," ), wxT( "," ) },
253};
254
255
256BOOST_DATA_TEST_CASE( AltiumPinDesignatorToKiCadProperties,
257 boost::unit_test::data::make( pin_designator_cases ),
258 data )
259{
260 BOOST_CHECK_EQUAL( AltiumPinDesignatorToKiCad( data.input ), data.expected );
261}
262
263
264// A value too large for an int is clamped, which turns a bogus dimension into a coordinate at
265// the edge of the board. Callers that place geometry need to know the difference
266BOOST_AUTO_TEST_CASE( ConvertToKicadUnitFlagsClampedValues )
267{
268 bool outOfRange = true;
269
270 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ConvertToKicadUnit( 10000., &outOfRange ), 25400 );
271 BOOST_CHECK( !outOfRange );
272
273 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ConvertToKicadUnit( -10000., &outOfRange ), -25400 );
274 BOOST_CHECK( !outOfRange );
275
276 // -111339.9634mil, the vertex TIDA-00204_revE3_PCB.PcbDoc carries in a copper polygon
277 const int32_t clamped = ALTIUM_PROPS_UTILS::ConvertToKicadUnit( -1113399634., &outOfRange );
278
279 BOOST_CHECK( outOfRange );
280 BOOST_CHECK_LT( clamped, -2000000000 );
281
283 -clamped );
284 BOOST_CHECK( outOfRange );
285
286 // The flag is optional and the clamp still happens without it
288}
289
290
291BOOST_AUTO_TEST_CASE( ReadKicadUnitFlagsClampedValues )
292{
293 const std::map<wxString, wxString> props = {
294 { wxT( "SANE" ), wxT( "1000mil" ) },
295 { wxT( "HUGE" ), wxT( "-111339.9634mil" ) },
296 { wxT( "JUNK" ), wxT( "not a number" ) },
297 };
298
299 bool outOfRange = true;
300
301 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "SANE" ), wxT( "0mil" ),
302 &outOfRange ),
303 25400000 );
304 BOOST_CHECK( !outOfRange );
305
306 ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "HUGE" ), wxT( "0mil" ), &outOfRange );
307 BOOST_CHECK( outOfRange );
308
309 // An unparseable or absent property yields zero, which is in range rather than clamped
310 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "JUNK" ), wxT( "0mil" ),
311 &outOfRange ),
312 0 );
313 BOOST_CHECK( !outOfRange );
314
315 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "ABSENT" ), wxT( "0mil" ),
316 &outOfRange ),
317 0 );
318 BOOST_CHECK( !outOfRange );
319}
320
wxString AltiumPinDesignatorToKiCad(const wxString &aDesignator)
Convert an Altium pin designator string to the equivalent KiCad pin number.
wxString AltiumSchSpecialStringsToKiCadVariables(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
wxString AltiumPcbSpecialStringsToKiCadStrings(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
static int32_t ConvertToKicadUnit(const double aValue, bool *aOutOfRange=nullptr)
Convert a value in Altium's internal unit (0.1 uinch) to KiCad IU, clamped to the representable range...
static int32_t ReadKicadUnit(const std::map< wxString, wxString > &aProps, const wxString &aKey, const wxString &aDefault, bool *aOutOfRange=nullptr)
Declares the struct as the Boost test fixture.
static const std::vector< PIN_DESIGNATOR_CASE > pin_designator_cases
BOOST_AUTO_TEST_CASE(ConvertToKicadUnitFlagsClampedValues)
BOOST_DATA_TEST_CASE(AltiumSchSpecialStringsToKiCadVariablesProperties, boost::unit_test::data::make(sch_special_string_to_kicad_property), data)
Test conversation from Altium Schematic Special String to a KiCad String with variables.
static const std::vector< SPECIAL_STRINGS_TO_KICAD > sch_special_string_to_kicad_property
A list of valid test strings and the expected results.
std::ostream & operator<<(std::ostream &strm, SPECIAL_STRINGS_TO_KICAD const &data)
static const std::vector< SPECIAL_STRINGS_TO_KICAD > pcb_special_string_to_kicad_property
A list of valid test strings and the expected results.
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")