KiCad PCB EDA Suite
test_tracks_cleaner.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 (C) 2021 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, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
26 #include <board.h>
27 #include <board_commit.h>
28 #include <board_design_settings.h>
30 #include <tracks_cleaner.h>
31 #include <cleanup_item.h>
32 #include <drc/drc_item.h>
34 #include <tool/tool_manager.h>
35 
37 {
39  m_settingsManager( true /* headless */ )
40  { }
41 
43  std::unique_ptr<BOARD> m_board;
44 };
45 
46 
48 {
49  wxString m_File;
50  bool m_Shorts;
57 };
58 
59 
60 BOOST_FIXTURE_TEST_CASE( FailedToCleanRegressionTests, TRACK_CLEANER_TEST_FIXTURE )
61 {
62  /*
63  * This one ensures that certain cleanup items are indeed found and marked for cleanup.
64  */
65  std::vector<TEST_DESCRIPTION> tests =
66  {
67  // short redundant redundant dangling tracks dangling
68  // circuits vias tracks tracks in pads vias expected
69  { "issue2904", false, false, false, true, false, false, 9 },
70  { "issue5093", false, false, false, false, true, false, 118 },
71  { "issue7004", false, true, false, false, false, true, 25 },
72  { "issue8883", true, true, true, true, false, true, 80 },
73  { "issue10916", false, false, true, false, false, false, 0 }
74  };
75 
76  for( const TEST_DESCRIPTION& entry : tests )
77  {
78  KI_TEST::LoadBoard( m_settingsManager, entry.m_File, m_board );
79  KI_TEST::FillZones( m_board.get(), 6 );
80  m_board->GetConnectivity()->RecalculateRatsnest();
81 
82  TOOL_MANAGER toolMgr;
83  toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
84 
85  BOARD_COMMIT commit( &toolMgr );
86  TRACKS_CLEANER cleaner( m_board.get(), commit );
87  std::vector< std::shared_ptr<CLEANUP_ITEM> > dryRunItems;
88  std::vector< std::shared_ptr<CLEANUP_ITEM> > realRunItems;
89 
90  cleaner.CleanupBoard( true, &dryRunItems, entry.m_Shorts,
91  entry.m_RedundantVias,
92  entry.m_RedundantTracks,
93  entry.m_DanglingTracks,
94  entry.m_TracksInPads,
95  entry.m_DanglingVias );
96 
97  cleaner.CleanupBoard( true, &realRunItems, entry.m_Shorts,
98  entry.m_RedundantVias,
99  entry.m_RedundantTracks,
100  entry.m_DanglingTracks,
101  entry.m_TracksInPads,
102  entry.m_DanglingVias );
103 
104  if( dryRunItems.size() == entry.m_Expected && realRunItems.size() == entry.m_Expected )
105  {
106  BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
107  BOOST_TEST_MESSAGE( wxString::Format( "Track cleaner regression: %s, passed",
108  entry.m_File ) );
109  }
110  else
111  {
112  BOOST_CHECK_EQUAL( dryRunItems.size(), entry.m_Expected );
113  BOOST_CHECK_EQUAL( realRunItems.size(), entry.m_Expected );
114 
115  std::map<KIID, EDA_ITEM*> itemMap;
116  m_board->FillItemMap( itemMap );
117 
118  for( const std::shared_ptr<CLEANUP_ITEM>& item : realRunItems )
119  {
120  BOOST_TEST_MESSAGE( item->ShowReport( EDA_UNITS::INCHES, RPT_SEVERITY_ERROR,
121  itemMap ) );
122  }
123 
124  BOOST_ERROR( wxString::Format( "Track cleaner regression: %s, failed",
125  entry.m_File ) );
126  }
127  }
128 }
129 
130 
132 {
133  /*
134  * This one just makes sure that the dry-run counts agree with the "real" counts, and that
135  * the cleaning doesn't produce any connectivity changes.
136  */
137  std::vector<wxString> tests = { "issue832",
138  "issue4257",
139  "issue8909" };
140 
141  for( const wxString& relPath : tests )
142  {
143  KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
144  KI_TEST::FillZones( m_board.get(), 6 );
145  m_board->GetConnectivity()->RecalculateRatsnest();
146 
147  TOOL_MANAGER toolMgr;
148  toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
149 
150  BOARD_COMMIT commit( &toolMgr );
151  TRACKS_CLEANER cleaner( m_board.get(), commit );
152  std::vector< std::shared_ptr<CLEANUP_ITEM> > dryRunItems;
153  std::vector< std::shared_ptr<CLEANUP_ITEM> > realRunItems;
154 
155  cleaner.CleanupBoard( true, &dryRunItems, true, // short circuits
156  true, // redundant vias
157  true, // redundant tracks
158  true, // dangling tracks
159  true, // tracks in pads
160  true ); // dangling vias
161 
162  cleaner.CleanupBoard( true, &realRunItems, true, // short circuits
163  true, // redundant vias
164  true, // redundant tracks
165  true, // dangling tracks
166  true, // tracks in pads
167  true ); // dangling vias
168 
169  BOOST_CHECK_EQUAL( dryRunItems.size(), realRunItems.size() );
170 
171  std::vector<DRC_ITEM> violations;
172  BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
173 
174  bds.m_DRCEngine->SetViolationHandler(
175  [&]( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )
176  {
177  if( aItem->GetErrorCode() == DRCE_UNCONNECTED_ITEMS )
178  violations.push_back( *aItem );
179  } );
180 
181  bds.m_DRCEngine->RunTests( EDA_UNITS::MILLIMETRES, true, false );
182 
183  if( violations.empty() )
184  {
185  BOOST_TEST_MESSAGE( wxString::Format( "Track cleaner regression: %s, passed",
186  relPath ) );
187  }
188  else
189  {
190  std::map<KIID, EDA_ITEM*> itemMap;
191  m_board->FillItemMap( itemMap );
192 
193  for( const DRC_ITEM& item : violations )
194  {
195  BOOST_TEST_MESSAGE( item.ShowReport( EDA_UNITS::INCHES, RPT_SEVERITY_ERROR,
196  itemMap ) );
197  }
198 
199  BOOST_ERROR( wxString::Format( "Track cleaner regression: %s, failed",
200  relPath ) );
201  }
202  }
203 }
204 
Master controller class:
Definition: tool_manager.h:54
std::unique_ptr< BOARD > m_board
void CleanupBoard(bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aCleanVias, bool aRemoveMisConnected, bool aMergeSegments, bool aDeleteUnconnected, bool aDeleteTracksinPad, bool aDeleteDanglingVias)
the cleanup function.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
BOOST_FIXTURE_TEST_CASE(FailedToCleanRegressionTests, TRACK_CLEANER_TEST_FIXTURE)
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
void FillZones(BOARD *m_board, int aFillVersion)
std::shared_ptr< DRC_ENGINE > m_DRCEngine
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
Container for design settings for a BOARD object.