KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_erc_signal_pin_to_pin.cpp
Go to the documentation of this file.
1#include <boost/test/unit_test.hpp>
3#include <erc/erc.h>
4#include <erc/erc_settings.h>
5#include <erc/erc_report.h>
7#include <connection_graph.h>
8#include <locale_io.h>
9
16
17BOOST_AUTO_TEST_SUITE( ERCSignal )
18
19// Validate that pins incompatible across multi-net grouped signals trigger ERC.
20// Schematic now defines 3 separate two-pin signals (X1-X2 power_out vs power_out,
21// X3-X4 power_out vs output, X6-X7 output vs output). All three should produce
22// pin-to-pin mismatch markers (warning or error depending on matrix severity).
24{
26 KI_TEST::LoadSchematic( m_settingsManager, wxString( "erc_signal_pin_to_pin" ), m_schematic );
27
28 ERC_SETTINGS& settings = m_schematic->ErcSettings();
31 // Ensure signals are constructed before ERC tests.
32 m_schematic->ConnectionGraph()->Recalculate( m_schematic->BuildSheetListSortedByPageNumbers(), true );
33 // Manually promote all potential signals prior to ERC.
34 {
35 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
36 int idx = 1;
37 for( const auto& pot : graph->GetPotentialSignals() )
38 {
39 if( !pot ) continue;
40 wxString name = wxString::Format( wxS("ERC_SIG_%d"), idx++ );
41 graph->CreateSignalFromPotential( pot.get(), name );
42 }
43 }
44 m_schematic->ConnectionGraph()->RunERC();
45
46 ERC_TESTER tester( m_schematic.get() );
47 tester.TestPinToPin();
48
49 SHEETLIST_ERC_ITEMS_PROVIDER provider( m_schematic.get() );
51
52 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
53
54 const int expectedMismatches = 3;
55
56 int mismatchCount = 0;
57 for( size_t i = 0; i < provider.GetCount(); ++i )
58 {
59 auto item = provider.GetItem( i );
60 if( !item )
61 continue;
62 auto code = item->GetErrorCode();
63 if( code == ERCE_PIN_TO_PIN_WARNING || code == ERCE_PIN_TO_PIN_ERROR )
64 mismatchCount++;
65 }
66
67 if( mismatchCount != expectedMismatches )
68 {
69 // Debug dump of signals when expectation fails
70 auto* graph = m_schematic->ConnectionGraph();
71 // Ensure signals are rebuilt explicitly in case RunERC did not force it
72 graph->Recalculate( m_schematic->BuildSheetListSortedByPageNumbers(), true );
73 const auto& signals = graph->GetSignals();
74 std::ostringstream oss;
75 oss << "DEBUG Pin-to-Pin mismatch failure: expected=" << expectedMismatches
76 << " got=" << mismatchCount << " totalItems=" << provider.GetCount()
77 << " signals=" << signals.size() << "\n";
78 for( size_t si = 0; si < signals.size(); ++si )
79 {
80 const auto& sig = signals[si];
81 if( !sig ) continue;
82 oss << " Signal[" << si << "] name='" << sig->GetName() << "' nets={";
83 for( const auto& n : sig->GetNets() ) oss << n << ",";
84 oss << "} pins={";
85 // Collect pins per net via public GetAllSubgraphs API
86 for( const auto& n : sig->GetNets() )
87 {
88 const auto& subgraphs = graph->GetAllSubgraphs( n );
89 for( CONNECTION_SUBGRAPH* sg : subgraphs )
90 {
91 for( SCH_ITEM* item : sg->GetItems() )
92 {
93 if( item->Type() == SCH_PIN_T )
94 {
95 SCH_PIN* p = static_cast<SCH_PIN*>( item );
96 oss << p->GetParentSymbol()->GetRef( &sg->GetSheet() ) << ":" << p->GetNumber() << "=" << (int) p->GetType() << ";";
97 }
98 }
99 }
100 }
101 oss << "}\n";
102 }
103 // Also list all item codes for clarity
104 oss << "All ERC item codes (severity filtered): ";
105 for( size_t i = 0; i < provider.GetCount(); ++i )
106 {
107 auto it = provider.GetItem( i );
108 if( it )
109 oss << (int) it->GetErrorCode() << ",";
110 }
111 oss << "\n";
112
113 BOOST_CHECK_MESSAGE( mismatchCount == expectedMismatches,
114 oss.str() + reportWriter.GetTextReport() );
115 }
116 else
117 BOOST_CHECK_MESSAGE( mismatchCount == expectedMismatches,
118 "Expected 3 pin-to-pin mismatch errors but got " << mismatchCount
119 << "\n" << reportWriter.GetTextReport() );
120}
121
122// New test: ensure power input pin on one net is considered driven by power output on another net in same signal.
123BOOST_FIXTURE_TEST_CASE( ERCSignalPowerInputDrivenAcrossSignal, ERC_SIGNAL_TEST_FIXTURE )
124{
126 KI_TEST::LoadSchematic( m_settingsManager, wxString( "erc_signal_input_power_driven" ), m_schematic );
127
128 ERC_SETTINGS& settings = m_schematic->ErcSettings();
131 m_schematic->ConnectionGraph()->Recalculate( m_schematic->BuildSheetListSortedByPageNumbers(), true );
132 // Promote potential signals prior to ERC.
133 {
134 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
135 int idx = 1;
136 for( const auto& pot : graph->GetPotentialSignals() )
137 {
138 if( !pot ) continue;
139 wxString name = wxString::Format( wxS("ERC_SIG_%d"), idx++ );
140 graph->CreateSignalFromPotential( pot.get(), name );
141 }
142 }
143 m_schematic->ConnectionGraph()->RunERC();
144
145 ERC_TESTER tester( m_schematic.get() );
146 tester.TestPinToPin();
147
148 SHEETLIST_ERC_ITEMS_PROVIDER provider( m_schematic.get() );
150
151 // Count any POWER PIN NOT DRIVEN markers (should be zero due to suppression across signal)
152 int powerNotDriven = 0;
153 for( size_t i = 0; i < provider.GetCount(); ++i )
154 {
155 auto item = provider.GetItem( i );
156 if( item && item->GetErrorCode() == ERCE_POWERPIN_NOT_DRIVEN )
157 powerNotDriven++;
158 }
159
160 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
161 BOOST_CHECK_MESSAGE( powerNotDriven == 0, "Expected no ERCE_POWERPIN_NOT_DRIVEN errors due to cross-signal driver; got " << powerNotDriven << "\n" << reportWriter.GetTextReport() );
162}
163
const char * name
Calculate the connectivity of a schematic and generates netlists.
A subgraph is a set of items that are electrically connected on a single sheet.
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
int TestPinToPin()
Checks the full netlist against the pin-to-pin connectivity requirements.
Definition erc.cpp:890
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
int GetErrorCode() const
Definition rc_item.h:154
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:250
const wxString & GetNumber() const
Definition sch_pin.h:124
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:312
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.
virtual const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const =0
@ ERCE_POWERPIN_NOT_DRIVEN
Power input pin connected to some others pins but no power out pin to drive it.
@ ERCE_LIB_SYMBOL_MISMATCH
Symbol doesn't match copy in library.
@ ERCE_PIN_TO_PIN_WARNING
@ ERCE_LIB_SYMBOL_ISSUES
Symbol not found in active libraries.
@ ERCE_PIN_TO_PIN_ERROR
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
std::vector< FAB_LAYER_COLOR > dummy
std::unique_ptr< SCHEMATIC > m_schematic
BOOST_FIXTURE_TEST_CASE(BoardTypes, PROTO_TEST_FIXTURE)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_FIXTURE_TEST_CASE(ERCSignalPinToPin, ERC_SIGNAL_TEST_FIXTURE)
BOOST_AUTO_TEST_SUITE_END()
@ SCH_PIN_T
Definition typeinfo.h:157