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 along
17 * with this program. If not, see <http://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 terminal-ref / terminal-pin override maps 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 std::map<wxString, std::pair<KIID, KIID>> termPins;
94 termPins[wxT( "FIRST" )] = std::make_pair( livePinUA, livePinUB );
95 graph->SetNetChainTerminalOverrides( termPins );
96
97 // Promote a second one so we have something to collide with.
98 if( potentials.size() > 1 )
99 {
100 SCH_NETCHAIN* second = graph->CreateNetChainFromPotential( potentials[1].get(),
101 wxT( "SECOND" ) );
102 BOOST_REQUIRE( second );
103
104 // Renaming FIRST -> SECOND should fail (collision).
105 BOOST_CHECK( !graph->RenameCommittedNetChain( wxT( "FIRST" ), wxT( "SECOND" ) ) );
106 BOOST_CHECK_EQUAL( committed->GetName(), wxT( "FIRST" ) );
107 }
108
109 // Empty / unchanged renames should fail.
110 BOOST_CHECK( !graph->RenameCommittedNetChain( wxT( "FIRST" ), wxEmptyString ) );
111 BOOST_CHECK( !graph->RenameCommittedNetChain( wxEmptyString, wxT( "X" ) ) );
112 BOOST_CHECK( !graph->RenameCommittedNetChain( wxT( "FIRST" ), wxT( "FIRST" ) ) );
113
114 // Successful rename.
115 BOOST_CHECK( graph->RenameCommittedNetChain( wxT( "FIRST" ), wxT( "RENAMED" ) ) );
116 BOOST_CHECK_EQUAL( committed->GetName(), wxT( "RENAMED" ) );
117
118 // The override maps must follow the rename: old key gone, new key carries
119 // the previous values.
120 const auto& nccOverrides = graph->GetNetChainNetClassOverrides();
121 BOOST_CHECK( nccOverrides.find( wxT( "FIRST" ) ) == nccOverrides.end() );
122 BOOST_CHECK( nccOverrides.find( wxT( "RENAMED" ) ) != nccOverrides.end() );
123 BOOST_CHECK_EQUAL( nccOverrides.at( wxT( "RENAMED" ) ), wxT( "DDR_DATA" ) );
124
125 const auto& colOverrides = graph->GetNetChainColorOverrides();
126 BOOST_CHECK( colOverrides.find( wxT( "FIRST" ) ) == colOverrides.end() );
127 BOOST_CHECK( colOverrides.find( wxT( "RENAMED" ) ) != colOverrides.end() );
128
129 // Terminal-ref overrides must follow the rename so RebuildNetChains() restores
130 // the chain under the new name on the next graph rebuild.
131 const auto& refOverrides = graph->GetNetChainTerminalRefOverrides();
132 BOOST_CHECK( refOverrides.find( wxT( "FIRST" ) ) == refOverrides.end() );
133 BOOST_REQUIRE( refOverrides.find( wxT( "RENAMED" ) ) != refOverrides.end() );
134 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).first.ref, liveRefA );
135 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).first.pin, livePinA );
136 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).second.ref, liveRefB );
137 BOOST_CHECK_EQUAL( refOverrides.at( wxT( "RENAMED" ) ).second.pin, livePinB );
138
139 const auto& pinOverrides = graph->GetNetChainTerminalOverrides();
140 BOOST_CHECK( pinOverrides.find( wxT( "FIRST" ) ) == pinOverrides.end() );
141 BOOST_REQUIRE( pinOverrides.find( wxT( "RENAMED" ) ) != pinOverrides.end() );
142 BOOST_CHECK( pinOverrides.at( wxT( "RENAMED" ) ).first == livePinUA );
143 BOOST_CHECK( pinOverrides.at( wxT( "RENAMED" ) ).second == livePinUB );
144
145 // After Recalculate the chain must still appear only under the new name; the
146 // committed restore path keys on m_netChainTerminalRefOverrides, so a stale
147 // "FIRST" entry would resurrect a duplicate chain.
148 graph->Recalculate( m_schematic->BuildSheetListSortedByPageNumbers(), true );
149
150 {
151 const auto& chains = graph->GetCommittedNetChains();
152 bool sawFirst = false;
153 bool sawRenamed = false;
154
155 for( const std::unique_ptr<SCH_NETCHAIN>& s : chains )
156 {
157 if( !s )
158 continue;
159
160 if( s->GetName() == wxT( "FIRST" ) )
161 sawFirst = true;
162
163 if( s->GetName() == wxT( "RENAMED" ) )
164 sawRenamed = true;
165 }
166
167 BOOST_CHECK( !sawFirst );
168 BOOST_CHECK( sawRenamed );
169 }
170
171 // Rejecting deletion of an unknown chain.
172 BOOST_CHECK( !graph->DeleteCommittedNetChain( wxT( "DOES_NOT_EXIST" ) ) );
173 BOOST_CHECK( !graph->DeleteCommittedNetChain( wxEmptyString ) );
174
175 // Successful deletion drops the chain and clears its overrides.
176 BOOST_CHECK( graph->DeleteCommittedNetChain( wxT( "RENAMED" ) ) );
177
178 {
179 const auto& netChains = graph->GetCommittedNetChains();
180 bool found = false;
181
182 for( const std::unique_ptr<SCH_NETCHAIN>& s : netChains )
183 {
184 if( s && s->GetName() == wxT( "RENAMED" ) )
185 found = true;
186 }
187
188 BOOST_CHECK( !found );
189 }
190
191 BOOST_CHECK( graph->GetNetChainNetClassOverrides().find( wxT( "RENAMED" ) )
192 == graph->GetNetChainNetClassOverrides().end() );
193 BOOST_CHECK( graph->GetNetChainColorOverrides().find( wxT( "RENAMED" ) )
194 == graph->GetNetChainColorOverrides().end() );
195 BOOST_CHECK( graph->GetNetChainTerminalRefOverrides().find( wxT( "RENAMED" ) )
196 == graph->GetNetChainTerminalRefOverrides().end() );
197 BOOST_CHECK( graph->GetNetChainTerminalOverrides().find( wxT( "RENAMED" ) )
198 == graph->GetNetChainTerminalOverrides().end() );
199}
Calculate the connectivity of a schematic and generates 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...
void SetNetChainTerminalOverrides(const std::map< wxString, std::pair< KIID, KIID > > &aOverrides)
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).
const std::map< wxString, std::pair< KIID, KIID > > & GetNetChainTerminalOverrides() const
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:105
Definition kiid.h:48
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
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")