KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_net_chains.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
24
25#include <connection_graph.h>
26#include <schematic.h>
27#include <sch_sheet.h>
29#include <locale_io.h>
30
38
39BOOST_FIXTURE_TEST_CASE( RebuildSignals_GroupsFourNetsIntoOneSignal, SIGNALS_TEST_FIXTURE )
40{
42 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_four_nets" ), m_schematic );
43 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
44 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
45 graph->Recalculate( sheets, /*aUnconditional=*/true );
46
47 const auto& netChains = graph->GetPotentialNetChains();
48 bool foundFourNetSignal = false;
49 for( const auto& sig : netChains )
50 {
51 if( sig && sig->GetNets().size() == 4 )
52 {
53 foundFourNetSignal = true;
54 break;
55 }
56 }
57
58 BOOST_CHECK_MESSAGE( foundFourNetSignal,
59 "Expected at least one signal composed of exactly 4 nets to be built" );
60}
61
62BOOST_FIXTURE_TEST_CASE( RebuildSignals_RespectsSignalLabelAndKeepsGrouping, SIGNALS_TEST_FIXTURE )
63{
65 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_four_nets_labeled" ), m_schematic );
66 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
67 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
68 graph->Recalculate( sheets, /*aUnconditional=*/true );
69
70 const auto& netChains = graph->GetPotentialNetChains();
71 bool foundLabeled = false;
72 for( const auto& sig : netChains )
73 {
74 if( !sig )
75 continue;
76
77 wxString name = sig->GetName();
78 if( name.StartsWith( wxString( "/" ) ) )
79 name = name.Mid( 1 );
80
81 if( sig->GetNets().size() == 4 && name == wxString( "SIG" ) )
82 {
83 foundLabeled = true;
84 break;
85 }
86 }
87
88 BOOST_CHECK_MESSAGE( foundLabeled,
89 "Expected a 4-net signal named 'SIG' to be built from label" );
90}
91
92BOOST_FIXTURE_TEST_CASE( RebuildSignals_WithPullupBranch_ExcludesPowerBranch, SIGNALS_TEST_FIXTURE )
93{
95 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_with_pullup" ), m_schematic );
96 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
97 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
98 graph->Recalculate( sheets, /*aUnconditional=*/true );
99
100 const auto& netChains = graph->GetPotentialNetChains();
101
102 // Pullup fixture has two resistors driving a single net through a pullup to VCC. The chain
103 // walker should produce a multi-net chain that does NOT pull VCC into the group, since power
104 // nets are sinks rather than chain participants.
105 bool mainSignalExcludesVCC = false;
106 for( const auto& sig : netChains )
107 {
108 if( !sig )
109 continue;
110
111 const auto& nets = sig->GetNets();
112
113 if( nets.size() < 2 )
114 continue;
115
116 bool containsVCC = false;
117 for( const wxString& n : nets )
118 {
119 wxString nn = n;
120
121 if( nn.StartsWith( wxString( "/" ) ) )
122 nn = nn.Mid( 1 );
123
124 if( nn.CmpNoCase( wxString( "VCC" ) ) == 0 )
125 {
126 containsVCC = true;
127 break;
128 }
129 }
130
131 if( !containsVCC )
132 {
133 mainSignalExcludesVCC = true;
134 break;
135 }
136 }
137
138 BOOST_CHECK_MESSAGE( mainSignalExcludesVCC,
139 "Expected at least one multi-net signal that does not include VCC "
140 "(power branch should not be merged into the main signal)" );
141}
142
143BOOST_FIXTURE_TEST_CASE( RebuildSignals_WithBypassCap_ExcludesPowerBranch, SIGNALS_TEST_FIXTURE )
144{
146 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_with_bypass" ), m_schematic );
147 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
148 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
149 graph->Recalculate( sheets, /*aUnconditional=*/true );
150
151 const auto& netChains = graph->GetPotentialNetChains();
152
153 // Bypass-cap fixture has a signal path with a decoupling capacitor to GND. The chain walker
154 // should produce a multi-net chain that does NOT pull GND into the group, since power nets
155 // are sinks rather than chain participants.
156 bool mainSignalExcludesGND = false;
157 for( const auto& sig : netChains )
158 {
159 if( !sig )
160 continue;
161
162 const auto& nets = sig->GetNets();
163
164 if( nets.size() < 2 )
165 continue;
166
167 bool containsGND = false;
168 for( const wxString& n : nets )
169 {
170 wxString nn = n;
171
172 if( nn.StartsWith( wxString( "/" ) ) )
173 nn = nn.Mid( 1 );
174
175 if( nn.CmpNoCase( wxString( "GND" ) ) == 0 )
176 {
177 containsGND = true;
178 break;
179 }
180 }
181
182 if( !containsGND )
183 {
184 mainSignalExcludesGND = true;
185 break;
186 }
187 }
188
189 BOOST_CHECK_MESSAGE( mainSignalExcludesGND,
190 "Expected at least one multi-net signal that does not include GND "
191 "(power branch should not be merged into the main signal)" );
192}
193
194// EOF
const char * name
Calculate the connectivity of a schematic and generates netlists.
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...
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
std::vector< FAB_LAYER_COLOR > dummy
SETTINGS_MANAGER m_settingsManager
std::unique_ptr< SCHEMATIC > m_schematic
BOOST_FIXTURE_TEST_CASE(RebuildSignals_GroupsFourNetsIntoOneSignal, SIGNALS_TEST_FIXTURE)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")