KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_erc_unconnected_wire_endpoints.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
22
23#include <connection_graph.h>
24#include <schematic.h>
25#include <erc/erc_settings.h>
26#include <erc/erc.h>
27#include <erc/erc_report.h>
29#include <locale_io.h>
30#include <advanced_config.h>
31#include <sch_marker.h>
32#include <algorithm>
33#include <map>
34#include <tuple>
35#include <set>
36#include <scoped_set_reset.h>
37
38namespace
39{
40struct ERC_REGRESSION_TEST_FIXTURE
41{
42 ERC_REGRESSION_TEST_FIXTURE() {}
43
44 SETTINGS_MANAGER m_settingsManager;
45 std::unique_ptr<SCHEMATIC> m_schematic;
46};
47} // namespace
48
49
50BOOST_FIXTURE_TEST_CASE( ERCWireEndpointsUsePublishedState, ERC_REGRESSION_TEST_FIXTURE )
51{
52 LOCALE_IO locale;
53 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
54 SCOPED_SET_RESET restore( enabled, enabled );
55 using DIAGNOSTIC = std::tuple<KIID, KIID_PATH, int, int, wxString>;
56
57 for( const wxString& fixture : { wxString( "erc_wire_endpoints" ), wxString( "unconnected_bus_entry_qa" ) } )
58 {
59 BOOST_TEST_CONTEXT( fixture )
60 {
61 enabled = false;
62 KI_TEST::LoadSchematic( m_settingsManager, fixture, m_schematic );
63
64 for( auto& [code, severity] : m_schematic->ErcSettings().m_ERCSeverities )
65 severity = RPT_SEVERITY_IGNORE;
66
67 m_schematic->ErcSettings().m_ERCSeverities[ERCE_UNCONNECTED_WIRE_ENDPOINT] = RPT_SEVERITY_ERROR;
68 m_schematic->ConnectionGraph()->RunERC();
69 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
70 const auto diagnostics = [&]( bool aRecordExclusions = false )
71 {
73 std::multiset<DIAGNOSTIC> result;
74 std::vector<std::pair<SCH_SCREEN*, SCH_MARKER*>> markers;
75
76 for( int i = 0; i < errors.GetCount(); ++i )
77 {
78 const auto item = std::static_pointer_cast<ERC_ITEM>( errors.GetItem( i ) );
79 auto* marker = static_cast<SCH_MARKER*>( item->GetParent() );
80
81 if( aRecordExclusions )
82 marker->SetExcluded( true, "Retained endpoint" );
83
84 BOOST_CHECK( marker->IsExcluded() );
85 BOOST_CHECK_EQUAL( marker->GetComment(), wxString( "Retained endpoint" ) );
87 result.emplace( item->GetMainItemID(), item->GetSpecificSheetPath().PathRef(),
88 marker->GetPosition().x, marker->GetPosition().y, item->GetErrorMessage( false ) );
89 markers.emplace_back( item->GetSpecificSheetPath().LastScreen(), marker );
90 }
91
92 if( aRecordExclusions )
93 m_schematic->RecordERCExclusions();
94
95 for( const auto& [screen, marker] : markers )
96 screen->DeleteItem( marker );
97
98 return result;
99 };
100 const auto expected = diagnostics( true );
101 BOOST_REQUIRE( !expected.empty() );
102 enabled = true;
103 m_schematic->RebuildConnectivity();
104 BOOST_CHECK_EQUAL( m_schematic->ConnectionGraph()->RunERC(), expected.size() );
105 std::set<SCH_SCREEN*> screens;
106 size_t markerCount = 0;
107
108 for( const SCH_SHEET_PATH& path : m_schematic->Hierarchy() )
109 {
110 if( screens.insert( path.LastScreen() ).second )
111 {
112 for( SCH_ITEM* marker : path.LastScreen()->Items().OfType( SCH_MARKER_T ) )
113 ++markerCount;
114 }
115 }
116
117 BOOST_CHECK_EQUAL( markerCount, expected.size() );
118 m_schematic->ResolveERCExclusionsPostUpdate();
119 BOOST_CHECK( diagnostics() == expected );
120 }
121 }
122}
123
124
125BOOST_FIXTURE_TEST_CASE( ERCFloatingWiresUsePublishedConnectivity, ERC_REGRESSION_TEST_FIXTURE )
126{
127 LOCALE_IO locale;
128 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
129 SCOPED_SET_RESET restore( enabled, enabled );
130 using GROUP = std::pair<KIID_PATH, std::vector<KIID>>;
131
132 for( const wxString& fixture : { wxString( "erc_wire_endpoints" ), wxString( "unconnected_bus_entry_qa" ) } )
133 {
134 BOOST_TEST_CONTEXT( fixture )
135 {
136 enabled = false;
137 KI_TEST::LoadSchematic( m_settingsManager, fixture, m_schematic );
138
139 for( auto& [code, severity] : m_schematic->ErcSettings().m_ERCSeverities )
140 severity = RPT_SEVERITY_IGNORE;
141
142 m_schematic->ErcSettings().m_ERCSeverities[ERCE_WIRE_DANGLING] = RPT_SEVERITY_ERROR;
143 std::map<KIID, VECTOR2I> positions;
144
145 for( const SCH_SHEET_PATH& path : m_schematic->Hierarchy() )
146 {
147 for( SCH_ITEM* item : path.LastScreen()->Items() )
148 positions.emplace( item->m_Uuid, item->GetPosition() );
149 }
150
151 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
152 const auto diagnostics = [&]( bool aCanonical )
153 {
155 std::multiset<GROUP> result;
156 std::vector<std::pair<SCH_SCREEN*, SCH_MARKER*>> markers;
157
158 for( int i = 0; i < errors.GetCount(); ++i )
159 {
160 const auto item = std::static_pointer_cast<ERC_ITEM>( errors.GetItem( i ) );
161 auto* marker = static_cast<SCH_MARKER*>( item->GetParent() );
162 BOOST_CHECK_EQUAL( item->GetErrorCode(), ERCE_WIRE_DANGLING );
163 auto ids = item->GetIDs();
164 std::erase( ids, niluuid );
165 BOOST_REQUIRE( !ids.empty() );
166
167 if( aCanonical )
168 {
169 BOOST_CHECK( std::is_sorted( ids.begin(), ids.end() ) );
170 BOOST_CHECK( marker->GetPosition() == positions.at( ids.front() ) );
171 BOOST_CHECK( marker->IsExcluded() );
172 BOOST_CHECK_EQUAL( marker->GetComment(), wxString( "Retained floating wire" ) );
173 }
174 else
175 {
176 marker->SetExcluded( true, "Retained floating wire" );
177 }
178
179 std::sort( ids.begin(), ids.end() );
180 result.emplace( item->GetSpecificSheetPath().PathRef(), std::move( ids ) );
181 markers.emplace_back( item->GetSpecificSheetPath().LastScreen(), marker );
182 }
183
184 if( !aCanonical )
185 m_schematic->RecordERCExclusions();
186
187 for( const auto& [screen, marker] : markers )
188 screen->DeleteItem( marker );
189
190 return result;
191 };
192 m_schematic->ConnectionGraph()->RunERC();
193 const auto expected = diagnostics( false );
194 BOOST_REQUIRE( !expected.empty() );
195 enabled = true;
196 m_schematic->RebuildConnectivity();
197 m_schematic->ConnectionGraph()->Reset();
198 BOOST_CHECK_EQUAL( m_schematic->ConnectionGraph()->RunERC(), expected.size() );
199 m_schematic->ResolveERCExclusionsPostUpdate();
200 BOOST_CHECK( diagnostics( true ) == expected );
201 m_schematic->ErcSettings().m_ERCSeverities[ERCE_WIRE_DANGLING] = RPT_SEVERITY_IGNORE;
202 BOOST_CHECK_EQUAL( m_schematic->ConnectionGraph()->RunERC(), 0 );
203 }
204 }
205}
206
207
208BOOST_FIXTURE_TEST_CASE( ERCUnconnectedWireEndpoints, ERC_REGRESSION_TEST_FIXTURE )
209{
211
212 // Check for Errors when using rule area netclass directives
213 std::vector<std::pair<wxString, int>> tests = { { "erc_wire_endpoints", 4 } };
214
215 for( const std::pair<wxString, int>& test : tests )
216 {
217 KI_TEST::LoadSchematic( m_settingsManager, test.first, m_schematic );
218
219 ERC_SETTINGS& settings = m_schematic->ErcSettings();
220 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
221
222 // Skip the "Modified symbol" warning
225
226 // Configure the rules under test
229
230 m_schematic->ConnectionGraph()->RunERC();
231
233
234 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
235
236 BOOST_CHECK_MESSAGE( errors.GetCount() == test.second,
237 "Expected " << test.second << " errors in " << test.first.ToStdString()
238 << " but got " << errors.GetCount() << "\n"
239 << reportWriter.GetTextReport() );
240 }
241}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
wxString GetTextReport()
Returns the ERC report in "text" (human readable) format in the C-locale.
Container for ERC settings.
std::map< int, SEVERITY > m_ERCSeverities
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
void SetExcluded(bool aExcluded, const wxString &aComment=wxEmptyString)
Definition marker_base.h:91
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
RAII class that sets an value at construction and resets it to the original value at destruction.
An implementation of the RC_ITEM_LIST interface which uses the global SHEETLIST to fulfill the contra...
int GetCount(int aSeverity=-1) const override
void SetSeverities(int aSeverities) override
std::shared_ptr< RC_ITEM > GetItem(int aIndex) const override
Retrieve a RC_ITEM by index.
@ ERCE_UNCONNECTED_WIRE_ENDPOINT
A label is connected to more than one wire.
@ ERCE_LIB_SYMBOL_MISMATCH
Symbol doesn't match copy in library.
@ ERCE_LIB_SYMBOL_ISSUES
Symbol not found in active libraries.
@ ERCE_WIRE_DANGLING
Some wires are not connected to anything else.
KIID niluuid(0)
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
std::vector< FAB_LAYER_COLOR > dummy
BOOST_FIXTURE_TEST_CASE(ERCWireEndpointsUsePublishedState, ERC_REGRESSION_TEST_FIXTURE)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
std::string path
VECTOR3I expected(15, 30, 45)
BOOST_TEST_CONTEXT("Test Clearance")
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_MARKER_T
Definition typeinfo.h:154