KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_annotation_units_integration.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 "eeschema_test_utils.h"
22
23#include <sch_reference_list.h>
24#include <sch_sheet_path.h>
25#include <lib_id.h>
26#include <refdes_tracker.h>
27#include <sch_symbol.h>
28
29#include <memory>
30
32{
33protected:
36 SCH_REFERENCE createTestReference( const wxString& aRef, const wxString& aValue, int aUnit,
37 const wxString& aLibName = wxS( "TestPart" ) )
38 {
39 SCH_SYMBOL* symbol = m_symbols.emplace_back( std::make_unique<SCH_SYMBOL>() ).get();
40 symbol->SetLibId( LIB_ID( wxEmptyString, aLibName ) );
41
43 SCH_REFERENCE ref( symbol, path );
44 ref.SetRef( aRef );
45 ref.SetValue( aValue );
46 ref.SetUnit( aUnit );
47
48 return ref;
49 }
50
51private:
52 std::vector<std::unique_ptr<SCH_SYMBOL>> m_symbols;
53};
54
55
56BOOST_FIXTURE_TEST_SUITE( SchReferenceListUnits, TEST_ANNOTATION_UNITS_INTEGRATION )
57
58
59
60BOOST_AUTO_TEST_CASE( RefDesTrackerIntegration )
61{
62 // Test the integration between SCH_REFERENCE_LIST and REFDES_TRACKER
63 auto tracker = std::make_shared<REFDES_TRACKER>( false );
64
65 // Preload some references
66 tracker->Insert( "U1" );
67 tracker->Insert( "U3" );
68 tracker->Insert( "R1" );
69 tracker->Insert( "R2" );
70 tracker->Insert( "R5" );
71
72 // Test GetNextRefDesForUnits respects previously inserted references
73 SCH_REFERENCE uRef = createTestReference( "U", "LM358", 1 );
74 std::map<int, std::vector<SCH_REFERENCE>> emptyMap;
75 std::vector<int> emptyUnits;
76
77 tracker->SetReuseRefDes( false );
78
79 int nextU = tracker->GetNextRefDesForUnits( uRef, emptyMap, emptyUnits, 1 );
80 BOOST_CHECK_EQUAL( nextU, 2 ); // Should skip U1, get U2
81
82 SCH_REFERENCE rRef = createTestReference( "R", "1k", 1 );
83 int nextR = tracker->GetNextRefDesForUnits( rRef, emptyMap, emptyUnits, 1 );
84 BOOST_CHECK_EQUAL( nextR, 3 ); // Should skip R1,R2, get R3
85
86 SCH_REFERENCE icRef = createTestReference( "IC", "74HC00", 1 );
87 int nextIC = tracker->GetNextRefDesForUnits( icRef, emptyMap, emptyUnits, 1 );
88 BOOST_CHECK_EQUAL( nextIC, 1 ); // New prefix, should get IC1
89
90 // Test with minimum values
91 int nextU_min5 = tracker->GetNextRefDesForUnits( uRef, emptyMap, emptyUnits, 5 );
92 BOOST_CHECK_EQUAL( nextU_min5, 5 ); // Should get U5 (min value 5)
93
94 // Verify all references were inserted
95 BOOST_CHECK( tracker->Contains( "U2" ) );
96 BOOST_CHECK( tracker->Contains( "R3" ) );
97 BOOST_CHECK( tracker->Contains( "IC1" ) );
98 BOOST_CHECK( tracker->Contains( "U5" ) );
99}
100
101
102BOOST_AUTO_TEST_CASE( SerializationWithComplexRefs )
103{
104 auto tracker = std::make_shared<REFDES_TRACKER>( false );
105
106 // Add references through various methods
107 tracker->Insert( "U1" );
108 tracker->Insert( "U3" );
109
110 // Use GetNextRefDesForUnits to get U2
111 SCH_REFERENCE uRef = createTestReference( "U", "LM358", 1 );
112 std::map<int, std::vector<SCH_REFERENCE>> emptyMap;
113 std::vector<int> emptyUnits;
114
115 tracker->SetReuseRefDes( false );
116
117 int next = tracker->GetNextRefDesForUnits( uRef, emptyMap, emptyUnits, 1 );
118 BOOST_CHECK_EQUAL( next, 2 ); // Gets U2
119
120 tracker->Insert( "IC1" );
121 tracker->Insert( "IC5" );
122
123 SCH_REFERENCE icRef = createTestReference( "IC", "74HC00", 1 );
124 next = tracker->GetNextRefDesForUnits( icRef, emptyMap, emptyUnits, 3 );
125 BOOST_CHECK_EQUAL( next, 3 ); // Gets IC3
126
127 // Test serialization captures all state
128 std::string serialized = tracker->Serialize();
129
130 auto tracker2 = std::make_shared<REFDES_TRACKER>( false );
131 BOOST_CHECK( tracker2->Deserialize( serialized ) );
132
133 // Verify all references are preserved
134 BOOST_CHECK( tracker2->Contains( "U1" ) );
135 BOOST_CHECK( tracker2->Contains( "U2" ) );
136 BOOST_CHECK( tracker2->Contains( "U3" ) );
137 BOOST_CHECK( tracker2->Contains( "IC1" ) );
138 BOOST_CHECK( tracker2->Contains( "IC3" ) );
139 BOOST_CHECK( tracker2->Contains( "IC5" ) );
140
141 // Verify next references continue correctly using GetNextRefDesForUnits
142 tracker2->SetReuseRefDes( false );
143 next = tracker2->GetNextRefDesForUnits( uRef, emptyMap, emptyUnits, 1 );
145
146 next = tracker2->GetNextRefDesForUnits( icRef, emptyMap, emptyUnits, 1 );
148}
149
150
A generic fixture for loading schematics and associated settings for qa tests.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
A helper to define a symbol's reference designator in a schematic.
void SetValue(const wxString &aValue)
void SetRef(const wxString &aReference)
void SetUnit(int aUnit)
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:69
void SetLibId(const LIB_ID &aName)
SCH_REFERENCE createTestReference(const wxString &aRef, const wxString &aValue, int aUnit, const wxString &aLibName=wxS("TestPart"))
SCH_REFERENCE holds a raw pointer to its symbol and CompareLibName dereferences it,...
std::vector< std::unique_ptr< SCH_SYMBOL > > m_symbols
CITER next(CITER it)
Definition ptree.cpp:120
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
BOOST_AUTO_TEST_CASE(RefDesTrackerIntegration)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_CHECK_EQUAL(result, "25.4")