KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_constraint_ownership.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
20#include <memory>
21
23
24#include <board.h>
25#include <footprint.h>
27
28
29BOOST_AUTO_TEST_SUITE( ConstraintSolverOwnership )
30
31
32BOOST_AUTO_TEST_CASE( BoardAddRemoveResolve )
33{
34 BOARD board;
35
38 KIID id = c->m_Uuid;
39
40 board.Add( c );
41
42 BOOST_REQUIRE_EQUAL( board.Constraints().size(), 1 );
43 BOOST_CHECK( board.Constraints().front() == c );
44
45 // The KIID cache and the linear-scan fallback both resolve the constraint.
46 BOOST_CHECK( board.ResolveItem( id, true ) == c );
47
48 board.Remove( c );
49 BOOST_CHECK( board.Constraints().empty() );
50
51 delete c;
52}
53
54
55BOOST_AUTO_TEST_CASE( BoardRunOnChildrenVisitsConstraint )
56{
57 BOARD board;
58
60 board.Add( c );
61
62 bool seen = false;
63 board.RunOnChildren(
64 [&]( BOARD_ITEM* aItem )
65 {
66 if( aItem == c )
67 seen = true;
68 },
70
71 BOOST_CHECK( seen );
72}
73
74
75BOOST_AUTO_TEST_CASE( FootprintCloneRoundTrip )
76{
77 BOARD board;
78
79 auto fp = std::make_unique<FOOTPRINT>( &board );
80
84 c->SetValue( 3.0 );
85 fp->Add( c );
86
87 BOOST_REQUIRE_EQUAL( fp->Constraints().size(), 1 );
88
89 // Copy constructor must clone the constraint faithfully (uuid preserved).
90 std::unique_ptr<FOOTPRINT> clone( static_cast<FOOTPRINT*>( fp->Clone() ) );
91
92 BOOST_REQUIRE_EQUAL( clone->Constraints().size(), 1 );
93
94 PCB_CONSTRAINT* original = fp->Constraints().front();
95 PCB_CONSTRAINT* copied = clone->Constraints().front();
96
97 BOOST_CHECK( copied != original );
98 BOOST_CHECK( *copied == *original );
99}
100
101
102BOOST_AUTO_TEST_CASE( FootprintTeardownDeletesConstraints )
103{
104 BOARD board;
105
106 {
107 FOOTPRINT fp( &board );
109 BOOST_CHECK_EQUAL( fp.Constraints().size(), 1 );
110
111 // The constraint is owned by the footprint; its dtor frees it (asan would
112 // flag a leak or double-free otherwise).
113 }
114}
115
116
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1355
const CONSTRAINTS & Constraints() const
Geometric constraints (#2329) owned by this board.
Definition board.h:465
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const override
Invoke a function on all children.
Definition board.cpp:707
void Remove(BOARD_ITEM *aBoardItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL) override
Removes an item from the container.
Definition board.cpp:1503
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:1913
const KIID m_Uuid
Definition eda_item.h:531
CONSTRAINTS & Constraints()
Definition footprint.h:387
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition kiid.h:44
A geometric constraint between board items (issue #2329).
void AddMember(const KIID &aItem, CONSTRAINT_ANCHOR aAnchor=CONSTRAINT_ANCHOR::WHOLE, int aIndex=-1)
void SetValue(std::optional< double > aValue)
@ NO_RECURSE
Definition eda_item.h:50
@ WHOLE
The item as a whole (a segment as a line, a circle).
@ START
First endpoint of a segment or arc.
@ END
Second endpoint of a segment or arc.
@ VERTICAL
A segment (or two points) is vertical.
@ HORIZONTAL
A segment (or two points) is horizontal.
@ PARALLEL
Two segments are parallel.
@ EQUAL_LENGTH
Two segments have equal length.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(BoardAddRemoveResolve)
BOOST_AUTO_TEST_SUITE_END()
bool copied
BOOST_CHECK_EQUAL(result, "25.4")