KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue25280_pin_sync_units.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
25
27#include "eeschema_test_utils.h"
28
29#include <lib_symbol.h>
30#include <sch_pin.h>
31#include <sch_io/sch_io.h>
32#include <sch_io/sch_io_mgr.h>
34
35#include <wx/filename.h>
36
37#include <memory>
38
39
45static std::unique_ptr<LIB_SYMBOL> loadQuadBuffer()
46{
47 wxFileName libPath( KI_TEST::GetEeschemaTestDataDir() );
48 libPath.AppendDir( "variants" );
49 libPath.SetFullName( "pic_programmer.kicad_sym" );
50
51 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
52
53 // The plugin cache owns the returned symbol, so hand back a copy
54 LIB_SYMBOL* cached = pi->LoadSymbol( libPath.GetFullPath(), wxS( "74LS125" ) );
55
56 return cached ? std::make_unique<LIB_SYMBOL>( *cached ) : nullptr;
57}
58
59
64{
66 {
67 if( pin->GetUnit() == 1 && pin->GetType() == ELECTRICAL_PINTYPE::PT_INPUT
68 && pin->GetOrientation() == PIN_ORIENTATION::PIN_RIGHT )
69 {
70 return pin;
71 }
72 }
73
74 return nullptr;
75}
76
77
82static int countMatchingPins( LIB_SYMBOL& aSymbol, const SCH_PIN& aPin )
83{
84 int count = 0;
85
87 {
88 if( pin != &aPin && pin->GetUnit() != aPin.GetUnit()
89 && pin->GetPosition() == aPin.GetPosition()
90 && pin->GetOrientation() == aPin.GetOrientation()
91 && pin->GetType() == aPin.GetType() && pin->IsVisible() == aPin.IsVisible()
92 && pin->GetName() == aPin.GetName() )
93 {
94 count++;
95 }
96 }
97
98 return count;
99}
100
101
102BOOST_AUTO_TEST_SUITE( Issue25280PinSyncUnits )
103
104
105
109BOOST_AUTO_TEST_CASE( CommonToAllUnitsRemovesSubsumedPins )
110{
111 std::unique_ptr<LIB_SYMBOL> symbol = loadQuadBuffer();
112 BOOST_REQUIRE( symbol );
113
114 SCH_PIN* edited = firstGateInput( *symbol );
115 BOOST_REQUIRE( edited );
116
117 const VECTOR2I pinPos = edited->GetPosition();
118 const size_t pinsBefore = symbol->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ).size();
119 const int expectedRemovals = countMatchingPins( *symbol, *edited );
120
121 BOOST_REQUIRE_GT( expectedRemovals, 0 );
122
123 // The dialog copies the pin before the edit and writes unit 0 when "Common to all units
124 // in symbol" is checked.
125 SCH_PIN originalPin( *edited );
126 edited->SetUnit( 0 );
127
128 int removed = SYMBOL_EDITOR_PIN_TOOL::SynchronizeOtherUnits( symbol.get(), edited, originalPin );
129
130 BOOST_CHECK_EQUAL( removed, expectedRemovals );
131 BOOST_CHECK_EQUAL( symbol->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ).size(),
132 pinsBefore - static_cast<size_t>( expectedRemovals ) );
133
134 // The edited pin survives, and no unit kept a duplicate of it.
135 int survivorsAtPos = 0;
136
137 for( SCH_PIN* pin : symbol->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
138 {
139 BOOST_CHECK( pin->GetParentSymbol() == symbol.get() );
140
141 if( pin->GetPosition() == pinPos )
142 survivorsAtPos++;
143 }
144
145 BOOST_CHECK_EQUAL( survivorsAtPos, 1 );
146 BOOST_CHECK_EQUAL( edited->GetUnit(), 0 );
147}
148
149
154BOOST_AUTO_TEST_CASE( PerUnitEditPropagatesWithoutRemoving )
155{
156 std::unique_ptr<LIB_SYMBOL> symbol = loadQuadBuffer();
157 BOOST_REQUIRE( symbol );
158
159 SCH_PIN* edited = firstGateInput( *symbol );
160 BOOST_REQUIRE( edited );
161
162 const VECTOR2I pinPos = edited->GetPosition();
163 const size_t pinsBefore = symbol->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ).size();
164 const int expectedMatches = countMatchingPins( *symbol, *edited );
165
166 BOOST_REQUIRE_GT( expectedMatches, 0 );
167
168 SCH_PIN originalPin( *edited );
169
170 edited->SetName( wxS( "SYNCED" ) );
171 edited->SetNameTextSize( edited->GetNameTextSize() + schIUScale.MilsToIU( 10 ) );
172
173 int removed = SYMBOL_EDITOR_PIN_TOOL::SynchronizeOtherUnits( symbol.get(), edited, originalPin );
174
175 BOOST_CHECK_EQUAL( removed, 0 );
176 BOOST_CHECK_EQUAL( symbol->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ).size(), pinsBefore );
177
178 int propagated = 0;
179
180 for( SCH_PIN* pin : symbol->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
181 {
182 if( pin == edited || pin->GetPosition() != pinPos )
183 continue;
184
185 BOOST_CHECK_EQUAL( pin->GetName(), edited->GetName() );
186 BOOST_CHECK_EQUAL( pin->GetNameTextSize(), edited->GetNameTextSize() );
187 propagated++;
188 }
189
190 BOOST_CHECK_EQUAL( propagated, expectedMatches );
191}
192
193
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
Define a library symbol object.
Definition lib_symbol.h:114
std::vector< SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const override
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
int GetUnit() const
Definition sch_item.h:243
virtual void SetUnit(int aUnit)
Definition sch_item.h:242
void SetName(const wxString &aName)
Definition sch_pin.cpp:545
bool IsVisible() const
Definition sch_pin.cpp:513
const wxString & GetName() const
Definition sch_pin.cpp:527
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:386
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:378
int GetNameTextSize() const
Definition sch_pin.cpp:856
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:435
void SetNameTextSize(int aSize)
Definition sch_pin.cpp:870
static int SynchronizeOtherUnits(LIB_SYMBOL *aSymbol, SCH_PIN *aPin, const SCH_PIN &aOriginalPin)
Propagate the edits made to aPin to the matching pins of the other units.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:33
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
#define ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
static std::unique_ptr< LIB_SYMBOL > loadQuadBuffer()
Load 74LS125, a quad tri-state buffer whose four interchangeable units carry identically placed pins ...
static SCH_PIN * firstGateInput(LIB_SYMBOL &aSymbol)
The input pin of the first gate, i.e.
BOOST_AUTO_TEST_CASE(CommonToAllUnitsRemovesSubsumedPins)
Making a pin common to all units deletes the pins it subsumes in the other units.
static int countMatchingPins(LIB_SYMBOL &aSymbol, const SCH_PIN &aPin)
Pins of the other units that the edit is expected to subsume: same position, orientation,...
KIBIS_PIN * pin
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708