KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_connectivity_dump.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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#include <boost/test/data/test_case.hpp>
23
28#include <connection_graph.h>
29#include <advanced_config.h>
30#include <sch_commit.h>
31#include <sch_label.h>
32#include <sch_line.h>
33#include <sch_pin.h>
34#include <sch_screen.h>
35#include <sch_sheet.h>
36#include <sch_symbol.h>
37#include <schematic.h>
39#include <tool/tool_manager.h>
40
41#include <json_common.h>
42#include <algorithm>
43#include <map>
44#include <set>
45#include <scoped_set_reset.h>
46
47
48namespace
49{
50// Keep this exhaustive corpus small because every item edit compares two full rebuilds
51// Native fixtures from the disabled entries in test_incremental_netlister.cpp's RemoveAddItems
52const char* const ORACLE_FIXTURES[] = {
53 "incremental_test", "issue10430", "issue10926_1", "issue11926", "issue12505", "issue12814",
54 "issue13112", "issue13162", "issue13212", "issue13431", "issue13591", "issue16223", "issue6588", "issue9367"
55};
56
57const char* const PARITY_FIXTURES[] = {
58 "incremental_test", "issue10430", "issue10926_1", "issue11926", "issue12505", "issue12814",
59 "issue13112", "issue13162", "issue13212", "issue13431", "issue13591", "issue16223", "issue6588", "issue9367",
60 "issue18346", "unconnected_bus_entry_qa", "netlist_exporter_unit_metadata_per_unit",
61 "netlists/multinetclasses/multinetclasses", "issue22286/bugtest", "issue7203", "netlists/jumpers/jumpers",
62 "issue23058/issue23058", "netlists/bus_junctions/bus_junctions",
63 "netlists/prefix_bus_alias/prefix_bus_alias",
64 "issue9673/issue9673", "erc_directive_label_not_connected", "netlists/legacy_power/legacy_power",
65 "same_local_global_power", "issue1768/issue1768", "issue18092/issue18092", "issue22864/Test_Move_Grid",
66 "issue23840/BusAndVectors"
67};
68
69void CheckRows( const SCH_CONNECTIVITY::DUMP_ROWS& aExpected, const SCH_CONNECTIVITY::DUMP_ROWS& aActual )
70{
71 if( aExpected == aActual )
72 return;
73
74 BOOST_ERROR( nlohmann::json::diff( nlohmann::json::parse( SCH_CONNECTIVITY::Dump( aExpected ) ),
75 nlohmann::json::parse( SCH_CONNECTIVITY::Dump( aActual ) ) )
76 .dump( 2 ) );
77}
78
79
80// Legacy records each island's own driver while the engine records the whole net's winner
81void EraseDrivers( nlohmann::json& aValue )
82{
83 if( aValue.is_object() )
84 {
85 aValue.erase( "driver" );
86 aValue.erase( "driver_path" );
87 }
88
89 if( aValue.is_structured() )
90 {
91 for( auto& child : aValue )
92 EraseDrivers( child );
93 }
94}
95}
96
97
99{
101 std::unique_ptr<SCHEMATIC> schematic;
102};
103
104
105BOOST_FIXTURE_TEST_SUITE( ConnectivityDump, CONNECTIVITY_DUMP_FIXTURE )
106
107BOOST_AUTO_TEST_CASE( ShadowDumpResolvesClassesWithoutLegacyAssignments )
108{
109 KI_TEST::LoadSchematic( settings, "netlists/multinetclasses/multinetclasses", schematic );
110 auto& facade = schematic->Connectivity();
111 facade.Update( *schematic );
112 const auto legacy = SCH_CONNECTIVITY::Dump( *schematic );
113 const auto engine = SCH_CONNECTIVITY::Dump( *schematic, facade );
114 auto classes = schematic->Project().GetProjectFile().NetSettings();
115 classes->SetNetclassLabelAssignment( "/NET_1", { "CORRUPTED" } );
116 classes->ClearCacheForNet( "/NET_1" );
117
118 // Parity would be vacuous if the engine Dump read the cached assignments legacy rows use
119 BOOST_REQUIRE_NE( SCH_CONNECTIVITY::Dump( *schematic ), legacy );
120 BOOST_CHECK_EQUAL( SCH_CONNECTIVITY::Dump( *schematic, facade ), engine );
121}
122
124 LegacyAndEngineDumpsMatch,
125 boost::unit_test::data::make( PARITY_FIXTURES ), fixture )
126{
127 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
128 SCOPED_SET_RESET restore( enabled, false );
129
130 BOOST_TEST_CONTEXT( fixture )
131 {
132 KI_TEST::LoadSchematic( settings, fixture, schematic );
133 auto& facade = schematic->Connectivity();
134 facade.Update( *schematic );
135 auto legacy = nlohmann::json::parse( SCH_CONNECTIVITY::Dump( *schematic ) );
136 auto engine = nlohmann::json::parse( SCH_CONNECTIVITY::Dump( *schematic, facade ) );
137 EraseDrivers( legacy );
138 EraseDrivers( engine );
139 BOOST_CHECK_MESSAGE( legacy == engine, nlohmann::json::diff( legacy, engine ).dump( 2 ) );
140 }
141}
142
143BOOST_AUTO_TEST_CASE( NativeBusNamesPreserveMembership )
144{
145 using namespace SCH_CONNECTIVITY;
146 using KEY = std::pair<std::string, std::string>;
147 KI_TEST::LoadSchematic( settings, "issue9673/issue9673", schematic );
148 auto& facade = schematic->Connectivity();
149 facade.Update( *schematic );
150 const auto& keys = facade.Keys();
151 const auto legacy = nlohmann::json::parse( SCH_CONNECTIVITY::Dump( *schematic ) );
152 const auto shadow = nlohmann::json::parse( SCH_CONNECTIVITY::Dump( *schematic, facade ) );
153 std::map<KEY, std::set<KEY>> legacyBuses;
154 std::map<int, std::set<KEY>> legacyNets;
155 std::map<KEY, const nlohmann::json*> shadowRows;
156
157 for( const auto& row : shadow )
158 shadowRows.emplace( KEY{ row.at( "path" ), row.at( "item" ) }, &row );
159
160 for( const auto& row : legacy )
161 {
162 const KEY key{ row.at( "path" ), row.at( "item" ) };
163 const auto path = schematic->Hierarchy().GetSheetPathByKIIDPath(
164 KIID_PATH( wxString::FromUTF8( key.first ) ) );
166 const auto* item = path->LastScreen()->GetConnectivityItem( KIID( key.second ) );
167 BOOST_REQUIRE( item );
168 const auto* connection = item->Connection( &*path );
169
170 if( !connection || !connection->Driver() )
171 continue;
172
173 if( connection->IsBus() )
174 legacyBuses[KEY{ row.at( "driver_path" ), row.at( "driver" ) }].insert( key );
175 else if( connection->IsNet() )
176 {
177 BOOST_REQUIRE_GT( connection->NetCode(), 0 );
178 legacyNets[connection->NetCode()].insert( key );
179 const auto after = shadowRows.find( key );
180 BOOST_REQUIRE( after != shadowRows.end() );
181 BOOST_CHECK( row.at( "name" ) == after->second->at( "name" ) );
182 BOOST_CHECK( row.at( "type" ) == after->second->at( "type" ) );
183 }
184 }
185
186 const auto itemKeys = [&]( const auto& items )
187 {
188 std::set<KEY> result;
189
190 for( const ITEM_KEY& item : items )
191 result.emplace( keys.Instance( item.inst ).AsString().ToStdString(),
192 item.item.AsString().ToStdString() );
193
194 return result;
195 };
196 const auto checkFields = []( const auto& before, const auto& after )
197 {
198 for( const char* field : { "name", "local_name", "type", "netclasses" } )
199 BOOST_CHECK_MESSAGE( before.at( field ) == after.at( field ), field );
200 };
201 const auto checkMembers = [&]( const auto& self, const auto& before, const auto& after ) -> void
202 {
203 BOOST_REQUIRE_EQUAL( before.size(), after.size() );
204
205 for( size_t i = 0; i < before.size(); ++i )
206 {
207 checkFields( before[i], after[i] );
208 self( self, before[i].at( "members" ), after[i].at( "members" ) );
209 }
210 };
211
212 for( const char* name : { "/{FOO BAR HAM EGGS}", "/{MIXED_BUS}", "/A[0..4]", "/D[0..7]" } )
213 {
214 const auto row = std::find_if( legacy.begin(), legacy.end(),
215 [&]( const auto& value ) { return value.at( "name" ) == name; } );
216 BOOST_REQUIRE( row != legacy.end() );
217 const auto path = schematic->Hierarchy().GetSheetPathByKIIDPath(
218 KIID_PATH( wxString::FromUTF8( row->at( "path" ).get<std::string>() ) ) );
220 const KIID itemId( row->at( "item" ).get<std::string>() );
221 const auto instance = keys.FindInstance( path->Path() );
222 BOOST_REQUIRE( instance );
224 {
225 const auto result = facade.Published().Rows().find( ITEM_KEY{ itemId, *instance } );
226 BOOST_REQUIRE( result != facade.Published().Rows().end() );
227 const auto& component = facade.Published().Components().at( result->second.component );
228 BOOST_REQUIRE( component.name != INVALID_ID );
229 const auto actual = itemKeys( component.content->items );
230 const auto& expectedItems = legacyBuses.at( KEY{ row->at( "driver_path" ), row->at( "driver" ) } );
231 BOOST_CHECK_MESSAGE( actual == expectedItems, nlohmann::json( actual ).dump()
232 << " expected " << nlohmann::json( expectedItems ).dump() );
233 BOOST_CHECK_EQUAL( keys.Name( component.name ).ToStdString( wxConvUTF8 ), name );
234
235 for( const auto& before : legacy )
236 {
237 const KEY key{ before.at( "path" ), before.at( "item" ) };
238
239 if( !expectedItems.contains( key ) )
240 continue;
241
242 const auto after = shadowRows.find( key );
243 BOOST_REQUIRE( after != shadowRows.end() );
244 BOOST_REQUIRE( !before.at( "members" ).empty() );
245 checkFields( before, *after->second );
246 checkMembers( checkMembers, before.at( "members" ), after->second->at( "members" ) );
247 }
248 }
249 }
250
251 for( const auto& [code, items] : legacyNets )
252 {
253 const KEY& first = *items.begin();
254 const auto instance = keys.FindInstance( KIID_PATH( wxString::FromUTF8( first.first ) ) );
255 BOOST_REQUIRE( instance );
256 BOOST_TEST_CONTEXT( code )
257 {
258 const auto row = facade.Published().Rows().find( ITEM_KEY{ KIID( first.second ), *instance } );
259 BOOST_REQUIRE( row != facade.Published().Rows().end() );
260 const auto& component = facade.Published().Components().at( row->second.component );
261 BOOST_CHECK( itemKeys( component.content->items ) == items );
262 }
263 }
264
265 BOOST_CHECK_GT( legacyNets.size(), 0u );
266}
267
268BOOST_AUTO_TEST_CASE( NativeMemberPriorityPreservesScalarPartitions )
269{
270 using namespace SCH_CONNECTIVITY;
271 using KEY = std::pair<std::string, std::string>;
272
273 for( const char* fixture : { "netlists/hierarchy_aliases/hierarchy_aliases",
274 "netlists/multinetclasses/multinetclasses" } )
275 {
276 BOOST_TEST_CONTEXT( fixture )
277 {
278 KI_TEST::LoadSchematic( settings, fixture, schematic );
279 auto& facade = schematic->Connectivity();
280 facade.Update( *schematic );
281 const auto& keys = facade.Keys();
282 const auto legacy = nlohmann::json::parse( SCH_CONNECTIVITY::Dump( *schematic ) );
283 const auto shadow = nlohmann::json::parse( SCH_CONNECTIVITY::Dump( *schematic, facade ) );
284 BOOST_REQUIRE_EQUAL( legacy.size(), shadow.size() );
285
286 for( size_t i = 0; i < legacy.size(); ++i )
287 {
288 BOOST_REQUIRE( legacy[i].at( "path" ) == shadow[i].at( "path" ) );
289 BOOST_REQUIRE( legacy[i].at( "item" ) == shadow[i].at( "item" ) );
290 }
291
292 const auto paths = schematic->Hierarchy();
293 std::map<int, std::set<KEY>> nets;
294 std::map<int, std::string> names;
295
296 for( const auto& row : legacy )
297 {
298 const KEY key{ row.at( "path" ), row.at( "item" ) };
299 const auto path = paths.GetSheetPathByKIIDPath( KIID_PATH( wxString::FromUTF8( key.first ) ) );
301 const auto* item = path->LastScreen()->GetConnectivityItem( KIID( key.second ) );
302 BOOST_REQUIRE( item );
303 const auto* connection = item->Connection( &*path );
304
305 if( connection && connection->IsNet() && connection->Driver() )
306 {
307 BOOST_REQUIRE_GT( connection->NetCode(), 0 );
308 nets[connection->NetCode()].insert( key );
309 names[connection->NetCode()] = connection->Name().ToStdString( wxConvUTF8 );
310 }
311 }
312
313 for( const auto& [code, items] : nets )
314 {
315 const auto& first = *items.begin();
316 const auto instance = keys.FindInstance( KIID_PATH( wxString::FromUTF8( first.first ) ) );
317 BOOST_REQUIRE( instance );
318 const auto row = facade.Published().Rows().find( ITEM_KEY{ KIID( first.second ), *instance } );
319 BOOST_REQUIRE( row != facade.Published().Rows().end() );
320 const auto& component = facade.Published().Components().at( row->second.component );
321 std::set<KEY> actual;
322
323 for( const ITEM_KEY& item : component.content->items )
324 actual.emplace( keys.Instance( item.inst ).AsString().ToStdString(),
325 item.item.AsString().ToStdString() );
326
327 BOOST_CHECK_MESSAGE( actual == items, "Net code " << code );
328 BOOST_REQUIRE( component.name != INVALID_ID );
329 BOOST_CHECK_EQUAL( keys.Name( component.name ).ToStdString( wxConvUTF8 ), names.at( code ) );
330 }
331 }
332 }
333}
334
335BOOST_AUTO_TEST_CASE( HeadlessCommitPublishesCompletedCleanup )
336{
337 auto& config = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
338 SCOPED_SET_RESET restoreEngine( config.m_ConnectivityEngine, config.m_ConnectivityEngine );
339
340 for( bool engine : { false, true } )
341 {
342 BOOST_TEST_CONTEXT( "new engine " << engine )
343 {
344 config.m_ConnectivityEngine = engine;
345 KI_TEST::LoadSchematic( settings, "issue12505", schematic );
346 schematic->RebuildConnectivity();
347 SCH_SCREEN* screen = schematic->GetCurrentScreen();
348 const KIID junction( "2934c2df-d750-4949-9bb5-2d5234f61386" );
349 auto* wire = dynamic_cast<SCH_LINE*>(
350 screen->GetConnectivityItem( KIID( "06af46a6-6f60-4f17-95ae-2a85a89f02a6" ) ) );
351 auto* upper = dynamic_cast<SCH_LINE*>(
352 screen->GetConnectivityItem( KIID( "ac6a6b32-a43c-4495-b132-b2f68491aae2" ) ) );
353 auto* lower = dynamic_cast<SCH_LINE*>(
354 screen->GetConnectivityItem( KIID( "f145de0f-02ed-4631-85ea-7126ccf81633" ) ) );
355 BOOST_REQUIRE( wire && upper && lower );
356 SCH_ITEM* dot = screen->GetConnectivityItem( junction );
357 BOOST_REQUIRE( dot );
358 const VECTOR2I position = dot->GetPosition();
359 BOOST_REQUIRE( wire->IsEndPoint( position ) && upper->IsEndPoint( position )
360 && lower->IsEndPoint( position ) );
361 BOOST_REQUIRE_EQUAL( screen->GetBusesAndWires( position, false ).size(), 3u );
362 const std::set<int> directions{ wire->GetAngleFrom( position ), upper->GetAngleFrom( position ),
363 lower->GetAngleFrom( position ) };
364 BOOST_REQUIRE_EQUAL( directions.size(), 3u );
365 const VECTOR2I start = upper->GetStartPoint();
366 const VECTOR2I end = lower->GetEndPoint();
367 int notifications = 0;
368 auto subscription = schematic->Connectivity().Subscribe(
369 [&]( const SCH_CONNECTIVITY::CHANGE_SET& )
370 {
372 BOOST_CHECK( !screen->GetConnectivityItem( junction ) );
373 } );
374 TOOL_MANAGER manager;
375 manager.SetEnvironment( schematic.get(), nullptr, nullptr, nullptr, nullptr );
376 SCH_COMMIT move( &manager );
377 schematic->CleanUp( &move, screen );
378 BOOST_REQUIRE( move.Empty() );
379 BOOST_REQUIRE( screen->GetConnectivityItem( junction ) == dot );
380 BOOST_CHECK( dot->GetPosition() == position );
381 move.Modify( wire, screen );
382 wire->Move( VECTOR2I( 10000000, 10000000 ) );
383 BOOST_REQUIRE( !wire->IsEndPoint( position ) );
384 BOOST_REQUIRE( upper->IsEndPoint( position ) && lower->IsEndPoint( position ) );
385 BOOST_REQUIRE_NE( upper->GetAngleFrom( position ), lower->GetAngleFrom( position ) );
386 BOOST_REQUIRE_EQUAL( screen->GetBusesAndWires( position, false ).size(), 2u );
387 move.Push( "Disconnect native junction", SKIP_UNDO );
388 BOOST_CHECK( !screen->GetConnectivityItem( junction ) );
389 BOOST_CHECK_EQUAL( notifications, engine ? 1 : 0 );
390 size_t merged = 0;
391
392 for( SCH_ITEM* item : screen->Items() )
393 {
394 BOOST_CHECK( !item->HasFlag( STRUCT_DELETED ) );
395
396 if( auto* line = dynamic_cast<SCH_LINE*>( item );
397 line && line->IsEndPoint( start ) && line->IsEndPoint( end ) )
398 ++merged;
399 }
400
401 BOOST_CHECK_EQUAL( merged, 1u );
402 const auto dump = [&]()
403 {
404 return engine ? SCH_CONNECTIVITY::Dump( *schematic, schematic->Connectivity() )
405 : SCH_CONNECTIVITY::Dump( *schematic );
406 };
407 const std::string incremental = dump();
408 schematic->RebuildConnectivity();
409 BOOST_CHECK_EQUAL( incremental, dump() );
410 }
411 }
412}
413
414
416 PreviouslyDisabledFixturesMatchFullAndFreshRebuildsAfterCommits,
417 boost::unit_test::data::make( ORACLE_FIXTURES ), fixture )
418{
419 auto& config = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
420 SCOPED_SET_RESET restoreEngine( config.m_ConnectivityEngine, config.m_ConnectivityEngine );
421 SCOPED_SET_RESET restoreIncremental( config.m_IncrementalConnectivity, config.m_IncrementalConnectivity );
422 config.m_ConnectivityEngine = true;
423 config.m_IncrementalConnectivity = true;
424
425 KI_TEST::LoadSchematic( settings, fixture, schematic );
426 TOOL_MANAGER manager;
427 manager.SetEnvironment( schematic.get(), nullptr, nullptr, nullptr, nullptr );
428 SCH_COMMIT preparation( &manager );
429 schematic->CleanUpConnections( &preparation, GLOBAL_CLEANUP );
430 preparation.Push( "Prepare native oracle geometry", SKIP_UNDO | SKIP_CONNECTIVITY | DELETE_REMOVED_ITEMS );
431 schematic->RebuildConnectivity();
432 SCH_SYMBOL* symbol = nullptr;
433
434 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
435 {
436 for( SCH_ITEM* item : path.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
437 {
438 SCH_SYMBOL* candidate = static_cast<SCH_SYMBOL*>( item );
439
440 if( !candidate->IsPower() && !candidate->GetPins( &path ).empty() )
441 {
442 symbol = candidate;
443 break;
444 }
445 }
446
447 if( symbol )
448 break;
449 }
450
451 BOOST_REQUIRE( symbol );
452 BOOST_REQUIRE( !symbol->GetParentGroup() );
453 std::set<SCH_SCREEN*> seenScreens;
454
455 const auto isGeometry =
456 []( const SCH_ITEM* item )
457 {
458 return item->IsConnectable()
459 && ( item->Type() == SCH_LINE_T || item->Type() == SCH_BUS_WIRE_ENTRY_T
460 || item->Type() == SCH_BUS_BUS_ENTRY_T || item->Type() == SCH_JUNCTION_T
461 || item->Type() == SCH_NO_CONNECT_T );
462 };
463
464 std::vector<std::pair<SCH_SCREEN*, std::unique_ptr<SCH_ITEM>>> geometry;
465
466 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
467 {
468 if( !seenScreens.insert( path.LastScreen() ).second )
469 continue;
470
471 for( SCH_ITEM* item : path.LastScreen()->Items() )
472 {
473 if( isGeometry( item ) )
474 {
475 BOOST_REQUIRE( !item->GetParentGroup() );
476 geometry.emplace_back( path.LastScreen(), static_cast<SCH_ITEM*>( item->Clone() ) );
477 }
478 }
479 }
480
481 std::sort( geometry.begin(), geometry.end(),
482 []( const auto& a, const auto& b )
483 {
484 return a.second->m_Uuid < b.second->m_Uuid;
485 } );
486
487 const VECTOR2I offset( 10000000, 10000000 );
489 rebuilt.Update( *schematic, true );
490
491 const auto verify =
492 [&]()
493 {
494 for( SCH_SCREEN* screen : seenScreens )
495 {
496 for( SCH_ITEM* item : screen->Items() )
497 {
498 BOOST_REQUIRE_MESSAGE( !item->HasFlag( STRUCT_DELETED ),
499 "Cleanup left a deleted item on the screen: "
500 << item->m_Uuid.AsString().ToStdString() );
501 }
502 }
503
504 const auto incremental = SCH_CONNECTIVITY::CaptureRows( *schematic, schematic->Connectivity() );
505 rebuilt.Update( *schematic, true );
506 CheckRows( incremental, SCH_CONNECTIVITY::CaptureRows( *schematic, rebuilt ) );
507 // A separate fresh engine also detects stale content-keyed caches retained by a full rebuild
509 fresh.Update( *schematic, true );
510 CheckRows( incremental, SCH_CONNECTIVITY::CaptureRows( *schematic, fresh ) );
511 std::map<int, int> toFresh;
512 std::map<int, int> fromFresh;
513 const auto& current = schematic->Connectivity();
514
515 for( const auto& [key, row] : current.Published().Rows() )
516 {
517 const auto& path = current.Keys().Instance( key.inst );
518 const auto actual = current.Connection( key.item, path );
519 const auto full = rebuilt.Connection( key.item, path );
520 const auto empty = fresh.Connection( key.item, path );
521 BOOST_REQUIRE( actual && full && empty );
522 BOOST_CHECK_EQUAL( actual->NetCode(), full->NetCode() );
523 BOOST_CHECK_EQUAL( actual->NetCode() == 0, empty->NetCode() == 0 );
524 const auto forward = toFresh.emplace( actual->NetCode(), empty->NetCode() ).first;
525 const auto reverse = fromFresh.emplace( empty->NetCode(), actual->NetCode() ).first;
526 BOOST_CHECK_EQUAL( forward->second, empty->NetCode() );
527 BOOST_CHECK_EQUAL( reverse->second, actual->NetCode() );
528 }
529 };
530
531 const auto original = SCH_CONNECTIVITY::CaptureRows( *schematic, schematic->Connectivity() );
532
533 const auto restoreGeometry =
534 [&]()
535 {
536 // A wire edit can remove junctions and merge other wires. Restore all native geometry
537 // through a commit, preserving engine history while recovering the exact source state
538 SCH_COMMIT restore( &manager );
539 std::vector<SCH_ITEM*> removed;
540 std::vector<std::pair<SCH_SCREEN*, std::unique_ptr<SCH_ITEM>>> added;
541
542 for( SCH_SCREEN* screen : seenScreens )
543 {
544 for( SCH_ITEM* item : screen->Items() )
545 {
546 if( isGeometry( item ) )
547 {
548 restore.Remove( item, screen );
549 removed.push_back( item );
550 }
551 }
552 }
553
554 for( const auto& [screen, source] : geometry )
555 {
556 auto copy = std::unique_ptr<SCH_ITEM>( static_cast<SCH_ITEM*>( source->Clone() ) );
557 BOOST_REQUIRE( copy && copy->m_Uuid == source->m_Uuid );
558 restore.Add( copy.get(), screen );
559 added.emplace_back( screen, std::move( copy ) );
560 }
561
562 restore.Push( "Restore native oracle geometry", SKIP_UNDO );
563
564 for( auto& [screen, item] : added )
565 {
566 BOOST_REQUIRE( screen->CheckIfOnDrawList( item.get() ) );
567 item.release();
568 }
569
570 for( SCH_ITEM* item : removed )
571 delete item;
572
573 // The original dump already passed verify, so matching it covers both rebuilds
574 CheckRows( original, SCH_CONNECTIVITY::CaptureRows( *schematic, schematic->Connectivity() ) );
575
576 // Net codes follow update history, so the rebuild must see every state the commit did
577 rebuilt.Update( *schematic, true );
578 };
579
580 for( const VECTOR2I& movement : { offset, VECTOR2I( -offset.x, -offset.y ) } )
581 {
582 SCH_SCREEN* screen = symbol->GetParentScreen();
583 SCH_COMMIT commit( &manager );
584 commit.Modify( symbol, screen );
585 symbol->Move( movement );
586 commit.Push( "Move native oracle symbol", SKIP_UNDO );
587 BOOST_CHECK( !schematic->Connectivity().Published().Changes().Empty() );
588 verify();
589 }
590
591 restoreGeometry();
592 SCH_SCREEN* symbolScreen = symbol->GetParentScreen();
593 // Restore the same item and pin identities after destroying the original object
594 auto symbolReplacement = std::make_unique<SCH_SYMBOL>( *symbol );
595 SCH_COMMIT removal( &manager );
596 removal.Remove( symbol, symbolScreen );
597 removal.Push( "Remove native oracle symbol", SKIP_UNDO );
598 BOOST_REQUIRE( !symbolScreen->CheckIfOnDrawList( symbol ) );
599 delete symbol;
600 verify();
601 BOOST_CHECK( original != SCH_CONNECTIVITY::CaptureRows( *schematic, schematic->Connectivity() ) );
602
603 SCH_COMMIT addition( &manager );
604 addition.Add( symbolReplacement.get(), symbolScreen );
605 addition.Push( "Restore native oracle symbol", SKIP_UNDO );
606 BOOST_REQUIRE( symbolScreen->CheckIfOnDrawList( symbolReplacement.get() ) );
607 symbolReplacement.release();
608 verify();
609 restoreGeometry();
610
611 std::vector<SCH_LABEL_BASE*> labels;
612
613 for( SCH_SCREEN* screen : seenScreens )
614 {
615 for( SCH_ITEM* item : screen->Items() )
616 {
617 if( item->Type() == SCH_LABEL_T || item->Type() == SCH_GLOBAL_LABEL_T || item->Type() == SCH_HIER_LABEL_T )
618 labels.push_back( static_cast<SCH_LABEL_BASE*>( item ) );
619 }
620 }
621
622 std::sort( labels.begin(), labels.end(),
623 []( const auto* a, const auto* b )
624 {
625 return a->m_Uuid < b->m_Uuid;
626 } );
627
628 for( SCH_LABEL_BASE* label : labels )
629 {
630 const wxString originalText = label->GetText();
631
632 for( const wxString& text : { wxString( "ORACLE_" ) + originalText, originalText } )
633 {
634 SCH_COMMIT rename( &manager );
635 rename.Modify( label, label->GetParentScreen() );
636 label->SetText( text );
637 rename.Push( "Rename native oracle label", SKIP_UNDO );
638 verify();
639 }
640 }
641
642 for( const auto& [screen, source] : geometry )
643 {
644 const KIID& id = source->m_Uuid;
645
646 BOOST_TEST_CONTEXT( "geometry item " << id.AsString().ToStdString() )
647 {
648 SCH_ITEM* item = screen->GetConnectivityItem( id );
649 BOOST_REQUIRE( item );
650 BOOST_REQUIRE( !item->GetParentGroup() );
651 const VECTOR2I expectedPosition = item->GetPosition() + offset;
652 SCH_COMMIT move( &manager );
653 move.Modify( item, screen );
654 item->Move( offset );
655 BOOST_CHECK( item->GetPosition() == expectedPosition );
656 move.Push( "Move native oracle geometry", SKIP_UNDO );
657 BOOST_CHECK( !schematic->Connectivity().Published().Changes().Empty() );
658 verify();
659 restoreGeometry();
660
661 item = screen->GetConnectivityItem( id );
662 BOOST_REQUIRE( item );
663 SCH_COMMIT removal( &manager );
664 removal.Remove( item, screen );
665 removal.Push( "Remove native oracle geometry", SKIP_UNDO );
666 BOOST_REQUIRE( !screen->CheckIfOnDrawList( item ) );
667 delete item;
668
669 verify();
670 BOOST_CHECK( original != SCH_CONNECTIVITY::CaptureRows( *schematic, schematic->Connectivity() ) );
671
672 restoreGeometry();
673 }
674 }
675
676 verify();
677 BOOST_TEST_MESSAGE( fixture << ": checked " << labels.size() << " native label renames and restores, and "
678 << geometry.size() << " native geometry move/remove/restore sequences" );
679 const auto incremental = SCH_CONNECTIVITY::CaptureRows( *schematic, schematic->Connectivity() );
680 CheckRows( original, incremental );
681 schematic->RebuildConnectivity();
682 CheckRows( incremental, SCH_CONNECTIVITY::CaptureRows( *schematic, schematic->Connectivity() ) );
683}
684
685
686BOOST_DATA_TEST_CASE_F( CONNECTIVITY_DUMP_FIXTURE, NativeLabelBatchMatchesFullRebuildAndRestores,
687 boost::unit_test::data::make( { "issue7203", "netlists/hierarchy_aliases/hierarchy_aliases" } ),
688 fixture )
689{
690 KI_TEST::LoadSchematic( settings, fixture, schematic );
691 const auto hierarchy = schematic->Hierarchy();
692 SCH_SCREEN* screen = hierarchy.front().LastScreen();
693
694 for( const auto& path : hierarchy )
695 {
696 SCH_SCREEN* candidate = path.LastScreen();
697 auto labels = candidate->Items().OfType( SCH_LABEL_T );
698 const auto instances = std::ranges::count_if( hierarchy, [&]( const auto& other )
699 {
700 return other.LastScreen() == candidate;
701 } );
702
703 if( instances > 1 && labels.begin() != labels.end() )
704 screen = candidate;
705 }
706
707 std::vector<SCH_SHEET_PATH> affected;
708
709 for( const auto& path : hierarchy )
710 {
711 if( path.LastScreen() == screen )
712 affected.push_back( path );
713 }
714
715 BOOST_REQUIRE_EQUAL( affected.size(), hierarchy.size() == 1 ? 1u : 2u );
716 std::vector<SCH_ITEM*> sources;
717
718 for( SCH_ITEM* item : screen->Items().OfType( SCH_LABEL_T ) )
719 sources.push_back( item );
720
721 BOOST_REQUIRE( !sources.empty() );
722 std::ranges::sort( sources, []( const SCH_ITEM* a, const SCH_ITEM* b ) { return a->m_Uuid < b->m_Uuid; } );
723 SCH_CONNECTIVITY::FACADE incremental;
724 incremental.Update( *schematic );
725 const std::string baseline = SCH_CONNECTIVITY::Dump( *schematic, incremental );
726 struct BATCH
727 {
728 SCH_SCREEN* screen;
729 std::vector<std::unique_ptr<SCH_ITEM>> items;
730
731 ~BATCH()
732 {
733 for( const auto& item : items )
734 {
735 if( screen->CheckIfOnDrawList( item.get() ) )
736 screen->Remove( item.get(), false );
737 }
738 }
739 } batch{ screen, {} };
740 batch.items.reserve( 500 );
741
742 for( size_t i = 0; i < 500; ++i )
743 {
744 std::unique_ptr<SCH_ITEM> copy( sources[i % sources.size()]->Duplicate( false ) );
745 copy->SetParentGroup( nullptr );
746 batch.items.push_back( std::move( copy ) );
747 }
748
750
751 for( int cycle = 0; cycle < 2; ++cycle )
752 {
753 for( const auto& item : batch.items )
754 screen->Append( item.get(), false );
755
756 incremental.Update( *schematic );
757
758 for( const auto& path : affected )
759 {
760 for( const auto& item : batch.items )
761 BOOST_REQUIRE( incremental.Connection( item->m_Uuid, path.Path() ) );
762 }
763
764 const std::string edited = SCH_CONNECTIVITY::Dump( *schematic, incremental );
765 BOOST_CHECK_NE( edited, baseline );
766 rebuilt.Update( *schematic, true );
767 BOOST_CHECK_EQUAL( edited, SCH_CONNECTIVITY::Dump( *schematic, rebuilt ) );
768
769 for( const auto& item : batch.items )
770 BOOST_REQUIRE( screen->Remove( item.get(), false ) );
771
772 incremental.Update( *schematic );
773 BOOST_CHECK_EQUAL( baseline, SCH_CONNECTIVITY::Dump( *schematic, incremental ) );
774
775 for( const auto& path : affected )
776 {
777 for( const auto& item : batch.items )
778 BOOST_CHECK( !incremental.Connection( item->m_Uuid, path.Path() ) );
779 }
780
781 rebuilt.Update( *schematic, true );
782 BOOST_CHECK_EQUAL( baseline, SCH_CONNECTIVITY::Dump( *schematic, rebuilt ) );
783 }
784}
785
const char * name
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:86
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
const KIID m_Uuid
Definition eda_item.h:599
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
Definition kiid.h:46
wxString AsString() const
Definition kiid.cpp:264
Commit for the schematic and symbol editors.
Definition sch_commit.h:54
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Model-owned source resolver.
void Update(SCHEMATIC &aSchematic, bool aRebuild=false)
std::optional< ITEM_VIEW > Connection(const KIID &aItem, const KIID_PATH &aPath) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
SCH_SCREEN * GetParentScreen() const
Definition sch_item.cpp:308
virtual void Move(const VECTOR2I &aMoveVector)
Move the item by aMoveVector to a new position.
Definition sch_item.h:422
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
bool IsEndPoint(const VECTOR2I &aPoint) const override
Test if aPt is an end point of this schematic object.
Definition sch_line.h:88
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
std::vector< SCH_LINE * > GetBusesAndWires(const VECTOR2I &aPosition, bool aIgnoreEndpoints=false) const
Return buses and wires passing through aPosition.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
SCH_ITEM * GetConnectivityItem(const KIID &aId) const
Resolve a drawing item or a connectable child on this screen; ambiguous IDs return null.
bool CheckIfOnDrawList(const SCH_ITEM *aItem) const
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:76
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
Definition sch_symbol.h:852
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
bool IsPower() const override
RAII class that sets an value at construction and resets it to the original value at destruction.
Master controller class:
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
static bool empty(const wxTextEntryBase *aCtrl)
static std::string ToStdString(const wxString &aStr)
#define STRUCT_DELETED
flag indication structures to be erased
std::string source
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
Value keys and the key session of the schematic connectivity engine.
std::map< std::pair< KIID_PATH, KIID >, DUMP_ROW > DUMP_ROWS
Rows by sheet path and item.
Definition conn_dump.h:64
DUMP_ROWS CaptureRows(SCHEMATIC &aSchematic)
Canonical, pointer-free connectivity rows for migration and rebuild comparisons.
constexpr uint32_t INVALID_ID
Marks an unset handle.
Definition conn_keys.h:47
std::string Dump(const DUMP_ROWS &aRows)
#define SKIP_CONNECTIVITY
Definition sch_commit.h:41
#define DELETE_REMOVED_ITEMS
Definition sch_commit.h:43
#define SKIP_UNDO
Definition sch_commit.h:38
@ GLOBAL_CLEANUP
Definition schematic.h:101
std::unique_ptr< SCHEMATIC > schematic
Difference between two publications.
One item or pin in one sheet instance.
Definition conn_keys.h:77
KIID item
The item or pin KIID.
Definition conn_keys.h:78
INST_ID inst
The sheet instance that shows the item.
Definition conn_keys.h:79
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(ShadowDumpResolvesClassesWithoutLegacyAssignments)
BOOST_DATA_TEST_CASE_F(CONNECTIVITY_DUMP_FIXTURE, LegacyAndEngineDumpsMatch, boost::unit_test::data::make(PARITY_FIXTURES), fixture)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
std::string path
VECTOR2I end
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
int actual
wxString result
Test unit parsing edge cases and error handling.
@ BATCH
Tool that runs in the background without any user intervention.
Definition tool_base.h:48
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_NO_CONNECT_T
Definition typeinfo.h:156
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:163
@ SCH_HIER_LABEL_T
Definition typeinfo.h:165
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:158
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:157
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:164
@ SCH_JUNCTION_T
Definition typeinfo.h:155
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708