KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_net_chain_remove_rename.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 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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
24
25#include <connection_graph.h>
26#include <schematic.h>
27#include <sch_netchain.h>
28#include <sch_sheet.h>
30#include <locale_io.h>
31
32
40
41
42// Promote a potential, then rename + delete it via the Phase 8.1 APIs.
43// Validates that:
44// * RenameCommittedNetChain rekeys the override maps and rejects collisions.
45// * DeleteCommittedNetChain removes from m_committedNetChains and clears overrides.
46BOOST_FIXTURE_TEST_CASE( NetChain_RemoveRenameRoundTrip, NETCHAIN_RENAME_FIXTURE )
47{
49 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_four_nets" ), m_schematic );
50
51 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
52 BOOST_REQUIRE( graph );
53
54 graph->Recalculate( m_schematic->BuildSheetListSortedByPageNumbers(), true );
55
56 const auto& potentials = graph->GetPotentialNetChains();
57 BOOST_REQUIRE( !potentials.empty() );
58
59 // Seed netclass + color override maps so RenameCommittedNetChain has
60 // something to rekey on the simple paths.
61 std::map<wxString, wxString> classes;
62 std::map<wxString, KIGFX::COLOR4D> colors;
63 classes[wxT( "FIRST" )] = wxT( "DDR_DATA" );
64 colors[wxT( "FIRST" )] = KIGFX::COLOR4D( 0.1, 0.2, 0.3, 1.0 );
65 graph->SetNetChainNetClassOverrides( classes );
66 graph->SetNetChainColorOverrides( colors );
67
68 SCH_NETCHAIN* committed =
69 graph->CreateNetChainFromPotential( potentials.front().get(), wxT( "FIRST" ) );
70
71 BOOST_REQUIRE( committed );
72 BOOST_CHECK_EQUAL( committed->GetName(), wxT( "FIRST" ) );
73 BOOST_CHECK_EQUAL( committed->GetNetClass(), wxT( "DDR_DATA" ) );
74
75 // Seed the saved terminal references from the live committed
76 // chain so the values match whatever pins the fixture's potential picked.
77 // This exercises the same restore path RebuildNetChains() takes after a
78 // file reload, and stays valid if the fixture is edited.
79 const wxString liveRefA = committed->GetTerminalRef( 0 );
80 const wxString livePinA = committed->GetTerminalPinNum( 0 );
81 const wxString liveRefB = committed->GetTerminalRef( 1 );
82 const wxString livePinB = committed->GetTerminalPinNum( 1 );
83 const KIID livePinUA = committed->GetTerminalPinA();
84 const KIID livePinUB = committed->GetTerminalPinB();
85
86 BOOST_REQUIRE( !liveRefA.IsEmpty() );
87 BOOST_REQUIRE( !liveRefB.IsEmpty() );
88
89 std::map<wxString, CONNECTION_GRAPH::CHAIN_TERMINAL_REFS> termRefs;
90 termRefs[wxT( "FIRST" )] = { { liveRefA, livePinA }, { liveRefB, livePinB } };
91 graph->SetNetChainTerminalRefOverrides( termRefs );
92
93
94
95 // Promote a second one so we have something to collide with.
96 if( potentials.size() > 1 )
97 {
98 SCH_NETCHAIN* second = graph->CreateNetChainFromPotential( potentials[1].get(),
99 wxT( "SECOND" ) );
100 BOOST_REQUIRE( second );
101
102 // Renaming FIRST -> SECOND should fail (collision).
103 BOOST_CHECK( !graph->RenameCommittedNetChain( wxT( "FIRST" ), wxT( "SECOND" ) ) );
104 BOOST_CHECK_EQUAL( committed->GetName(), wxT( "FIRST" ) );
105 }
106
107 // Empty / unchanged renames should fail.
108 BOOST_CHECK( !graph->RenameCommittedNetChain( wxT( "FIRST" ), wxEmptyString ) );
109 BOOST_CHECK( !graph->RenameCommittedNetChain( wxEmptyString, wxT( "X" ) ) );
110 BOOST_CHECK( !graph->RenameCommittedNetChain( wxT( "FIRST" ), wxT( "FIRST" ) ) );
111 BOOST_CHECK( !graph->RenameCommittedNetChain( wxT( "FIRST" ), wxT( "BAD NAME" ) ) );
112
113 // Successful rename.
114 BOOST_CHECK( graph->RenameCommittedNetChain( wxT( "FIRST" ), wxT( "RENAMED" ) ) );
115 BOOST_CHECK_EQUAL( committed->GetName(), wxT( "RENAMED" ) );
116
117 // The override maps must follow the rename: old key gone, new key carries
118 // the previous values.
119 const auto& nccOverrides = graph->GetNetChainNetClassOverrides();
120 BOOST_CHECK( nccOverrides.find( wxT( "FIRST" ) ) == nccOverrides.end() );
121 BOOST_CHECK( nccOverrides.find( wxT( "RENAMED" ) ) != nccOverrides.end() );
122 BOOST_CHECK_EQUAL( nccOverrides.at( wxT( "RENAMED" ) ), wxT( "DDR_DATA" ) );
123
124 const auto& colOverrides = graph->GetNetChainColorOverrides();
125 BOOST_CHECK( colOverrides.find( wxT( "FIRST" ) ) == colOverrides.end() );
126 BOOST_CHECK( colOverrides.find( wxT( "RENAMED" ) ) != colOverrides.end() );
127
128 // Terminal-ref overrides must follow the rename so RebuildNetChains() restores
129 // the chain under the new name on the next graph rebuild.
130 const auto& refOverrides = graph->GetNetChainTerminalRefOverrides();
131 BOOST_CHECK( refOverrides.find( wxT( "FIRST" ) ) == refOverrides.end() );
132 BOOST_REQUIRE( refOverrides.find( wxT( "RENAMED" ) ) != refOverrides.end() );
133 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).first.ref, liveRefA );
134 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).first.pin, livePinA );
135 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).second.ref, liveRefB );
136 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).second.pin, livePinB );
137
138 BOOST_CHECK( committed->GetTerminalPinA() == livePinUA );
139 BOOST_CHECK( committed->GetTerminalPinB() == livePinUB );
140
141 // After Recalculate the chain must still appear only under the new name; the
142 // committed restore path keys on m_netChainTerminalRefOverrides, so a stale
143 // "FIRST" entry would resurrect a duplicate chain.
144 graph->Recalculate( m_schematic->BuildSheetListSortedByPageNumbers(), true );
145
146 {
147 const auto& chains = graph->GetCommittedNetChains();
148 bool sawFirst = false;
149 bool sawRenamed = false;
150
151 for( const std::unique_ptr<SCH_NETCHAIN>& s : chains )
152 {
153 if( !s )
154 continue;
155
156 if( s->GetName() == wxT( "FIRST" ) )
157 sawFirst = true;
158
159 if( s->GetName() == wxT( "RENAMED" ) )
160 sawRenamed = true;
161 }
162
163 BOOST_CHECK( !sawFirst );
164 BOOST_CHECK( sawRenamed );
165 }
166
167 // Rejecting deletion of an unknown chain.
168 BOOST_CHECK( !graph->DeleteCommittedNetChain( wxT( "DOES_NOT_EXIST" ) ) );
169 BOOST_CHECK( !graph->DeleteCommittedNetChain( wxEmptyString ) );
170
171 // Successful deletion drops the chain and clears its overrides.
172 BOOST_CHECK( graph->DeleteCommittedNetChain( wxT( "RENAMED" ) ) );
173
174 {
175 const auto& netChains = graph->GetCommittedNetChains();
176 bool found = false;
177
178 for( const std::unique_ptr<SCH_NETCHAIN>& s : netChains )
179 {
180 if( s && s->GetName() == wxT( "RENAMED" ) )
181 found = true;
182 }
183
184 BOOST_CHECK( !found );
185 }
186
187 BOOST_CHECK( graph->GetNetChainNetClassOverrides().find( wxT( "RENAMED" ) )
188 == graph->GetNetChainNetClassOverrides().end() );
189 BOOST_CHECK( graph->GetNetChainColorOverrides().find( wxT( "RENAMED" ) )
190 == graph->GetNetChainColorOverrides().end() );
191 BOOST_CHECK( graph->GetNetChainTerminalRefOverrides().find( wxT( "RENAMED" ) )
192 == graph->GetNetChainTerminalRefOverrides().end() );
193}
Calculate the connectivity of a schematic and generate netlists.
void SetNetChainColorOverrides(const std::map< wxString, COLOR4D > &aOverrides)
const std::map< wxString, COLOR4D > & GetNetChainColorOverrides() const
void SetNetChainNetClassOverrides(const std::map< wxString, wxString > &aOverrides)
Stash per-net-chain netclass overrides read from the schematic file.
void SetNetChainTerminalRefOverrides(const std::map< wxString, CHAIN_TERMINAL_REFS > &aRefs)
SCH_NETCHAIN * CreateNetChainFromPotential(SCH_NETCHAIN *aPotential, const wxString &aName)
Promote a potential net chain to an actual user net chain with the provided name.
bool RenameCommittedNetChain(const wxString &aOld, const wxString &aNew)
Rename a committed net chain.
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Update the connection graph for the given list of sheets.
const std::vector< std::unique_ptr< SCH_NETCHAIN > > & GetPotentialNetChains() const
Potential net chains are inferred groupings produced by RebuildNetChains() but not yet user-committed...
const std::map< wxString, wxString > & GetNetChainNetClassOverrides() const
const std::vector< std::unique_ptr< SCH_NETCHAIN > > & GetCommittedNetChains() const
Return user-created (committed) net chains (legacy accessor retained under net-chain API).
bool DeleteCommittedNetChain(const wxString &aName)
Delete a committed net chain by name.
const std::map< wxString, CHAIN_TERMINAL_REFS > & GetNetChainTerminalRefOverrides() const
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition kiid.h:46
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
A net chain is a collection of nets that are connected together through passive components.
const KIID & GetTerminalPinB() const
const wxString & GetTerminalRef(int aIdx) const
const wxString & GetNetClass() const
const wxString & GetName() const
const wxString & GetTerminalPinNum(int aIdx) const
const KIID & GetTerminalPinA() const
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
std::vector< FAB_LAYER_COLOR > dummy
std::unique_ptr< SCHEMATIC > m_schematic
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_FIXTURE_TEST_CASE(NetChain_RemoveRenameRoundTrip, NETCHAIN_RENAME_FIXTURE)
BOOST_CHECK_EQUAL(result, "25.4")