KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pcb_handle_key.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
21
22#include <set>
23
24#include <board.h>
25#include <pcb_shape.h>
26#include <tool/edit_points.h>
28
29
30namespace
31{
32
33// Built the same way the point editor behaviors build their points in MakePoints()
34std::shared_ptr<EDIT_POINTS> makeQuadCollection( EDA_ITEM* aParent, int aOffset = 0 )
35{
36 std::shared_ptr<EDIT_POINTS> points = std::make_shared<EDIT_POINTS>( aParent );
37
38 points->AddPoint( VECTOR2I( aOffset, 0 ) );
39 points->AddPoint( VECTOR2I( aOffset + 100, 0 ) );
40 points->AddPoint( VECTOR2I( aOffset + 100, 100 ) );
41 points->AddPoint( VECTOR2I( aOffset, 100 ) );
42 points->AddLine( points->Point( 0 ), points->Point( 1 ) );
43 points->AddLine( points->Point( 1 ), points->Point( 2 ) );
44
45 return points;
46}
47
48} // namespace
49
50
51BOOST_AUTO_TEST_SUITE( PcbHandleKeyTests )
52
53
54BOOST_AUTO_TEST_CASE( PointSelectionFlagsAreIndependent )
55{
56 EDIT_POINT point( VECTOR2I( 0, 0 ) );
57
58 BOOST_CHECK( !point.IsSelected() );
59 BOOST_CHECK( !point.IsDependent() );
60
61 point.SetSelected();
62 point.SetActive();
63 point.SetHover();
64
65 BOOST_CHECK( point.IsSelected() );
66 BOOST_CHECK( !point.IsDependent() );
67
68 point.SetActive( false );
69 point.SetHover( false );
70
71 BOOST_CHECK( point.IsSelected() );
72 BOOST_CHECK( !point.IsActive() );
73 BOOST_CHECK( !point.IsHover() );
74
75 point.SetDependent();
76 point.SetSelected( false );
77
78 BOOST_CHECK( point.IsDependent() );
79 BOOST_CHECK( !point.IsSelected() );
80}
81
82
83BOOST_AUTO_TEST_CASE( KeyCreationForPointsAndLines )
84{
85 BOARD board;
86 PCB_SHAPE* shape = new PCB_SHAPE( &board, SHAPE_T::POLY );
87 board.Add( shape );
88
89 std::shared_ptr<EDIT_POINTS> points = makeQuadCollection( shape );
90
91 std::optional<PCB_HANDLE_KEY> pointKey = PCB_HANDLE_KEY::Make( *points, &points->Point( 2 ) );
92
93 BOOST_REQUIRE( pointKey.has_value() );
94 BOOST_CHECK( pointKey->Parent() == shape->m_Uuid );
95 BOOST_CHECK( !pointKey->IsLine() );
96 BOOST_CHECK_EQUAL( pointKey->Index(), 2 );
97
98 std::optional<PCB_HANDLE_KEY> lineKey = PCB_HANDLE_KEY::Make( *points, &points->Line( 1 ) );
99
100 BOOST_REQUIRE( lineKey.has_value() );
101 BOOST_CHECK( lineKey->IsLine() );
102 BOOST_CHECK_EQUAL( lineKey->Index(), 1 );
103
104 EDIT_POINT foreign( VECTOR2I( 5, 5 ) );
105
106 BOOST_CHECK( !PCB_HANDLE_KEY::Make( *points, &foreign ).has_value() );
107 BOOST_CHECK( !PCB_HANDLE_KEY::Make( *points, nullptr ).has_value() );
108}
109
110
111BOOST_AUTO_TEST_CASE( KeyResolvesAfterRegeneration )
112{
113 BOARD board;
114 PCB_SHAPE* shape = new PCB_SHAPE( &board, SHAPE_T::POLY );
115 board.Add( shape );
116
117 std::shared_ptr<EDIT_POINTS> before = makeQuadCollection( shape );
118
119 std::optional<PCB_HANDLE_KEY> pointKey = PCB_HANDLE_KEY::Make( *before, &before->Point( 2 ) );
120 std::optional<PCB_HANDLE_KEY> lineKey = PCB_HANDLE_KEY::Make( *before, &before->Line( 0 ) );
121
122 BOOST_REQUIRE( pointKey.has_value() );
123 BOOST_REQUIRE( lineKey.has_value() );
124
125 // Rebuild the collection with the same parent and the same handle counts, but moved
126 std::shared_ptr<EDIT_POINTS> after = makeQuadCollection( shape, 50 );
127
128 EDIT_POINT* resolvedPoint = pointKey->Resolve( *after );
129 EDIT_POINT* resolvedLine = lineKey->Resolve( *after );
130
131 BOOST_REQUIRE( resolvedPoint != nullptr );
132 BOOST_CHECK( resolvedPoint == &after->Point( 2 ) );
133 BOOST_CHECK_EQUAL( resolvedPoint->GetPosition().x, 150 );
134
135 BOOST_REQUIRE( resolvedLine != nullptr );
136 BOOST_CHECK( resolvedLine == &after->Line( 0 ) );
137}
138
139
140BOOST_AUTO_TEST_CASE( KeyEqualityDeduplicates )
141{
142 BOARD board;
143 PCB_SHAPE* shapeA = new PCB_SHAPE( &board, SHAPE_T::POLY );
144 PCB_SHAPE* shapeB = new PCB_SHAPE( &board, SHAPE_T::POLY );
145 board.Add( shapeA );
146 board.Add( shapeB );
147
148 std::shared_ptr<EDIT_POINTS> pointsA = makeQuadCollection( shapeA );
149 std::shared_ptr<EDIT_POINTS> pointsB = makeQuadCollection( shapeB );
150
151 std::optional<PCB_HANDLE_KEY> key1 = PCB_HANDLE_KEY::Make( *pointsA, &pointsA->Point( 1 ) );
152 std::optional<PCB_HANDLE_KEY> key2 = PCB_HANDLE_KEY::Make( *pointsA, &pointsA->Point( 1 ) );
153 std::optional<PCB_HANDLE_KEY> otherIndex = PCB_HANDLE_KEY::Make( *pointsA, &pointsA->Point( 2 ) );
154 std::optional<PCB_HANDLE_KEY> otherKind = PCB_HANDLE_KEY::Make( *pointsA, &pointsA->Line( 1 ) );
155 std::optional<PCB_HANDLE_KEY> otherParent = PCB_HANDLE_KEY::Make( *pointsB, &pointsB->Point( 1 ) );
156
157 BOOST_REQUIRE( key1 && key2 && otherIndex && otherKind && otherParent );
158
159 BOOST_CHECK( *key1 == *key2 );
160 BOOST_CHECK( !( *key1 == *otherIndex ) );
161 BOOST_CHECK( !( *key1 == *otherKind ) );
162 BOOST_CHECK( !( *key1 == *otherParent ) );
163
164 std::set<PCB_HANDLE_KEY> picked;
165 picked.insert( *key1 );
166 picked.insert( *key2 );
167 picked.insert( *otherIndex );
168 picked.insert( *otherKind );
169 picked.insert( *otherParent );
170
171 BOOST_CHECK_EQUAL( picked.size(), 4 );
172}
173
174
175BOOST_AUTO_TEST_CASE( MissingIdentityInvalidates )
176{
177 BOARD board;
178 PCB_SHAPE* shapeA = new PCB_SHAPE( &board, SHAPE_T::POLY );
179 PCB_SHAPE* shapeB = new PCB_SHAPE( &board, SHAPE_T::POLY );
180 board.Add( shapeA );
181 board.Add( shapeB );
182
183 std::shared_ptr<EDIT_POINTS> before = makeQuadCollection( shapeA );
184
185 std::optional<PCB_HANDLE_KEY> pointKey = PCB_HANDLE_KEY::Make( *before, &before->Point( 2 ) );
186 std::optional<PCB_HANDLE_KEY> lineKey = PCB_HANDLE_KEY::Make( *before, &before->Line( 1 ) );
187
188 BOOST_REQUIRE( pointKey.has_value() );
189 BOOST_REQUIRE( lineKey.has_value() );
190
191 // A different parent fails even with the same handle counts
192 std::shared_ptr<EDIT_POINTS> otherParent = makeQuadCollection( shapeB );
193
194 BOOST_CHECK( pointKey->Resolve( *otherParent ) == nullptr );
195
196 // Changing the handle counts kills the key even though the index still exists
197 std::shared_ptr<EDIT_POINTS> fewerPoints = std::make_shared<EDIT_POINTS>( shapeA );
198 fewerPoints->AddPoint( VECTOR2I( 0, 0 ) );
199 fewerPoints->AddPoint( VECTOR2I( 100, 0 ) );
200 fewerPoints->AddPoint( VECTOR2I( 100, 100 ) );
201 fewerPoints->AddLine( fewerPoints->Point( 0 ), fewerPoints->Point( 1 ) );
202 fewerPoints->AddLine( fewerPoints->Point( 1 ), fewerPoints->Point( 2 ) );
203
204 BOOST_CHECK( pointKey->Resolve( *fewerPoints ) == nullptr );
205
206 // The same number of points but fewer lines counts as a change too
207 std::shared_ptr<EDIT_POINTS> fewerLines = std::make_shared<EDIT_POINTS>( shapeA );
208 fewerLines->AddPoint( VECTOR2I( 0, 0 ) );
209 fewerLines->AddPoint( VECTOR2I( 100, 0 ) );
210 fewerLines->AddPoint( VECTOR2I( 100, 100 ) );
211 fewerLines->AddPoint( VECTOR2I( 0, 100 ) );
212 fewerLines->AddLine( fewerLines->Point( 0 ), fewerLines->Point( 1 ) );
213
214 BOOST_CHECK( lineKey->Resolve( *fewerLines ) == nullptr );
215}
216
217
218BOOST_AUTO_TEST_CASE( AStaleKeyNamesTheSameHandleButStopsResolving )
219{
220 BOARD board;
221 PCB_SHAPE* shape = new PCB_SHAPE( &board, SHAPE_T::POLY );
222 board.Add( shape );
223
224 std::shared_ptr<EDIT_POINTS> points = makeQuadCollection( shape );
225
226 std::optional<PCB_HANDLE_KEY> before = PCB_HANDLE_KEY::Make( *points, &points->Point( 0 ) );
227 BOOST_REQUIRE( before );
228
229 points->AddPoint( VECTOR2I( 50, 50 ) );
230
231 std::optional<PCB_HANDLE_KEY> after = PCB_HANDLE_KEY::Make( *points, &points->Point( 0 ) );
232 BOOST_REQUIRE( after );
233
234 // Both name corner 0 of the same shape, so a picked set holds that handle once
235 BOOST_CHECK( *before == *after );
236
237 std::set<PCB_HANDLE_KEY> picked{ *before };
238
239 BOOST_CHECK( !picked.insert( *after ).second );
240
241 // The key made before the shape gained a corner no longer resolves, so the caller
242 // drops it instead of moving whichever handle now sits at that index
243 BOOST_CHECK( after->Resolve( *points ) == &points->Point( 0 ) );
244 BOOST_CHECK( before->Resolve( *points ) == nullptr );
245}
246
247
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1548
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:599
Represent a single point that can be used for modifying items.
Definition edit_points.h:44
bool IsActive() const
void SetDependent(bool aDependent=true)
bool IsDependent() const
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition edit_points.h:72
void SetHover(bool aHover=true)
void SetSelected(bool aSelected=true)
bool IsHover() const
void SetActive(bool aActive=true)
bool IsSelected() const
static std::optional< PCB_HANDLE_KEY > Make(const EDIT_POINTS &aPoints, const EDIT_POINT *aPoint)
Build the key for a handle in a collection.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(PointSelectionFlagsAreIndependent)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708