KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue23058_jumper_pin_groups.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 3
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 * https://www.gnu.org/licenses/gpl-3.0.en.html
19 * or you may search the http://www.gnu.org website for the version 32 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
30
33
34#include <connection_graph.h>
35#include <schematic.h>
36#include <sch_sheet.h>
37#include <sch_screen.h>
39#include <lib_symbol.h>
40#include <sch_pin.h>
41#include <sch_symbol.h>
42#include <locale_io.h>
43
45{
47 {
48 m_settingsManager.LoadProject( "" );
49 m_schematic = std::make_unique<SCHEMATIC>( &m_settingsManager.Prj() );
50 m_schematic->Reset();
51 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
52
53 SCH_SHEET* root = new SCH_SHEET( m_schematic.get() );
54 SCH_SCREEN* screen = new SCH_SCREEN( m_schematic.get() );
55 root->SetScreen( screen );
56
57 m_schematic->AddTopLevelSheet( root );
58 m_schematic->RemoveTopLevelSheet( defaultSheet );
59 delete defaultSheet;
60 }
61
63 std::unique_ptr<SCHEMATIC> m_schematic;
64};
65
66
68{
69 SCH_SCREEN* screen = m_schematic->RootScreen();
71 path.push_back( &m_schematic->Root() );
72
73 LIB_SYMBOL* libSym = new LIB_SYMBOL( "SW_SPST", nullptr );
74
75 SCH_PIN* pin1 = new SCH_PIN( libSym );
76 pin1->SetNumber( "1" );
78 pin1->SetPosition( VECTOR2I( -508000, 0 ) );
79 libSym->AddDrawItem( pin1 );
80
81 SCH_PIN* pin2 = new SCH_PIN( libSym );
82 pin2->SetNumber( "2" );
84 pin2->SetPosition( VECTOR2I( 508000, 0 ) );
85 libSym->AddDrawItem( pin2 );
86
87 // Add a jumper pin group that references non-existent pin "3"
88 std::set<wxString> group;
89 group.insert( wxT( "1" ) );
90 group.insert( wxT( "3" ) );
91 libSym->JumperPinGroups().push_back( group );
92
93 SCH_SYMBOL* sym = new SCH_SYMBOL( *libSym, libSym->GetLibId(), &path, 0, 0,
94 VECTOR2I( 15621000, 6223000 ) );
95 sym->UpdatePins();
96 screen->Append( sym );
97
98 // This must not crash even though pin "3" doesn't exist
99 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
100 BOOST_CHECK_NO_THROW( m_schematic->ConnectionGraph()->Recalculate( sheets, true ) );
101
102 delete libSym;
103}
104
105
107{
108 SCH_SCREEN* screen = m_schematic->RootScreen();
110 path.push_back( &m_schematic->Root() );
111
112 LIB_SYMBOL* libSym = new LIB_SYMBOL( "SW_SPST", nullptr );
113
114 SCH_PIN* pin1 = new SCH_PIN( libSym );
115 pin1->SetNumber( "1" );
117 pin1->SetPosition( VECTOR2I( -508000, 0 ) );
118 libSym->AddDrawItem( pin1 );
119
120 // Add a jumper pin group where ALL pin numbers are invalid
121 std::set<wxString> group;
122 group.insert( wxT( "5" ) );
123 group.insert( wxT( "6" ) );
124 libSym->JumperPinGroups().push_back( group );
125
126 SCH_SYMBOL* sym = new SCH_SYMBOL( *libSym, libSym->GetLibId(), &path, 0, 0,
127 VECTOR2I( 15621000, 6223000 ) );
128 sym->UpdatePins();
129 screen->Append( sym );
130
131 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
132 BOOST_CHECK_NO_THROW( m_schematic->ConnectionGraph()->Recalculate( sheets, true ) );
133
134 delete libSym;
135}
136
137
139{
140 SCH_SCREEN* screen = m_schematic->RootScreen();
142 path.push_back( &m_schematic->Root() );
143
144 LIB_SYMBOL* libSym = new LIB_SYMBOL( "SW_SPDT", nullptr );
145
146 SCH_PIN* pin1 = new SCH_PIN( libSym );
147 pin1->SetNumber( "1" );
149 pin1->SetPosition( VECTOR2I( -508000, 0 ) );
150 libSym->AddDrawItem( pin1 );
151
152 SCH_PIN* pin2 = new SCH_PIN( libSym );
153 pin2->SetNumber( "2" );
155 pin2->SetPosition( VECTOR2I( 508000, 0 ) );
156 libSym->AddDrawItem( pin2 );
157
158 SCH_PIN* pin3 = new SCH_PIN( libSym );
159 pin3->SetNumber( "3" );
161 pin3->SetPosition( VECTOR2I( 508000, 254000 ) );
162 libSym->AddDrawItem( pin3 );
163
164 // Add a valid jumper pin group
165 std::set<wxString> group;
166 group.insert( wxT( "1" ) );
167 group.insert( wxT( "3" ) );
168 libSym->JumperPinGroups().push_back( group );
169
170 SCH_SYMBOL* sym = new SCH_SYMBOL( *libSym, libSym->GetLibId(), &path, 0, 0,
171 VECTOR2I( 15621000, 6223000 ) );
172 sym->UpdatePins();
173 screen->Append( sym );
174
175 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
176 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
177
178 path = sheets[0];
179
180 // Verify that pins 1 and 3 are connected via the jumper group
181 SCH_PIN* schPin1 = sym->GetPin( wxT( "1" ) );
182 SCH_PIN* schPin3 = sym->GetPin( wxT( "3" ) );
183
184 BOOST_REQUIRE( schPin1 );
185 BOOST_REQUIRE( schPin3 );
186
187 bool connected = false;
188
189 for( SCH_ITEM* item : schPin1->ConnectedItems( path ) )
190 {
191 if( item == schPin3 )
192 {
193 connected = true;
194 break;
195 }
196 }
197
198 BOOST_CHECK_MESSAGE( connected, "Pins 1 and 3 should be connected via jumper pin group" );
199
200 delete libSym;
201}
202
203
205{
207
208 KI_TEST::LoadSchematic( m_settingsManager, "issue23058/issue23058", m_schematic );
209
210 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
211
212 // This must not crash. The schematic has a jumper pin group referencing
213 // pin "3" but the symbol only has pins "1" and "2".
214 BOOST_CHECK_NO_THROW( m_schematic->ConnectionGraph()->Recalculate( sheets, true ) );
215}
Define a library symbol object.
Definition lib_symbol.h:83
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:152
std::vector< std::set< wxString > > & JumperPinGroups()
Each jumper pin group is a set of pin numbers that should be treated as internally connected.
Definition lib_symbol.h:760
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
const SCH_ITEM_VEC & ConnectedItems(const SCH_SHEET_PATH &aPath)
Retrieve the set of items connected to this item on the given sheet.
Definition sch_item.cpp:539
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:644
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:251
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:335
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Schematic symbol object.
Definition sch_symbol.h:76
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
SCH_PIN * GetPin(const wxString &number) const
Find a symbol pin by number.
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:43
std::vector< FAB_LAYER_COLOR > dummy
Test for issue #23058: Jumper pin groups with out-of-bounds pin numbers cause crash.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_FIXTURE_TEST_CASE(JumperPinGroupOutOfBounds, JUMPER_PIN_GROUP_FIXTURE)
std::string path
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695