KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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_BINARY_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_PROPS_UTILS::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 { "a100mil", 0 },
197};
198
199
203BOOST_AUTO_TEST_CASE( PropertiesReadKicadUnit )
204{
205 for( const auto& c : read_kicad_unit_property )
206 {
207 BOOST_TEST_CONTEXT( c.input + " -> " + wxString::Format( wxT( "%i" ), c.exp_result ) )
208 {
209 std::map<wxString, wxString> properties = { { "TEST", c.input } };
210
211 int result = ALTIUM_PROPS_UTILS::ReadKicadUnit( properties, "TEST", "0mil" );
212
213 // These are all valid
214 BOOST_CHECK_EQUAL( result, c.exp_result );
215 }
216 }
217}
218
220{
221 std::string input;
222 std::map<wxString, wxString> exp_result;
223};
224
228static const std::vector<READ_PROPERTIES_CASE> read_properties = {
229 // Empty
230 { "", {} },
231 { "\0", {} },
232 { "|", {} },
233 { "|\0", {} },
234 { "||", {} },
235 { "||\0", {} },
236 // Empty key-value pair
237 { "|=", { { "", "" } } },
238 { "|=\0", { { "", "" } } },
239 { "| = ", { { "", "" } } },
240 { "| = \0", { { "", "" } } },
241 // Single key-value pair
242 { "|A=\0", { { "A", "" } } },
243 { "|A=B", { { "A", "B" } } },
244 { "|A=B\0", { { "A", "B" } } },
245 { "|A=B|", { { "A", "B" } } },
246 { "|A=B|\0", { { "A", "B" } } },
247 // Multiple key-value pairs
248 { "|A=B|C=D|\0", { { "A", "B" }, { "C", "D" } } },
249 // Same key multiple times
250 { "|A=B|A=C\0", { { "A", "B" } } },
251 { "|A=B|A=C|A=D|A=E|A=F\0", { { "A", "B" } } },
252 // Always upper case key
253 { "|a=b\0", { { "A", "b" } } },
254 { "|abc123=b\0", { { "ABC123", "b" } } },
255 // Trim whitespaces, TODO: correct?
256 { "|A= B\0", { { "A", " B" } } },
257 { "|A=B \0", { { "A", "B" } } },
258 { "| A=B\0", { { "A", "B" } } },
259 { "|A =B\0", { { "A", "B" } } },
260 { "|A=\nB\n\0", { { "A", "\nB" } } },
261 // Escaping and other special cases, TODO: extend
262 //{ "|A=||\0", {{"A", "|"}} },
263 { "|A==\0", { { "A", "=" } } },
264 { "|A=a\na\0", { { "A", "a\na" } } },
265 { "|A=a\ta\0", { { "A", "a\ta" } } },
266 // Encoding, TODO: extend
267 { "|%UTF8%A=abc\0", { { "%UTF8%A", "abc" } } },
268 { "|%UTF8%A=\xc2\xa6\0", { { "%UTF8%A", { "\xc2\xa6", wxConvUTF8 } } } }, // Convert to '|' ?
269 // Correct reading errors
270 { "|A|B=C\0", { { "B", "C" } } },
271 { "|A=B|C\0", { { "A", "B" } } },
272};
273
277BOOST_AUTO_TEST_CASE( ReadProperties )
278{
279 for( const auto& c : read_properties )
280 {
281 BOOST_TEST_CONTEXT( wxString::Format( wxT( "'%s'" ), c.input ) )
282 {
283 size_t size = 4 + c.input.size();
284 std::unique_ptr<char[]> content = std::make_unique<char[]>( size );
285
286 *content.get() = c.input.size();
287 std::memcpy( content.get() + 4, c.input.c_str(), c.input.size() );
288
289 ALTIUM_BINARY_PARSER parser( content, size );
290
291 std::map<wxString, wxString> result = parser.ReadProperties();
292
293 BOOST_CHECK_EQUAL( parser.HasParsingError(), false );
294 BOOST_CHECK_EQUAL( parser.GetRemainingBytes(), 0 );
295
296 BOOST_CHECK_EQUAL( result.size(), c.exp_result.size() );
297 for( const auto& kv : c.exp_result )
298 {
299 BOOST_CHECK_EQUAL( 1, result.count( kv.first ) );
300 BOOST_CHECK_EQUAL( result.at( kv.first ), kv.second );
301 }
302 }
303 }
304}
305
306BOOST_AUTO_TEST_SUITE_END()
size_t GetRemainingBytes() const
std::map< wxString, wxString > ReadProperties(std::function< std::map< wxString, wxString >(const std::string &)> handleBinaryData=[](const std::string &) { return std::map< wxString, wxString >();})
static int32_t ReadKicadUnit(const std::map< wxString, wxString > &aProps, const wxString &aKey, const wxString &aDefault)
static int32_t ConvertToKicadUnit(const double aValue)
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)