KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_return_path_reference_net.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#include <boost/test/unit_test.hpp>
21
23
24#include <filesystem>
25#include <fstream>
26
27#include <board.h>
29#include <drc/drc_engine.h>
30#include <drc/drc_item.h>
31#include <netinfo.h>
32#include <pcb_marker.h>
34
35
36// One F.Cu chain track with two B.Cu zones underneath: one on net "/GND" and
37// one on net "/VCC". Both zones cover the trunk fully so without a net
38// filter the check passes. With `(net "GND")` the GND zone alone qualifies
39// and the trunk is still covered. With `(net "PWR_*")` no zone qualifies and
40// the trunk is reported as uncovered.
41static const char* DUAL_ZONE_PCB = "net_chains/return_path_dual_zone.kicad_pcb";
42
43
44namespace
45{
46size_t runDrc( const std::string& aDru, const std::string& aSubdir )
47{
48 namespace fs = std::filesystem;
49 fs::path tmpDir = fs::temp_directory_path() / aSubdir;
50 fs::create_directories( tmpDir );
51 fs::path druPath = tmpDir / "rn.kicad_dru";
52
53 {
54 std::ofstream out( druPath );
55 out << aDru;
56 }
57
58 PCB_IO_KICAD_SEXPR plugin;
59 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
60 plugin.LoadBoard( KI_TEST::GetPcbnewTestDataDir() + DUAL_ZONE_PCB, board.get() );
61 board->BuildConnectivity();
62
63 NETINFO_ITEM* sig = board->FindNet( wxS( "/CHAIN_RN" ) );
64 BOOST_REQUIRE( sig );
65 sig->SetNetChain( wxS( "RN" ) );
66
67 BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
68 auto drcEngine = std::make_shared<DRC_ENGINE>( board.get(), &bds );
69 wxFileName ruleFile( druPath.string() );
70 drcEngine->InitEngine( ruleFile );
71 bds.m_DRCEngine = drcEngine;
72
80
81 size_t markerCount = 0;
82 drcEngine->SetViolationHandler(
83 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I&, int,
84 const std::function<void( PCB_MARKER* )>& )
85 {
86 if( aItem->GetErrorCode() == DRCE_NET_CHAIN_RETURN_PATH_BREAK )
87 markerCount++;
88 } );
89
90 drcEngine->RunTests( EDA_UNITS::MM, true, false );
91
92 fs::remove( druPath );
93 return markerCount;
94}
95}
96
97
98BOOST_AUTO_TEST_SUITE( DRCReturnPathReferenceNet )
99
100
101// Without (net …): both zones qualify, trunk fully covered → 0 markers.
102BOOST_AUTO_TEST_CASE( ReferenceNetEmptyAcceptsAnyZoneRegression )
103{
104 const std::string dru =
105 R"((version 1)
106(rule "RP"
107 (condition "A.NetClass == 'Default'")
108 (constraint length (min 0mm) (max 200mm))
109 (constraint return_path (layer "B.Cu"))
110)
111)";
112 BOOST_CHECK_EQUAL( runDrc( dru, "kicad_drc_rn_any" ), 0u );
113}
114
115
116// (net "GND") plus only-GND zone covering trunk → 0 markers.
117BOOST_AUTO_TEST_CASE( ReferenceNetGndShadowedByGndPasses )
118{
119 const std::string dru =
120 R"((version 1)
121(rule "RP"
122 (condition "A.NetClass == 'Default'")
123 (constraint length (min 0mm) (max 200mm))
124 (constraint return_path (layer "B.Cu") (net "GND"))
126)";
127 // The GND zone only covers the upper half of the track shadow (y < 0).
128 // The trunk runs at y=0 with width 0.2mm — partial coverage. The marker
129 // count depends on whether half-coverage triggers; just assert no false
130 // negative when the zone net matches at all.
131 size_t n = runDrc( dru, "kicad_drc_rn_gnd_match" );
132 BOOST_CHECK_LE( n, 1u );
133}
134
136// (net "PWR_*") matches no zone → trunk reported as uncovered.
137BOOST_AUTO_TEST_CASE( ReferenceNetWildcardNoMatch )
138{
139 const std::string dru =
140 R"((version 1)
141(rule "RP"
142 (condition "A.NetClass == 'Default'")
143 (constraint length (min 0mm) (max 200mm))
144 (constraint return_path (layer "B.Cu") (net "PWR_*"))
145)
146)";
147 size_t n = runDrc( dru, "kicad_drc_rn_wildcard_nomatch" );
148 BOOST_CHECK_GE( n, 1u );
149}
150
151
152// (net "GND*") matches the GND zone → covered (or partially covered).
153BOOST_AUTO_TEST_CASE( ReferenceNetWildcardMatch )
154{
155 const std::string dru =
156 R"((version 1)
157(rule "RP"
158 (condition "A.NetClass == 'Default'")
159 (constraint length (min 0mm) (max 200mm))
160 (constraint return_path (layer "B.Cu") (net "GND*"))
161)
162)";
163 size_t n = runDrc( dru, "kicad_drc_rn_wildcard_match" );
164 BOOST_CHECK_LE( n, 1u );
165}
166
167
General utilities for PCB file IO for QA programs.
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
Handle the data for a net.
Definition netinfo.h:46
void SetNetChain(const wxString &aNetChain)
Definition netinfo.h:113
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:37
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:80
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:70
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:58
@ DRCE_NET_CHAIN_RETURN_PATH_BREAK
Definition drc_item.h:104
@ DRCE_DANGLING_VIA
Definition drc_item.h:48
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:81
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
static const char * DUAL_ZONE_PCB
BOOST_AUTO_TEST_CASE(ReferenceNetEmptyAcceptsAnyZoneRegression)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683