KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_skew.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
21#include <qa_utils/file_utils.h>
23#include <board.h>
25#include <drc/drc_engine.h>
26#include <pad.h>
27#include <pcb_track.h>
28#include <pcb_marker.h>
29#include <footprint.h>
30#include <drc/drc_engine.h>
31#include <drc/drc_item.h>
33
34#include <filesystem>
35#include <fstream>
36
37
38namespace
39{
40struct DRC_REGRESSION_TEST_FIXTURE
41{
42 DRC_REGRESSION_TEST_FIXTURE()
43 { }
44
45 SETTINGS_MANAGER m_settingsManager;
46 std::unique_ptr<BOARD> m_board;
47};
48} // namespace
49
50
51BOOST_FIXTURE_TEST_CASE( DRCSkew, DRC_REGRESSION_TEST_FIXTURE )
52{
53 // Check for minimum copper connection errors
54
55 std::vector<std::pair<wxString, int>> tests =
56 {
57 { "skew_within_diff_pairs_drc", 2 },
58 { "skew_group_matched_drc", 5 }
59 };
60
61 for( const std::pair<wxString, int>& test : tests )
62 {
63 KI_TEST::LoadBoard( m_settingsManager, test.first, m_board );
64 KI_TEST::FillZones( m_board.get() );
65
66 std::vector<DRC_ITEM> violations;
67 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
68
69 // Disable DRC tests not useful or not handled in this testcase
76 // These DRC tests are not useful and do not work because they need a footprint library
77 // associated to the board
80
81 // Ensure that our desired error is fired
83
85 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
86 const std::function<void( PCB_MARKER* )>& aPathGenerator )
87 {
88 if( bds.GetSeverity( aItem->GetErrorCode() ) == SEVERITY::RPT_SEVERITY_ERROR )
89 violations.push_back( *aItem );
90 } );
91
92 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
93
94 if( violations.size() == test.second )
95 {
96 BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
97 BOOST_TEST_MESSAGE( wxString::Format( "DRC skew: %s, passed", test.first ) );
98 }
99 else
100 {
101 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::INCH );
102
103 BOARD_RC_ITEM_RESOLVER resolver( *m_board );
104
105 for( const DRC_ITEM& item : violations )
106 {
107 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR,
108 resolver ) );
109 }
110
111 BOOST_ERROR( wxString::Format( "DRC skew: %s, failed (violations found %d expected %d)",
112 test.first, (int)violations.size(), test.second ) );
113 }
114 }
115}
116
117
118static size_t runInlineSkewRule( SETTINGS_MANAGER& aSettingsManager, std::unique_ptr<BOARD>& aBoard,
119 const wxString& aMin, const wxString& aMax, const std::string& aTmpSubdir )
120{
121 namespace fs = std::filesystem;
122
123 KI_TEST::SCOPED_TEMP_DIR tmpDir( wxString::FromUTF8( aTmpSubdir ) );
124
125 fs::path druPath = tmpDir.Path() / "skew.kicad_dru";
126
127 {
128 std::ofstream druOut( druPath );
129 druOut << wxString::Format( "(version 1)\n"
130 "(rule \"skew\"\n"
131 " (constraint skew (min %smm) (max %smm) (within_diff_pairs))\n"
132 " (condition \"A.inDiffPair('*')\")\n"
133 ")\n",
134 aMin, aMax )
135 .ToStdString();
136 }
137
138 KI_TEST::LoadBoard( aSettingsManager, "diff_pair_uncoupled_tuning_drc", aBoard );
139
140 BOARD_DESIGN_SETTINGS& bds = aBoard->GetDesignSettings();
141
142 auto drcEngine = std::make_shared<DRC_ENGINE>( aBoard.get(), &bds );
143 wxFileName ruleFile( druPath.string() );
144 drcEngine->InitEngine( ruleFile );
145 bds.m_DRCEngine = drcEngine;
146
154
155 size_t violations = 0;
156
157 drcEngine->SetViolationHandler(
158 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I&, int,
159 const std::function<void( PCB_MARKER* )>& )
160 {
161 if( aItem->GetErrorCode() == DRCE_SKEW_OUT_OF_RANGE )
162 ++violations;
163 } );
164
165 drcEngine->RunTests( EDA_UNITS::MM, true, false );
166
167 std::error_code ec;
168 fs::remove( druPath, ec );
169
170 return violations;
171}
172
173
174// Skew is measured against the longest net of the pair, so that net is measured against itself
175// and reads exactly zero. The pair is skewed 0.0961 mm, which clears a 0.05 mm minimum.
176BOOST_FIXTURE_TEST_CASE( SkewMinimumNotTestedAgainstReferenceNet, DRC_REGRESSION_TEST_FIXTURE )
177{
178 size_t violations =
179 runInlineSkewRule( m_settingsManager, m_board, wxS( "0.05" ), wxS( "0.55" ), "kicad_drc_skew_min_ref" );
180
181 BOOST_CHECK_MESSAGE( violations == 0, "Expected no violation, got " << violations );
182}
183
184
185// The minimum must still be enforced on every other net, otherwise a deliberate non-zero skew
186// cannot be given a lower bound at all.
187BOOST_FIXTURE_TEST_CASE( SkewMinimumStillCatchesUnderSkewedPair, DRC_REGRESSION_TEST_FIXTURE )
188{
189 size_t violations =
190 runInlineSkewRule( m_settingsManager, m_board, wxS( "0.45" ), wxS( "0.55" ), "kicad_drc_skew_min_under" );
191
192 BOOST_CHECK_MESSAGE( violations == 1, "Expected one violation, got " << violations );
193}
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:349
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
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
Definition file_utils.h:61
@ DRCE_SKEW_OUT_OF_RANGE
Definition drc_item.h:122
@ DRCE_VIA_DIAMETER
Definition drc_item.h:70
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:48
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:96
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:86
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:69
@ DRCE_STARVED_THERMAL
Definition drc_item.h:58
@ DRCE_COPPER_SLIVER
Definition drc_item.h:107
@ DRCE_DANGLING_VIA
Definition drc_item.h:59
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:97
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
void FillZones(BOARD *m_board)
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
static size_t runInlineSkewRule(SETTINGS_MANAGER &aSettingsManager, std::unique_ptr< BOARD > &aBoard, const wxString &aMin, const wxString &aMax, const std::string &aTmpSubdir)
BOOST_FIXTURE_TEST_CASE(DRCSkew, DRC_REGRESSION_TEST_FIXTURE)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708