KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_erc_label_connectivity.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 <advanced_config.h>
24#include <connection_graph.h>
25#include <erc/erc.h>
26#include <erc/erc_settings.h>
27#include <locale_io.h>
28#include <sch_marker.h>
29#include <schematic.h>
31#include <map>
32#include <set>
33#include <tuple>
34#include <scoped_set_reset.h>
35
36
37BOOST_AUTO_TEST_CASE( ERCLabelPoliciesUseCapturedConnectivity )
38{
39 LOCALE_IO locale;
40 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
41 SCOPED_SET_RESET restore( enabled, enabled );
42 const std::vector<std::tuple<wxString, int, size_t>> fixtures = {
43 { "erc_directive_label_not_connected", ERCE_LABEL_NOT_CONNECTED, 1 },
44 { "issue22854/test", ERCE_LABEL_NOT_CONNECTED, 0 },
45 { "issue13212", ERCE_SINGLE_GLOBAL_LABEL, 3 },
46 { "erc_label_test", ERCE_SINGLE_GLOBAL_LABEL, 4 },
47 { "issue23840/BusAndVectors", ERCE_SINGLE_GLOBAL_LABEL, 2 }
48 };
49
50 for( const auto& [fixture, code, expectedCount] : fixtures )
51 {
52 BOOST_TEST_CONTEXT( fixture.ToStdString() )
53 {
54 enabled = false;
55 SETTINGS_MANAGER settings;
56 std::unique_ptr<SCHEMATIC> schematic;
57 KI_TEST::LoadSchematic( settings, fixture, schematic );
58
59 for( auto& [errorCode, severity] : schematic->ErcSettings().m_ERCSeverities )
60 severity = errorCode == code ? RPT_SEVERITY_ERROR : RPT_SEVERITY_IGNORE;
61
62 using DIAGNOSTICS = std::map<std::pair<KIID_PATH, KIID>, VECTOR2I>;
63 const auto collect = [&]()
64 {
65 DIAGNOSTICS result;
66 std::set<SCH_SCREEN*> screens;
67
68 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
69 screens.insert( path.LastScreen() );
70
71 for( SCH_SCREEN* screen : screens )
72 {
73 std::vector<SCH_MARKER*> markers;
74
75 for( SCH_ITEM* item : screen->Items().OfType( SCH_MARKER_T ) )
76 {
77 auto* marker = static_cast<SCH_MARKER*>( item );
78 markers.push_back( marker );
79 const auto error = std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() );
80 const SCH_ITEM* label = schematic->ResolveItem( error->GetMainItemID(), nullptr, true );
81 BOOST_REQUIRE( label );
82
83 if( code == ERCE_LABEL_NOT_CONNECTED && label->Type() != SCH_DIRECTIVE_LABEL_T )
84 continue;
85
86 BOOST_CHECK( marker->IsExcluded() );
87 BOOST_CHECK_EQUAL( marker->GetComment(), wxString( "Retained label policy" ) );
88 BOOST_CHECK_EQUAL( error->GetErrorCode(), code );
89 BOOST_CHECK_EQUAL( error->MainItemHasSheetPath(), code == ERCE_SINGLE_GLOBAL_LABEL );
90 BOOST_CHECK( !error->AuxItemHasSheetPath() );
91 BOOST_CHECK( error->GetAuxItemID() == niluuid );
92 BOOST_CHECK_EQUAL( error->GetIDs().size(), 1 );
93 const auto key = std::make_pair( error->GetSpecificSheetPath().PathRef(), label->m_Uuid );
94 BOOST_CHECK( result.emplace( key, marker->GetPosition() ).second );
95 }
96
97 for( SCH_MARKER* marker : markers )
98 screen->DeleteItem( marker );
99 }
100
101 return result;
102 };
103 schematic->ConnectionGraph()->RunERC();
104
105 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
106 {
107 for( SCH_ITEM* item : path.LastScreen()->Items().OfType( SCH_MARKER_T ) )
108 static_cast<SCH_MARKER*>( item )->SetExcluded( true, "Retained label policy" );
109 }
110
111 schematic->RecordERCExclusions();
112 const auto expected = collect();
113 BOOST_REQUIRE_EQUAL( expected.size(), expectedCount );
114
115 enabled = true;
116 schematic->RebuildConnectivity();
117 schematic->ConnectionGraph()->Reset();
119 schematic->ResolveERCExclusionsPostUpdate();
120 BOOST_CHECK( collect() == expected );
121 BOOST_CHECK_EQUAL( schematic->ConnectionGraph()->RunERC(), expected.size() );
122 schematic->ResolveERCExclusionsPostUpdate();
123 BOOST_CHECK( collect() == expected );
124 schematic->ErcSettings().m_ERCSeverities[code] = RPT_SEVERITY_IGNORE;
126 BOOST_CHECK( collect().empty() );
127 }
128 }
129}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
const KIID m_Uuid
Definition eda_item.h:597
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
static int TestConnectivity(SCHEMATIC &aSchematic)
Definition erc.cpp:2215
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
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.
static bool empty(const wxTextEntryBase *aCtrl)
@ ERCE_LABEL_NOT_CONNECTED
Label not connected to any pins.
@ ERCE_SINGLE_GLOBAL_LABEL
A label only exists once in the schematic.
KIID niluuid(0)
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
BOOST_AUTO_TEST_CASE(ERCLabelPoliciesUseCapturedConnectivity)
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_DIRECTIVE_LABEL_T
Definition typeinfo.h:167
@ SCH_MARKER_T
Definition typeinfo.h:154
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683