KiCad PCB EDA Suite
test_netlists.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) 2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
21 #include "eeschema_test_utils.h"
22 
23 #include <connection_graph.h>
24 #include <netlist_exporter_kicad.h>
27 #include <project.h>
28 #include <sch_io_mgr.h>
29 #include <sch_sheet.h>
30 #include <schematic.h>
33 
34 
36 {
37 public:
39  m_schematic( nullptr ),
40  m_manager( true )
41  {
42  m_pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD );
43  }
44 
45  void loadSchematic( const wxString& aBaseName );
46 
47  wxString getNetlistFileName( bool aTest = false );
48 
49  void writeNetlist();
50 
51  void compareNetlists();
52 
53  void cleanup();
54 
55  void doNetlistTest( const wxString& aBaseName );
56 
59 
61 
63 };
64 
65 
66 static wxString getSchematicFile( const wxString& aBaseName )
67 {
68  wxFileName fn = KI_TEST::GetEeschemaTestDataDir();
69  fn.AppendDir( "netlists" );
70  fn.AppendDir( aBaseName );
71  fn.SetName( aBaseName );
72  fn.SetExt( KiCadSchematicFileExtension );
73 
74  return fn.GetFullPath();
75 }
76 
77 
78 void TEST_NETLISTS_FIXTURE::loadSchematic( const wxString& aBaseName )
79 {
80  wxString fn = getSchematicFile( aBaseName );
81 
82  BOOST_TEST_MESSAGE( fn );
83 
84  wxFileName pro( fn );
85  pro.SetExt( ProjectFileExtension );
86 
87  m_manager.LoadProject( pro.GetFullPath() );
88 
90 
94 
95  BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true );
96 
98 
99  SCH_SCREENS screens( m_schematic.Root() );
100 
101  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
102  screen->UpdateLocalLibSymbolLinks();
103 
105 
106  // Restore all of the loaded symbol instances from the root sheet screen.
108 
109  sheets.AnnotatePowerSymbols();
110 
111  // NOTE: This is required for multi-unit symbols to be correct
112  // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
113  for( SCH_SHEET_PATH& sheet : sheets )
114  sheet.UpdateAllScreenReferences();
115 
116  // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
117  // SchematicCleanUp must be freed from its UI dependencies.
118 
119  m_schematic.ConnectionGraph()->Recalculate( sheets, true );
120 }
121 
122 
124 {
125  wxFileName netFile = m_schematic.Prj().GetProjectFullName();
126 
127  if( aTest )
128  netFile.SetName( netFile.GetName() + "_test" );
129 
130  netFile.SetExt( NetlistFileExtension );
131 
132  return netFile.GetFullPath();
133 }
134 
135 
137 {
138  auto exporter = std::make_unique<NETLIST_EXPORTER_KICAD>( &m_schematic );
139  BOOST_REQUIRE_EQUAL( exporter->WriteNetlist( getNetlistFileName( true ), 0 ), true );
140 }
141 
142 
144 {
145  NETLIST golden;
146  NETLIST test;
147 
148  {
149  std::unique_ptr<NETLIST_READER> netlistReader(
150  NETLIST_READER::GetNetlistReader( &golden, getNetlistFileName(), wxEmptyString ) );
151 
152  BOOST_REQUIRE_NO_THROW( netlistReader->LoadNetlist() );
153  }
154 
155  {
156  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
157  &test, getNetlistFileName( true ), wxEmptyString ) );
158 
159  BOOST_REQUIRE_NO_THROW( netlistReader->LoadNetlist() );
160  }
161 
162  // Number of components should match
163  BOOST_REQUIRE_EQUAL( golden.GetCount(), test.GetCount() );
164 
165  for( unsigned i = 0; i < golden.GetCount(); i++ )
166  {
167  COMPONENT* goldenComp = golden.GetComponent( i );
168  COMPONENT* refComp = test.GetComponentByReference( goldenComp->GetReference() );
169 
170  // Retrieval by reference
171  BOOST_REQUIRE_NE( refComp, nullptr );
172 
173  // Retrieval by KIID
174  COMPONENT* pathComp = test.GetComponentByPath( goldenComp->GetPath() );
175  BOOST_REQUIRE_NE( pathComp, nullptr );
176 
177  // We should have found the same component
178  BOOST_REQUIRE_EQUAL( refComp->GetReference(), pathComp->GetReference() );
179 
180  // And that component should have the same number of attached nets
181  BOOST_REQUIRE_EQUAL( goldenComp->GetNetCount(), refComp->GetNetCount() );
182 
183  for( unsigned net = 0; net < goldenComp->GetNetCount(); net++ )
184  {
185  const COMPONENT_NET& goldenNet = goldenComp->GetNet( net );
186  const COMPONENT_NET& testNet = refComp->GetNet( net );
187 
188  // The two nets at the same index should be identical
189  BOOST_REQUIRE_EQUAL( goldenNet.GetPinName(), testNet.GetPinName() );
190  BOOST_REQUIRE_EQUAL( goldenNet.GetNetName(), testNet.GetNetName() );
191  }
192  }
193 }
194 
195 
197 {
198  wxRemoveFile( getNetlistFileName( true ) );
199 }
200 
201 
202 void TEST_NETLISTS_FIXTURE::doNetlistTest( const wxString& aBaseName )
203 {
204  loadSchematic( aBaseName );
205  writeNetlist();
206  compareNetlists();
207  cleanup();
208 }
209 
210 
211 BOOST_FIXTURE_TEST_SUITE( Netlists, TEST_NETLISTS_FIXTURE )
212 
213 
214 BOOST_AUTO_TEST_CASE( FindPlugin )
215 {
216  BOOST_CHECK_NE( m_pi, nullptr );
217 }
218 
219 
220 BOOST_AUTO_TEST_CASE( GlobalPromotion )
221 {
222  doNetlistTest( "test_global_promotion" );
223 }
224 
225 
226 BOOST_AUTO_TEST_CASE( GlobalPromotion2 )
227 {
228  doNetlistTest( "test_global_promotion_2" );
229 }
230 
231 
233 {
234  doNetlistTest( "video" );
235 }
236 
237 
238 BOOST_AUTO_TEST_CASE( ComplexHierarchy )
239 {
240  doNetlistTest( "complex_hierarchy" );
241 }
242 
243 
244 BOOST_AUTO_TEST_CASE( WeakVectorBusDisambiguation )
245 {
246  doNetlistTest( "weak_vector_bus_disambiguation" );
247 }
248 
249 
250 
251 BOOST_AUTO_TEST_SUITE_END()
const std::string NetlistFileExtension
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void UpdateSymbolInstances(const std::vector< COMPONENT_INSTANCE_REFERENCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:91
void doNetlistTest(const wxString &aBaseName)
Holds all the data relating to one schematic A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:44
CONNECTION_GRAPH * ConnectionGraph() const
Definition: schematic.h:131
const std::string ProjectFileExtension
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false)
Updates the connection graph for the given list of sheets.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
virtual SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=NULL, const PROPERTIES *aProperties=NULL)
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Definition: sch_plugin.cpp:51
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return...
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:224
const std::vector< COMPONENT_INSTANCE_REFERENCE > & GetSymbolInstances() const
Definition: sch_screen.h:496
VTBL_ENTRY void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:257
COMPONENT_NET is used to store the component pin name to net name (and pin function) associations sto...
Definition: pcb_netlist.h:44
void SetRoot(SCH_SHEET *aRootSheet)
Initializes the schematic with a new root sheet.
Definition: schematic.cpp:99
wxString getNetlistFileName(bool aTest=false)
const wxString & GetNetName() const
Definition: pcb_netlist.h:60
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:153
NETLIST stores all of information read from a netlist along with the flags used to update the NETLIST...
Definition: pcb_netlist.h:194
unsigned GetNetCount() const
Definition: pcb_netlist.h:133
const wxString & GetReference() const
Definition: pcb_netlist.h:147
Definition of file extensions used in Kicad.
SCHEMATIC m_schematic
Schematic to load
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:115
void SetProject(PROJECT *aPrj)
Definition: schematic.cpp:72
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:135
wxFileName GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
COMPONENT * GetComponent(unsigned aIndex)
Function GetComponent returns the COMPONENT at aIndex.
Definition: pcb_netlist.h:233
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
void Reset()
Initializes this schematic to a blank one, unloading anything existing.
Definition: schematic.cpp:49
SETTINGS_MANAGER m_manager
SCH_SHEET & Root() const
Definition: schematic.h:96
void loadSchematic(const wxString &aBaseName)
virtual const wxString & GetError() const
Return an error string to the caller.
Definition: sch_plugin.cpp:177
const std::string KiCadSchematicFileExtension
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:79
const wxString & GetPinName() const
Definition: pcb_netlist.h:59
static wxString getSchematicFile(const wxString &aBaseName)
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
SCH_SCREEN * RootScreen() const
Helper to retreive the screen of the root sheet.
Definition: schematic.cpp:112
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:162
BOOST_AUTO_TEST_CASE(FindPlugin)
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:521
SCH_SHEET_PATH & CurrentSheet() const
Definition: schematic.h:121