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, you may find one at
18 * http://www.gnu.org/licenses/
19 */
20
29
32
33#include <connection_graph.h>
34#include <schematic.h>
35#include <erc/erc_settings.h>
36#include <erc/erc.h>
37#include <erc/erc_report.h>
39#include <locale_io.h>
40
41
43{
45 m_settingsManager( true /* headless */ )
46 { }
47
49 std::unique_ptr<SCHEMATIC> m_schematic;
50};
51
52
59{
61
62 // Test schematic with a symbol that has:
63 // - One pin (VCC) connected to +5V net
64 // - One pin labeled "GND" connected to non-ground net "OTHER"
65 // - A separate GND symbol connected to "GND" net (establishes ground reference)
66 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_error", m_schematic );
67
68 ERC_SETTINGS& settings = m_schematic->ErcSettings();
69 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
70
71 // Skip the symbol library warnings for this test
74
75 // Enable the ground pin test
77
78 m_schematic->ConnectionGraph()->RunERC();
79
80 ERC_TESTER tester( m_schematic.get() );
81 tester.TestGroundPins();
82
84
85 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
86
87 BOOST_CHECK_MESSAGE( errors.GetCount() == 1,
88 "Expected 1 ERCE_GROUND_PIN_NOT_GROUND error but got "
89 << errors.GetCount() << "\n" << reportWriter.GetTextReport() );
90
91 // Verify the error is the correct type
92 bool foundGroundPinError = false;
93 for( unsigned i = 0; i < errors.GetCount(); i++ )
94 {
95 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
96 {
97 foundGroundPinError = true;
98 break;
99 }
100 }
101
102 BOOST_CHECK_MESSAGE( foundGroundPinError,
103 "Expected to find ERCE_GROUND_PIN_NOT_GROUND error\n"
104 << reportWriter.GetTextReport() );
105}
106
107
113{
115
116 // Test schematic with a symbol where the GND pin is correctly connected to a ground net
117 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_ok", m_schematic );
118
119 ERC_SETTINGS& settings = m_schematic->ErcSettings();
120 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
121
122 // Skip the symbol library warnings
125
126 // Enable the ground pin test
128
129 m_schematic->ConnectionGraph()->RunERC();
130
131 ERC_TESTER tester( m_schematic.get() );
132 tester.TestGroundPins();
133
135
136 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
137
138 // Should have no ground pin errors since the GND pin is correctly connected
139 int groundPinErrors = 0;
140 for( unsigned i = 0; i < errors.GetCount(); i++ )
141 {
142 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
143 {
144 groundPinErrors++;
145 }
146 }
147
148 BOOST_CHECK_MESSAGE( groundPinErrors == 0,
149 "Expected 0 ERCE_GROUND_PIN_NOT_GROUND errors but got "
150 << groundPinErrors << "\n" << reportWriter.GetTextReport() );
151}
152
153
159{
161
162 // Test schematic with a symbol that has multiple ground-labeled pins:
163 // - One GND pin correctly connected to ground net
164 // - One GND_ALT pin connected to non-ground net
165 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_mixed", m_schematic );
166
167 ERC_SETTINGS& settings = m_schematic->ErcSettings();
168 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
169
170 // Skip the symbol library warnings
173
174 // Enable the ground pin test
176
177 m_schematic->ConnectionGraph()->RunERC();
178
179 ERC_TESTER tester( m_schematic.get() );
180 tester.TestGroundPins();
181
183
184 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
185
186 // Should have exactly 1 ground pin error for the GND_ALT pin
187 int groundPinErrors = 0;
188 for( unsigned i = 0; i < errors.GetCount(); i++ )
189 {
190 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
191 {
192 groundPinErrors++;
193 }
194 }
195
196 BOOST_CHECK_MESSAGE( groundPinErrors == 1,
197 "Expected 1 ERCE_GROUND_PIN_NOT_GROUND error but got "
198 << groundPinErrors << "\n" << reportWriter.GetTextReport() );
199}
200
201
207{
209
210 // Test schematic with a symbol that has a GND pin but no ground net exists anywhere
211 // The error should only trigger when there IS a ground net somewhere but the GND pin
212 // is not connected to it
213 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_no_ground_net", m_schematic );
214
215 ERC_SETTINGS& settings = m_schematic->ErcSettings();
216 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
217
218 // Skip the symbol library warnings
221
222 // Enable the ground pin test
224
225 m_schematic->ConnectionGraph()->RunERC();
226
227 ERC_TESTER tester( m_schematic.get() );
228 tester.TestGroundPins();
229
231
232 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
233
234 // Should have no ground pin errors since there's no ground net to compare against
235 int groundPinErrors = 0;
236 for( unsigned i = 0; i < errors.GetCount(); i++ )
237 {
238 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
239 {
240 groundPinErrors++;
241 }
242 }
243
244 BOOST_CHECK_MESSAGE( groundPinErrors == 0,
245 "Expected 0 ERCE_GROUND_PIN_NOT_GROUND errors but got "
246 << groundPinErrors << "\n" << reportWriter.GetTextReport() );
247}
248
249
255{
257
258 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_error", m_schematic );
259
260 ERC_SETTINGS& settings = m_schematic->ErcSettings();
261 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
262
263 // Skip the symbol library warnings
266
267 int groundPinErrors = 0;
268 // Disable the ground pin test
270
271 m_schematic->ConnectionGraph()->RunERC();
272
273 ERC_TESTER tester( m_schematic.get() );
274 tester.TestGroundPins();
275
277
278 for( unsigned i = 0; i < errors.GetCount(); i++ )
279 {
280 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
281 {
282 groundPinErrors++;
283 }
284 }
285
286 BOOST_CHECK_MESSAGE( groundPinErrors == 0,
287 "Expected 0 errors when test is disabled but got " << groundPinErrors );
288
289 // Now re-enable the ground pin test and count again
291 m_schematic->ConnectionGraph()->RunERC();
292
293 ERC_TESTER tester2( m_schematic.get() );
294 tester2.TestGroundPins();
296
297 groundPinErrors = 0;
298
299 for( unsigned i = 0; i < errors.GetCount(); i++ )
300 {
301 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
302 {
303 groundPinErrors++;
304 }
305 }
306
307 BOOST_CHECK_MESSAGE( groundPinErrors >= 1,
308 "Expected at least 1 error when test is enabled but got " << groundPinErrors );
309}
310
311
317{
319
320 KI_TEST::LoadSchematic( m_settingsManager, "ground_pin_test_error", m_schematic );
321
322 ERC_SETTINGS& settings = m_schematic->ErcSettings();
323 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
324
325 // Skip the symbol library warnings
328
329 // Enable the ground pin test
331
332 m_schematic->ConnectionGraph()->RunERC();
333
334 ERC_TESTER tester( m_schematic.get() );
335 tester.TestGroundPins();
336
338
339 // Find the ground pin error and verify its content
340 bool foundGroundPinError = false;
341 wxString errorMessage;
342
343 for( unsigned i = 0; i < errors.GetCount(); i++ )
344 {
345 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
346 {
347 foundGroundPinError = true;
348 errorMessage = errors.GetItem( i )->GetErrorMessage();
349
350 // Verify the error message contains expected content
351 BOOST_CHECK_MESSAGE( errorMessage.Contains( "Pin" ),
352 "Error message should contain 'Pin': " << errorMessage.ToStdString() );
353
354 BOOST_CHECK_MESSAGE( errorMessage.Contains( "GND" ),
355 "Error message should contain 'GND': " << errorMessage.ToStdString() );
356
357 BOOST_CHECK_MESSAGE( errorMessage.Contains( "not connected to ground net" ),
358 "Error message should contain expected text: " << errorMessage.ToStdString() );
359
360 // Verify that the error has the pin item associated
361 std::shared_ptr<RC_ITEM> ercItem = errors.GetItem( i );
362 BOOST_CHECK_MESSAGE( ercItem->GetMainItemID() != niluuid,
363 "ERC item should have a main item (the pin)" );
364
365 break;
366 }
367 }
368
369 BOOST_CHECK_MESSAGE( foundGroundPinError,
370 "Should have found a ground pin error to test message content" );
371}
372
373
379{
381
382 // Test data: schema name and expected number of ERCE_GROUND_PIN_NOT_GROUND errors
383 std::vector<std::pair<wxString, int>> tests =
384 {
385 { "ground_pin_test_error", 1 }, // GND pin on non-ground net while symbol has ground
386 { "ground_pin_test_ok", 0 }, // GND pin correctly connected
387 { "ground_pin_test_mixed", 1 }, // Mixed: one correct, one incorrect
388 { "ground_pin_test_no_ground_net", 0 } // No ground net anywhere (no error triggered)
389 };
390
391 for( const std::pair<wxString, int>& test : tests )
392 {
393 KI_TEST::LoadSchematic( m_settingsManager, test.first, m_schematic );
394
395 ERC_SETTINGS& settings = m_schematic->ErcSettings();
396 SHEETLIST_ERC_ITEMS_PROVIDER errors( m_schematic.get() );
397
398 // Skip the symbol library warnings
401
402 // Enable the ground pin test
404
405 m_schematic->ConnectionGraph()->RunERC();
406
407 ERC_TESTER tester( m_schematic.get() );
408
409 // Run all ERC tests to ensure ground pin test integrates properly
413 tester.TestMissingUnits();
414 tester.TestNoConnectPins();
415 tester.TestPinToPin();
416 tester.TestGroundPins(); // Our test
417 tester.TestSimilarLabels();
418
420
421 // Count only ground pin errors
422 int groundPinErrors = 0;
423 for( unsigned i = 0; i < errors.GetCount(); i++ )
424 {
425 if( errors.GetItem( i )->GetErrorCode() == ERCE_GROUND_PIN_NOT_GROUND )
426 {
427 groundPinErrors++;
428 }
429 }
430
431 ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MM );
432
433 BOOST_CHECK_MESSAGE( groundPinErrors == test.second,
434 "Expected " << test.second << " ERCE_GROUND_PIN_NOT_GROUND errors in "
435 << test.first.ToStdString() << " but got " << groundPinErrors
436 << "\n" << reportWriter.GetTextReport() );
437 }
438}
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:936
int TestSimilarLabels()
Checks for labels that differ only in capitalization.
Definition erc.cpp:1391
int TestMultUnitPinConflicts()
Checks if shared pins on multi-unit symbols have been connected to different nets.
Definition erc.cpp:1176
int TestConflictingBusAliases()
Check that there are no conflicting bus alias definitions in the schematic.
Definition erc.cpp:439
int TestNoConnectPins()
In KiCad 5 and earlier, you could connect stuff up to pins with NC electrical type.
Definition erc.cpp:867
int TestGroundPins()
Checks for ground-labeled pins not on a ground net while another pin is.
Definition erc.cpp:1231
int TestMissingUnits()
Test for uninstantiated units of multi unit symbols.
Definition erc.cpp:545
int TestMultiunitFootprints()
Test if all units of each multiunit symbol have the same footprint assigned.
Definition erc.cpp:484
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
virtual wxString GetErrorMessage() const
Definition rc_item.cpp:39
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...