KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_net_chain_synthetic_filter.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, 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 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <connection_graph.h>
25#include <sch_netchain.h>
26#include <sch_screen.h>
27#include <sch_sheet.h>
28#include <schematic.h>
30#include <locale_io.h>
32
33#include <wx/ffile.h>
34#include <wx/filename.h>
35#include <wx/stdpaths.h>
36#include <wx/xml/xml.h>
37
38
39// Test backdoor declared in connection_graph.h. Pushes a fully-formed committed chain
40// into the graph so SaveSchematicFile / NETLIST_EXPORTER_XML have something to serialize.
42 std::unique_ptr<SCH_NETCHAIN> aChain );
43
44
46{
49 {
50 m_workDir.AssignDir( wxStandardPaths::Get().GetTempDir() );
51 m_workDir.AppendDir(
52 wxString::Format( wxT( "kicad_qa_netchain_synth_%lu" ),
53 static_cast<unsigned long>( wxGetProcessId() ) ) );
54
55 wxFileName::Mkdir( m_workDir.GetFullPath(), 0755, wxPATH_MKDIR_FULL );
56
57 wxString projectPath = m_workDir.GetFullPath() + wxT( "synth_filter.kicad_pro" );
58 m_tempFiles.push_back( projectPath );
59
60 m_settingsManager.LoadProject( projectPath.ToStdString() );
62 }
63
65 {
66 m_schematic.reset();
67
68 for( const wxString& file : m_tempFiles )
69 {
70 if( wxFileExists( file ) )
71 wxRemoveFile( file );
72 }
73
74 if( m_workDir.DirExists() )
75 wxFileName::Rmdir( m_workDir.GetFullPath(), wxPATH_RMDIR_RECURSIVE );
76 }
77
78 wxString PathInWorkDir( const wxString& aLeaf )
79 {
80 wxString full = m_workDir.GetFullPath() + aLeaf;
81 m_tempFiles.push_back( full );
82 return full;
83 }
84
86 PROJECT* m_project = nullptr;
87 std::unique_ptr<SCHEMATIC> m_schematic;
88 wxFileName m_workDir;
89 std::vector<wxString> m_tempFiles;
90};
91
92
93static wxXmlNode* find_child( wxXmlNode* parent, const wxString& name )
94{
95 for( wxXmlNode* child = parent->GetChildren(); child; child = child->GetNext() )
96 {
97 if( child->GetName() == name )
98 return child;
99 }
100
101 return nullptr;
102}
103
104
118BOOST_FIXTURE_TEST_CASE( NetChainSyntheticNamesAreFilteredFromOutputs,
120{
122
123 const wxString chainName = wxT( "TEST_SYNTH_FILTER_CHAIN" );
124 const wxString realNetA = wxT( "/SIG_A" );
125 const wxString realNetB = wxT( "/SIG_B" );
126 const wxString synthName = wxString( SCH_NETCHAIN::SYNTHETIC_NET_PREFIX )
127 + wxT( "0xdeadbeef" );
128
129 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
130 m_schematic->SetProject( m_project );
131 m_schematic->CreateDefaultScreens();
132
133 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
134 BOOST_REQUIRE( !topSheets.empty() );
135
136 SCH_SHEET* topSheet = topSheets[0];
137 SCH_SCREEN* topScreen = topSheet->GetScreen();
138 BOOST_REQUIRE( topScreen );
139
140 wxString rootFileName = PathInWorkDir( wxT( "synth_filter.kicad_sch" ) );
141 topSheet->SetFileName( wxT( "synth_filter.kicad_sch" ) );
142 topScreen->SetFileName( rootFileName );
143
144 m_schematic->RefreshHierarchy();
145
146 auto chain = std::make_unique<SCH_NETCHAIN>();
147 chain->SetName( chainName );
148 chain->AddNet( realNetA );
149 chain->AddNet( realNetB );
150 chain->AddNet( synthName );
151
152 // Terminal refs are required by the sexpr writer; without them the chain is skipped
153 // before the synthetic-net filter loop runs and this test would not exercise the filter.
154 chain->SetTerminalRefs( wxT( "U1" ), wxT( "1" ), wxT( "U2" ), wxT( "2" ) );
155
156 boost_test_inject_committed_net_chain( *m_schematic->ConnectionGraph(), std::move( chain ) );
157
158 BOOST_REQUIRE_EQUAL( m_schematic->ConnectionGraph()->GetCommittedNetChains().size(), 1u );
159
160 // 1. XML netlist exporter (KiCad-internal flag emits <net_chains>).
161 wxFileName xmlFile( rootFileName );
162 xmlFile.SetName( xmlFile.GetName() + wxT( "_netlist" ) );
163 xmlFile.SetExt( wxT( "xml" ) );
164 m_tempFiles.push_back( xmlFile.GetFullPath() );
165
166 {
168 std::unique_ptr<NETLIST_EXPORTER_XML> exporter =
169 std::make_unique<NETLIST_EXPORTER_XML>( m_schematic.get() );
170
171 BOOST_REQUIRE( exporter->WriteNetlist( xmlFile.GetFullPath(), GNL_OPT_KICAD,
172 reporter ) );
173 BOOST_REQUIRE( reporter.GetMessages().IsEmpty() );
174 }
175
176 BOOST_REQUIRE( wxFileExists( xmlFile.GetFullPath() ) );
177
178 // Raw text scan catches the synthetic prefix anywhere in the document.
179 {
180 wxFFile rawXml( xmlFile.GetFullPath(), "rb" );
181 BOOST_REQUIRE( rawXml.IsOpened() );
182
183 wxString xmlText;
184 rawXml.ReadAll( &xmlText );
185 rawXml.Close();
186
188 xmlText.Find( wxString( SCH_NETCHAIN::SYNTHETIC_NET_PREFIX ) ) == wxNOT_FOUND,
189 "XML netlist must not contain synthetic __SG_* net names" );
190 }
191
192 // Structural check: real nets remain in the chain's <members>; synthetic name does not.
193 {
194 wxXmlDocument xdoc;
195 BOOST_REQUIRE( xdoc.Load( xmlFile.GetFullPath() ) );
196 BOOST_REQUIRE( xdoc.GetRoot() );
197
198 wxXmlNode* netChains = find_child( xdoc.GetRoot(), wxT( "net_chains" ) );
199 BOOST_REQUIRE( netChains );
200
201 wxXmlNode* targetChain = nullptr;
202
203 for( wxXmlNode* xchain = netChains->GetChildren(); xchain; xchain = xchain->GetNext() )
204 {
205 if( xchain->GetName() != wxT( "net_chain" ) )
206 continue;
207
208 if( xchain->GetAttribute( wxT( "name" ), wxEmptyString ) == chainName )
209 {
210 targetChain = xchain;
211 break;
212 }
213 }
214
215 BOOST_REQUIRE_MESSAGE( targetChain, "Committed chain missing from XML output" );
216
217 wxXmlNode* members = find_child( targetChain, wxT( "members" ) );
218 BOOST_REQUIRE( members );
219
220 std::set<wxString> emittedNets;
221
222 for( wxXmlNode* xmem = members->GetChildren(); xmem; xmem = xmem->GetNext() )
223 {
224 if( xmem->GetName() != wxT( "member" ) )
225 continue;
226
227 emittedNets.insert( xmem->GetAttribute( wxT( "net" ), wxEmptyString ) );
228 }
229
230 BOOST_CHECK( emittedNets.count( realNetA ) == 1u );
231 BOOST_CHECK( emittedNets.count( realNetB ) == 1u );
232 BOOST_CHECK_MESSAGE( emittedNets.count( synthName ) == 0u,
233 "Synthetic net leaked into XML <member> list" );
234 }
235
236 // 2. sexpr writer must already filter synthetic names; reloading the file must
237 // yield a chain that resolves with the real members intact.
238 {
239 SCH_IO_KICAD_SEXPR saver;
240 BOOST_REQUIRE_NO_THROW( saver.SaveSchematicFile( rootFileName, topSheet,
241 m_schematic.get() ) );
242 BOOST_REQUIRE( wxFileExists( rootFileName ) );
243
244 wxFFile rawSexpr( rootFileName, "rb" );
245 BOOST_REQUIRE( rawSexpr.IsOpened() );
246
247 wxString sexprText;
248 rawSexpr.ReadAll( &sexprText );
249 rawSexpr.Close();
250
252 sexprText.Find( wxString( SCH_NETCHAIN::SYNTHETIC_NET_PREFIX ) ) == wxNOT_FOUND,
253 "kicad_sch must not contain synthetic __SG_* net names" );
254
255 // Guard against the early-skip path in sch_io_kicad_sexpr.cpp: if terminal refs were
256 // missing the writer would emit no net_chain section and the synthetic-prefix check
257 // above would pass vacuously.
258 BOOST_CHECK_MESSAGE( sexprText.Find( wxT( "(net_chain" ) ) != wxNOT_FOUND,
259 "kicad_sch must contain the committed net_chain section" );
260 BOOST_CHECK_MESSAGE( sexprText.Find( chainName ) != wxNOT_FOUND,
261 "kicad_sch must reference the committed chain by name" );
262 BOOST_CHECK_MESSAGE( sexprText.Find( realNetA ) != wxNOT_FOUND,
263 "kicad_sch must retain real net A in the chain's nets list" );
264 BOOST_CHECK_MESSAGE( sexprText.Find( realNetB ) != wxNOT_FOUND,
265 "kicad_sch must retain real net B in the chain's nets list" );
266 }
267
268 // 3. Reload the saved file. RebuildNetChains needs real schematic items to repopulate
269 // GetCommittedNetChains(), so instead we verify the parser-side member-net overrides
270 // that the IO layer hands to the connection graph during load.
271 {
272 SCH_IO_KICAD_SEXPR loader;
273 SCHEMATIC reloaded( nullptr );
274
275 reloaded.SetProject( m_project );
276
277 SCH_SHEET* loadedRoot = nullptr;
278 BOOST_REQUIRE_NO_THROW(
279 loadedRoot = loader.LoadSchematicFile( rootFileName, &reloaded ) );
280 BOOST_REQUIRE( loadedRoot );
281
282 const auto& overrides = reloaded.ConnectionGraph()->GetNetChainMemberNetOverrides();
283 auto it = overrides.find( chainName );
284 BOOST_REQUIRE_MESSAGE( it != overrides.end(),
285 "Reloaded schematic missing chain member-net override" );
286
287 const std::set<wxString>& reloadedNets = it->second;
288 BOOST_CHECK( !reloadedNets.empty() );
289 BOOST_CHECK( reloadedNets.count( realNetA ) == 1u );
290 BOOST_CHECK( reloadedNets.count( realNetB ) == 1u );
291
292 for( const wxString& n : reloadedNets )
293 {
295 !n.StartsWith( SCH_NETCHAIN::SYNTHETIC_NET_PREFIX ),
296 "Reloaded chain leaked a synthetic __SG_* member" );
297 }
298 }
299}
const char * name
Calculate the connectivity of a schematic and generates netlists.
const std::map< wxString, std::set< wxString > > & GetNetChainMemberNetOverrides() const
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
Container for project specific data.
Definition project.h:62
Holds all the data relating to one schematic.
Definition schematic.h:90
void SetProject(PROJECT *aPrj)
CONNECTION_GRAPH * ConnectionGraph() const
Definition schematic.h:201
A SCH_IO derivation for loading schematic files using the new s-expression file format.
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
static constexpr char SYNTHETIC_NET_PREFIX[]
Prefix used when synthesising net names for unnamed subgraphs.
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:376
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
A wrapper for reporting to a wxString object.
Definition reporter.h:189
@ GNL_OPT_KICAD
std::vector< FAB_LAYER_COLOR > dummy
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
IbisParser parser & reporter
void boost_test_inject_committed_net_chain(CONNECTION_GRAPH &aGraph, std::unique_ptr< SCH_NETCHAIN > aChain)
BOOST_FIXTURE_TEST_CASE(NetChainSyntheticNamesAreFilteredFromOutputs, NETCHAIN_SYNTHETIC_FILTER_FIXTURE)
Regression: synthetic per-run subgraph names (__SG_*) embed subgraph codes that are not stable across...
static wxXmlNode * find_child(wxXmlNode *parent, const wxString &name)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
const SHAPE_LINE_CHAIN chain