KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_netlist_exporter_xml_stacked.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
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 3
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 at
18 * http://www.gnu.org/licenses/
19 */
20
23
24#include <set>
25#include <vector>
26
27#include <schematic.h>
30
31#include <wx/filename.h>
32#include <wx/xml/xml.h>
33
35{
36 XML_STACKED_PIN_FIXTURE() : m_settingsManager( true /* headless */ ) {}
37
39 std::unique_ptr<SCHEMATIC> m_schematic;
40};
41
42static std::set<wxString> as_set( const std::initializer_list<const char*>& init )
43{
44 std::set<wxString> out;
45 for( const char* s : init )
46 out.emplace( wxString::FromUTF8( s ) );
47 return out;
48}
49
50static wxXmlNode* find_child( wxXmlNode* parent, const wxString& name )
51{
52 for( wxXmlNode* child = parent->GetChildren(); child; child = child->GetNext() )
53 {
54 if( child->GetName() == name )
55 return child;
56 }
57
58 return nullptr;
59}
60
61static std::vector<wxXmlNode*> find_children( wxXmlNode* parent, const wxString& name )
62{
63 std::vector<wxXmlNode*> out;
64
65 for( wxXmlNode* child = parent->GetChildren(); child; child = child->GetNext() )
66 {
67 if( child->GetName() == name )
68 out.push_back( child );
69 }
70
71 return out;
72}
73
74BOOST_FIXTURE_TEST_CASE( NetlistExporterXML_StackedPinNomenclature, XML_STACKED_PIN_FIXTURE )
75{
76 // Load schematic with stacked pin numbers
77 KI_TEST::LoadSchematic( m_settingsManager, wxT( "stacked_pin_nomenclature" ), m_schematic );
78
79 // Write XML netlist to a test file next to the project
80 wxFileName netFile = m_schematic->Project().GetProjectFullName();
81 netFile.SetName( netFile.GetName() + wxT( "_xml_test" ) );
82 netFile.SetExt( wxT( "xml" ) );
83
84 if( wxFileExists( netFile.GetFullPath() ) )
85 wxRemoveFile( netFile.GetFullPath() );
86
87 WX_STRING_REPORTER reporter;
88 std::unique_ptr<NETLIST_EXPORTER_XML> exporter =
89 std::make_unique<NETLIST_EXPORTER_XML>( m_schematic.get() );
90
91 bool success = exporter->WriteNetlist( netFile.GetFullPath(), 0, reporter );
92 BOOST_REQUIRE( success && reporter.GetMessages().IsEmpty() );
93
94 // Parse the XML back
95 wxXmlDocument xdoc;
96 BOOST_REQUIRE( xdoc.Load( netFile.GetFullPath() ) );
97
98 wxXmlNode* root = xdoc.GetRoot();
99 BOOST_REQUIRE( root );
100
101 wxXmlNode* nets = find_child( root, wxT( "nets" ) );
102 BOOST_REQUIRE( nets );
103
104 // Collect pin sets for R1 on each power net
105 std::set<wxString> setA;
106 std::set<wxString> setB;
107 int foundSets = 0;
108
109 for( wxXmlNode* net : find_children( nets, wxT( "net" ) ) )
110 {
111 wxString netName = net->GetAttribute( wxT( "name" ), wxEmptyString );
112 if( netName != wxT( "VCC" ) && netName != wxT( "GND" ) )
113 continue;
114
115 std::set<wxString>* target = ( foundSets == 0 ? &setA : &setB );
116
117 for( wxXmlNode* node : find_children( net, wxT( "node" ) ) )
118 {
119 if( node->GetAttribute( wxT( "ref" ), wxEmptyString ) != wxT( "R1" ) )
120 continue;
121
122 wxString pin = node->GetAttribute( wxT( "pin" ), wxEmptyString );
123 wxString pinfunction = node->GetAttribute( wxT( "pinfunction" ), wxEmptyString );
124 wxString pintype = node->GetAttribute( wxT( "pintype" ), wxEmptyString );
125
126 // Expect pinfunction to equal the expanded number when base name is empty
127 BOOST_CHECK_EQUAL( pinfunction, pin );
128 // Expect plain passive type (no +no_connect on these nets)
129 BOOST_CHECK_EQUAL( pintype, wxT( "passive" ) );
130
131 target->insert( pin );
132 }
133
134 foundSets++;
135 }
136
137 // We should have found two power nets with R1 nodes
138 BOOST_REQUIRE_EQUAL( foundSets, 2 );
139
140 // Expect one side to be 1..5 and the other to be 6,7,9,10,11 (order independent)
141 const std::set<wxString> expectedTop = as_set( { "1", "2", "3", "4", "5" } );
142 const std::set<wxString> expectedBot = as_set( { "6", "7", "9", "10", "11" } );
143
144 bool matchA = ( setA == expectedTop && setB == expectedBot );
145 bool matchB = ( setA == expectedBot && setB == expectedTop );
146 BOOST_CHECK( matchA || matchB );
147
148 // Cleanup test artifact
149 wxRemoveFile( netFile.GetFullPath() );
150}
const char * name
A wrapper for reporting to a wxString object.
Definition reporter.h:191
const wxString & GetMessages() const
Definition reporter.cpp:77
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
std::unique_ptr< SCHEMATIC > m_schematic
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_FIXTURE_TEST_CASE(NetlistExporterXML_StackedPinNomenclature, XML_STACKED_PIN_FIXTURE)
static wxXmlNode * find_child(wxXmlNode *parent, const wxString &name)
static std::vector< wxXmlNode * > find_children(wxXmlNode *parent, const wxString &name)
static std::set< wxString > as_set(const std::initializer_list< const char * > &init)
BOOST_CHECK_EQUAL(result, "25.4")