KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_copper_sliver.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 2
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
20
22#include <boost/test/data/test_case.hpp>
24
25#include <board.h>
27#include <drc/drc_engine.h>
28#include <drc/drc_report.h>
29#include <pad.h>
30#include <pcb_track.h>
31#include <pcb_marker.h>
32#include <footprint.h>
33#include <drc/drc_engine.h>
34#include <drc/drc_item.h>
36
37#include <thread>
38#include <chrono>
39#include <exception>
40#include <fstream>
41
42#include <json_common.h>
43#include <wx/filefn.h>
44#include <wx/filename.h>
45
46
47namespace
48{
49struct DRC_REGRESSION_TEST_FIXTURE
50{
51 DRC_REGRESSION_TEST_FIXTURE()
52 { }
53
54 ~DRC_REGRESSION_TEST_FIXTURE()
55 {
56 // Ensure proper cleanup
57 if( m_board && m_board->GetDesignSettings().m_DRCEngine )
58 {
59 m_board->GetDesignSettings().m_DRCEngine->ClearViolationHandler();
60 }
61
62 if( m_board )
63 {
64 m_board->SetProject( nullptr );
65 m_board = nullptr;
66 }
67 }
68
69 SETTINGS_MANAGER m_settingsManager;
70 std::unique_ptr<BOARD> m_board;
71};
72
73
74nlohmann::json writeAndReadJsonReport( BOARD* aBoard )
75{
76 auto markersProvider =
77 std::make_shared<DRC_ITEMS_PROVIDER>( aBoard, MARKER_BASE::MARKER_DRC, MARKER_BASE::MARKER_DRAWING_SHEET );
78 auto ratsnestProvider = std::make_shared<DRC_ITEMS_PROVIDER>( aBoard, MARKER_BASE::MARKER_RATSNEST );
79 auto fpWarningsProvider = std::make_shared<DRC_ITEMS_PROVIDER>( aBoard, MARKER_BASE::MARKER_PARITY );
80
82 markersProvider->SetSeverities( allSeverities );
83 ratsnestProvider->SetSeverities( allSeverities );
84 fpWarningsProvider->SetSeverities( allSeverities );
85
86 DRC_REPORT report( aBoard, EDA_UNITS::MM, markersProvider, ratsnestProvider, fpWarningsProvider );
87
88 wxString jsonPath = wxFileName::CreateTempFileName( wxT( "kicad-drc-json-" ) );
89 bool written = report.WriteJsonReport( jsonPath );
90
91 nlohmann::json reportJson;
92
93 if( written )
94 {
95 std::ifstream reportStream( jsonPath.fn_str() );
96
97 if( reportStream.is_open() )
98 reportStream >> reportJson;
99 }
100
101 wxRemoveFile( jsonPath );
102 BOOST_REQUIRE( written );
103
104 return reportJson;
105}
106} // namespace
107
108
109// clang-format off
110static const std::vector<std::pair<wxString, int>> DRCCopperSliver_cases =
111{
112 { "sliver", 1 },
113 { "issue14449", 0 },
114 { "issue14549", 0 },
115 { "issue14549_2", 0 },
116 { "issue14559", 0 }
117};
118// clang-format on
119
120
121BOOST_DATA_TEST_CASE_F( DRC_REGRESSION_TEST_FIXTURE, DRCCopperSliver,
122 boost::unit_test::data::make( DRCCopperSliver_cases ), test )
123{
124 // Check for minimum copper connection errors
125
126 KI_TEST::LoadBoard( m_settingsManager, test.first, m_board );
127
128 // Validate board was loaded successfully
129 BOOST_REQUIRE_MESSAGE( m_board, "Failed to load board for test: " + test.first.ToStdString() );
130
131 KI_TEST::FillZones( m_board.get() );
132
133 std::vector<DRC_ITEM> violations;
134 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
135
136 // Validate DRC engine is properly initialized
137 BOOST_REQUIRE_MESSAGE( bds.m_DRCEngine, "DRC engine not initialized for test: " + test.first.ToStdString() );
138
139 // Disable DRC tests not useful or not handled in this testcase
140 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
142
143 // Ensure that our desired error is fired
145
147 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
148 const std::function<void( PCB_MARKER* )>& aPathGenerator )
149 {
150 if( bds.GetSeverity( aItem->GetErrorCode() ) == SEVERITY::RPT_SEVERITY_ERROR )
151 violations.push_back( *aItem );
152 } );
153
154 BOOST_TEST_CHECKPOINT( "Running copper sliver drc" );
155
156 // Add exception handling around DRC engine run to catch potential crashes
157 try
158 {
159 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
160
161 // Add a small delay to allow any background threads to complete
162 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
163
164 BOOST_TEST_CHECKPOINT( "DRC engine run completed successfully" );
165 }
166 catch( const std::exception& e )
167 {
168 BOOST_ERROR( wxString::Format( "DRC copper sliver: %s, exception during RunTests: %s",
169 test.first, e.what() ) );
170 return;
171 }
172 catch( ... )
173 {
174 BOOST_ERROR( wxString::Format( "DRC copper sliver: %s, unknown exception during RunTests",
175 test.first ) );
176 return;
177 }
178
179 // Clear the violation handler to prevent any potential issues with cleanup
180 BOOST_TEST_CHECKPOINT( "Clearing violation handler" );
182
183 if( violations.size() == test.second )
184 {
185 BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
186 BOOST_TEST_MESSAGE( wxString::Format( "DRC copper sliver: %s, passed", test.first ) );
187 }
188 else
189 {
190 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::INCH );
191
192 BOARD_RC_ITEM_RESOLVER resolver( *m_board );
193
194 // Report violations with bounds checking
195 for( size_t i = 0; i < violations.size() && i < 100; ++i ) // Limit output to prevent excessive logging
196 {
197 try
198 {
199 const DRC_ITEM& item = violations[i];
200 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, resolver ) );
201 }
202 catch( const std::exception& e )
203 {
204 BOOST_TEST_MESSAGE( wxString::Format( "Error reporting violation %zu: %s", i, e.what() ) );
205 }
206 catch( ... )
207 {
208 BOOST_TEST_MESSAGE( wxString::Format( "Unknown error reporting violation %zu", i ) );
209 }
210 }
211
212 BOOST_ERROR( wxString::Format( "DRC copper sliver: %s, failed (violations found %d "
213 "expected %d)",
214 test.first,
215 (int) violations.size(),
216 test.second ) );
217 }
218}
219
220
221BOOST_FIXTURE_TEST_CASE( DRCCopperSliverJsonReportsMarkerLocation, DRC_REGRESSION_TEST_FIXTURE )
222{
223 KI_TEST::LoadBoard( m_settingsManager, "sliver", m_board );
224 BOOST_REQUIRE( m_board );
225
226 KI_TEST::FillZones( m_board.get() );
227
228 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
230
231 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
233
235
236 VECTOR2I expectedPos;
237 int expectedLayer = UNDEFINED_LAYER;
238 int sliverCount = 0;
239
241 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
242 const std::function<void( PCB_MARKER* )>& aPathGenerator )
243 {
244 if( aItem->GetErrorCode() != DRCE_COPPER_SLIVER )
245 return;
246
247 expectedPos = aPos;
248 expectedLayer = aLayer;
249 ++sliverCount;
250
251 PCB_MARKER* marker = new PCB_MARKER( aItem, aPos, aLayer );
252
253 if( aPathGenerator )
254 aPathGenerator( marker );
255
256 m_board->Add( marker );
257 } );
258
259 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
261
262 BOOST_REQUIRE_EQUAL( sliverCount, 1 );
263 BOOST_REQUIRE( IsValidLayer( expectedLayer ) );
264
265 nlohmann::json reportJson = writeAndReadJsonReport( m_board.get() );
266
267 bool foundSliver = false;
268
269 for( const auto& violation : reportJson["violations"] )
270 {
271 if( violation.value( "type", std::string() ) != "copper_sliver" )
272 continue;
273
274 foundSliver = true;
275 BOOST_REQUIRE( violation.contains( "pos" ) );
276 BOOST_REQUIRE( violation.contains( "layer" ) );
277 BOOST_CHECK( violation["items"].empty() );
278
279 const double expectedX = pcbIUScale.IUTomm( expectedPos.x );
280 const double expectedY = pcbIUScale.IUTomm( expectedPos.y );
281 const double actualX = violation["pos"]["x"].get<double>();
282 const double actualY = violation["pos"]["y"].get<double>();
283
284 BOOST_CHECK_SMALL( actualX - expectedX, 1e-9 );
285 BOOST_CHECK_SMALL( actualY - expectedY, 1e-9 );
286 BOOST_CHECK_EQUAL( violation["layer"].get<std::string>(),
287 m_board->GetLayerName( ToLAYER_ID( expectedLayer ) ).ToStdString() );
288 }
289
290 BOOST_CHECK( foundSliver );
291}
292
293
294BOOST_FIXTURE_TEST_CASE( DRCJsonReportOmitsUnknownMarkerLocation, DRC_REGRESSION_TEST_FIXTURE )
295{
296 KI_TEST::LoadBoard( m_settingsManager, "sliver", m_board );
297 BOOST_REQUIRE( m_board );
298
299 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
300
301 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
303
305
306 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_MISSING_FOOTPRINT );
307 drcItem->SetErrorMessage( wxT( "Missing footprint" ) );
308 m_board->Add( new PCB_MARKER( drcItem, VECTOR2I(), UNDEFINED_LAYER ) );
309
310 nlohmann::json reportJson = writeAndReadJsonReport( m_board.get() );
311
312 bool foundMissingFootprint = false;
313
314 for( const auto& violation : reportJson["schematic_parity"] )
315 {
316 if( violation.value( "type", std::string() ) != "missing_footprint" )
317 continue;
318
319 foundMissingFootprint = true;
320 BOOST_CHECK( !violation.contains( "pos" ) );
321 BOOST_CHECK( !violation.contains( "layer" ) );
322 }
323
324 BOOST_CHECK( foundMissingFootprint );
325}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
SEVERITY GetSeverity(int aDRCErrorCode) const
Return the severity of a given DRC error code.
Implements the RC_ITEM_RESOLVER interface to resolve PCB items by their KIID.
Definition drc_item.h:351
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:164
void ClearViolationHandler()
Definition drc_engine.h:169
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:505
@ MARKER_DRAWING_SHEET
Definition marker_base.h:51
virtual wxString ShowReport(UNITS_PROVIDER *aUnitsProvider, SEVERITY aSeverity, const RC_ITEM_RESOLVER &aResolver) const
Translate this object into a text string suitable for saving to disk in a report.
Definition rc_item.cpp:104
static bool empty(const wxTextEntryBase *aCtrl)
@ DRCE_COPPER_SLIVER
Definition drc_item.h:108
@ DRCE_FIRST
Definition drc_item.h:47
@ DRCE_LAST
Definition drc_item.h:145
@ DRCE_MISSING_FOOTPRINT
Definition drc_item.h:89
static std::string ToStdString(const wxString &aStr)
@ UNDEFINED_LAYER
Definition layer_ids.h:57
bool IsValidLayer(int aLayerId)
Test whether a given integer is a valid layer index, i.e.
Definition layer_ids.h:722
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
void FillZones(BOARD *m_board)
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_FIXTURE_TEST_CASE(DRCCopperSliverJsonReportsMarkerLocation, DRC_REGRESSION_TEST_FIXTURE)
static const std::vector< std::pair< wxString, int > > DRCCopperSliver_cases
BOOST_DATA_TEST_CASE_F(DRC_REGRESSION_TEST_FIXTURE, DRCCopperSliver, boost::unit_test::data::make(DRCCopperSliver_cases), test)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708