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
39{
41
43 std::unique_ptr<SCHEMATIC> m_schematic;
44};
45
46
47BOOST_FIXTURE_TEST_CASE( ERCWireEndpointsUsePublishedState, ERC_REGRESSION_TEST_FIXTURE )
48{
49 LOCALE_IO locale;
50 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
51 SCOPED_SET_RESET restore( enabled, enabled );
52 using DIAGNOSTIC = std::tuple<KIID, KIID_PATH, int, int, wxString>;
53
54 for( const wxString& fixture : { wxString( "erc_wire_endpoints" ), wxString( "unconnected_bus_entry_qa" ) } )
55 {
56 BOOST_TEST_CONTEXT( fixture )
57 {
58 enabled = false;
59 KI_TEST::LoadSchematic( m_settingsManager, fixture, m_schematic );
60
61 for( auto& [code, severity] : m_schematic->ErcSettings().m_ERCSeverities )
62 severity = RPT_SEVERITY_IGNORE;
63
64 m_schematic->ErcSettings().m_ERCSeverities[ERCE_UNCONNECTED_WIRE_ENDPOINT] = RPT_SEVERITY_ERROR;
65 m_schematic->ConnectionGraph()->RunERC();
66 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
67 const auto diagnostics = [&]( bool aRecordExclusions = false )
68 {
70 std::multiset<DIAGNOSTIC> result;
71 std::vector<std::pair<SCH_SCREEN*, SCH_MARKER*>> markers;
72
73 for( int i = 0; i < errors.GetCount(); ++i )
74 {
75 const auto item = std::static_pointer_cast<ERC_ITEM>( errors.GetItem( i ) );
76 auto* marker = static_cast<SCH_MARKER*>( item->GetParent() );
77
78 if( aRecordExclusions )
79 marker->SetExcluded( true, "Retained endpoint" );
80
81 BOOST_CHECK( marker->IsExcluded() );
82 BOOST_CHECK_EQUAL( marker->GetComment(), wxString( "Retained endpoint" ) );
84 result.emplace( item->GetMainItemID(), item->GetSpecificSheetPath().PathRef(),
85 marker->GetPosition().x, marker->GetPosition().y, item->GetErrorMessage( false ) );
86 markers.emplace_back( item->GetSpecificSheetPath().LastScreen(), marker );
87 }
88
89 if( aRecordExclusions )
90 m_schematic->RecordERCExclusions();
91
92 for( const auto& [screen, marker] : markers )
93 screen->DeleteItem( marker );
94
95 return result;
96 };
97 const auto expected = diagnostics( true );
98 BOOST_REQUIRE( !expected.empty() );
99 enabled = true;
100 m_schematic->RebuildConnectivity();
101 BOOST_CHECK_EQUAL( m_schematic->ConnectionGraph()->RunERC(), expected.size() );
102 std::set<SCH_SCREEN*> screens;
103 size_t markerCount = 0;
104
105 for( const SCH_SHEET_PATH& path : m_schematic->Hierarchy() )
106 {
107 if( screens.insert( path.LastScreen() ).second )
108 {
109 for( SCH_ITEM* marker : path.LastScreen()->Items().OfType( SCH_MARKER_T ) )
110 ++markerCount;
111 }
112 }
113
114 BOOST_CHECK_EQUAL( markerCount, expected.size() );
115 m_schematic->ResolveERCExclusionsPostUpdate();
116 BOOST_CHECK( diagnostics() == expected );
117 }
118 }
119}
120
121
122BOOST_FIXTURE_TEST_CASE( ERCFloatingWiresUsePublishedConnectivity, ERC_REGRESSION_TEST_FIXTURE )
123{
124 LOCALE_IO locale;
125 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
126 SCOPED_SET_RESET restore( enabled, enabled );
127 using GROUP = std::pair<KIID_PATH, std::vector<KIID>>;
128
129 for( const wxString& fixture : { wxString( "erc_wire_endpoints" ), wxString( "unconnected_bus_entry_qa" ) } )
130 {
131 BOOST_TEST_CONTEXT( fixture )
132 {
133 enabled = false;
134 KI_TEST::LoadSchematic( m_settingsManager, fixture, m_schematic );
135
136 for( auto& [code, severity] : m_schematic->ErcSettings().m_ERCSeverities )
137 severity = RPT_SEVERITY_IGNORE;
138
139 m_schematic->ErcSettings().m_ERCSeverities[ERCE_WIRE_DANGLING] = RPT_SEVERITY_ERROR;
140 std::map<KIID, VECTOR2I> positions;
141
142 for( const SCH_SHEET_PATH& path : m_schematic->Hierarchy() )
143 {
144 for( SCH_ITEM* item : path.LastScreen()->Items() )
145 positions.emplace( item->m_Uuid, item->GetPosition() );
146 }
147
148 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
149 const auto diagnostics = [&]( bool aCanonical )
150 {
152 std::multiset<GROUP> result;
153 std::vector<std::pair<SCH_SCREEN*, SCH_MARKER*>> markers;
154
155 for( int i = 0; i < errors.GetCount(); ++i )
156 {
157 const auto item = std::static_pointer_cast<ERC_ITEM>( errors.GetItem( i ) );
158 auto* marker = static_cast<SCH_MARKER*>( item->GetParent() );
159 BOOST_CHECK_EQUAL( item->GetErrorCode(), ERCE_WIRE_DANGLING );
160 auto ids = item->GetIDs();
161 std::erase( ids, niluuid );
162 BOOST_REQUIRE( !ids.empty() );
163
164 if( aCanonical )
165 {
166 BOOST_CHECK( std::is_sorted( ids.begin(), ids.end() ) );
167 BOOST_CHECK( marker->GetPosition() == positions.at( ids.front() ) );
168 BOOST_CHECK( marker->IsExcluded() );
169 BOOST_CHECK_EQUAL( marker->GetComment(), wxString( "Retained floating wire" ) );
170 }
171 else
172 {
173 marker->SetExcluded( true, "Retained floating wire" );
174 }
175
176 std::sort( ids.begin(), ids.end() );
177 result.emplace( item->GetSpecificSheetPath().PathRef(), std::move( ids ) );
178 markers.emplace_back( item->GetSpecificSheetPath().LastScreen(), marker );
179 }
180
181 if( !aCanonical )
182 m_schematic->RecordERCExclusions();
183
184 for( const auto& [screen, marker] : markers )
185 screen->DeleteItem( marker );
186
187 return result;
188 };
189 m_schematic->ConnectionGraph()->RunERC();
190 const auto expected = diagnostics( false );
191 BOOST_REQUIRE( !expected.empty() );
192 enabled = true;
193 m_schematic->RebuildConnectivity();
194 m_schematic->ConnectionGraph()->Reset();
195 BOOST_CHECK_EQUAL( m_schematic->ConnectionGraph()->RunERC(), expected.size() );
196 m_schematic->ResolveERCExclusionsPostUpdate();
197 BOOST_CHECK( diagnostics( true ) == expected );
198 m_schematic->ErcSettings().m_ERCSeverities[ERCE_WIRE_DANGLING] = RPT_SEVERITY_IGNORE;
199 BOOST_CHECK_EQUAL( m_schematic->ConnectionGraph()->RunERC(), 0 );
200 }
201 }
202}
203
204
206{
208
209 // Check for Errors when using rule area netclass directives
210 std::vector<std::pair<wxString, int>> tests = { { "erc_wire_endpoints", 4 } };
211
212 for( const std::pair<wxString, int>& test : tests )
213 {
214 KI_TEST::LoadSchematic( m_settingsManager, test.first, m_schematic );
215
216 ERC_SETTINGS& settings = m_schematic->ErcSettings();
217 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
218
219 // Skip the "Modified symbol" warning
222
223 // Configure the rules under test
226
227 m_schematic->ConnectionGraph()->RunERC();
228
230
231 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
232
233 BOOST_CHECK_MESSAGE( errors.GetCount() == test.second,
234 "Expected " << test.second << " errors in " << test.first.ToStdString()
235 << " but got " << errors.GetCount() << "\n"
236 << reportWriter.GetTextReport() );
237 }
238}
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
std::unique_ptr< SCHEMATIC > m_schematic
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