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}", {} },
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}", {} },
106 { "='$'+'{A}'", "\\${A}", {} },
107 { "='A'+'B'", "AB", {} },
108 { "='A'+' '", "A ", {} },
109 { "=' '+'B'", " 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", "${A} ${B}", {} },
117 { "='A'+B+'C'+D", "A${B}C${D}", {} },
118 // Some special cases we do not know yet how to handle correctly. But we should not crash ;)
119 { "=+", "", {} },
120 { "=++", "", {} },
121 { "=+++", "", {} },
122 { "=B+", "${B}", {} },
123 { "=+B", "${B}", {} },
124 { "=B++", "${B}", {} },
125 { "=+B+", "${B}", {} },
126 { "=++B", "${B}", {} },
127 { " =", " =", {} },
128 { "= ", "", {} },
129 { "= A", "${A}", {} },
130 { "=A ", "${A}", {} },
131 { "='A'B", "A", {} },
132 { "=A'B'", "B", {} },
133 { "=A'B", "B", {} },
134 { "=A+ 'B'", "${A}B", {} },
135};
136
137
141BOOST_DATA_TEST_CASE( AltiumSchSpecialStringsToKiCadVariablesProperties,
142 boost::unit_test::data::make(sch_special_string_to_kicad_property),
143 data )
144{
145 wxString result = AltiumSchSpecialStringsToKiCadVariables( data.input, data.override );
146
147 // These are all valid
148 BOOST_CHECK_EQUAL( result, data.exp_result );
149}
150
151
155static const std::vector<SPECIAL_STRINGS_TO_KICAD> pcb_special_string_to_kicad_property = {
156 // Empty
157 { "", "", {} },
158 // No Special Strings
159 { "A", "A", {} },
160 { " A", " A", {} },
161 { "A ", "A ", {} },
162 { "A=B", "A=B", {} },
163 { "A=B+C", "A=B+C", {} },
164 { ".XXX = +/-.025", ".XXX = +/-.025", {} }, // a note that starts with a dot, not a special string
165 { "A\nB", "A\nB", {} },
166 { "A\tB", "A\tB", {} },
167 { "This is a long text with spaces", "This is a long text with spaces", {} },
168 // Text format (underscore,...), TODO: add
169 // Escaping, TODO: add
170 { "'", "'", {} },
171 { "'A'", "'A'", {} },
172 { "$", "$", {} },
173 { "{", "{", {} },
174 { "}", "}", {} },
175 { "${A}", "${A}", {} }, // TODO: correct substitution
176 // Simple special strings starting with dot
177 { ".A", "${A}", {} },
178 { ".A", "${C}", { { "A", "C" } } },
179 { ".A_B", "${A_B}", {} },
180 // Concatenated special strings
181 { "'.A'", "${A}", {} },
182 { "'.A''.B'", "${A}${B}", {} },
183 { "'.A''.B'", "${C}${B}", { { "A", "C" } } },
184 { "'.A''.B'", "${CC}${D}", { { "A", "CC" }, { "B", "D" } } },
185 { "A='.A', B='.B'", "A=${A}, B=${B}", {} },
186 // Case-insensitive special strings
187 { ".A", "${C}", { { "A", "C" } } },
188 { ".a", "${C}", { { "A", "C" } } },
189 { ".AB", "${C}", { { "AB", "C" } } },
190 { ".aB", "${C}", { { "AB", "C" } } },
191 { ".Ab", "${C}", { { "AB", "C" } } },
192 { ".ab", "${C}", { { "AB", "C" } } },
193 // Some special cases we do not know yet how to handle correctly. But we should not crash ;)
194 { "''", "''", {} },
195 { " .", " .", {} },
196 { ". ", "${ }", {} },
197 { ". A", "${ A}", {} },
198 { ".A ", "${A }", {} },
199 { " .A", " .A", {} },
200 { "...", "${..}", {} },
201};
202
203
207BOOST_DATA_TEST_CASE( AltiumPcbSpecialStringsToKiCadStringsProperties,
208 boost::unit_test::data::make(pcb_special_string_to_kicad_property),
209 data )
210{
211 wxString result = AltiumPcbSpecialStringsToKiCadStrings( data.input, data.override );
212
213 // These are all valid
214 BOOST_CHECK_EQUAL( result, data.exp_result );
215}
216
218{
219 wxString input;
220 wxString expected;
221};
222
223std::ostream& operator<<( std::ostream& strm, PIN_DESIGNATOR_CASE const& data )
224{
225 return strm << "[" << data.input << " -> " << data.expected << "]";
226}
227
228// Verifies GitLab issue 23709 handling: Altium encodes pins tied to multiple physical
229// pads as a comma-separated designator string; KiCad expects stacked-pin bracket notation.
230static const std::vector<PIN_DESIGNATOR_CASE> pin_designator_cases = {
231 // Single-designator inputs return after whitespace trim only
232 { wxT( "" ), wxT( "" ) },
233 { wxT( "1" ), wxT( "1" ) },
234 { wxT( "A1" ), wxT( "A1" ) },
235 { wxT( "GND" ), wxT( "GND" ) },
236 { wxT( " 1 " ), wxT( "1" ) },
237 { wxT( " A1 " ), wxT( "A1" ) },
238
239 // Basic multi-designator conversion
240 { wxT( "1,2" ), wxT( "[1,2]" ) },
241 { wxT( "1,2,3" ), wxT( "[1,2,3]" ) },
242 { wxT( "A1,A2,A3" ), wxT( "[A1,A2,A3]" ) },
243
244 // Whitespace around tokens is trimmed
245 { wxT( "1, 2, 3" ), wxT( "[1,2,3]" ) },
246 { wxT( " 1 , 2 , 3 " ), wxT( "[1,2,3]" ) },
247
248 // Stray empty tokens (trailing comma, duplicate comma) are dropped
249 { wxT( "1,2," ), wxT( "[1,2]" ) },
250 { wxT( "1,,2" ), wxT( "[1,2]" ) },
251
252 // Degenerate inputs collapse back to a single bare token or to the original
253 { wxT( "1," ), wxT( "1" ) },
254 { wxT( "," ), wxT( "," ) },
255};
256
257
258BOOST_DATA_TEST_CASE( AltiumPinDesignatorToKiCadProperties,
259 boost::unit_test::data::make( pin_designator_cases ),
260 data )
261{
262 BOOST_CHECK_EQUAL( AltiumPinDesignatorToKiCad( data.input ), data.expected );
263}
264
265
266// A value too large for an int is clamped, which turns a bogus dimension into a coordinate at
267// the edge of the board. Callers that place geometry need to know the difference
268BOOST_AUTO_TEST_CASE( ConvertToKicadUnitFlagsClampedValues )
269{
270 bool outOfRange = true;
271
272 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ConvertToKicadUnit( 10000., &outOfRange ), 25400 );
273 BOOST_CHECK( !outOfRange );
274
275 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ConvertToKicadUnit( -10000., &outOfRange ), -25400 );
276 BOOST_CHECK( !outOfRange );
277
278 // -111339.9634mil, the vertex TIDA-00204_revE3_PCB.PcbDoc carries in a copper polygon
279 const int32_t clamped = ALTIUM_PROPS_UTILS::ConvertToKicadUnit( -1113399634., &outOfRange );
280
281 BOOST_CHECK( outOfRange );
282 BOOST_CHECK_LT( clamped, -2000000000 );
283
285 -clamped );
286 BOOST_CHECK( outOfRange );
287
288 // The flag is optional and the clamp still happens without it
290}
291
292
293BOOST_AUTO_TEST_CASE( ReadKicadUnitFlagsClampedValues )
294{
295 const std::map<wxString, wxString> props = {
296 { wxT( "SANE" ), wxT( "1000mil" ) },
297 { wxT( "HUGE" ), wxT( "-111339.9634mil" ) },
298 { wxT( "JUNK" ), wxT( "not a number" ) },
299 };
300
301 bool outOfRange = true;
302
303 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "SANE" ), wxT( "0mil" ),
304 &outOfRange ),
305 25400000 );
306 BOOST_CHECK( !outOfRange );
307
308 ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "HUGE" ), wxT( "0mil" ), &outOfRange );
309 BOOST_CHECK( outOfRange );
310
311 // An unparseable or absent property yields zero, which is in range rather than clamped
312 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "JUNK" ), wxT( "0mil" ),
313 &outOfRange ),
314 0 );
315 BOOST_CHECK( !outOfRange );
316
317 BOOST_CHECK_EQUAL( ALTIUM_PROPS_UTILS::ReadKicadUnit( props, wxT( "ABSENT" ), wxT( "0mil" ),
318 &outOfRange ),
319 0 );
320 BOOST_CHECK( !outOfRange );
321}
322
wxString AltiumPinDesignatorToKiCad(const wxString &aDesignator)
Convert an Altium pin designator string to the equivalent KiCad pin number.
wxString AltiumPcbSpecialStringsToKiCadStrings(const wxString &aString, const std::map< wxString, wxString > &aOverrides, std::set< wxString > *aUnmapped)
Convert the special strings in Altium PCB text to KiCad variables.
wxString AltiumSchSpecialStringsToKiCadVariables(const wxString &aString, const std::map< wxString, wxString > &aOverrides, std::set< wxString > *aUnmapped)
Convert the special strings in Altium schematic text to KiCad variables.
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")