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, you may find one here:
18 * https://www.gnu.org/licenses/gpl-3.0.en.html
19 */
20
21#include <boost/test/unit_test.hpp>
22
25
26#include <connection_graph.h>
27#include <schematic.h>
28#include <sch_sheet.h>
30#include <locale_io.h>
31
39
40BOOST_FIXTURE_TEST_CASE( RebuildSignals_GroupsFourNetsIntoOneSignal, SIGNALS_TEST_FIXTURE )
41{
43 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_four_nets" ), m_schematic );
44 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
45 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
46 graph->Recalculate( sheets, /*aUnconditional=*/true );
47
48 const auto& netChains = graph->GetPotentialNetChains();
49 bool foundFourNetSignal = false;
50 for( const auto& sig : netChains )
51 {
52 if( sig && sig->GetNets().size() == 4 )
53 {
54 foundFourNetSignal = true;
55 break;
56 }
57 }
58
59 BOOST_CHECK_MESSAGE( foundFourNetSignal,
60 "Expected at least one signal composed of exactly 4 nets to be built" );
61}
62
63BOOST_FIXTURE_TEST_CASE( RebuildSignals_RespectsSignalLabelAndKeepsGrouping, SIGNALS_TEST_FIXTURE )
64{
66 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_four_nets_labeled" ), m_schematic );
67 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
68 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
69 graph->Recalculate( sheets, /*aUnconditional=*/true );
70
71 const auto& netChains = graph->GetPotentialNetChains();
72 bool foundLabeled = false;
73 for( const auto& sig : netChains )
74 {
75 if( !sig )
76 continue;
77
78 wxString name = sig->GetName();
79 if( name.StartsWith( wxString( "/" ) ) )
80 name = name.Mid( 1 );
81
82 if( sig->GetNets().size() == 4 && name == wxString( "SIG" ) )
83 {
84 foundLabeled = true;
85 break;
86 }
87 }
88
89 BOOST_CHECK_MESSAGE( foundLabeled,
90 "Expected a 4-net signal named 'SIG' to be built from label" );
91}
92
93BOOST_FIXTURE_TEST_CASE( RebuildSignals_WithPullupBranch_ExcludesPowerBranch, SIGNALS_TEST_FIXTURE )
94{
96 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_with_pullup" ), m_schematic );
97 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
98 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
99 graph->Recalculate( sheets, /*aUnconditional=*/true );
100
101 const auto& netChains = graph->GetPotentialNetChains();
102
103 // Pullup fixture has two resistors driving a single net through a pullup to VCC. The chain
104 // walker should produce a multi-net chain that does NOT pull VCC into the group, since power
105 // nets are sinks rather than chain participants.
106 bool mainSignalExcludesVCC = false;
107 for( const auto& sig : netChains )
108 {
109 if( !sig )
110 continue;
111
112 const auto& nets = sig->GetNets();
113
114 if( nets.size() < 2 )
115 continue;
116
117 bool containsVCC = false;
118 for( const wxString& n : nets )
119 {
120 wxString nn = n;
121
122 if( nn.StartsWith( wxString( "/" ) ) )
123 nn = nn.Mid( 1 );
124
125 if( nn.CmpNoCase( wxString( "VCC" ) ) == 0 )
126 {
127 containsVCC = true;
128 break;
129 }
130 }
131
132 if( !containsVCC )
133 {
134 mainSignalExcludesVCC = true;
135 break;
136 }
137 }
138
139 BOOST_CHECK_MESSAGE( mainSignalExcludesVCC,
140 "Expected at least one multi-net signal that does not include VCC "
141 "(power branch should not be merged into the main signal)" );
142}
143
144BOOST_FIXTURE_TEST_CASE( RebuildSignals_WithBypassCap_ExcludesPowerBranch, SIGNALS_TEST_FIXTURE )
145{
147 KI_TEST::LoadSchematic( m_settingsManager, wxString( "net_chains_with_bypass" ), m_schematic );
148 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
149 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
150 graph->Recalculate( sheets, /*aUnconditional=*/true );
151
152 const auto& netChains = graph->GetPotentialNetChains();
153
154 // Bypass-cap fixture has a signal path with a decoupling capacitor to GND. The chain walker
155 // should produce a multi-net chain that does NOT pull GND into the group, since power nets
156 // are sinks rather than chain participants.
157 bool mainSignalExcludesGND = false;
158 for( const auto& sig : netChains )
159 {
160 if( !sig )
161 continue;
162
163 const auto& nets = sig->GetNets();
164
165 if( nets.size() < 2 )
166 continue;
167
168 bool containsGND = false;
169 for( const wxString& n : nets )
170 {
171 wxString nn = n;
172
173 if( nn.StartsWith( wxString( "/" ) ) )
174 nn = nn.Mid( 1 );
175
176 if( nn.CmpNoCase( wxString( "GND" ) ) == 0 )
177 {
178 containsGND = true;
179 break;
180 }
181 }
182
183 if( !containsGND )
184 {
185 mainSignalExcludesGND = true;
186 break;
187 }
188 }
189
190 BOOST_CHECK_MESSAGE( mainSignalExcludesGND,
191 "Expected at least one multi-net signal that does not include GND "
192 "(power branch should not be merged into the main signal)" );
193}
194
195// 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:41
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")