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