KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue24107_multiunit_grouping.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
33
34#include <boost/test/unit_test.hpp>
35
36#include <lib_id.h>
37#include <lib_symbol.h>
38#include <sch_reference_list.h>
39#include <sch_screen.h>
40#include <sch_sheet.h>
41#include <sch_sheet_path.h>
42#include <sch_symbol.h>
43#include <schematic.h>
46
48
49#include <wx/filename.h>
50#include <wx/stdpaths.h>
51
52
54{
56 {
57 wxString tempDir = wxStandardPaths::Get().GetTempDir();
58 wxString projectPath = tempDir + wxFileName::GetPathSeparator()
59 + wxT( "test_issue24107.kicad_pro" );
60 m_tempFiles.push_back( projectPath );
61
62 m_settingsManager.LoadProject( projectPath.ToStdString() );
63 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
65 m_schematic->SetProject( m_project );
66 m_schematic->CreateDefaultScreens();
67 }
68
70 {
71 for( const wxString& file : m_tempFiles )
72 {
73 if( wxFileExists( file ) )
74 wxRemoveFile( file );
75 }
76
77 m_schematic.reset();
78 }
79
83 std::shared_ptr<LIB_SYMBOL> makeMultiUnitLib( const wxString& aLibItemName, int aUnitCount,
84 const wxString& aValue )
85 {
86 auto lib = std::make_shared<LIB_SYMBOL>( aLibItemName );
87 lib->SetUnitCount( aUnitCount, false );
88 lib->GetReferenceField().SetText( wxT( "U" ) );
89 lib->GetValueField().SetText( aValue );
90
91 LIB_ID id;
92 id.SetLibNickname( wxT( "TestLib" ) );
93 id.SetLibItemName( aLibItemName );
94 lib->SetLibId( id );
95
96 return lib;
97 }
98
103 SCH_SYMBOL* placeUnit( const std::shared_ptr<LIB_SYMBOL>& aLib, int aUnit,
104 const VECTOR2I& aPos )
105 {
106 SCH_SHEET* rootSheet = m_schematic->GetTopLevelSheets()[0];
107 SCH_SHEET_PATH sheetPath;
108 sheetPath.push_back( rootSheet );
109
110 SCH_SYMBOL* symbol = new SCH_SYMBOL( *aLib, aLib->GetLibId(), &sheetPath, aUnit, 0,
111 aPos, m_schematic.get() );
112 symbol->SetUnit( aUnit );
113 symbol->SetUnitSelection( &sheetPath, aUnit );
114
115 rootSheet->GetScreen()->Append( symbol );
116
117 return symbol;
118 }
119
121 std::unique_ptr<SCHEMATIC> m_schematic;
123 std::vector<wxString> m_tempFiles;
124};
125
126
127BOOST_FIXTURE_TEST_SUITE( Issue24107MultiUnitGrouping, ISSUE24107_FIXTURE )
128
129
130
142BOOST_AUTO_TEST_CASE( UnplacedUnitsAreScopedToLibraryIdentity )
143{
144 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
145 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
146
147 placeUnit( ad8620Lib, 1, VECTOR2I( 0, 0 ) );
148 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 0 ) );
149 placeUnit( ad8620Lib, 3, VECTOR2I( 2000000, 0 ) );
150
151 SCH_SYMBOL* opa = placeUnit( opa1664Lib, 1, VECTOR2I( 0, 1000000 ) );
152
153 std::set<int> missing = GetUnplacedUnitsForSymbol( *opa );
154
155 // Units 2, 3 and 4 of OPA1664 itself have not been placed.
156 BOOST_CHECK_EQUAL( missing.size(), 3 );
157 BOOST_CHECK( missing.count( 1 ) == 0 );
158 BOOST_CHECK( missing.count( 2 ) == 1 );
159 BOOST_CHECK( missing.count( 3 ) == 1 );
160 BOOST_CHECK( missing.count( 4 ) == 1 );
161}
162
163
169BOOST_AUTO_TEST_CASE( UnannotatedUnitsFromOtherLibraryAreIgnored )
170{
171 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
172 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
173
174 SCH_SYMBOL* opaA = placeUnit( opa1664Lib, 1, VECTOR2I( 0, 0 ) );
175
176 placeUnit( ad8620Lib, 1, VECTOR2I( 0, 1000000 ) );
177 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 1000000 ) );
178
179 std::set<int> missing = GetUnplacedUnitsForSymbol( *opaA );
180
181 BOOST_CHECK_EQUAL( missing.size(), 3 );
182 BOOST_CHECK( missing.count( 1 ) == 0 );
183 BOOST_CHECK( missing.count( 2 ) == 1 );
184 BOOST_CHECK( missing.count( 3 ) == 1 );
185 BOOST_CHECK( missing.count( 4 ) == 1 );
186}
187
188
194BOOST_AUTO_TEST_CASE( SameLibraryUnitsStillCount )
195{
196 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
197 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
198
199 SCH_SYMBOL* adA = placeUnit( ad8620Lib, 1, VECTOR2I( 0, 0 ) );
200 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 0 ) );
201 placeUnit( opa1664Lib, 1, VECTOR2I( 0, 1000000 ) );
202
203 std::set<int> missing = GetUnplacedUnitsForSymbol( *adA );
204
205 // Only AD8620 unit 3 remains unplaced.
206 BOOST_CHECK_EQUAL( missing.size(), 1 );
207 BOOST_CHECK( missing.count( 3 ) == 1 );
208}
209
210
217BOOST_AUTO_TEST_CASE( IsUnannotatedUnitOccupiedRespectsLibraryIdentity )
218{
219 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
220 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
221
222 placeUnit( ad8620Lib, 1, VECTOR2I( 0, 0 ) );
223 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 0 ) );
224 placeUnit( ad8620Lib, 3, VECTOR2I( 2000000, 0 ) );
225
226 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
228 hierarchy.GetSymbols( refs, SYMBOL_FILTER_ALL );
229
230 const wxString unannotatedRef = wxT( "U?" );
231
232 // OPA1664: every unit must report unoccupied even though the reference string matches
233 // AD8620 placements 1..3.
234 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 1 ) );
235 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 2 ) );
236 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 3 ) );
237 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 4 ) );
238
239 // AD8620: units 1..3 must report occupied.
240 BOOST_CHECK( IsUnannotatedUnitOccupied( refs, unannotatedRef, ad8620Lib->GetLibId(), 1 ) );
241 BOOST_CHECK( IsUnannotatedUnitOccupied( refs, unannotatedRef, ad8620Lib->GetLibId(), 2 ) );
242 BOOST_CHECK( IsUnannotatedUnitOccupied( refs, unannotatedRef, ad8620Lib->GetLibId(), 3 ) );
243}
244
245
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:96
Container for project specific data.
Definition project.h:62
virtual void SetUnit(int aUnit)
Definition sch_item.h:232
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetSymbols(SCH_REFERENCE_LIST &aReferences, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
Schematic symbol object.
Definition sch_symbol.h:69
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SYMBOL_FILTER_ALL
bool IsUnannotatedUnitOccupied(const SCH_REFERENCE_LIST &aRefs, const wxString &aRef, const LIB_ID &aLibId, int aUnit)
Decide whether aUnit of an unannotated multi-unit symbol is already placed.
std::set< int > GetUnplacedUnitsForSymbol(const SCH_SYMBOL &aSym)
Get a list of unplaced (i.e.
std::shared_ptr< LIB_SYMBOL > makeMultiUnitLib(const wxString &aLibItemName, int aUnitCount, const wxString &aValue)
Build a fresh multi-unit library symbol with the given lib name, value and unit count.
std::unique_ptr< SCHEMATIC > m_schematic
SCH_SYMBOL * placeUnit(const std::shared_ptr< LIB_SYMBOL > &aLib, int aUnit, const VECTOR2I &aPos)
Place a single unit of a multi-unit symbol in the schematic.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(UnplacedUnitsAreScopedToLibraryIdentity)
Reproduces the user-reported flow.
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683