KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_erc_ground_pins.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
28
31
32#include <connection_graph.h>
33#include <schematic.h>
34#include <erc/erc_settings.h>
35#include <erc/erc.h>
36#include <erc/erc_report.h>
38#include <locale_io.h>
39
40
50
51
58{
60
61 // Test schematic with a symbol that has:
62 // - One pin (VCC) connected to +5V net
63 // - One pin labeled "GND" connected to non-ground net "OTHER"
64 // - A separate GND symbol connected to "GND" net (establishes ground reference)
65 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_error", m_schematic );
66
67 ERC_SETTINGS& settings = m_schematic->ErcSettings();
68 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
69
70 // Skip the symbol library warnings for this test
73
74 // Enable the ground pin test
76
77 m_schematic->ConnectionGraph()->RunERC();
78
79 ERC_TESTER tester( m_schematic.get() );
80 tester.TestGroundPins();
81
83
84 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
85
86 BOOST_CHECK_MESSAGE( errors.GetCount() == 1,
87 "Expected 1 ERCE_GROUND_PIN_NOT_GROUND error but got "
88 << errors.GetCount() << "\n" << reportWriter.GetTextReport() );
89
90 // Verify the error is the correct type
91 bool foundGroundPinError = false;
92 for( unsigned i = 0; i < errors.GetCount(); i++ )
93 {
94 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
95 {
96 foundGroundPinError = true;
97 break;
98 }
99 }
100
101 BOOST_CHECK_MESSAGE( foundGroundPinError,
102 "Expected to find ERCE_GROUND_PIN_NOT_GROUND error\n"
103 << reportWriter.GetTextReport() );
104}
105
106
112{
114
115 // Test schematic with a symbol where the GND pin is correctly connected to a ground net
116 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_ok", m_schematic );
117
118 ERC_SETTINGS& settings = m_schematic->ErcSettings();
119 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
120
121 // Skip the symbol library warnings
124
125 // Enable the ground pin test
127
128 m_schematic->ConnectionGraph()->RunERC();
129
130 ERC_TESTER tester( m_schematic.get() );
131 tester.TestGroundPins();
132
134
135 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
136
137 // Should have no ground pin errors since the GND pin is correctly connected
138 int groundPinErrors = 0;
139 for( unsigned i = 0; i < errors.GetCount(); i++ )
140 {
141 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
142 {
143 groundPinErrors++;
144 }
145 }
146
147 BOOST_CHECK_MESSAGE( groundPinErrors == 0,
148 "Expected 0 ERCE_GROUND_PIN_NOT_GROUND errors but got "
149 << groundPinErrors << "\n" << reportWriter.GetTextReport() );
150}
151
152
158{
160
161 // Test schematic with a symbol that has multiple ground-labeled pins:
162 // - One GND pin correctly connected to ground net
163 // - One GND_ALT pin connected to non-ground net
164 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_mixed", m_schematic );
165
166 ERC_SETTINGS& settings = m_schematic->ErcSettings();
167 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
168
169 // Skip the symbol library warnings
172
173 // Enable the ground pin test
175
176 m_schematic->ConnectionGraph()->RunERC();
177
178 ERC_TESTER tester( m_schematic.get() );
179 tester.TestGroundPins();
180
182
183 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
184
185 // Should have exactly 1 ground pin error for the GND_ALT pin
186 int groundPinErrors = 0;
187 for( unsigned i = 0; i < errors.GetCount(); i++ )
188 {
189 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
190 {
191 groundPinErrors++;
192 }
193 }
194
195 BOOST_CHECK_MESSAGE( groundPinErrors == 1,
196 "Expected 1 ERCE_GROUND_PIN_NOT_GROUND error but got "
197 << groundPinErrors << "\n" << reportWriter.GetTextReport() );
198}
199
200
206{
208
209 // Test schematic with a symbol that has a GND pin but no ground net exists anywhere
210 // The error should only trigger when there IS a ground net somewhere but the GND pin
211 // is not connected to it
212 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_no_ground_net", m_schematic );
213
214 ERC_SETTINGS& settings = m_schematic->ErcSettings();
215 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
216
217 // Skip the symbol library warnings
220
221 // Enable the ground pin test
223
224 m_schematic->ConnectionGraph()->RunERC();
225
226 ERC_TESTER tester( m_schematic.get() );
227 tester.TestGroundPins();
228
230
231 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
232
233 // Should have no ground pin errors since there's no ground net to compare against
234 int groundPinErrors = 0;
235 for( unsigned i = 0; i < errors.GetCount(); i++ )
236 {
237 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
238 {
239 groundPinErrors++;
240 }
241 }
242
243 BOOST_CHECK_MESSAGE( groundPinErrors == 0,
244 "Expected 0 ERCE_GROUND_PIN_NOT_GROUND errors but got "
245 << groundPinErrors << "\n" << reportWriter.GetTextReport() );
246}
247
248
254{
256
257 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_error", m_schematic );
258
259 ERC_SETTINGS& settings = m_schematic->ErcSettings();
260 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
261
262 // Skip the symbol library warnings
265
266 int groundPinErrors = 0;
267 // Disable the ground pin test
269
270 m_schematic->ConnectionGraph()->RunERC();
271
272 ERC_TESTER tester( m_schematic.get() );
273 tester.TestGroundPins();
274
276
277 for( unsigned i = 0; i < errors.GetCount(); i++ )
278 {
279 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
280 {
281 groundPinErrors++;
282 }
283 }
284
285 BOOST_CHECK_MESSAGE( groundPinErrors == 0,
286 "Expected 0 errors when test is disabled but got " << groundPinErrors );
287
288 // Now re-enable the ground pin test and count again
290 m_schematic->ConnectionGraph()->RunERC();
291
292 ERC_TESTER tester2( m_schematic.get() );
293 tester2.TestGroundPins();
295
296 groundPinErrors = 0;
297
298 for( unsigned i = 0; i < errors.GetCount(); i++ )
299 {
300 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
301 {
302 groundPinErrors++;
303 }
304 }
305
306 BOOST_CHECK_MESSAGE( groundPinErrors >= 1,
307 "Expected at least 1 error when test is enabled but got " << groundPinErrors );
308}
309
310
316{
318
319 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_error", m_schematic );
320
321 ERC_SETTINGS& settings = m_schematic->ErcSettings();
322 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
323
324 // Skip the symbol library warnings
327
328 // Enable the ground pin test
330
331 m_schematic->ConnectionGraph()->RunERC();
332
333 ERC_TESTER tester( m_schematic.get() );
334 tester.TestGroundPins();
335
337
338 // Find the ground pin error and verify its content
339 bool foundGroundPinError = false;
340 wxString errorMessage;
341
342 for( unsigned i = 0; i < errors.GetCount(); i++ )
343 {
344 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
345 {
346 foundGroundPinError = true;
347 errorMessage = errors.GetItem( i )->GetErrorMessage( false );
348
349 // Verify the error message contains expected content
350 BOOST_CHECK_MESSAGE( errorMessage.Contains( "Pin" ),
351 "Error message should contain 'Pin': " << errorMessage.ToStdString() );
352
353 BOOST_CHECK_MESSAGE( errorMessage.Contains( "GND" ),
354 "Error message should contain 'GND': " << errorMessage.ToStdString() );
355
356 BOOST_CHECK_MESSAGE( errorMessage.Contains( "not connected to ground net" ),
357 "Error message should contain expected text: " << errorMessage.ToStdString() );
358
359 // Verify that the error has the pin item associated
360 std::shared_ptr<RC_ITEM> ercItem = errors.GetItem( i );
361 BOOST_CHECK_MESSAGE( ercItem->GetMainItemID() != niluuid,
362 "ERC item should have a main item (the pin)" );
363
364 break;
365 }
366 }
367
368 BOOST_CHECK_MESSAGE( foundGroundPinError,
369 "Should have found a ground pin error to test message content" );
370}
371
372
379{
381
382 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_earth", m_schematic );
383
384 ERC_SETTINGS& settings = m_schematic->ErcSettings();
385 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
386
390
391 m_schematic->ConnectionGraph()->RunERC();
392
393 ERC_TESTER tester( m_schematic.get() );
394 tester.TestGroundPins();
395
397
398 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
399
400 int groundPinErrors = 0;
401
402 for( unsigned i = 0; i < errors.GetCount(); i++ )
403 {
404 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
405 {
406 groundPinErrors++;
407 }
408 }
409
410 BOOST_CHECK_MESSAGE( groundPinErrors == 0,
411 "Expected 0 ERCE_GROUND_PIN_NOT_GROUND errors (Earth is a valid ground net) but got "
412 << groundPinErrors << "\n" << reportWriter.GetTextReport() );
413}
414
415
421{
423
424 // Test data: schema name and expected number of ERCE_GROUND_PIN_NOT_GROUND errors
425 std::vector<std::pair<wxString, int>> tests =
426 {
427 { "ground_pin_test_error", 1 }, // GND pin on non-ground net while symbol has ground
428 { "ground_pin_test_ok", 0 }, // GND pin correctly connected
429 { "ground_pin_test_mixed", 1 }, // Mixed: one correct, one incorrect
430 { "ground_pin_test_no_ground_net", 0 }, // No ground net anywhere (no error triggered)
431 { "ground_pin_test_earth", 0 } // GND pins on GND and Earth nets (both valid ground)
432 };
433
434 for( const std::pair<wxString, int>& test : tests )
435 {
436 KI_TEST::LoadSchematic( m_settingsManager, test.first, m_schematic );
437
438 ERC_SETTINGS& settings = m_schematic->ErcSettings();
439 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
440
441 // Skip the symbol library warnings
444
445 // Enable the ground pin test
447
448 m_schematic->ConnectionGraph()->RunERC();
449
450 ERC_TESTER tester( m_schematic.get() );
451
452 // Run all ERC tests to ensure ground pin test integrates properly
455 tester.TestMissingUnits();
456 tester.TestNoConnectPins();
457 tester.TestPinToPin();
458 tester.TestGroundPins(); // Our test
459 tester.TestSimilarLabels();
460
462
463 // Count only ground pin errors
464 int groundPinErrors = 0;
465 for( unsigned i = 0; i < errors.GetCount(); i++ )
466 {
467 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
468 {
469 groundPinErrors++;
470 }
471 }
472
473 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
474
475 BOOST_CHECK_MESSAGE( groundPinErrors == test.second,
476 "Expected " << test.second << " ERCE_GROUND_PIN_NOT_GROUND errors in "
477 << test.first.ToStdString() << " but got " << groundPinErrors
478 << "\n" << reportWriter.GetTextReport() );
479 }
480}
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:980
int TestSimilarLabels()
Checks for labels that differ only in capitalization.
Definition erc.cpp:1747
int TestMultUnitPinConflicts()
Checks if shared pins on multi-unit symbols have been connected to different nets.
Definition erc.cpp:1426
int TestNoConnectPins()
In KiCad 5 and earlier, you could connect stuff up to pins with NC electrical type.
Definition erc.cpp:889
int TestGroundPins()
Checks for ground-labeled pins not on a ground net while another pin is.
Definition erc.cpp:1559
int TestMissingUnits()
Test for uninstantiated units of multi unit symbols.
Definition erc.cpp:568
int TestMultiunitFootprints()
Test if all units of each multiunit symbol have the same footprint assigned.
Definition erc.cpp:507
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
int GetErrorCode() const
Definition rc_item.h:157
virtual wxString GetErrorMessage(bool aTranslate) const
Definition rc_item.cpp:37
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_GROUND_PIN_NOT_GROUND
A ground-labeled pin is not on a ground net while another pin is.
@ ERCE_LIB_SYMBOL_MISMATCH
Symbol doesn't match copy in library.
@ ERCE_LIB_SYMBOL_ISSUES
Symbol not found in active libraries.
KIID niluuid(0)
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(ERCGroundPinMismatch, ERC_GROUND_PIN_TEST_FIXTURE)
Test case: Pin with "GND" in its name connected to non-ground net while another pin in the same symbo...
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")