KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_rule_area_destruction.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 3
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
29
31
32#include <schematic.h>
33#include <sch_label.h>
34#include <sch_line.h>
35#include <sch_pin.h>
36#include <sch_rule_area.h>
37#include <sch_screen.h>
38#include <sch_symbol.h>
39
40
41BOOST_AUTO_TEST_SUITE( SchRuleAreaDestruction )
42
43
44
50BOOST_AUTO_TEST_CASE( RuleAreaDestroyedBeforeContainedItems )
51{
52 auto ruleArea = std::make_unique<SCH_RULE_AREA>();
53 auto line1 = std::make_unique<SCH_LINE>( VECTOR2I( 0, 0 ), LAYER_WIRE );
54 auto line2 = std::make_unique<SCH_LINE>( VECTOR2I( 100, 0 ), LAYER_WIRE );
55
56 // Simulate what RefreshContainedItemsAndDirectives does
57 ruleArea->_GetContainedItems().insert( line1.get() );
58 line1->AddRuleAreaToCache( ruleArea.get() );
59
60 ruleArea->_GetContainedItems().insert( line2.get() );
61 line2->AddRuleAreaToCache( ruleArea.get() );
62
63 BOOST_CHECK_EQUAL( line1->GetRuleAreaCache().size(), 1u );
64 BOOST_CHECK_EQUAL( line2->GetRuleAreaCache().size(), 1u );
65
66 // Destroy rule area first (the problematic ordering from FreeDrawList)
67 ruleArea.reset();
68
69 // Contained items should no longer reference the destroyed rule area
70 BOOST_CHECK_EQUAL( line1->GetRuleAreaCache().size(), 0u );
71 BOOST_CHECK_EQUAL( line2->GetRuleAreaCache().size(), 0u );
72
73 // Destroying items should not crash (previously called RemoveItem on freed memory)
74 line1.reset();
75 line2.reset();
76}
77
78
85BOOST_AUTO_TEST_CASE( ContainedItemsDestroyedBeforeRuleArea )
86{
87 auto ruleArea = std::make_unique<SCH_RULE_AREA>();
88 auto line1 = std::make_unique<SCH_LINE>( VECTOR2I( 0, 0 ), LAYER_WIRE );
89 auto line2 = std::make_unique<SCH_LINE>( VECTOR2I( 100, 0 ), LAYER_WIRE );
90
91 ruleArea->_GetContainedItems().insert( line1.get() );
92 line1->AddRuleAreaToCache( ruleArea.get() );
93
94 ruleArea->_GetContainedItems().insert( line2.get() );
95 line2->AddRuleAreaToCache( ruleArea.get() );
96
97 BOOST_CHECK_EQUAL( ruleArea->_GetContainedItems().size(), 2u );
98
99 // Destroy items first (the normal ordering)
100 line1.reset();
101 line2.reset();
102
103 // Rule area should have had items removed by their destructors
104 BOOST_CHECK_EQUAL( ruleArea->_GetContainedItems().size(), 0u );
105
106 ruleArea.reset();
107}
108
109
114BOOST_AUTO_TEST_CASE( CacheRefreshClearsReciprocalLinks )
115{
116 SCHEMATIC schematic( nullptr );
117 SCH_SCREEN screen( &schematic );
118 auto* ruleArea = new SCH_RULE_AREA();
119 auto* symbol = new SCH_SYMBOL();
120 auto* directive = new SCH_DIRECTIVE_LABEL( VECTOR2I( 1000, 1000 ) );
121
122 symbol->GetRawPins().push_back( std::make_unique<SCH_PIN>( symbol, wxT( "1" ), wxEmptyString, KIID() ) );
123 SCH_PIN* pin = symbol->GetRawPins().front().get();
124
125 ruleArea->_GetContainedItems().insert( pin );
126 pin->AddRuleAreaToCache( ruleArea );
127 ruleArea->_GetContainedDirectives().insert( directive );
128 directive->AddConnectedRuleArea( ruleArea );
129
130 screen.Append( ruleArea );
131 screen.Append( symbol, false );
132 screen.Append( directive );
133
134 std::unordered_set<SCH_SCREEN*> screens = { &screen };
135 SCH_RULE_AREA::UpdateRuleAreasInScreens( screens, nullptr );
136
137 BOOST_CHECK( pin->GetRuleAreaCache().empty() );
138 BOOST_CHECK( directive->GetConnectedRuleAreas().empty() );
139}
140
141
Definition kiid.h:46
Holds all the data relating to one schematic.
Definition schematic.h:149
static std::vector< std::pair< SCH_RULE_AREA *, SCH_SCREEN * > > UpdateRuleAreasInScreens(std::unordered_set< SCH_SCREEN * > &screens, KIGFX::SCH_VIEW *view)
Update all rule area connectvity / caches in the given sheet paths.
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Schematic symbol object.
Definition sch_symbol.h:75
@ LAYER_WIRE
Definition layer_ids.h:480
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
KIBIS_PIN * pin
BOOST_AUTO_TEST_CASE(RuleAreaDestroyedBeforeContainedItems)
Verify that destroying a rule area before its contained items does not crash.
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708