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