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 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
29
34
35
39BOOST_FIXTURE_TEST_SUITE( AltiumParser, ALTIUM_BINARY_PARSER_FIXTURE )
40
41
46static const std::vector<std::tuple<int, int>> altium_to_kicad_unit = {
47 // Some simple values
48 { 0, 0 },
49 { 1, 0 },
50 { 2, 10 },
51 { 3, 10 },
52 { 10, 30 },
53 { 20, 50 },
54 { 30, 80 },
55 // Edge Cases
56 { 845466002, 2147483640 },
57 { -845466002, -2147483640 },
58 // Clamp bigger values
59 { 845466003, 2147483640 },
60 { -845466003, -2147483640 },
61 { 1000000000, 2147483640 },
62 { -1000000000, -2147483640 },
63 // imperial rounded units as input (rounded to the near 10 nm value)
64 { 100, 250 },
65 { 200, 510 },
66 { 300, 760 },
67 { 400, 1020 },
68 { 500, 1270 },
69 { 600, 1520 },
70 { 700, 1780 },
71 { 800, 2030 },
72 { 900, 2290 },
73 { 1000, 2540 },
74 // metric rounded units as input
75 { 394, 1000 },
76 { 787, 2000 },
77 { 1181, 3000 },
78 { 1575, 4000 },
79 { 1969, 5000 },
80 { 2362, 6000 },
81 { 2756, 7000 },
82 { 3150, 8000 },
83 { 3543, 9000 },
84 { 3937, 10000 },
85 { 39370, 100000 },
86 { 78740, 200000 },
87 { 118110, 300000 },
88 { 157480, 400000 },
89 { 196850, 500000 },
90 { 236220, 600000 },
91 { 275591, 700000 },
92 { 314961, 800000 },
93 { 354331, 900000 },
94 { 393701, 1000000 },
95 { -394, -1000 },
96 { -787, -2000 },
97 { -1181, -3000 },
98 { -1575, -4000 },
99 { -1969, -5000 },
100 { -2362, -6000 },
101 { -2756, -7000 },
102 { -3150, -8000 },
103 { -3543, -9000 },
104 { -3937, -10000 },
105 { -39370, -100000 },
106 { -78740, -200000 },
107 { -118110, -300000 },
108 { -157480, -400000 },
109 { -196850, -500000 },
110 { -236220, -600000 },
111 { -275591, -700000 },
112 { -314961, -800000 },
113 { -354331, -900000 },
114 { -393701, -1000000 },
115};
116
120BOOST_DATA_TEST_CASE( ConvertToKicadUnit,
121 boost::unit_test::data::make(altium_to_kicad_unit),
122 input_value,
123 expected_result )
124{
126
127 // These are all valid
128 BOOST_CHECK_EQUAL( result, expected_result );
129}
130
134static const std::vector<std::tuple<wxString, int>> read_kicad_unit_property = {
135 // Empty (use default)
136 { "", 0 },
137 // Some simple cases
138 { "0mil", 0 },
139 { "1mil", 25400 },
140 { "+1mil", 25400 },
141 { "-1mil", -25400 },
142 // Decimal Places
143 { "0.1mil", 2540 },
144 { "-0.1mil", -2540 },
145 { "0.01mil", 250 },
146 { "-0.01mil", -250 },
147 { "0.001mil", 30 },
148 { "-0.001mil", -30 },
149 { "0.0001mil", 0 },
150 { "-0.0001mil", 0 },
151 { "0.00001mil", 0 },
152 { "-0.00001mil", -0 },
153 // Big Numbers
154 { "10mil", 254000 },
155 { "-10mil", -254000 },
156 { "100mil", 2540000 },
157 { "-100mil", -2540000 },
158 { "1000mil", 25400000 },
159 { "-1000mil", -25400000 },
160 { "10000mil", 254000000 },
161 { "-10000mil", -254000000 },
162 // Edge Cases, values are rounded to the near available 10nm
163 { "84546.6002mil", 2147483640 },
164 { "-84546.6002mil", -2147483640 },
165 // Clamp bigger values, values are rounded to the near available 10nm
166 { "84546.6003mil", 2147483640 },
167 { "-84546.6003mil", -2147483640 },
168 { "100000mil", 2147483640 },
169 { "-100000mil", -2147483640 },
170 { "1000000mil", 2147483640 },
171 { "-1000000mil", -2147483640 },
172 { "10000000mil", 2147483640 },
173 { "-10000000mil", -2147483640 },
174 // Incorrect suffix
175 { "100", 0 },
176 { "100mils", 0 },
177 // Incorrect prefix
178 { "a100mil", 0 },
179};
180
181
185BOOST_DATA_TEST_CASE( PropertiesReadKicadUnit,
186 boost::unit_test::data::make(read_kicad_unit_property),
187 input_value,
188 expected_result )
189{
190 std::map<wxString, wxString> properties = { { "TEST", input_value } };
191
192 int result = ALTIUM_PROPS_UTILS::ReadKicadUnit( properties, "TEST", "0mil" );
193
194 // These are all valid
195 BOOST_CHECK_EQUAL( result, expected_result );
196}
197
198
202static const std::vector<std::tuple<std::string, std::map<wxString, wxString>>> read_properties = {
203 // Empty
204 { "", {} },
205 { "\0", {} },
206 { "|", {} },
207 { "|\0", {} },
208 { "||", {} },
209 { "||\0", {} },
210 // Empty key-value pair
211 { "|=", { { "", "" } } },
212 { "|=\0", { { "", "" } } },
213 { "| = ", { { "", "" } } },
214 { "| = \0", { { "", "" } } },
215 // Single key-value pair
216 { "|A=\0", { { "A", "" } } },
217 { "|A=B", { { "A", "B" } } },
218 { "|A=B\0", { { "A", "B" } } },
219 { "|A=B|", { { "A", "B" } } },
220 { "|A=B|\0", { { "A", "B" } } },
221 { "A=\0", { { "A", "" } } },
222 { "A=B", { { "A", "B" } } },
223 { "A=B\0", { { "A", "B" } } },
224 { "A=B|", { { "A", "B" } } },
225 { "A=B|\0", { { "A", "B" } } },
226 // Multiple key-value pairs
227 { "|A=B|C=D|\0", { { "A", "B" }, { "C", "D" } } },
228 { "A=B|C=D|\0", { { "A", "B" }, { "C", "D" } } },
229 // Same key multiple times
230 { "|A=B|A=C\0", { { "A", "B" } } },
231 { "|A=B|A=C|A=D|A=E|A=F\0", { { "A", "B" } } },
232 // Always upper case key
233 { "|a=b\0", { { "A", "b" } } },
234 { "|abc123=b\0", { { "ABC123", "b" } } },
235 // Trim whitespaces, TODO: correct?
236 { "|A= B\0", { { "A", " B" } } },
237 { "|A=B \0", { { "A", "B" } } },
238 { "| A=B\0", { { "A", "B" } } },
239 { "|A =B\0", { { "A", "B" } } },
240 { "|A=\nB\n\0", { { "A", "\nB" } } },
241 { "A= B\0", { { "A", " B" } } },
242 { "A=B \0", { { "A", "B" } } },
243 { " A=B\0", { { "A", "B" } } },
244 { "A =B\0", { { "A", "B" } } },
245 { "A=\nB\n\0", { { "A", "\nB" } } },
246 // Escaping and other special cases, TODO: extend
247 //{ "|A=||\0", {{"A", "|"}} },
248 { "|A==\0", { { "A", "=" } } },
249 { "|A=a\na\0", { { "A", "a\na" } } },
250 { "|A=a\ta\0", { { "A", "a\ta" } } },
251 // Encoding, TODO: extend
252 { "|%UTF8%A=abc\0", { { "%UTF8%A", "abc" } } },
253 { "|%UTF8%A=\xc2\xa6\0", { { "%UTF8%A", { "\xc2\xa6", wxConvUTF8 } } } }, // Convert to '|' ?
254 // Correct reading errors
255 { "|A|B=C\0", { { "B", "C" } } },
256 { "|A=B|C\0", { { "A", "B" } } },
257};
258
262BOOST_DATA_TEST_CASE( ReadProperties,
263 boost::unit_test::data::make(read_properties),
264 input_value,
265 expected_result )
266{
267 size_t size = 4 + input_value.size();
268 std::unique_ptr<char[]> content = std::make_unique<char[]>( size );
269
270 *content.get() = input_value.size();
271 std::memcpy( content.get() + 4, input_value.c_str(), input_value.size() );
272
273 ALTIUM_BINARY_PARSER parser( content, size );
274
275 std::map<wxString, wxString> result = parser.ReadProperties();
276
277 BOOST_CHECK_EQUAL( parser.HasParsingError(), false );
279
280 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(),
281 expected_result.begin(), expected_result.end() );
282}
283
284
291BOOST_AUTO_TEST_CASE( ReadPropertiesBinaryNullBytePreserved )
292{
293 // Build a binary record whose payload ends with 0x00.
294 // The MSB of the 4-byte length field signals "binary" to ReadProperties.
295 const std::string payload = { '\x01', '\x02', '\x03', '\x00' };
296 uint32_t rawLength = static_cast<uint32_t>( payload.size() ) | 0x01000000;
297
298 size_t totalSize = 4 + payload.size();
299 std::unique_ptr<char[]> content = std::make_unique<char[]>( totalSize );
300
301 std::memcpy( content.get(), &rawLength, 4 );
302 std::memcpy( content.get() + 4, payload.data(), payload.size() );
303
304 // Capture the raw string that handleBinaryData receives so we can check its length.
305 std::string captured;
306
307 auto captureBinaryData = [&]( const std::string& aData ) -> std::map<wxString, wxString>
308 {
309 captured = aData;
310 return {};
311 };
312
313 ALTIUM_BINARY_PARSER parser( content, totalSize );
314
315 parser.ReadProperties( captureBinaryData );
316
317 BOOST_CHECK_EQUAL( parser.HasParsingError(), false );
318 BOOST_CHECK_EQUAL( captured.size(), payload.size() );
319 BOOST_CHECK_EQUAL( captured, payload );
320}
321
322
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)
STL namespace.
BOOST_DATA_TEST_CASE(ConvertToKicadUnit, boost::unit_test::data::make(altium_to_kicad_unit), input_value, expected_result)
Test conversation from Altium internal units into KiCad internal units.
static const std::vector< std::tuple< int, int > > altium_to_kicad_unit
Declares the struct as the Boost test fixture.
BOOST_AUTO_TEST_CASE(ReadPropertiesBinaryNullBytePreserved)
Verify that ReadProperties does not strip a trailing 0x00 byte from binary records.
static const std::vector< std::tuple< wxString, int > > read_kicad_unit_property
A list of valid test strings and the expected results.
static const std::vector< std::tuple< std::string, std::map< wxString, wxString > > > read_properties
A list of valid test strings and the expected result map.
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")