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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 */
20
34
35#include <boost/test/unit_test.hpp>
36
37#include <lib_id.h>
38#include <lib_symbol.h>
39#include <sch_reference_list.h>
40#include <sch_screen.h>
41#include <sch_sheet.h>
42#include <sch_sheet_path.h>
43#include <sch_symbol.h>
44#include <schematic.h>
47
49
50#include <wx/filename.h>
51#include <wx/stdpaths.h>
52
53
55{
57 {
58 wxString tempDir = wxStandardPaths::Get().GetTempDir();
59 wxString projectPath = tempDir + wxFileName::GetPathSeparator()
60 + wxT( "test_issue24107.kicad_pro" );
61 m_tempFiles.push_back( projectPath );
62
63 m_settingsManager.LoadProject( projectPath.ToStdString() );
64 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
66 m_schematic->SetProject( m_project );
67 m_schematic->CreateDefaultScreens();
68 }
69
71 {
72 for( const wxString& file : m_tempFiles )
73 {
74 if( wxFileExists( file ) )
75 wxRemoveFile( file );
76 }
77
78 m_schematic.reset();
79 }
80
84 std::shared_ptr<LIB_SYMBOL> makeMultiUnitLib( const wxString& aLibItemName, int aUnitCount,
85 const wxString& aValue )
86 {
87 auto lib = std::make_shared<LIB_SYMBOL>( aLibItemName );
88 lib->SetUnitCount( aUnitCount, false );
89 lib->GetReferenceField().SetText( wxT( "U" ) );
90 lib->GetValueField().SetText( aValue );
91
92 LIB_ID id;
93 id.SetLibNickname( wxT( "TestLib" ) );
94 id.SetLibItemName( aLibItemName );
95 lib->SetLibId( id );
96
97 return lib;
98 }
99
104 SCH_SYMBOL* placeUnit( const std::shared_ptr<LIB_SYMBOL>& aLib, int aUnit,
105 const VECTOR2I& aPos )
106 {
107 SCH_SHEET* rootSheet = m_schematic->GetTopLevelSheets()[0];
108 SCH_SHEET_PATH sheetPath;
109 sheetPath.push_back( rootSheet );
110
111 SCH_SYMBOL* symbol = new SCH_SYMBOL( *aLib, aLib->GetLibId(), &sheetPath, aUnit, 0,
112 aPos, m_schematic.get() );
113 symbol->SetUnit( aUnit );
114 symbol->SetUnitSelection( &sheetPath, aUnit );
115
116 rootSheet->GetScreen()->Append( symbol );
117
118 return symbol;
119 }
120
122 std::unique_ptr<SCHEMATIC> m_schematic;
124 std::vector<wxString> m_tempFiles;
125};
126
127
128BOOST_FIXTURE_TEST_SUITE( Issue24107MultiUnitGrouping, ISSUE24107_FIXTURE )
129
130
131
143BOOST_AUTO_TEST_CASE( UnplacedUnitsAreScopedToLibraryIdentity )
144{
145 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
146 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
147
148 placeUnit( ad8620Lib, 1, VECTOR2I( 0, 0 ) );
149 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 0 ) );
150 placeUnit( ad8620Lib, 3, VECTOR2I( 2000000, 0 ) );
151
152 SCH_SYMBOL* opa = placeUnit( opa1664Lib, 1, VECTOR2I( 0, 1000000 ) );
153
154 std::set<int> missing = GetUnplacedUnitsForSymbol( *opa );
155
156 // Units 2, 3 and 4 of OPA1664 itself have not been placed.
157 BOOST_CHECK_EQUAL( missing.size(), 3 );
158 BOOST_CHECK( missing.count( 1 ) == 0 );
159 BOOST_CHECK( missing.count( 2 ) == 1 );
160 BOOST_CHECK( missing.count( 3 ) == 1 );
161 BOOST_CHECK( missing.count( 4 ) == 1 );
162}
163
164
170BOOST_AUTO_TEST_CASE( UnannotatedUnitsFromOtherLibraryAreIgnored )
171{
172 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
173 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
174
175 SCH_SYMBOL* opaA = placeUnit( opa1664Lib, 1, VECTOR2I( 0, 0 ) );
176
177 placeUnit( ad8620Lib, 1, VECTOR2I( 0, 1000000 ) );
178 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 1000000 ) );
179
180 std::set<int> missing = GetUnplacedUnitsForSymbol( *opaA );
181
182 BOOST_CHECK_EQUAL( missing.size(), 3 );
183 BOOST_CHECK( missing.count( 1 ) == 0 );
184 BOOST_CHECK( missing.count( 2 ) == 1 );
185 BOOST_CHECK( missing.count( 3 ) == 1 );
186 BOOST_CHECK( missing.count( 4 ) == 1 );
187}
188
189
195BOOST_AUTO_TEST_CASE( SameLibraryUnitsStillCount )
196{
197 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
198 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
199
200 SCH_SYMBOL* adA = placeUnit( ad8620Lib, 1, VECTOR2I( 0, 0 ) );
201 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 0 ) );
202 placeUnit( opa1664Lib, 1, VECTOR2I( 0, 1000000 ) );
203
204 std::set<int> missing = GetUnplacedUnitsForSymbol( *adA );
205
206 // Only AD8620 unit 3 remains unplaced.
207 BOOST_CHECK_EQUAL( missing.size(), 1 );
208 BOOST_CHECK( missing.count( 3 ) == 1 );
209}
210
211
218BOOST_AUTO_TEST_CASE( IsUnannotatedUnitOccupiedRespectsLibraryIdentity )
219{
220 auto ad8620Lib = makeMultiUnitLib( wxT( "AD8620" ), 3, wxT( "AD8620" ) );
221 auto opa1664Lib = makeMultiUnitLib( wxT( "OPA1664" ), 4, wxT( "OPA1664" ) );
222
223 placeUnit( ad8620Lib, 1, VECTOR2I( 0, 0 ) );
224 placeUnit( ad8620Lib, 2, VECTOR2I( 1000000, 0 ) );
225 placeUnit( ad8620Lib, 3, VECTOR2I( 2000000, 0 ) );
226
227 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
229 hierarchy.GetSymbols( refs, SYMBOL_FILTER_ALL );
230
231 const wxString unannotatedRef = wxT( "U?" );
232
233 // OPA1664: every unit must report unoccupied even though the reference string matches
234 // AD8620 placements 1..3.
235 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 1 ) );
236 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 2 ) );
237 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 3 ) );
238 BOOST_CHECK( !IsUnannotatedUnitOccupied( refs, unannotatedRef, opa1664Lib->GetLibId(), 4 ) );
239
240 // AD8620: units 1..3 must report occupied.
241 BOOST_CHECK( IsUnannotatedUnitOccupied( refs, unannotatedRef, ad8620Lib->GetLibId(), 1 ) );
242 BOOST_CHECK( IsUnannotatedUnitOccupied( refs, unannotatedRef, ad8620Lib->GetLibId(), 2 ) );
243 BOOST_CHECK( IsUnannotatedUnitOccupied( refs, unannotatedRef, ad8620Lib->GetLibId(), 3 ) );
244}
245
246
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:100
Container for project specific data.
Definition project.h:66
virtual void SetUnit(int aUnit)
Definition sch_item.h:238
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:48
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:143
Schematic symbol object.
Definition sch_symbol.h:76
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:687