KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_resolve_drivers.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, 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
26#include <connection_graph.h>
27#include <sch_label.h>
28#include <sch_pin.h>
29#include <sch_symbol.h>
30#include <sch_sheet.h>
31#include <sch_sheet_pin.h>
32#include <lib_symbol.h>
33#include <vector>
34
36{
37public:
39 subgraph( &graph )
40 {}
41
43 {
44 for( SCH_ITEM* item : looseItems )
45 delete item;
46
47 for( SCH_SYMBOL* sym : symbols )
48 delete sym;
49
50 for( LIB_SYMBOL* lib : libs )
51 delete lib;
52 }
53
54 SCH_LABEL* MakeLabel( const wxString& aText )
55 {
56 SCH_LABEL* label = new SCH_LABEL( VECTOR2I( 0, 0 ), aText );
58 conn->ConfigureFromLabel( aText );
59 looseItems.push_back( label );
60 return label;
61 }
62
63 SCH_PIN* MakePin( LIB_SYMBOL* aLib, ELECTRICAL_PINTYPE aType, const wxString& aRef )
64 {
65 SCH_PIN* libPin = new SCH_PIN( aLib );
66 libPin->SetNumber( "1" );
67 libPin->SetName( "P" );
68 libPin->SetType( aType );
69 libPin->SetPosition( VECTOR2I( 0, 0 ) );
70 aLib->AddDrawItem( libPin );
71
72 SCH_SYMBOL* symbol = new SCH_SYMBOL( *aLib, aLib->GetLibId(), &sheetPath, 0, 0, VECTOR2I( 0, 0 ) );
73 symbol->SetRef( &sheetPath, aRef );
74 symbol->UpdatePins();
75 symbols.push_back( symbol );
76
77 SCH_PIN* pin = symbol->GetPins( &sheetPath )[0];
78 SCH_CONNECTION* conn = pin->GetOrInitConnection( sheetPath, &graph );
79 conn->ConfigureFromLabel( "NET" );
80 return pin;
81 }
82
84 {
85 LIB_SYMBOL* lib = new LIB_SYMBOL( wxS( "G_PWR" ), nullptr );
86 lib->SetGlobalPower();
87 libs.push_back( lib );
88 return MakePin( lib, ELECTRICAL_PINTYPE::PT_POWER_IN, wxS( "PWR1" ) );
89 }
90
92 {
93 LIB_SYMBOL* lib = new LIB_SYMBOL( wxS( "L_PWR" ), nullptr );
94 lib->SetLocalPower();
95 libs.push_back( lib );
96 return MakePin( lib, ELECTRICAL_PINTYPE::PT_POWER_IN, wxS( "PWR2" ) );
97 }
98
100 {
101 LIB_SYMBOL* lib = new LIB_SYMBOL( wxS( "REG" ), nullptr );
102 lib->SetNormal();
103 libs.push_back( lib );
104 return MakePin( lib, ELECTRICAL_PINTYPE::PT_OUTPUT, wxS( "U1" ) );
105 }
106
107 SCH_SHEET_PIN* MakeSheetPin( const wxString& aText, LABEL_FLAG_SHAPE aShape )
108 {
110 pin->SetText( aText );
111 pin->SetShape( aShape );
113 SCH_CONNECTION* conn = pin->GetOrInitConnection( sheetPath, &graph );
114 conn->ConfigureFromLabel( aText );
115 looseItems.push_back( pin );
116 return pin;
117 }
118
123
124 std::vector<LIB_SYMBOL*> libs;
125 std::vector<SCH_SYMBOL*> symbols;
126 std::vector<SCH_ITEM*> looseItems;
127};
128
130{
131 SCH_LABEL* subset = MakeLabel( wxS( "BUS[1..3]" ) );
132 SCH_LABEL* superset = MakeLabel( wxS( "BUS[1..4]" ) );
133
134 subgraph.AddItem( subset );
135 subgraph.AddItem( superset );
136
137 subgraph.ResolveDrivers( false );
138 BOOST_CHECK_EQUAL( subgraph.GetDriver(), superset );
139 subgraph.RemoveItem( subset );
140
141 // Check that the superset is still the driver after removing the subset
142 subgraph.ResolveDrivers( false );
143 BOOST_CHECK_EQUAL( subgraph.GetDriver(), superset );
144 subgraph.RemoveItem( superset );
145
146 // Check group labels as well
147 subset = MakeLabel( wxS( "BUS{ ONE TWO THREE }" ) );
148 superset = MakeLabel( wxS( "BUS{ ONE TWO THREE FOUR }" ) );
149
150 subgraph.AddItem( subset );
151 subgraph.AddItem( superset );
152
153 subgraph.ResolveDrivers( false );
154 BOOST_CHECK_EQUAL( subgraph.GetDriver(), superset );
155}
156
158{
159 SCH_PIN* regular = MakeRegularPin();
160 subgraph.AddItem( regular );
161 subgraph.ResolveDrivers( false );
162 BOOST_CHECK_EQUAL( subgraph.GetDriver(), regular );
163
164 SCH_PIN* local = MakeLocalPowerPin();
165 subgraph.AddItem( local );
166 subgraph.ResolveDrivers( false );
167 BOOST_CHECK_EQUAL( subgraph.GetDriver(), local );
168
169 SCH_PIN* global = MakeGlobalPowerPin();
170 subgraph.AddItem( global );
171 subgraph.ResolveDrivers( false );
172 BOOST_CHECK_EQUAL( subgraph.GetDriver(), global );
173}
174
176{
177 SCH_LABEL* good = MakeLabel( wxS( "VCC" ) );
178 SCH_LABEL* bad = MakeLabel( wxS( "Net-Pad1" ) );
179
180 subgraph.AddItem( good );
181 subgraph.AddItem( bad );
182
183 subgraph.ResolveDrivers( false );
184 BOOST_CHECK_EQUAL( subgraph.GetDriver(), good );
185}
186
188{
189 SCH_LABEL* aaa = MakeLabel( wxS( "AAA" ) );
190 SCH_LABEL* bbb = MakeLabel( wxS( "BBB" ) );
191
192 subgraph.AddItem( aaa );
193 subgraph.AddItem( bbb );
194
195 subgraph.ResolveDrivers( false );
196 BOOST_CHECK_EQUAL( subgraph.GetDriver(), aaa );
197}
198
200{
201 SCH_SHEET_PIN* input = MakeSheetPin( wxS( "IN" ), LABEL_FLAG_SHAPE::L_INPUT );
202 SCH_SHEET_PIN* output = MakeSheetPin( wxS( "OUT" ), LABEL_FLAG_SHAPE::L_OUTPUT );
203
204 subgraph.AddItem( input );
205 subgraph.AddItem( output );
206
207 subgraph.ResolveDrivers( false );
208 BOOST_CHECK_EQUAL( subgraph.GetDriver(), output );
209}
210
Calculate the connectivity of a schematic and generates netlists.
A subgraph is a set of items that are electrically connected on a single sheet.
Define a library symbol object.
Definition: lib_symbol.h:85
const LIB_ID & GetLibId() const override
Definition: lib_symbol.h:155
void SetGlobalPower()
Definition: lib_symbol.cpp:475
void SetLocalPower()
Definition: lib_symbol.cpp:439
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Definition: lib_symbol.cpp:785
void SetNormal()
Definition: lib_symbol.cpp:503
SCH_LABEL * MakeLabel(const wxString &aText)
SCH_PIN * MakePin(LIB_SYMBOL *aLib, ELECTRICAL_PINTYPE aType, const wxString &aRef)
std::vector< SCH_SYMBOL * > symbols
std::vector< SCH_ITEM * > looseItems
std::vector< LIB_SYMBOL * > libs
SCH_SHEET_PIN * MakeSheetPin(const wxString &aText, LABEL_FLAG_SHAPE aShape)
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
void ConfigureFromLabel(const wxString &aLabel)
Configures the connection given a label.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
SCH_CONNECTION * GetOrInitConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Definition: sch_item.cpp:457
void SetNumber(const wxString &aNumber)
Definition: sch_pin.cpp:541
void SetName(const wxString &aName)
Definition: sch_pin.cpp:375
void SetPosition(const VECTOR2I &aPos) override
Definition: sch_pin.h:224
void SetType(ELECTRICAL_PINTYPE aType)
Definition: sch_pin.cpp:289
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:66
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:47
Schematic symbol object.
Definition: sch_symbol.h:75
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
Definition: sch_symbol.cpp:288
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
Definition: sch_symbol.cpp:600
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:36
LABEL_FLAG_SHAPE
Definition: sch_label.h:99
@ L_OUTPUT
Definition: sch_label.h:101
@ L_INPUT
Definition: sch_label.h:100
BOOST_CHECK_EQUAL(ret, c.m_exp_result)
BOOST_FIXTURE_TEST_CASE(BusSupersetPreference, RESOLVE_DRIVERS_FIXTURE)
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695