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  };
74 
75  for( const TEST_DESCRIPTION& entry : tests )
76  {
77  KI_TEST::LoadBoard( m_settingsManager, entry.m_File, m_board );
78  KI_TEST::FillZones( m_board.get(), 6 );
79  m_board->GetConnectivity()->RecalculateRatsnest();
80 
81  TOOL_MANAGER toolMgr;
82  toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
83 
84  BOARD_COMMIT commit( &toolMgr );
85  TRACKS_CLEANER cleaner( m_board.get(), commit );
86  std::vector< std::shared_ptr<CLEANUP_ITEM> > dryRunItems;
87  std::vector< std::shared_ptr<CLEANUP_ITEM> > realRunItems;
88 
89  cleaner.CleanupBoard( true, &dryRunItems, entry.m_Shorts,
90  entry.m_RedundantVias,
91  entry.m_RedundantTracks,
92  entry.m_DanglingTracks,
93  entry.m_TracksInPads,
94  entry.m_DanglingVias );
95 
96  cleaner.CleanupBoard( true, &realRunItems, entry.m_Shorts,
97  entry.m_RedundantVias,
98  entry.m_RedundantTracks,
99  entry.m_DanglingTracks,
100  entry.m_TracksInPads,
101  entry.m_DanglingVias );
102 
103  if( dryRunItems.size() == entry.m_Expected && realRunItems.size() == entry.m_Expected )
104  {
105  BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
106  BOOST_TEST_MESSAGE( wxString::Format( "Track cleaner regression: %s, passed",
107  entry.m_File ) );
108  }
109  else
110  {
111  BOOST_CHECK_EQUAL( dryRunItems.size(), entry.m_Expected );
112  BOOST_CHECK_EQUAL( realRunItems.size(), entry.m_Expected );
113 
114  std::map<KIID, EDA_ITEM*> itemMap;
115  m_board->FillItemMap( itemMap );
116 
117  for( const std::shared_ptr<CLEANUP_ITEM>& item : realRunItems )
118  {
119  BOOST_TEST_MESSAGE( item->ShowReport( EDA_UNITS::INCHES, RPT_SEVERITY_ERROR,
120  itemMap ) );
121  }
122 
123  BOOST_ERROR( wxString::Format( "Track cleaner regression: %s, failed",
124  entry.m_File ) );
125  }
126  }
127 }
128 
129 
131 {
132  /*
133  * This one just makes sure that the dry-run counts agree with the "real" counts, and that
134  * the cleaning doesn't produce any connectivity changes.
135  */
136  std::vector<wxString> tests = { "issue832",
137  "issue4257",
138  "issue8909" };
139 
140  for( const wxString& relPath : tests )
141  {
142  KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
143  KI_TEST::FillZones( m_board.get(), 6 );
144  m_board->GetConnectivity()->RecalculateRatsnest();
145 
146  TOOL_MANAGER toolMgr;
147  toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
148 
149  BOARD_COMMIT commit( &toolMgr );
150  TRACKS_CLEANER cleaner( m_board.get(), commit );
151  std::vector< std::shared_ptr<CLEANUP_ITEM> > dryRunItems;
152  std::vector< std::shared_ptr<CLEANUP_ITEM> > realRunItems;
153 
154  cleaner.CleanupBoard( true, &dryRunItems, true, // short circuits
155  true, // redundant vias
156  true, // redundant tracks
157  true, // dangling tracks
158  true, // tracks in pads
159  true ); // dangling vias
160 
161  cleaner.CleanupBoard( true, &realRunItems, true, // short circuits
162  true, // redundant vias
163  true, // redundant tracks
164  true, // dangling tracks
165  true, // tracks in pads
166  true ); // dangling vias
167 
168  BOOST_CHECK_EQUAL( dryRunItems.size(), realRunItems.size() );
169 
170  std::vector<DRC_ITEM> violations;
171  BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
172 
173  bds.m_DRCEngine->SetViolationHandler(
174  [&]( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )
175  {
176  if( aItem->GetErrorCode() == DRCE_UNCONNECTED_ITEMS )
177  violations.push_back( *aItem );
178  } );
179 
180  bds.m_DRCEngine->RunTests( EDA_UNITS::MILLIMETRES, true, false );
181 
182  if( violations.empty() )
183  {
184  BOOST_TEST_MESSAGE( wxString::Format( "Track cleaner regression: %s, passed",
185  relPath ) );
186  }
187  else
188  {
189  std::map<KIID, EDA_ITEM*> itemMap;
190  m_board->FillItemMap( itemMap );
191 
192  for( const DRC_ITEM& item : violations )
193  {
194  BOOST_TEST_MESSAGE( item.ShowReport( EDA_UNITS::INCHES, RPT_SEVERITY_ERROR,
195  itemMap ) );
196  }
197 
198  BOOST_ERROR( wxString::Format( "Track cleaner regression: %s, failed",
199  relPath ) );
200  }
201  }
202 }
203 
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.