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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <connection_graph.h>
23#include <sch_label.h>
24#include <sch_pin.h>
25#include <sch_symbol.h>
26#include <sch_sheet.h>
27#include <sch_sheet_pin.h>
28#include <lib_symbol.h>
29#include <vector>
30
32{
33public:
37
39 {
40 for( SCH_ITEM* item : looseItems )
41 delete item;
42
43 for( SCH_SYMBOL* sym : symbols )
44 delete sym;
45
46 for( LIB_SYMBOL* lib : libs )
47 delete lib;
48 }
49
50 SCH_LABEL* MakeLabel( const wxString& aText )
51 {
52 SCH_LABEL* label = new SCH_LABEL( VECTOR2I( 0, 0 ), aText );
54 conn->ConfigureFromLabel( aText );
55 looseItems.push_back( label );
56 return label;
57 }
58
59 SCH_PIN* MakePin( LIB_SYMBOL* aLib, ELECTRICAL_PINTYPE aType, const wxString& aRef )
60 {
61 SCH_PIN* libPin = new SCH_PIN( aLib );
62 libPin->SetNumber( "1" );
63 libPin->SetName( "P" );
64 libPin->SetType( aType );
65 libPin->SetPosition( VECTOR2I( 0, 0 ) );
66 aLib->AddDrawItem( libPin );
67
68 SCH_SYMBOL* symbol = new SCH_SYMBOL( *aLib, aLib->GetLibId(), &sheetPath, 0, 0, VECTOR2I( 0, 0 ) );
69 symbol->SetRef( &sheetPath, aRef );
70 symbol->UpdatePins();
71 symbols.push_back( symbol );
72
73 SCH_PIN* pin = symbol->GetPins( &sheetPath )[0];
74 SCH_CONNECTION* conn = pin->GetOrInitConnection( sheetPath, &graph );
75 conn->ConfigureFromLabel( "NET" );
76 return pin;
77 }
78
80 {
81 LIB_SYMBOL* lib = new LIB_SYMBOL( wxS( "G_PWR" ), nullptr );
82 lib->SetGlobalPower();
83 libs.push_back( lib );
84 return MakePin( lib, ELECTRICAL_PINTYPE::PT_POWER_IN, wxS( "PWR1" ) );
85 }
86
88 {
89 LIB_SYMBOL* lib = new LIB_SYMBOL( wxS( "L_PWR" ), nullptr );
90 lib->SetLocalPower();
91 libs.push_back( lib );
92 return MakePin( lib, ELECTRICAL_PINTYPE::PT_POWER_IN, wxS( "PWR2" ) );
93 }
94
96 {
97 LIB_SYMBOL* lib = new LIB_SYMBOL( wxS( "REG" ), nullptr );
98 lib->SetNormal();
99 libs.push_back( lib );
100 return MakePin( lib, ELECTRICAL_PINTYPE::PT_OUTPUT, wxS( "U1" ) );
101 }
102
103 SCH_SHEET_PIN* MakeSheetPin( const wxString& aText, LABEL_FLAG_SHAPE aShape )
104 {
106 pin->SetText( aText );
107 pin->SetShape( aShape );
109 SCH_CONNECTION* conn = pin->GetOrInitConnection( sheetPath, &graph );
110 conn->ConfigureFromLabel( aText );
111 looseItems.push_back( pin );
112 return pin;
113 }
114
119
120 std::vector<LIB_SYMBOL*> libs;
121 std::vector<SCH_SYMBOL*> symbols;
122 std::vector<SCH_ITEM*> looseItems;
123};
124
126{
127 SCH_LABEL* subset = MakeLabel( wxS( "BUS[1..3]" ) );
128 SCH_LABEL* superset = MakeLabel( wxS( "BUS[1..4]" ) );
129
130 subgraph.AddItem( subset );
131 subgraph.AddItem( superset );
132
133 subgraph.ResolveDrivers( false );
134 BOOST_CHECK_EQUAL( subgraph.GetDriver(), superset );
135 subgraph.RemoveItem( subset );
136
137 // Check that the superset is still the driver after removing the subset
138 subgraph.ResolveDrivers( false );
139 BOOST_CHECK_EQUAL( subgraph.GetDriver(), superset );
140 subgraph.RemoveItem( superset );
141
142 // Check group labels as well
143 subset = MakeLabel( wxS( "BUS{ ONE TWO THREE }" ) );
144 superset = MakeLabel( wxS( "BUS{ ONE TWO THREE FOUR }" ) );
145
146 subgraph.AddItem( subset );
147 subgraph.AddItem( superset );
148
149 subgraph.ResolveDrivers( false );
150 BOOST_CHECK_EQUAL( subgraph.GetDriver(), superset );
151}
152
154{
155 SCH_PIN* regular = MakeRegularPin();
156 subgraph.AddItem( regular );
157 subgraph.ResolveDrivers( false );
158 BOOST_CHECK_EQUAL( subgraph.GetDriver(), regular );
159
160 SCH_PIN* local = MakeLocalPowerPin();
161 subgraph.AddItem( local );
162 subgraph.ResolveDrivers( false );
163 BOOST_CHECK_EQUAL( subgraph.GetDriver(), local );
164
165 SCH_PIN* global = MakeGlobalPowerPin();
166 subgraph.AddItem( global );
167 subgraph.ResolveDrivers( false );
168 BOOST_CHECK_EQUAL( subgraph.GetDriver(), global );
169}
170
172{
173 SCH_LABEL* good = MakeLabel( wxS( "VCC" ) );
174 SCH_LABEL* bad = MakeLabel( wxS( "Net-Pad1" ) );
175
176 subgraph.AddItem( good );
177 subgraph.AddItem( bad );
178
179 subgraph.ResolveDrivers( false );
180 BOOST_CHECK_EQUAL( subgraph.GetDriver(), good );
181}
182
184{
185 SCH_LABEL* aaa = MakeLabel( wxS( "AAA" ) );
186 SCH_LABEL* bbb = MakeLabel( wxS( "BBB" ) );
187
188 subgraph.AddItem( aaa );
189 subgraph.AddItem( bbb );
190
191 subgraph.ResolveDrivers( false );
192 BOOST_CHECK_EQUAL( subgraph.GetDriver(), aaa );
193}
194
196{
197 SCH_SHEET_PIN* input = MakeSheetPin( wxS( "IN" ), LABEL_FLAG_SHAPE::L_INPUT );
198 SCH_SHEET_PIN* output = MakeSheetPin( wxS( "OUT" ), LABEL_FLAG_SHAPE::L_OUTPUT );
199
200 subgraph.AddItem( input );
201 subgraph.AddItem( output );
202
203 subgraph.ResolveDrivers( false );
204 BOOST_CHECK_EQUAL( subgraph.GetDriver(), output );
205}
206
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:79
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:148
void SetGlobalPower()
void SetLocalPower()
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
void SetNormal()
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:162
SCH_CONNECTION * GetOrInitConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Definition sch_item.cpp:604
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:734
void SetName(const wxString &aName)
Definition sch_pin.cpp:512
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:254
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:422
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.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
Schematic symbol object.
Definition sch_symbol.h:69
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
@ PT_OUTPUT
usual output
Definition pin_type.h:34
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:42
LABEL_FLAG_SHAPE
Definition sch_label.h:97
@ L_OUTPUT
Definition sch_label.h:99
@ L_INPUT
Definition sch_label.h:98
KIBIS_PIN * pin
nlohmann::json output
BOOST_FIXTURE_TEST_CASE(BusSupersetPreference, RESOLVE_DRIVERS_FIXTURE)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683