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 
24 /**
25  * @file test_altium_parser_sch.cpp
26  * Test suite for #ALTIUM_PARSER_SCH
27  */
28 
29 #include <qa_utils/wx_utils/unit_test_utils.h>
30 
31 #include <eeschema/sch_plugins/altium/altium_parser_sch.h>
32 
33 // Function declarations of private methods to test
34 int ReadKiCadUnitFrac( const std::map<wxString, wxString>& aProps,
35                        const wxString&                     aKey );
36 
37 
38 struct ALTIUM_PARSER_SCH_FIXTURE
39 {
ALTIUM_PARSER_SCH_FIXTUREALTIUM_PARSER_SCH_FIXTURE40     ALTIUM_PARSER_SCH_FIXTURE() {}
41 };
42 
43 
44 /**
45  * Declares the struct as the Boost test fixture.
46  */
47 BOOST_FIXTURE_TEST_SUITE( AltiumParserSch, ALTIUM_PARSER_SCH_FIXTURE )
48 
49 struct ALTIUM_TO_KICAD_UNIT_FRAC_CASE
50 {
51     wxString input;
52     wxString input_frac;
53     int      exp_result;
54 };
55 
56 /**
57  * A list of valid internal unit conversation factors
58  */
59 static const std::vector<ALTIUM_TO_KICAD_UNIT_FRAC_CASE> altium_to_kicad_unit_frac = {
60     // Some simple values
61     { "0", "0", 0 },
62     { "1", "0", 2540 },
63     { "2", "0", 5080 },
64     { "-1", "0", -2540 },
65     { "-2", "0", -5080 },
66     // Decimal Places
67     { "0", "1", 0 },
68     { "0", "10", 0 },
69     { "0", "100", 2 },
70     { "0", "1000", 25 },
71     { "0", "10000", 254 },
72     { "1", "10000", 2794 },
73     { "0", "-1", 0 },
74     { "0", "-10", 0 },
75     { "0", "-100", -2 },
76     { "0", "-1000", -25 },
77     { "0", "-10000", -254 },
78     { "-1", "-10000", -2794 },
79     // Edge Cases
80     // Clamp bigger values
81     // imperial rounded units as input
82     // metric rounded units as input
83 };
84 
85 /**
86  * Test conversation from Altium internal units into KiCad internal units using properties with FRAC
87  */
BOOST_AUTO_TEST_CASE(PropertiesReadKiCadUnitFracConversation)88 BOOST_AUTO_TEST_CASE( PropertiesReadKiCadUnitFracConversation )
89 {
90     for( const auto& c : altium_to_kicad_unit_frac )
91     {
92         BOOST_TEST_CONTEXT(
93                 wxString::Format( wxT( "%s FRAC %s -> %i" ), c.input, c.input_frac, c.exp_result ) )
94         {
95             std::map<wxString, wxString> properties = { { "TEST", c.input },
96                                                         { "TEST_FRAC", c.input_frac } };
97 
98             int result = ReadKiCadUnitFrac( properties, "TEST" );
99 
100             // These are all valid
101             BOOST_CHECK_EQUAL( result, c.exp_result );
102         }
103     }
104 }
105 
106 
107 BOOST_AUTO_TEST_SUITE_END()
108