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#include <boost/test/data/test_case.hpp>
31
33
35{
37};
38
39
43BOOST_FIXTURE_TEST_SUITE( AltiumParser, ALTIUM_BINARY_PARSER_FIXTURE )
44
45
50static const std::vector<std::tuple<int, int>> altium_to_kicad_unit = {
51 // Some simple values
52 { 0, 0 },
53 { 1, 0 },
54 { 2, 10 },
55 { 3, 10 },
56 { 10, 30 },
57 { 20, 50 },
58 { 30, 80 },
59 // Edge Cases
60 { 845466002, 2147483640 },
61 { -845466002, -2147483640 },
62 // Clamp bigger values
63 { 845466003, 2147483640 },
64 { -845466003, -2147483640 },
65 { 1000000000, 2147483640 },
66 { -1000000000, -2147483640 },
67 // imperial rounded units as input (rounded to the near 10 nm value)
68 { 100, 250 },
69 { 200, 510 },
70 { 300, 760 },
71 { 400, 1020 },
72 { 500, 1270 },
73 { 600, 1520 },
74 { 700, 1780 },
75 { 800, 2030 },
76 { 900, 2290 },
77 { 1000, 2540 },
78 // metric rounded units as input
79 { 394, 1000 },
80 { 787, 2000 },
81 { 1181, 3000 },
82 { 1575, 4000 },
83 { 1969, 5000 },
84 { 2362, 6000 },
85 { 2756, 7000 },
86 { 3150, 8000 },
87 { 3543, 9000 },
88 { 3937, 10000 },
89 { 39370, 100000 },
90 { 78740, 200000 },
91 { 118110, 300000 },
92 { 157480, 400000 },
93 { 196850, 500000 },
94 { 236220, 600000 },
95 { 275591, 700000 },
96 { 314961, 800000 },
97 { 354331, 900000 },
98 { 393701, 1000000 },
99 { -394, -1000 },
100 { -787, -2000 },
101 { -1181, -3000 },
102 { -1575, -4000 },
103 { -1969, -5000 },
104 { -2362, -6000 },
105 { -2756, -7000 },
106 { -3150, -8000 },
107 { -3543, -9000 },
108 { -3937, -10000 },
109 { -39370, -100000 },
110 { -78740, -200000 },
111 { -118110, -300000 },
112 { -157480, -400000 },
113 { -196850, -500000 },
114 { -236220, -600000 },
115 { -275591, -700000 },
116 { -314961, -800000 },
117 { -354331, -900000 },
118 { -393701, -1000000 },
119};
120
124BOOST_DATA_TEST_CASE( ConvertToKicadUnit,
125 boost::unit_test::data::make(altium_to_kicad_unit),
126 input_value,
127 expected_result )
128{
129 int result = ALTIUM_PROPS_UTILS::ConvertToKicadUnit( input_value );
130
131 // These are all valid
132 BOOST_CHECK_EQUAL( result, expected_result );
133}
134
138static const std::vector<std::tuple<wxString, int>> read_kicad_unit_property = {
139 // Empty (use default)
140 { "", 0 },
141 // Some simple cases
142 { "0mil", 0 },
143 { "1mil", 25400 },
144 { "+1mil", 25400 },
145 { "-1mil", -25400 },
146 // Decimal Places
147 { "0.1mil", 2540 },
148 { "-0.1mil", -2540 },
149 { "0.01mil", 250 },
150 { "-0.01mil", -250 },
151 { "0.001mil", 30 },
152 { "-0.001mil", -30 },
153 { "0.0001mil", 0 },
154 { "-0.0001mil", 0 },
155 { "0.00001mil", 0 },
156 { "-0.00001mil", -0 },
157 // Big Numbers
158 { "10mil", 254000 },
159 { "-10mil", -254000 },
160 { "100mil", 2540000 },
161 { "-100mil", -2540000 },
162 { "1000mil", 25400000 },
163 { "-1000mil", -25400000 },
164 { "10000mil", 254000000 },
165 { "-10000mil", -254000000 },
166 // Edge Cases, values are rounded to the near available 10nm
167 { "84546.6002mil", 2147483640 },
168 { "-84546.6002mil", -2147483640 },
169 // Clamp bigger values, values are rounded to the near available 10nm
170 { "84546.6003mil", 2147483640 },
171 { "-84546.6003mil", -2147483640 },
172 { "100000mil", 2147483640 },
173 { "-100000mil", -2147483640 },
174 { "1000000mil", 2147483640 },
175 { "-1000000mil", -2147483640 },
176 { "10000000mil", 2147483640 },
177 { "-10000000mil", -2147483640 },
178 // Incorrect suffix
179 { "100", 0 },
180 { "100mils", 0 },
181 // Incorrect prefix
182 { "a100mil", 0 },
183};
184
185
189BOOST_DATA_TEST_CASE( PropertiesReadKicadUnit,
190 boost::unit_test::data::make(read_kicad_unit_property),
191 input_value,
192 expected_result )
193{
194 std::map<wxString, wxString> properties = { { "TEST", input_value } };
195
196 int result = ALTIUM_PROPS_UTILS::ReadKicadUnit( properties, "TEST", "0mil" );
197
198 // These are all valid
199 BOOST_CHECK_EQUAL( result, expected_result );
200}
201
202
206static const std::vector<std::tuple<std::string, std::map<wxString, wxString>>> read_properties = {
207 // Empty
208 { "", {} },
209 { "\0", {} },
210 { "|", {} },
211 { "|\0", {} },
212 { "||", {} },
213 { "||\0", {} },
214 // Empty key-value pair
215 { "|=", { { "", "" } } },
216 { "|=\0", { { "", "" } } },
217 { "| = ", { { "", "" } } },
218 { "| = \0", { { "", "" } } },
219 // Single key-value pair
220 { "|A=\0", { { "A", "" } } },
221 { "|A=B", { { "A", "B" } } },
222 { "|A=B\0", { { "A", "B" } } },
223 { "|A=B|", { { "A", "B" } } },
224 { "|A=B|\0", { { "A", "B" } } },
225 { "A=\0", { { "A", "" } } },
226 { "A=B", { { "A", "B" } } },
227 { "A=B\0", { { "A", "B" } } },
228 { "A=B|", { { "A", "B" } } },
229 { "A=B|\0", { { "A", "B" } } },
230 // Multiple key-value pairs
231 { "|A=B|C=D|\0", { { "A", "B" }, { "C", "D" } } },
232 { "A=B|C=D|\0", { { "A", "B" }, { "C", "D" } } },
233 // Same key multiple times
234 { "|A=B|A=C\0", { { "A", "B" } } },
235 { "|A=B|A=C|A=D|A=E|A=F\0", { { "A", "B" } } },
236 // Always upper case key
237 { "|a=b\0", { { "A", "b" } } },
238 { "|abc123=b\0", { { "ABC123", "b" } } },
239 // Trim whitespaces, TODO: correct?
240 { "|A= B\0", { { "A", " B" } } },
241 { "|A=B \0", { { "A", "B" } } },
242 { "| A=B\0", { { "A", "B" } } },
243 { "|A =B\0", { { "A", "B" } } },
244 { "|A=\nB\n\0", { { "A", "\nB" } } },
245 { "A= B\0", { { "A", " B" } } },
246 { "A=B \0", { { "A", "B" } } },
247 { " A=B\0", { { "A", "B" } } },
248 { "A =B\0", { { "A", "B" } } },
249 { "A=\nB\n\0", { { "A", "\nB" } } },
250 // Escaping and other special cases, TODO: extend
251 //{ "|A=||\0", {{"A", "|"}} },
252 { "|A==\0", { { "A", "=" } } },
253 { "|A=a\na\0", { { "A", "a\na" } } },
254 { "|A=a\ta\0", { { "A", "a\ta" } } },
255 // Encoding, TODO: extend
256 { "|%UTF8%A=abc\0", { { "%UTF8%A", "abc" } } },
257 { "|%UTF8%A=\xc2\xa6\0", { { "%UTF8%A", { "\xc2\xa6", wxConvUTF8 } } } }, // Convert to '|' ?
258 // Correct reading errors
259 { "|A|B=C\0", { { "B", "C" } } },
260 { "|A=B|C\0", { { "A", "B" } } },
261};
262
266BOOST_DATA_TEST_CASE( ReadProperties,
267 boost::unit_test::data::make(read_properties),
268 input_value,
269 expected_result )
270{
271 size_t size = 4 + input_value.size();
272 std::unique_ptr<char[]> content = std::make_unique<char[]>( size );
273
274 *content.get() = input_value.size();
275 std::memcpy( content.get() + 4, input_value.c_str(), input_value.size() );
276
277 ALTIUM_BINARY_PARSER parser( content, size );
278
279 std::map<wxString, wxString> result = parser.ReadProperties();
280
281 BOOST_CHECK_EQUAL( parser.HasParsingError(), false );
282 BOOST_CHECK_EQUAL( parser.GetRemainingBytes(), 0 );
283
284 BOOST_CHECK_EQUAL_COLLECTIONS( result.begin(), result.end(),
285 expected_result.begin(), expected_result.end() );
286}
287
288
289BOOST_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)
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.
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.