KiCad PCB EDA Suite
test_drc_courtyard_invalid.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) 2018 KiCad Developers, see CHANGELOG.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 
25 
28 #include <board.h>
29 #include <footprint.h>
30 #include <pcb_marker.h>
31 #include <drc/drc_engine.h>
32 #include <drc/drc_item.h>
33 #include <widgets/ui_common.h>
34 
35 #include "../board_test_utils.h"
36 
37 
39 {
41 };
42 
43 
44 BOOST_FIXTURE_TEST_SUITE( DrcCourtyardInvalid, COURTYARD_TEST_FIXTURE )
45 
46 
47 /*
48  * A simple mock footprint with a set of courtyard rectangles and some other information
49  */
51 {
52  std::string m_refdes;
53  std::vector<SEG> m_segs;
55 };
56 
57 
59 {
60  std::string m_refdes;
62 };
63 
64 
65 std::ostream& operator<<( std::ostream& os, const COURTYARD_INVALID_INFO& aInvalid )
66 {
67  os << "COURTYARD_INVALID_INFO[ " << aInvalid.m_refdes;
68  os << ", code: " << aInvalid.m_drc_error_code << "]";
69  return os;
70 }
71 
72 
74 {
75  std::string m_case_name;
76  std::vector<COURTYARD_INVALID_TEST_FP> m_mods;
77  std::vector<COURTYARD_INVALID_INFO> m_exp_errors;
78 };
79 
80 
81 // clang-format off
82 static const std::vector<COURTYARD_INVALID_CASE> courtyard_invalid_cases =
83 {
84  {
85  // Empty board has no footprints to be invalid
86  "empty board",
87  {},
88  {},
89  },
90  {
91  "single footprint, no courtyard",
92  {
93  {
94  "U1",
95  {}, // Empty courtyard layer
96  { 0, 0 },
97  },
98  },
99  { // one error: the footprint has no courtyard
100  {
101  "U1",
103  },
104  },
105  },
106  {
107  "single footprint, unclosed courtyard",
108  {
109  {
110  "U1",
111  { // Unclosed polygon
112  { { 0, 0 }, { 0, Millimeter2iu( 10 ) } },
113  { { 0, Millimeter2iu( 10 ) }, { Millimeter2iu( 10 ), Millimeter2iu( 10 ) } },
114  },
115  { 0, 0 },
116  },
117  },
118  { // one error: the footprint has malformed courtyard
119  {
120  "U1",
122  },
123  },
124  },
125  {
126  "single footprint, disjoint courtyard",
127  {
128  {
129  "U1",
130  { // Unclosed polygon - two disjoint segments
131  { { 0, 0 }, { 0, Millimeter2iu( 10 ) } },
132  { { Millimeter2iu( 10 ), 0 }, { Millimeter2iu( 10 ), Millimeter2iu( 10 ) } },
133  },
134  { 0, 0 },
135  },
136  },
137  { // one error: the footprint has malformed courtyard
138  {
139  "U1",
141  },
142  },
143  },
144  {
145  "two footprints, one OK, one malformed",
146  {
147  {
148  "U1",
149  { // Closed polygon - triangle
150  {
151  { 0, 0 },
152  { 0, Millimeter2iu( 10 ) },
153  },
154  {
155  { 0, Millimeter2iu( 10 ) },
156  { Millimeter2iu( 10 ), Millimeter2iu( 10 ) }
157  },
158  {
159  { Millimeter2iu( 10 ), Millimeter2iu( 10 ) },
160  { 0, 0 }
161  },
162  },
163  { 0, 0 },
164  },
165  {
166  "U2",
167  { // Un-Closed polygon - one seg
168  {
169  { 0, 0 },
170  { 0, Millimeter2iu( 10 ) },
171  },
172  },
173  { 0, 0 },
174  },
175  },
176  { // one error: the second footprint has malformed courtyard
177  {
178  "U2",
180  },
181  },
182  },
183 };
184 // clang-format on
185 
186 
191 std::unique_ptr<FOOTPRINT> MakeInvalidCourtyardTestFP( BOARD& aBoard,
192  const COURTYARD_INVALID_TEST_FP& aFPDef )
193 {
194  std::unique_ptr<FOOTPRINT> footprint = std::make_unique<FOOTPRINT>( &aBoard );
195 
196  for( const SEG& seg : aFPDef.m_segs )
197  {
198  const PCB_LAYER_ID layer = F_CrtYd; // aRect.m_front ? F_CrtYd : B_CrtYd;
199  const int width = Millimeter2iu( 0.1 );
200 
201  KI_TEST::DrawSegment( *footprint, seg, width, layer );
202  }
203 
204  footprint->SetReference( aFPDef.m_refdes );
205 
206  // As of 2019-01-17, this has to go after adding the courtyards,
207  // or all the poly sets are empty when DRC'd
208  footprint->SetPosition( (wxPoint) aFPDef.m_pos );
209 
210  return footprint;
211 }
212 
213 
214 std::unique_ptr<BOARD> MakeBoard( const std::vector<COURTYARD_INVALID_TEST_FP>& aTestFPDefs )
215 {
216  auto board = std::make_unique<BOARD>();
217 
218  for( const COURTYARD_INVALID_TEST_FP& fpDef : aTestFPDefs )
219  {
220  std::unique_ptr<FOOTPRINT> footprint = MakeInvalidCourtyardTestFP( *board, fpDef );
221 
222  board->Add( footprint.release() );
223  }
224 
225  return board;
226 }
227 
228 
232 static bool InvalidMatchesExpected( BOARD& aBoard, const PCB_MARKER& aMarker,
233  const COURTYARD_INVALID_INFO& aInvalid )
234 {
235  auto reporter = std::static_pointer_cast<DRC_ITEM>( aMarker.GetRCItem() );
236  const FOOTPRINT* item_a = dynamic_cast<FOOTPRINT*>( aBoard.GetItem( reporter->GetMainItemID() ) );
237 
238  // This one is more than just a mis-match!
239  if( reporter->GetAuxItemID() != niluuid )
240  {
241  BOOST_WARN_MESSAGE( false, "Expected no auxiliary item for invalid courtyard DRC." );
242  return false;
243  }
244 
245  if( item_a->GetReference() != aInvalid.m_refdes )
246  return false;
247 
248  if( reporter->GetErrorCode() != aInvalid.m_drc_error_code )
249  return false;
250 
251  return true;
252 }
253 
254 
262 static void CheckInvalidsMatchExpected( BOARD& aBoard,
263  const std::vector<std::unique_ptr<PCB_MARKER>>& aMarkers,
264  const std::vector<COURTYARD_INVALID_INFO>& aExpInvalids )
265 {
266  KI_TEST::CheckUnorderedMatches( aExpInvalids, aMarkers,
267  [&]( const COURTYARD_INVALID_INFO& aInvalid,
268  const std::unique_ptr<PCB_MARKER>& aMarker )
269  {
270  return InvalidMatchesExpected( aBoard, *aMarker, aInvalid );
271  } );
272 }
273 
274 
276  const KI_TEST::BOARD_DUMPER& aDumper )
277 {
278  auto board = MakeBoard( aCase.m_mods );
279 
280  // Dump if env var set
281  aDumper.DumpBoardToFile( *board, aCase.m_case_name );
282 
283  BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
284 
285  // do the overlap tests - that's a different test, but if not set,
286  // the invalid courtyard checks don't run either
288 
289  // we will also check for missing courtyards here
291 
292  // list of markers to collect
293  std::vector<std::unique_ptr<PCB_MARKER>> markers;
294 
295  DRC_ENGINE drcEngine( board.get(), &board->GetDesignSettings() );
296 
297  drcEngine.InitEngine( wxFileName() );
298 
299  drcEngine.SetViolationHandler(
300  [&]( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )
301  {
302  if( aItem->GetErrorCode() == DRCE_OVERLAPPING_FOOTPRINTS
303  || aItem->GetErrorCode() == DRCE_MALFORMED_COURTYARD
304  || aItem->GetErrorCode() == DRCE_MISSING_COURTYARD )
305  {
306  markers.push_back( std::make_unique<PCB_MARKER>( aItem, aPos ) );
307  }
308  } );
309 
310  drcEngine.RunTests( EDA_UNITS::MILLIMETRES, true, false );
311 
312  CheckInvalidsMatchExpected( *board, markers, aCase.m_exp_errors );
313 }
314 
315 
316 BOOST_AUTO_TEST_CASE( InvalidCases )
317 {
318  for( const auto& c : courtyard_invalid_cases )
319  {
320  BOOST_TEST_CONTEXT( c.m_case_name )
321  {
322  DoCourtyardInvalidTest( c, m_dumper );
323  }
324  }
325 }
326 
327 BOOST_AUTO_TEST_SUITE_END()
BOARD_ITEM * GetItem(const KIID &aID) const
Definition: board.cpp:813
Design Rule Checker object that performs all the DRC tests.
Definition: drc_engine.h:80
std::vector< SEG > m_segs
Footprint Ref-Des (for identifying DRC errors)
std::vector< COURTYARD_INVALID_TEST_FP > m_mods
void DrawSegment(FOOTPRINT &aFootprint, const SEG &aSeg, int aWidth, PCB_LAYER_ID aLayer)
Draw a segment in the given footprint.
KIID niluuid(0)
static const std::vector< COURTYARD_INVALID_CASE > courtyard_invalid_cases
void DumpBoardToFile(BOARD &aBoard, const std::string &aName) const
Construction utilities for PCB tests.
const KI_TEST::BOARD_DUMPER m_dumper
PCB_LAYER_ID
A quick note on layer IDs:
std::vector< COURTYARD_INVALID_INFO > m_exp_errors
std::unique_ptr< BOARD > MakeBoard(const std::vector< COURTYARD_INVALID_TEST_FP > &aTestFPDefs)
Functions to provide common constants and other functions to assist in making a consistent UI.
VECTOR2I m_pos
List of segments that will be placed on the courtyard.
static void CheckInvalidsMatchExpected(BOARD &aBoard, const std::vector< std::unique_ptr< PCB_MARKER >> &aMarkers, const std::vector< COURTYARD_INVALID_INFO > &aExpInvalids)
Check that the produced markers match the expected.
void CheckUnorderedMatches(const EXP_CONT &aExpected, const FOUND_CONT &aFound, MATCH_PRED aMatchPredicate)
Check that a container of "found" objects matches a container of "expected" objects.
#define BOOST_TEST_CONTEXT(A)
void DoCourtyardInvalidTest(const COURTYARD_INVALID_CASE &aCase, const KI_TEST::BOARD_DUMPER &aDumper)
Definition: seg.h:41
std::unique_ptr< FOOTPRINT > MakeInvalidCourtyardTestFP(BOARD &aBoard, const COURTYARD_INVALID_TEST_FP &aFPDef)
Construct a FOOTPRINT to use in a courtyard test from a COURTYARD_TEST_FP definition.
BOOST_AUTO_TEST_CASE(InvalidCases)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
std::map< int, SEVERITY > m_DRCSeverities
General utilities for PCB file IO for QA programs.
static constexpr int Millimeter2iu(double mm)
A helper that contains logic to assist in dumping boards to disk depending on some environment variab...
std::shared_ptr< RC_ITEM > GetRCItem() const
Definition: marker_base.h:100
void InitEngine(const wxFileName &aRulePath)
Initializes the DRC engine.
Definition: drc_engine.cpp:607
Container for design settings for a BOARD object.
static bool InvalidMatchesExpected(BOARD &aBoard, const PCB_MARKER &aMarker, const COURTYARD_INVALID_INFO &aInvalid)
Check if a PCB_MARKER is described by a particular COURTYARD_INVALID_INFO object.
std::ostream & operator<<(std::ostream &os, const COURTYARD_INVALID_INFO &aInvalid)