KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_import_net_names.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 2 of the License, or
9 * (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
22#include <board.h>
24#include <eda_pattern_match.h>
27#include <import_net_names.h>
28#include <netinfo.h>
29#include <reporter.h>
31#include <set>
32#include <tuple>
33
35{
41
43 std::unique_ptr<BOARD> board;
44};
45
46BOOST_FIXTURE_TEST_SUITE( ImportedNetNames, IMPORT_NET_NAMES_FIXTURE )
47
48BOOST_AUTO_TEST_CASE( RenamePreservesConnectedItemsAndBothNetIndexes )
49{
50 const wxString original = wxS( "Net-(Q6-Pad2)" );
51 const wxString renamed = wxS( "Net-(Q6-B)" );
52 NETINFO_ITEM* net = board->FindNet( original );
53 BOOST_REQUIRE( net );
54 const int code = net->GetNetCode();
55 const unsigned netCount = board->GetNetInfo().GetNetCount();
56 std::vector<std::tuple<BOARD_CONNECTED_ITEM*, NETINFO_ITEM*, int, wxString>> before;
57 std::set<KICAD_T> renamedTypes;
58
59 for( BOARD_CONNECTED_ITEM* item : board->AllConnectedItems() )
60 {
61 before.emplace_back( item, item->GetNet(), item->GetNetCode(), item->GetNetname() );
62
63 if( item->GetNet() == net )
64 renamedTypes.insert( item->Type() );
65 }
66
67 BOOST_REQUIRE( renamedTypes.count( PCB_PAD_T ) );
68 BOOST_REQUIRE( renamedTypes.count( PCB_TRACE_T ) );
69 BOOST_REQUIRE( renamedTypes.count( PCB_ZONE_T ) );
72 { { original, renamed }, { wxS( "N999999999" ), wxS( "Net-(Absent-Pad1)" ) } }, reporter ) );
73 BOOST_CHECK( board->FindNet( original ) == nullptr );
74 BOOST_CHECK( board->FindNet( renamed ) == net );
75 BOOST_CHECK( board->FindNet( code ) == net );
76 BOOST_CHECK_EQUAL( board->GetNetInfo().GetNetCount(), netCount );
77 BOOST_CHECK( board->FindNet( wxS( "Net-(Absent-Pad1)" ) ) == nullptr );
78
79 for( const auto& [item, oldNet, oldCode, oldName] : before )
80 {
81 BOOST_CHECK( item->GetNet() == oldNet );
82 BOOST_CHECK_EQUAL( item->GetNetCode(), oldCode );
83 BOOST_CHECK_EQUAL( item->GetNetname(), oldNet == net ? renamed : oldName );
84 }
85}
86
87BOOST_AUTO_TEST_CASE( InvalidMappingsLeaveEveryNetUnchanged )
88{
89 const wxString first = wxS( "Net-(Q6-Pad2)" );
90 const wxString second = wxS( "Net-(Q6-Pad3)" );
91 BOOST_REQUIRE( board->FindNet( first ) );
92 BOOST_REQUIRE( board->FindNet( second ) );
93 BOOST_REQUIRE( board->FindNet( wxS( "GND" ) ) );
94 std::vector<std::tuple<NETINFO_ITEM*, int, wxString>> before;
95
96 for( NETINFO_ITEM* net : board->GetNetInfo() )
97 before.emplace_back( net, net->GetNetCode(), net->GetNetname() );
98
99 const std::vector<std::map<wxString, wxString>> invalid = {
100 { { first, wxS( "Net-(Q6-B)" ) }, { second, wxS( "GND" ) } },
101 { { first, wxS( "Net-(Q6-B)" ) }, { second, wxS( "Net-(Q6-B)" ) } },
102 { { first, wxS( "Net-(Q6-B)" ) }, { second, wxString() } },
103 { { wxString(), wxS( "Net-(Q6-B)" ) } }
104 };
105
106 for( const auto& names : invalid )
107 {
109 BOOST_CHECK( !ApplyImportedNetNameMap( *board, names, reporter ) );
110 BOOST_CHECK( !reporter.GetMessages().IsEmpty() );
111 BOOST_CHECK_EQUAL( board->GetNetInfo().GetNetCount(), before.size() );
112
113 for( const auto& [net, code, name] : before )
114 {
115 BOOST_CHECK_EQUAL( net->GetNetname(), name );
116 BOOST_CHECK_EQUAL( net->GetNetCode(), code );
117 BOOST_CHECK( board->FindNet( name ) == net );
118 BOOST_CHECK( board->FindNet( code ) == net );
119 }
120 }
121}
122
123BOOST_AUTO_TEST_CASE( SimultaneousNameSwapPreservesNetIdentity )
124{
125 const wxString first = wxS( "Net-(Q6-Pad2)" );
126 const wxString second = wxS( "Net-(Q6-Pad3)" );
127 NETINFO_ITEM* firstNet = board->FindNet( first );
128 NETINFO_ITEM* secondNet = board->FindNet( second );
129 BOOST_REQUIRE( firstNet && secondNet );
130 const int firstCode = firstNet->GetNetCode();
131 const int secondCode = secondNet->GetNetCode();
133 BOOST_REQUIRE( ApplyImportedNetNameMap( *board, { { first, second }, { second, first } }, reporter ) );
134 BOOST_CHECK( board->FindNet( first ) == secondNet );
135 BOOST_CHECK( board->FindNet( second ) == firstNet );
136 BOOST_CHECK( board->FindNet( firstCode ) == firstNet );
137 BOOST_CHECK( board->FindNet( secondCode ) == secondNet );
138}
139
140BOOST_AUTO_TEST_CASE( ExactNetclassAssignmentsFollowRenamedNets )
141{
142 const wxString original = wxS( "/ATN_IN" );
143 const wxString renamed = wxS( "/RENAMED_ATN_IN" );
144 NETINFO_ITEM* net = board->FindNet( original );
145 BOOST_REQUIRE( net );
146 auto settings = board->GetDesignSettings().m_NetSettings;
147 BOOST_REQUIRE( settings );
148 const auto assignedClass = settings->GetEffectiveNetClass( original );
149 BOOST_REQUIRE_EQUAL( assignedClass->GetName(), wxString( "HV" ) );
150 BOOST_REQUIRE( settings->GetEffectiveNetClass( renamed ) == settings->GetDefaultNetclass() );
151 const NETCLASS* netClass = net->GetNetClass();
152 std::vector<std::pair<wxString, wxString>> expectedPatterns;
153
154 for( const auto& [matcher, name] : settings->GetNetclassPatternAssignments() )
155 expectedPatterns.emplace_back( matcher->GetPattern() == original ? renamed : matcher->GetPattern(), name );
156
158 BOOST_REQUIRE( ApplyImportedNetNameMap( *board, { { original, renamed } }, reporter ) );
159 BOOST_CHECK( net->GetNetClass() == netClass );
160 BOOST_CHECK( settings->GetEffectiveNetClass( renamed ) == assignedClass );
161 BOOST_CHECK( settings->GetEffectiveNetClass( original ) == settings->GetDefaultNetclass() );
162 std::vector<std::pair<wxString, wxString>> actualPatterns;
163
164 for( const auto& [matcher, name] : settings->GetNetclassPatternAssignments() )
165 actualPatterns.emplace_back( matcher->GetPattern(), name );
166
167 BOOST_CHECK( actualPatterns == expectedPatterns );
168 settings->ClearAllCaches();
169 BOOST_CHECK( settings->GetEffectiveNetClass( renamed ) == assignedClass );
170}
171
172BOOST_AUTO_TEST_CASE( NetColorAssignmentsFollowRenamedNets )
173{
174 const wxString original = wxS( "/ATN_IN" );
175 const wxString renamed = wxS( "/RECOLOURED_ATN_IN" );
176 auto settings = board->GetDesignSettings().m_NetSettings;
177 BOOST_REQUIRE( settings );
178 const KIGFX::COLOR4D colour( 0.25, 0.5, 0.75, 1.0 );
179 settings->SetNetColorAssignment( original, colour );
180
182 BOOST_REQUIRE( ApplyImportedNetNameMap( *board, { { original, renamed } }, reporter ) );
183
184 const auto& colours = settings->GetNetColorAssignments();
185 BOOST_CHECK_EQUAL( colours.count( original ), 0 );
186 BOOST_REQUIRE_EQUAL( colours.count( renamed ), 1 );
187 BOOST_CHECK( colours.at( renamed ) == colour );
188}
189
190
const char * name
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:43
Handle the data for a net.
Definition netinfo.h:50
NETCLASS * GetNetClass()
Definition netinfo.h:101
int GetNetCode() const
Definition netinfo.h:104
A wrapper for reporting to a wxString object.
Definition reporter.h:242
Abstract pattern-matching tool and implementations.
bool ApplyImportedNetNameMap(BOARD &aBoard, const std::map< wxString, wxString > &aNames, REPORTER &aReporter)
Apply caller-selected imported net names without changing net identity or connectivity.
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
std::unique_ptr< BOARD > board
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(RenamePreservesConnectedItemsAndBothNetIndexes)
IbisParser parser & reporter
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88