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{
65 for( SCH_PIN* pin : aSymbol.GetPins() )
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
86 for( SCH_PIN* pin : aSymbol.GetPins() )
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->GetPins().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,
129 originalPin );
130
131 BOOST_CHECK_EQUAL( removed, expectedRemovals );
132 BOOST_CHECK_EQUAL( symbol->GetPins().size(),
133 pinsBefore - static_cast<size_t>( expectedRemovals ) );
134
135 // The edited pin survives, and no unit kept a duplicate of it.
136 int survivorsAtPos = 0;
137
138 for( SCH_PIN* pin : symbol->GetPins() )
139 {
140 BOOST_CHECK( pin->GetParentSymbol() == symbol.get() );
141
142 if( pin->GetPosition() == pinPos )
143 survivorsAtPos++;
144 }
145
146 BOOST_CHECK_EQUAL( survivorsAtPos, 1 );
147 BOOST_CHECK_EQUAL( edited->GetUnit(), 0 );
148}
149
150
155BOOST_AUTO_TEST_CASE( PerUnitEditPropagatesWithoutRemoving )
156{
157 std::unique_ptr<LIB_SYMBOL> symbol = loadQuadBuffer();
158 BOOST_REQUIRE( symbol );
159
160 SCH_PIN* edited = firstGateInput( *symbol );
161 BOOST_REQUIRE( edited );
162
163 const VECTOR2I pinPos = edited->GetPosition();
164 const size_t pinsBefore = symbol->GetPins().size();
165 const int expectedMatches = countMatchingPins( *symbol, *edited );
166
167 BOOST_REQUIRE_GT( expectedMatches, 0 );
168
169 SCH_PIN originalPin( *edited );
170
171 edited->SetName( wxS( "SYNCED" ) );
172 edited->SetNameTextSize( edited->GetNameTextSize() + schIUScale.MilsToIU( 10 ) );
173
174 int removed = SYMBOL_EDITOR_PIN_TOOL::SynchronizeOtherUnits( symbol.get(), edited,
175 originalPin );
176
177 BOOST_CHECK_EQUAL( removed, 0 );
178 BOOST_CHECK_EQUAL( symbol->GetPins().size(), pinsBefore );
179
180 int propagated = 0;
181
182 for( SCH_PIN* pin : symbol->GetPins() )
183 {
184 if( pin == edited || pin->GetPosition() != pinPos )
185 continue;
186
187 BOOST_CHECK_EQUAL( pin->GetName(), edited->GetName() );
188 BOOST_CHECK_EQUAL( pin->GetNameTextSize(), edited->GetNameTextSize() );
189 propagated++;
190 }
191
192 BOOST_CHECK_EQUAL( propagated, expectedMatches );
193}
194
195
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
Define a library symbol object.
Definition lib_symbol.h:119
std::vector< SCH_PIN * > GetPins() const override
int GetUnit() const
Definition sch_item.h:237
virtual void SetUnit(int aUnit)
Definition sch_item.h:236
void SetName(const wxString &aName)
Definition sch_pin.cpp:521
bool IsVisible() const
Definition sch_pin.cpp:489
const wxString & GetName() const
Definition sch_pin.cpp:503
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:362
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:354
int GetNameTextSize() const
Definition sch_pin.cpp:839
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:411
void SetNameTextSize(int aSize)
Definition sch_pin.cpp:853
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
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:683