KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_board_item_index.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 * or you may search the http://www.gnu.org website for the version 2 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
24#include <boost/test/unit_test.hpp>
25
26#include <memory>
27
28#include <board.h>
29#include <footprint.h>
30#include <pad.h>
31#include <pcb_shape.h>
32
33
34BOOST_AUTO_TEST_SUITE( BoardItemIndex )
35
36
37BOOST_AUTO_TEST_CASE( SwapItemDataReindexesFootprintChildren )
38{
39 BOARD* board = new BOARD();
40
41 auto footprint = std::make_unique<FOOTPRINT>( board );
42 auto pad = new PAD( footprint.get() );
43 pad->SetNumber( "1" );
44
45 PAD* originalPad = pad;
46 KIID originalPadId = originalPad->m_Uuid;
47
48 footprint->Add( pad );
49
50 FOOTPRINT* liveFootprint = footprint.get();
51 board->Add( footprint.release() );
52
53 auto image = std::unique_ptr<FOOTPRINT>( static_cast<FOOTPRINT*>( liveFootprint->Clone() ) );
54
55 liveFootprint->SwapItemData( image.get() );
56 image.reset();
57
58 BOARD_ITEM* resolved = board->ResolveItem( originalPadId, true );
59
60 BOOST_REQUIRE( resolved );
61 BOOST_CHECK_EQUAL( resolved->Type(), PCB_PAD_T );
62 BOOST_CHECK( resolved != originalPad );
63
64 delete board;
65}
66
67
68BOOST_AUTO_TEST_CASE( AttachedItemUuidRewriteDropsOldAlias )
69{
70 BOARD* board = new BOARD();
71
72 auto shape = std::make_unique<PCB_SHAPE>( board );
73 PCB_SHAPE* liveShape = shape.get();
74 const KIID oldId = liveShape->m_Uuid;
75 const KIID newId;
76
77 board->Add( shape.release() );
78
79 const_cast<KIID&>( liveShape->m_Uuid ) = newId;
80
81 BOOST_CHECK( board->ResolveItem( oldId, true ) == nullptr );
82 BOOST_CHECK_EQUAL( board->ResolveItem( newId, true ), liveShape );
83
84 delete board;
85}
86
87
88BOOST_AUTO_TEST_CASE( ResolvingNewUuidPurgesStaleOldAlias )
89{
90 BOARD* board = new BOARD();
91
92 auto shape = std::make_unique<PCB_SHAPE>( board );
93 PCB_SHAPE* liveShape = shape.get();
94 const KIID oldId = liveShape->m_Uuid;
95 const KIID newId;
96
97 board->Add( shape.release() );
98 const_cast<KIID&>( liveShape->m_Uuid ) = newId;
99
100 BOOST_CHECK_EQUAL( board->ResolveItem( newId, true ), liveShape );
101 BOOST_CHECK( board->GetItemByIdCache().contains( newId ) );
102 BOOST_CHECK( !board->GetItemByIdCache().contains( oldId ) );
103
104 delete board;
105}
106
107
108BOOST_AUTO_TEST_CASE( RebindItemUuidUpdatesCacheAtomically )
109{
110 BOARD* board = new BOARD();
111
112 auto shape = std::make_unique<PCB_SHAPE>( board );
113 PCB_SHAPE* liveShape = shape.get();
114 const KIID oldId = liveShape->m_Uuid;
115 const KIID newId;
116
117 board->Add( shape.release() );
118 board->RebindItemUuid( liveShape, newId );
119
120 BOOST_CHECK( liveShape->m_Uuid == newId );
121 BOOST_CHECK( !board->GetItemByIdCache().contains( oldId ) );
122 BOOST_CHECK( board->GetItemByIdCache().contains( newId ) );
123 BOOST_CHECK_EQUAL( board->ResolveItem( newId, true ), liveShape );
124
125 delete board;
126}
127
128
129BOOST_AUTO_TEST_CASE( AttachedChildSetUuidRebindsCache )
130{
131 BOARD* board = new BOARD();
132
133 auto footprint = std::make_unique<FOOTPRINT>( board );
134 auto pad = new PAD( footprint.get() );
135 PAD* livePad = pad;
136 const KIID oldId = livePad->m_Uuid;
137 const KIID newId;
138
139 footprint->Add( pad );
140 board->Add( footprint.release() );
141
142 livePad->SetUuid( newId );
143
144 BOOST_CHECK( livePad->m_Uuid == newId );
145 BOOST_CHECK( !board->GetItemByIdCache().contains( oldId ) );
146 BOOST_CHECK( board->GetItemByIdCache().contains( newId ) );
147 BOOST_CHECK_EQUAL( board->ResolveItem( newId, true ), livePad );
148
149 delete board;
150}
151
152
153BOOST_AUTO_TEST_CASE( CacheItemByIdCanonicalizesRewrittenUuid )
154{
155 BOARD* board = new BOARD();
156
157 auto shape = std::make_unique<PCB_SHAPE>( board );
158 PCB_SHAPE* liveShape = shape.get();
159 const KIID oldId = liveShape->m_Uuid;
160 const KIID newId;
161
162 board->Add( shape.release() );
163
164 const_cast<KIID&>( liveShape->m_Uuid ) = newId;
165 board->CacheItemById( liveShape );
166
167 BOOST_CHECK( !board->GetItemByIdCache().contains( oldId ) );
168 BOOST_CHECK( board->GetItemByIdCache().contains( newId ) );
169 BOOST_CHECK_EQUAL( board->ResolveItem( newId, true ), liveShape );
170
171 delete board;
172}
173
174
175BOOST_AUTO_TEST_CASE( RepairDuplicateItemUuidsKeepsEarlierTraversalWinner )
176{
177 BOARD* board = new BOARD();
178
179 auto footprint = std::make_unique<FOOTPRINT>( board );
180 FOOTPRINT* liveFootprint = footprint.get();
181 const KIID claimedId = liveFootprint->m_Uuid;
182
183 auto shape = std::make_unique<PCB_SHAPE>( board );
184 PCB_SHAPE* liveShape = shape.get();
185 const KIID originalShapeId = liveShape->m_Uuid;
186
187 board->Add( footprint.release() );
188 board->Add( shape.release() );
189
190 const_cast<KIID&>( liveShape->m_Uuid ) = claimedId;
191
193 BOOST_CHECK( liveFootprint->m_Uuid == claimedId );
194 BOOST_CHECK( liveShape->m_Uuid != claimedId );
195 BOOST_CHECK( liveShape->m_Uuid != originalShapeId );
196 BOOST_CHECK_EQUAL( board->ResolveItem( claimedId, true ), liveFootprint );
197 BOOST_CHECK( board->ResolveItem( originalShapeId, true ) == nullptr );
198 BOOST_CHECK_EQUAL( board->ResolveItem( liveShape->m_Uuid, true ), liveShape );
199
200 delete board;
201}
202
203
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
void SetUuid(const KIID &aUuid)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
void CacheItemById(BOARD_ITEM *aItem) const
Add an item to the item-by-id cache.
Definition board.cpp:1913
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1237
void RebindItemUuid(BOARD_ITEM *aItem, const KIID &aNewId)
Rebind the UUID of an attached item and keep the item-by-id cache coherent.
Definition board.cpp:2012
int RepairDuplicateItemUuids()
Rebind duplicate attached-item UUIDs so each live board item has a unique ID.
Definition board.cpp:2038
const std::unordered_map< KIID, BOARD_ITEM * > & GetItemByIdCache() const
Definition board.h:1428
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:1779
const KIID m_Uuid
Definition eda_item.h:528
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
EDA_ITEM * Clone() const override
Invoke a function on all children.
Definition kiid.h:48
Definition pad.h:55
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(SwapItemDataReindexesFootprintChildren)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:84