KiCad PCB EDA Suite
test_altium_parser.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) 2021 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
32
34{
36};
37
38
42BOOST_FIXTURE_TEST_SUITE( AltiumParser, ALTIUM_PARSER_FIXTURE )
43
45{
46 int input;
48};
49
55static const std::vector<ALTIUM_TO_KICAD_UNIT_CASE> altium_to_kicad_unit = {
56 // Some simple values
57 { 0, 0 },
58 { 1, 0 },
59 { 2, 10 },
60 { 3, 10 },
61 { 10, 30 },
62 { 20, 50 },
63 { 30, 80 },
64 // Edge Cases
65 { 845466002, 2147483640 },
66 { -845466002, -2147483640 },
67 // Clamp bigger values
68 { 845466003, 2147483640 },
69 { -845466003, -2147483640 },
70 { 1000000000, 2147483640 },
71 { -1000000000, -2147483640 },
72 // imperial rounded units as input (rounded to the near 10 nm value)
73 { 100, 250 },
74 { 200, 510 },
75 { 300, 760 },
76 { 400, 1020 },
77 { 500, 1270 },
78 { 600, 1520 },
79 { 700, 1780 },
80 { 800, 2030 },
81 { 900, 2290 },
82 { 1000, 2540 },
83 // metric rounded units as input
84 { 394, 1000 },
85 { 787, 2000 },
86 { 1181, 3000 },
87 { 1575, 4000 },
88 { 1969, 5000 },
89 { 2362, 6000 },
90 { 2756, 7000 },
91 { 3150, 8000 },
92 { 3543, 9000 },
93 { 3937, 10000 },
94 { 39370, 100000 },
95 { 78740, 200000 },
96 { 118110, 300000 },
97 { 157480, 400000 },
98 { 196850, 500000 },
99 { 236220, 600000 },
100 { 275591, 700000 },
101 { 314961, 800000 },
102 { 354331, 900000 },
103 { 393701, 1000000 },
104 { -394, -1000 },
105 { -787, -2000 },
106 { -1181, -3000 },
107 { -1575, -4000 },
108 { -1969, -5000 },
109 { -2362, -6000 },
110 { -2756, -7000 },
111 { -3150, -8000 },
112 { -3543, -9000 },
113 { -3937, -10000 },
114 { -39370, -100000 },
115 { -78740, -200000 },
116 { -118110, -300000 },
117 { -157480, -400000 },
118 { -196850, -500000 },
119 { -236220, -600000 },
120 { -275591, -700000 },
121 { -314961, -800000 },
122 { -354331, -900000 },
123 { -393701, -1000000 },
124};
125
129BOOST_AUTO_TEST_CASE( ConvertToKicadUnit )
130{
131 for( const auto& c : altium_to_kicad_unit )
132 {
133 BOOST_TEST_CONTEXT( wxString::Format( wxT( "%i -> %i" ), c.input, c.exp_result ) )
134 {
135 int result = ALTIUM_PARSER::ConvertToKicadUnit( c.input );
136
137 // These are all valid
138 BOOST_CHECK_EQUAL( result, c.exp_result );
139 }
140 }
141}
142
144{
145 wxString input;
147};
148
152static const std::vector<READ_KICAD_UNIT_CASE> read_kicad_unit_property = {
153 // Empty (use default)
154 { "", 0 },
155 // Some simple cases
156 { "0mil", 0 },
157 { "1mil", 25400 },
158 { "+1mil", 25400 },
159 { "-1mil", -25400 },
160 // Decimal Places
161 { "0.1mil", 2540 },
162 { "-0.1mil", -2540 },
163 { "0.01mil", 250 },
164 { "-0.01mil", -250 },
165 { "0.001mil", 30 },
166 { "-0.001mil", -30 },
167 { "0.0001mil", 0 },
168 { "-0.0001mil", 0 },
169 { "0.00001mil", 0 },
170 { "-0.00001mil", -0 },
171 // Big Numbers
172 { "10mil", 254000 },
173 { "-10mil", -254000 },
174 { "100mil", 2540000 },
175 { "-100mil", -2540000 },
176 { "1000mil", 25400000 },
177 { "-1000mil", -25400000 },
178 { "10000mil", 254000000 },
179 { "-10000mil", -254000000 },
180 // Edge Cases, values are rounded to the near available 10nm
181 { "84546.6002mil", 2147483640 },
182 { "-84546.6002mil", -2147483640 },
183 // Clamp bigger values, values are rounded to the near available 10nm
184 { "84546.6003mil", 2147483640 },
185 { "-84546.6003mil", -2147483640 },
186 { "100000mil", 2147483640 },
187 { "-100000mil", -2147483640 },
188 { "1000000mil", 2147483640 },
189 { "-1000000mil", -2147483640 },
190 { "10000000mil", 2147483640 },
191 { "-10000000mil", -2147483640 },
192 // Incorrect suffix
193 { "100", 0 },
194 { "100mils", 0 },
195 // Incorrect prefix
196 { "+-100mil", 0 },
197 { "a100mil", 0 },
198};
199
200
204BOOST_AUTO_TEST_CASE( PropertiesReadKicadUnit )
205{
206 for( const auto& c : read_kicad_unit_property )
207 {
208 BOOST_TEST_CONTEXT( c.input + " -> " + wxString::Format( wxT( "%i" ), c.exp_result ) )
209 {
210 std::map<wxString, wxString> properties = { { "TEST", c.input } };
211
212 int result = ALTIUM_PARSER::ReadKicadUnit( properties, "TEST", "0mil" );
213
214 // These are all valid
215 BOOST_CHECK_EQUAL( result, c.exp_result );
216 }
217 }
218}
219
221{
222 std::string input;
223 std::map<wxString, wxString> exp_result;
224};
225
229static const std::vector<READ_PROPERTIES_CASE> read_properties = {
230 // Empty
231 { "", {} },
232 { "\0", {} },
233 { "|", {} },
234 { "|\0", {} },
235 { "||", {} },
236 { "||\0", {} },
237 // Empty key-value pair
238 { "|=", { { "", "" } } },
239 { "|=\0", { { "", "" } } },
240 { "| = ", { { "", "" } } },
241 { "| = \0", { { "", "" } } },
242 // Single key-value pair
243 { "|A=\0", { { "A", "" } } },
244 { "|A=B", { { "A", "B" } } },
245 { "|A=B\0", { { "A", "B" } } },
246 { "|A=B|", { { "A", "B" } } },
247 { "|A=B|\0", { { "A", "B" } } },
248 // Multiple key-value pairs
249 { "|A=B|C=D|\0", { { "A", "B" }, { "C", "D" } } },
250 // Same key multiple times
251 { "|A=B|A=C\0", { { "A", "B" } } },
252 { "|A=B|A=C|A=D|A=E|A=F\0", { { "A", "B" } } },
253 // Always upper case key
254 { "|a=b\0", { { "A", "b" } } },
255 { "|abc123=b\0", { { "ABC123", "b" } } },
256 // Trim whitespaces, TODO: correct?
257 { "|A= B\0", { { "A", " B" } } },
258 { "|A=B \0", { { "A", "B" } } },
259 { "| A=B\0", { { "A", "B" } } },
260 { "|A =B\0", { { "A", "B" } } },
261 { "|A=\nB\n\0", { { "A", "\nB" } } },
262 // Escaping and other special cases, TODO: extend
263 //{ "|A=||\0", {{"A", "|"}} },
264 { "|A==\0", { { "A", "=" } } },
265 { "|A=a\na\0", { { "A", "a\na" } } },
266 { "|A=a\ta\0", { { "A", "a\ta" } } },
267 // Encoding, TODO: extend
268 { "|%UTF8%A=abc\0", { { "%UTF8%A", "abc" } } },
269 { "|%UTF8%A=\xc2\xa6\0", { { "%UTF8%A", { "\xc2\xa6", wxConvUTF8 } } } }, // Convert to '|' ?
270 // Correct reading errors
271 { "|A|B=C\0", { { "B", "C" } } },
272 { "|A=B|C\0", { { "A", "B" } } },
273};
274
278BOOST_AUTO_TEST_CASE( ReadProperties )
279{
280 for( const auto& c : read_properties )
281 {
282 BOOST_TEST_CONTEXT( wxString::Format( wxT( "'%s'" ), c.input ) )
283 {
284 size_t size = 4 + c.input.size();
285 std::unique_ptr<char[]> content = std::make_unique<char[]>( size );
286
287 *content.get() = c.input.size();
288 std::memcpy( content.get() + 4, c.input.c_str(), c.input.size() );
289
290 ALTIUM_PARSER parser( content, size );
291
292 std::map<wxString, wxString> result = parser.ReadProperties();
293
294 BOOST_CHECK_EQUAL( parser.HasParsingError(), false );
295 BOOST_CHECK_EQUAL( parser.GetRemainingBytes(), 0 );
296
297 BOOST_CHECK_EQUAL( result.size(), c.exp_result.size() );
298 for( const auto& kv : c.exp_result )
299 {
300 BOOST_CHECK_EQUAL( 1, result.count( kv.first ) );
301 BOOST_CHECK_EQUAL( result.at( kv.first ), kv.second );
302 }
303 }
304 }
305}
306
307BOOST_AUTO_TEST_SUITE_END()
size_t GetRemainingBytes() const
bool HasParsingError()
std::map< wxString, wxString > ReadProperties()
static int32_t ConvertToKicadUnit(const double aValue)
int32_t ReadKicadUnit()
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Declares the struct as the Boost test fixture.
std::map< wxString, wxString > exp_result
static const std::vector< ALTIUM_TO_KICAD_UNIT_CASE > altium_to_kicad_unit
A list of valid internal unit conversation factors Rem: altium to kicad importer rounds coordinates t...
static const std::vector< READ_KICAD_UNIT_CASE > read_kicad_unit_property
A list of valid test strings and the expected results.
BOOST_AUTO_TEST_CASE(ConvertToKicadUnit)
Test conversation from Altium internal units into KiCad internal units.
static const std::vector< READ_PROPERTIES_CASE > read_properties
A list of valid test strings and the expected result map.
#define kv
#define BOOST_TEST_CONTEXT(A)